mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-29 08:57:44 +08:00
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"
|
"github.com/moby/buildkit/util/progress/progressui"
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/tonistiigi/buildx/driver"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -40,7 +41,20 @@ type Inputs struct {
|
|||||||
InStream io.Reader
|
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{
|
so := client.SolveOpt{
|
||||||
Frontend: "dockerfile.v0",
|
Frontend: "dockerfile.v0",
|
||||||
FrontendAttrs: map[string]string{},
|
FrontendAttrs: map[string]string{},
|
||||||
@ -142,6 +156,9 @@ func (pw *ProgressWriter) Err() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pw *ProgressWriter) Status() chan *client.SolveStatus {
|
func (pw *ProgressWriter) Status() chan *client.SolveStatus {
|
||||||
|
if pw == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return pw.status
|
return pw.status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tonistiigi/buildx/commands"
|
"github.com/tonistiigi/buildx/commands"
|
||||||
"github.com/tonistiigi/buildx/version"
|
"github.com/tonistiigi/buildx/version"
|
||||||
|
|
||||||
|
_ "github.com/tonistiigi/buildx/driver/docker"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -7,12 +7,11 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/cli/cli"
|
"github.com/docker/cli/cli"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/moby/buildkit/client"
|
|
||||||
"github.com/moby/buildkit/session/auth/authprovider"
|
"github.com/moby/buildkit/session/auth/authprovider"
|
||||||
"github.com/moby/buildkit/util/appcontext"
|
"github.com/moby/buildkit/util/appcontext"
|
||||||
bkappdefaults "github.com/moby/buildkit/util/appdefaults"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tonistiigi/buildx/build"
|
"github.com/tonistiigi/buildx/build"
|
||||||
|
"github.com/tonistiigi/buildx/driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type buildOptions struct {
|
type buildOptions struct {
|
||||||
@ -94,8 +93,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
|
|||||||
}
|
}
|
||||||
opts.Exports = outputs
|
opts.Exports = outputs
|
||||||
|
|
||||||
// TODO: temporary
|
d, err := driver.GetDriver(ctx, "buildkit.default", nil, dockerCli.Client())
|
||||||
c, err := client.New(ctx, bkappdefaults.Address, client.WithFailFast())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -104,7 +102,7 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
pw := build.NewProgressWriter(ctx2, os.Stderr, in.progress)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/docker/cli/cli-plugins/plugin"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -9,6 +10,9 @@ func NewRootCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Short: "Build with BuildKit",
|
Short: "Build with BuildKit",
|
||||||
Use: "buildx",
|
Use: "buildx",
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return plugin.PersistentPreRunE(cmd, args)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
addCommands(cmd, dockerCli)
|
addCommands(cmd, dockerCli)
|
||||||
return cmd
|
return cmd
|
||||||
|
@ -2,24 +2,82 @@ package docker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
dockertypes "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/moby/buildkit/client"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/tonistiigi/buildx/driver"
|
"github.com/tonistiigi/buildx/driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
config driver.InitConfig
|
driver.InitConfig
|
||||||
version dockertypes.Version
|
version dockertypes.Version
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Bootstrap(context.Context, driver.Logger) error {
|
func (d *Driver) Bootstrap(ctx context.Context, l driver.Logger) error {
|
||||||
return errors.Errorf("bootstrap not implemented for %T", d)
|
_, 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) {
|
func (d *Driver) create(ctx context.Context, l driver.Logger) error {
|
||||||
return driver.Info{}, errors.Errorf("info not implemented for %T", d)
|
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 {
|
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)
|
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)
|
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 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"
|
"context"
|
||||||
|
|
||||||
"github.com/moby/buildkit/client"
|
"github.com/moby/buildkit/client"
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,14 +23,47 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
Status Status
|
Status Status
|
||||||
Platforms []specs.Platform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Driver interface {
|
type Driver interface {
|
||||||
Bootstrap(context.Context, Logger) error
|
Bootstrap(context.Context, Logger) error
|
||||||
Info(context.Context) (Info, error)
|
Info(context.Context) (*Info, error)
|
||||||
Stop(ctx context.Context, force bool) error
|
Stop(ctx context.Context, force bool) error
|
||||||
Rm(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
|
drivers[f.Name()] = f
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDefaultDriver() (Factory, error) {
|
func GetDefaultFactory() (Factory, error) {
|
||||||
if len(drivers) == 0 {
|
if len(drivers) == 0 {
|
||||||
return nil, errors.Errorf("no drivers available")
|
return nil, errors.Errorf("no drivers available")
|
||||||
}
|
}
|
||||||
@ -54,3 +54,17 @@ func GetDefaultDriver() (Factory, error) {
|
|||||||
})
|
})
|
||||||
return dd[0].f, nil
|
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…
x
Reference in New Issue
Block a user