From f302881c0d390790a6888b4e3df08722cfa03b3d Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 25 Mar 2019 19:01:31 -0700 Subject: [PATCH] driver: start implemeting bootstrap for docker Signed-off-by: Tonis Tiigi --- build/build.go | 19 ++++++++++- cmd/buildx/main.go | 2 ++ commands/build.go | 8 ++--- commands/root.go | 4 +++ driver/docker/driver.go | 70 ++++++++++++++++++++++++++++++++++++---- driver/docker/factory.go | 2 +- driver/driver.go | 42 +++++++++++++++++++++--- driver/manager.go | 16 ++++++++- 8 files changed, 144 insertions(+), 19 deletions(-) diff --git a/build/build.go b/build/build.go index 90bb31d7..92ca65b0 100644 --- a/build/build.go +++ b/build/build.go @@ -15,6 +15,7 @@ import ( "github.com/moby/buildkit/util/progress/progressui" specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" + "github.com/tonistiigi/buildx/driver" "golang.org/x/sync/errgroup" ) @@ -40,7 +41,20 @@ type Inputs struct { InStream io.Reader } -func Build(ctx context.Context, c *client.Client, opt Options, pw *ProgressWriter) (*client.SolveResponse, error) { +func Build(ctx context.Context, drivers []driver.Driver, opt Options, pw *ProgressWriter) (*client.SolveResponse, error) { + if len(drivers) == 0 { + return nil, errors.Errorf("driver required for build") + } + + if len(drivers) > 1 { + return nil, errors.Errorf("multiple drivers currently not supported") + } + + c, err := driver.Boot(ctx, drivers[0], pw.Status()) + if err != nil { + return nil, err + } + so := client.SolveOpt{ Frontend: "dockerfile.v0", FrontendAttrs: map[string]string{}, @@ -142,6 +156,9 @@ func (pw *ProgressWriter) Err() error { } func (pw *ProgressWriter) Status() chan *client.SolveStatus { + if pw == nil { + return nil + } return pw.status } diff --git a/cmd/buildx/main.go b/cmd/buildx/main.go index c991dca0..a8e2ff31 100644 --- a/cmd/buildx/main.go +++ b/cmd/buildx/main.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" "github.com/tonistiigi/buildx/commands" "github.com/tonistiigi/buildx/version" + + _ "github.com/tonistiigi/buildx/driver/docker" ) func main() { diff --git a/commands/build.go b/commands/build.go index ec638fe8..af007716 100644 --- a/commands/build.go +++ b/commands/build.go @@ -7,12 +7,11 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - "github.com/moby/buildkit/client" "github.com/moby/buildkit/session/auth/authprovider" "github.com/moby/buildkit/util/appcontext" - bkappdefaults "github.com/moby/buildkit/util/appdefaults" "github.com/spf13/cobra" "github.com/tonistiigi/buildx/build" + "github.com/tonistiigi/buildx/driver" ) type buildOptions struct { @@ -94,8 +93,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) error { } opts.Exports = outputs - // TODO: temporary - c, err := client.New(ctx, bkappdefaults.Address, client.WithFailFast()) + d, err := driver.GetDriver(ctx, "buildkit.default", nil, dockerCli.Client()) if err != nil { return err } @@ -104,7 +102,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) error { defer cancel() pw := build.NewProgressWriter(ctx2, os.Stderr, in.progress) - _, err = build.Build(ctx, c, opts, pw) + _, err = build.Build(ctx, []driver.Driver{d}, opts, pw) return err } diff --git a/commands/root.go b/commands/root.go index d693160f..eb858ae2 100644 --- a/commands/root.go +++ b/commands/root.go @@ -1,6 +1,7 @@ package commands import ( + "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" "github.com/spf13/cobra" ) @@ -9,6 +10,9 @@ func NewRootCmd(dockerCli command.Cli) *cobra.Command { cmd := &cobra.Command{ Short: "Build with BuildKit", Use: "buildx", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + return plugin.PersistentPreRunE(cmd, args) + }, } addCommands(cmd, dockerCli) return cmd diff --git a/driver/docker/driver.go b/driver/docker/driver.go index f7356b77..558569d6 100644 --- a/driver/docker/driver.go +++ b/driver/docker/driver.go @@ -2,24 +2,82 @@ package docker import ( "context" + "io" + "io/ioutil" + "github.com/docker/docker/api/types" dockertypes "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" + dockerclient "github.com/docker/docker/client" "github.com/moby/buildkit/client" "github.com/pkg/errors" "github.com/tonistiigi/buildx/driver" ) type Driver struct { - config driver.InitConfig + driver.InitConfig version dockertypes.Version } -func (d *Driver) Bootstrap(context.Context, driver.Logger) error { - return errors.Errorf("bootstrap not implemented for %T", d) +func (d *Driver) Bootstrap(ctx context.Context, l driver.Logger) error { + _, err := d.DockerAPI.ContainerInspect(ctx, d.Name) + if err != nil { + if dockerclient.IsErrNotFound(err) { + return d.create(ctx, l) + } + return err + } + return d.start(ctx, l) } -func (d *Driver) Info(context.Context) (driver.Info, error) { - return driver.Info{}, errors.Errorf("info not implemented for %T", d) +func (d *Driver) create(ctx context.Context, l driver.Logger) error { + rc, err := d.DockerAPI.ImageCreate(ctx, "moby/buildkit", types.ImageCreateOptions{}) + if err != nil { + return err + } + _, err = io.Copy(ioutil.Discard, rc) + if err != nil { + return err + } + _, err = d.DockerAPI.ContainerCreate(ctx, &container.Config{ + Image: "moby/buildkit", + }, &container.HostConfig{ + Privileged: true, + }, &network.NetworkingConfig{}, d.Name) + if err != nil { + return err + } + if err := d.start(ctx, l); err != nil { + return err + } + return nil +} + +func (d *Driver) start(ctx context.Context, l driver.Logger) error { + 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 } func (d *Driver) Stop(ctx context.Context, force bool) error { @@ -30,6 +88,6 @@ func (d *Driver) Rm(ctx context.Context, force bool) error { return errors.Errorf("rm not implemented for %T", d) } -func (d *Driver) Client() (*client.Client, error) { +func (d *Driver) Client(ctx context.Context) (*client.Client, error) { return nil, errors.Errorf("client not implemented for %T", d) } diff --git a/driver/docker/factory.go b/driver/docker/factory.go index f9905754..469f7c66 100644 --- a/driver/docker/factory.go +++ b/driver/docker/factory.go @@ -36,5 +36,5 @@ func (*factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver, return nil, errors.Wrapf(driver.ErrNotConnecting, err.Error()) } - return &Driver{config: cfg, version: v}, nil + return &Driver{InitConfig: cfg, version: v}, nil } diff --git a/driver/driver.go b/driver/driver.go index e90f7877..464cb6e5 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -4,7 +4,6 @@ import ( "context" "github.com/moby/buildkit/client" - specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" ) @@ -24,14 +23,47 @@ const ( ) type Info struct { - Status Status - Platforms []specs.Platform + Status Status } type Driver interface { Bootstrap(context.Context, Logger) error - Info(context.Context) (Info, error) + Info(context.Context) (*Info, error) Stop(ctx context.Context, force bool) error Rm(ctx context.Context, force bool) error - Client() (*client.Client, error) + Client(ctx context.Context) (*client.Client, error) +} + +func Boot(ctx context.Context, d Driver, status chan *client.SolveStatus) (*client.Client, error) { + try := 0 + for { + info, err := d.Info(ctx) + if err != nil { + return nil, err + } + try++ + if info.Status != Running { + if try > 2 { + return nil, errors.Errorf("failed to bootstrap %T driver in attempts", d) + } + if err := d.Bootstrap(ctx, func(s *client.SolveStatus) { + if status != nil { + status <- s + } + }); err != nil { + return nil, err + } + } + + c, err := d.Client(ctx) + if err != nil { + if errors.Cause(err) == ErrNotRunning && try <= 2 { + continue + } + return nil, err + } + return c, nil + } + + return nil, errors.Errorf("boot not implemented") } diff --git a/driver/manager.go b/driver/manager.go index b78224f8..f3dd8ce5 100644 --- a/driver/manager.go +++ b/driver/manager.go @@ -37,7 +37,7 @@ func Register(f Factory) { drivers[f.Name()] = f } -func GetDefaultDriver() (Factory, error) { +func GetDefaultFactory() (Factory, error) { if len(drivers) == 0 { return nil, errors.Errorf("no drivers available") } @@ -54,3 +54,17 @@ func GetDefaultDriver() (Factory, error) { }) return dd[0].f, nil } + +func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient) (Driver, error) { + if f == nil { + var err error + f, err = GetDefaultFactory() + if err != nil { + return nil, err + } + } + return f.New(ctx, InitConfig{ + Name: name, + DockerAPI: api, + }) +}