You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
4.7 KiB
Go
211 lines
4.7 KiB
Go
6 years ago
|
package docker
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
6 years ago
|
"io"
|
||
|
"io/ioutil"
|
||
6 years ago
|
"net"
|
||
|
"os"
|
||
|
"time"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/docker/docker/api/types"
|
||
6 years ago
|
dockertypes "github.com/docker/docker/api/types"
|
||
6 years ago
|
"github.com/docker/docker/api/types/container"
|
||
|
"github.com/docker/docker/api/types/network"
|
||
|
dockerclient "github.com/docker/docker/client"
|
||
6 years ago
|
"github.com/docker/docker/pkg/stdcopy"
|
||
6 years ago
|
"github.com/moby/buildkit/client"
|
||
|
"github.com/pkg/errors"
|
||
|
"github.com/tonistiigi/buildx/driver"
|
||
6 years ago
|
"github.com/tonistiigi/buildx/util/progress"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
var buildkitImage = "moby/buildkit:master" // TODO: make this verified and configuratble
|
||
|
|
||
6 years ago
|
type Driver struct {
|
||
6 years ago
|
driver.InitConfig
|
||
6 years ago
|
version dockertypes.Version
|
||
|
}
|
||
|
|
||
6 years ago
|
func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error {
|
||
|
return progress.Wrap("[internal] booting buildkit", l, func(sub progress.SubLogger) error {
|
||
|
_, err := d.DockerAPI.ContainerInspect(ctx, d.Name)
|
||
|
if err != nil {
|
||
|
if dockerclient.IsErrNotFound(err) {
|
||
|
return d.create(ctx, sub)
|
||
|
}
|
||
|
return err
|
||
6 years ago
|
}
|
||
6 years ago
|
return sub.Wrap("starting container "+d.Name, func() error {
|
||
|
if err := d.start(ctx, sub); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := d.wait(ctx); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
6 years ago
|
})
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
|
||
6 years ago
|
if err := l.Wrap("pulling image "+buildkitImage, func() error {
|
||
|
rc, err := d.DockerAPI.ImageCreate(ctx, buildkitImage, types.ImageCreateOptions{})
|
||
6 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
_, err = io.Copy(ioutil.Discard, rc)
|
||
6 years ago
|
return err
|
||
6 years ago
|
}); err != nil {
|
||
6 years ago
|
return err
|
||
|
}
|
||
6 years ago
|
if err := l.Wrap("creating container "+d.Name, func() error {
|
||
|
_, err := d.DockerAPI.ContainerCreate(ctx, &container.Config{
|
||
6 years ago
|
Image: buildkitImage,
|
||
6 years ago
|
}, &container.HostConfig{
|
||
|
Privileged: true,
|
||
|
}, &network.NetworkingConfig{}, d.Name)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := d.start(ctx, l); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 years ago
|
if err := d.wait(ctx); err != nil {
|
||
|
return err
|
||
|
}
|
||
6 years ago
|
return nil
|
||
|
}); err != nil {
|
||
6 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
6 years ago
|
func (d *Driver) wait(ctx context.Context) error {
|
||
|
try := 0
|
||
|
for {
|
||
|
if err := d.run(ctx, []string{"buildctl", "debug", "workers"}); err != nil {
|
||
|
if try > 10 {
|
||
|
return err
|
||
|
}
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
return ctx.Err()
|
||
|
case <-time.After(time.Duration(100+try*20) * time.Millisecond):
|
||
|
try++
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (d *Driver) exec(ctx context.Context, cmd []string) (string, net.Conn, error) {
|
||
|
execConfig := types.ExecConfig{
|
||
|
Cmd: cmd,
|
||
|
AttachStdin: true,
|
||
|
AttachStdout: true,
|
||
|
AttachStderr: true,
|
||
|
}
|
||
|
response, err := d.DockerAPI.ContainerExecCreate(ctx, d.Name, execConfig)
|
||
|
if err != nil {
|
||
|
return "", nil, err
|
||
|
}
|
||
|
|
||
|
execID := response.ID
|
||
|
if execID == "" {
|
||
|
return "", nil, errors.New("exec ID empty")
|
||
|
}
|
||
|
|
||
|
resp, err := d.DockerAPI.ContainerExecAttach(ctx, execID, types.ExecStartCheck{})
|
||
|
if err != nil {
|
||
|
return "", nil, err
|
||
|
}
|
||
|
return execID, resp.Conn, nil
|
||
|
}
|
||
|
|
||
|
func (d *Driver) run(ctx context.Context, cmd []string) error {
|
||
|
id, conn, err := d.exec(ctx, cmd)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if _, err := io.Copy(ioutil.Discard, conn); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
conn.Close()
|
||
|
resp, err := d.DockerAPI.ContainerExecInspect(ctx, id)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resp.ExitCode != 0 {
|
||
|
return errors.Errorf("exit code %d", resp.ExitCode)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
6 years ago
|
func (d *Driver) start(ctx context.Context, l progress.SubLogger) error {
|
||
6 years ago
|
return d.DockerAPI.ContainerStart(ctx, d.Name, types.ContainerStartOptions{})
|
||
|
}
|
||
|
|
||
|
func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
|
||
|
container, err := d.DockerAPI.ContainerInspect(ctx, d.Name)
|
||
|
if err != nil {
|
||
|
if dockerclient.IsErrNotFound(err) {
|
||
|
return &driver.Info{
|
||
|
Status: driver.Terminated,
|
||
|
}, nil
|
||
|
}
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if container.State.Running {
|
||
|
return &driver.Info{
|
||
|
Status: driver.Running,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
return &driver.Info{
|
||
|
Status: driver.Stopped,
|
||
|
}, nil
|
||
6 years ago
|
}
|
||
|
|
||
|
func (d *Driver) Stop(ctx context.Context, force bool) error {
|
||
|
return errors.Errorf("stop not implemented for %T", d)
|
||
|
}
|
||
|
|
||
|
func (d *Driver) Rm(ctx context.Context, force bool) error {
|
||
|
return errors.Errorf("rm not implemented for %T", d)
|
||
|
}
|
||
|
|
||
6 years ago
|
func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
|
||
6 years ago
|
_, conn, err := d.exec(ctx, []string{"buildctl", "dial-stdio"})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
conn = demuxConn(conn)
|
||
|
|
||
|
return client.New(ctx, "", client.WithDialer(func(string, time.Duration) (net.Conn, error) {
|
||
|
return conn, nil
|
||
|
}))
|
||
|
}
|
||
|
|
||
|
func demuxConn(c net.Conn) net.Conn {
|
||
|
pr, pw := io.Pipe()
|
||
|
// TODO: rewrite parser with Reader() to avoid goroutine switch
|
||
|
go stdcopy.StdCopy(pw, os.Stdout, c)
|
||
|
return &demux{
|
||
|
Conn: c,
|
||
|
Reader: pr,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type demux struct {
|
||
|
net.Conn
|
||
|
io.Reader
|
||
|
}
|
||
|
|
||
|
func (d *demux) Read(dt []byte) (int, error) {
|
||
|
return d.Reader.Read(dt)
|
||
6 years ago
|
}
|