mirror of https://github.com/docker/buildx.git
driver: start implemeting bootstrap for docker
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
parent
49f67b7e96
commit
f302881c0d
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue