2019-05-31 01:26:34 +02:00
|
|
|
// Package docker builds docker images
|
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2021-10-30 20:24:40 +02:00
|
|
|
"io"
|
2019-05-31 01:26:34 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-11-08 10:52:39 +02:00
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/logger"
|
|
|
|
"go-micro.dev/v4/runtime/local/build"
|
2019-05-31 01:26:34 +02:00
|
|
|
)
|
|
|
|
|
2019-11-19 18:09:43 +02:00
|
|
|
type Builder struct {
|
|
|
|
Options build.Options
|
2019-05-31 01:26:34 +02:00
|
|
|
Client *docker.Client
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:09:43 +02:00
|
|
|
func (d *Builder) Build(s *build.Source) (*build.Package, error) {
|
2019-05-31 01:26:34 +02:00
|
|
|
image := filepath.Join(s.Repository.Path, s.Repository.Name)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buf)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerFile := "Dockerfile"
|
|
|
|
|
|
|
|
// open docker file
|
|
|
|
f, err := os.Open(filepath.Join(s.Repository.Path, s.Repository.Name, dockerFile))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// read docker file
|
2021-10-30 20:24:40 +02:00
|
|
|
by, err := io.ReadAll(f)
|
2019-05-31 01:26:34 +02:00
|
|
|
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,
|
2021-10-30 20:24:40 +02:00
|
|
|
OutputStream: io.Discard,
|
2019-05-31 01:26:34 +02:00
|
|
|
RmTmpContainer: true,
|
|
|
|
SuppressOutput: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-19 18:09:43 +02:00
|
|
|
return &build.Package{
|
2019-05-31 01:26:34 +02:00
|
|
|
Name: image,
|
|
|
|
Path: image,
|
|
|
|
Type: "docker",
|
|
|
|
Source: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:09:43 +02:00
|
|
|
func (d *Builder) Clean(b *build.Package) error {
|
2019-05-31 01:26:34 +02:00
|
|
|
image := filepath.Join(b.Path, b.Name)
|
|
|
|
return d.Client.RemoveImage(image)
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:09:43 +02:00
|
|
|
func NewBuilder(opts ...build.Option) build.Builder {
|
|
|
|
options := build.Options{}
|
2019-05-31 01:26:34 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
endpoint := "unix:///var/run/docker.sock"
|
|
|
|
client, err := docker.NewClient(endpoint)
|
|
|
|
if err != nil {
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Log(logger.FatalLevel, err)
|
2019-05-31 01:26:34 +02:00
|
|
|
}
|
2019-11-19 18:09:43 +02:00
|
|
|
return &Builder{
|
2019-05-31 01:26:34 +02:00
|
|
|
Options: options,
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
}
|