mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-14 23:47:09 +08:00
commands: add implementations for create, use, rm, stop
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
@ -171,11 +171,28 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) {
|
||||
}
|
||||
|
||||
func (d *Driver) Stop(ctx context.Context, force bool) error {
|
||||
return errors.Errorf("stop not implemented for %T", d)
|
||||
info, err := d.Info(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Status == driver.Running {
|
||||
return d.DockerAPI.ContainerStop(ctx, d.Name, nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) Rm(ctx context.Context, force bool) error {
|
||||
return errors.Errorf("rm not implemented for %T", d)
|
||||
info, err := d.Info(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Status != driver.Terminated {
|
||||
return d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
|
||||
RemoveVolumes: true,
|
||||
Force: true,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
|
||||
|
@ -3,6 +3,7 @@ package docker
|
||||
import (
|
||||
"context"
|
||||
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tonistiigi/buildx/driver"
|
||||
)
|
||||
@ -25,8 +26,8 @@ func (*factory) Usage() string {
|
||||
return "docker-container"
|
||||
}
|
||||
|
||||
func (*factory) Priority(cfg driver.InitConfig) int {
|
||||
if cfg.DockerAPI == nil {
|
||||
func (*factory) Priority(ctx context.Context, api dockerclient.APIClient) int {
|
||||
if api == nil {
|
||||
return priorityUnsupported
|
||||
}
|
||||
return prioritySupported
|
||||
@ -44,3 +45,7 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
|
||||
|
||||
return &Driver{factory: f, InitConfig: cfg, version: v}, nil
|
||||
}
|
||||
|
||||
func (f *factory) AllowsInstances() bool {
|
||||
return true
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package docker
|
||||
import (
|
||||
"context"
|
||||
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tonistiigi/buildx/driver"
|
||||
)
|
||||
@ -25,12 +26,12 @@ func (*factory) Usage() string {
|
||||
return "docker"
|
||||
}
|
||||
|
||||
func (*factory) Priority(cfg driver.InitConfig) int {
|
||||
if cfg.DockerAPI == nil {
|
||||
func (*factory) Priority(ctx context.Context, api dockerclient.APIClient) int {
|
||||
if api == nil {
|
||||
return priorityUnsupported
|
||||
}
|
||||
|
||||
c, err := cfg.DockerAPI.DialHijack(context.TODO(), "/grpc", "h2c", nil)
|
||||
c, err := api.DialHijack(ctx, "/grpc", "h2c", nil)
|
||||
if err != nil {
|
||||
return priorityUnsupported
|
||||
}
|
||||
@ -51,3 +52,7 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
|
||||
|
||||
return &Driver{factory: f, InitConfig: cfg, version: v}, nil
|
||||
}
|
||||
|
||||
func (f *factory) AllowsInstances() bool {
|
||||
return false
|
||||
}
|
||||
|
@ -11,8 +11,9 @@ import (
|
||||
type Factory interface {
|
||||
Name() string
|
||||
Usage() string
|
||||
Priority(cfg InitConfig) int
|
||||
Priority(context.Context, dockerclient.APIClient) int
|
||||
New(ctx context.Context, cfg InitConfig) (Driver, error)
|
||||
AllowsInstances() bool
|
||||
}
|
||||
|
||||
type BuildkitConfig struct {
|
||||
@ -37,7 +38,7 @@ func Register(f Factory) {
|
||||
drivers[f.Name()] = f
|
||||
}
|
||||
|
||||
func GetDefaultFactory(ic InitConfig) (Factory, error) {
|
||||
func GetDefaultFactory(ctx context.Context, c dockerclient.APIClient, instanceRequired bool) (Factory, error) {
|
||||
if len(drivers) == 0 {
|
||||
return nil, errors.Errorf("no drivers available")
|
||||
}
|
||||
@ -47,7 +48,10 @@ func GetDefaultFactory(ic InitConfig) (Factory, error) {
|
||||
}
|
||||
dd := make([]p, 0, len(drivers))
|
||||
for _, f := range drivers {
|
||||
dd = append(dd, p{f: f, priority: f.Priority(ic)})
|
||||
if instanceRequired && !f.AllowsInstances() {
|
||||
continue
|
||||
}
|
||||
dd = append(dd, p{f: f, priority: f.Priority(ctx, c)})
|
||||
}
|
||||
sort.Slice(dd, func(i, j int) bool {
|
||||
return dd[i].priority < dd[j].priority
|
||||
@ -55,6 +59,18 @@ func GetDefaultFactory(ic InitConfig) (Factory, error) {
|
||||
return dd[0].f, nil
|
||||
}
|
||||
|
||||
func GetFactory(name string, instanceRequired bool) Factory {
|
||||
for _, f := range drivers {
|
||||
if instanceRequired && !f.AllowsInstances() {
|
||||
continue
|
||||
}
|
||||
if f.Name() == name {
|
||||
return f
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient) (Driver, error) {
|
||||
ic := InitConfig{
|
||||
DockerAPI: api,
|
||||
@ -62,7 +78,7 @@ func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.API
|
||||
}
|
||||
if f == nil {
|
||||
var err error
|
||||
f, err = GetDefaultFactory(ic)
|
||||
f, err = GetDefaultFactory(ctx, api, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user