2019-05-31 02:26:34 +03:00
|
|
|
// Package docker builds docker images
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-11-19 19:09:43 +03:00
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/build"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2019-05-31 02:26:34 +03:00
|
|
|
)
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
type dockerBuild struct {
|
2019-11-19 19:09:43 +03:00
|
|
|
Options build.Options
|
2019-05-31 02:26:34 +03:00
|
|
|
Client *docker.Client
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
func (d *dockerBuild) Package(name string, s *build.Source) (*build.Package, error) {
|
|
|
|
image := name
|
2019-05-31 02:26:34 +03:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buf)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerFile := "Dockerfile"
|
|
|
|
|
|
|
|
// open docker file
|
2020-08-11 18:51:58 +03:00
|
|
|
f, err := os.Open(filepath.Join(s.Path, dockerFile))
|
2019-05-31 02:26:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// read docker file
|
|
|
|
by, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tarHeader := &tar.Header{
|
|
|
|
Name: dockerFile,
|
|
|
|
Size: int64(len(by)),
|
|
|
|
}
|
|
|
|
err = tw.WriteHeader(tarHeader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = tw.Write(by)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr := bytes.NewReader(buf.Bytes())
|
|
|
|
|
|
|
|
err = d.Client.BuildImage(docker.BuildImageOptions{
|
|
|
|
Name: image,
|
|
|
|
Dockerfile: dockerFile,
|
|
|
|
InputStream: tr,
|
|
|
|
OutputStream: ioutil.Discard,
|
|
|
|
RmTmpContainer: true,
|
|
|
|
SuppressOutput: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-19 19:09:43 +03:00
|
|
|
return &build.Package{
|
2019-05-31 02:26:34 +03:00
|
|
|
Name: image,
|
|
|
|
Path: image,
|
|
|
|
Type: "docker",
|
|
|
|
Source: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
func (d *dockerBuild) Remove(b *build.Package) error {
|
|
|
|
return d.Client.RemoveImage(b.Name)
|
2019-05-31 02:26:34 +03:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
func (d *dockerBuild) String() string {
|
|
|
|
return "docker"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuild(opts ...build.Option) build.Build {
|
2019-11-19 19:09:43 +03:00
|
|
|
options := build.Options{}
|
2019-05-31 02:26:34 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
endpoint := "unix:///var/run/docker.sock"
|
|
|
|
client, err := docker.NewClient(endpoint)
|
|
|
|
if err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
logger.Fatal(err)
|
2019-05-31 02:26:34 +03:00
|
|
|
}
|
2020-08-11 18:51:58 +03:00
|
|
|
return &dockerBuild{
|
2019-05-31 02:26:34 +03:00
|
|
|
Options: options,
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
}
|