2019-03-26 06:14:39 +08:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-12 18:10:39 +08:00
|
|
|
"github.com/docker/buildx/store"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/util/progress"
|
2020-11-05 05:04:04 +08:00
|
|
|
clitypes "github.com/docker/cli/cli/config/types"
|
2019-03-26 06:14:39 +08:00
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-03-26 07:33:03 +08:00
|
|
|
var ErrNotRunning = errors.Errorf("driver not running")
|
2019-04-16 01:21:09 +08:00
|
|
|
var ErrNotConnecting = errors.Errorf("driver not connecting")
|
2019-03-26 07:33:03 +08:00
|
|
|
|
2019-03-26 06:14:39 +08:00
|
|
|
type Status int
|
|
|
|
|
|
|
|
const (
|
2019-04-16 01:21:09 +08:00
|
|
|
Inactive Status = iota
|
2019-03-26 06:14:39 +08:00
|
|
|
Starting
|
|
|
|
Running
|
|
|
|
Stopping
|
|
|
|
Stopped
|
|
|
|
)
|
|
|
|
|
2019-04-16 01:21:09 +08:00
|
|
|
func (s Status) String() string {
|
|
|
|
switch s {
|
|
|
|
case Inactive:
|
|
|
|
return "inactive"
|
|
|
|
case Starting:
|
|
|
|
return "starting"
|
|
|
|
case Running:
|
|
|
|
return "running"
|
|
|
|
case Stopping:
|
|
|
|
return "stopping"
|
|
|
|
case Stopped:
|
|
|
|
return "stopped"
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:14:39 +08:00
|
|
|
type Info struct {
|
2019-03-26 10:01:31 +08:00
|
|
|
Status Status
|
2019-11-12 18:10:39 +08:00
|
|
|
// DynamicNodes must be empty if the actual nodes are statically listed in the store
|
|
|
|
DynamicNodes []store.Node
|
2019-03-26 06:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-11-05 05:04:04 +08:00
|
|
|
type Auth interface {
|
|
|
|
GetAuthConfig(registryHostname string) (clitypes.AuthConfig, error)
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:14:39 +08:00
|
|
|
type Driver interface {
|
2019-04-11 09:57:54 +08:00
|
|
|
Factory() Factory
|
2020-11-16 12:49:58 +08:00
|
|
|
Bootstrap(context.Context, progress.Logger) error
|
2019-03-26 10:01:31 +08:00
|
|
|
Info(context.Context) (*Info, error)
|
2022-05-09 22:17:01 +08:00
|
|
|
Version(context.Context) (string, error)
|
2019-03-26 06:14:39 +08:00
|
|
|
Stop(ctx context.Context, force bool) error
|
2021-11-17 20:56:42 +08:00
|
|
|
Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error
|
2019-03-26 10:01:31 +08:00
|
|
|
Client(ctx context.Context) (*client.Client, error)
|
2019-04-11 09:57:54 +08:00
|
|
|
Features() map[Feature]bool
|
2020-10-15 15:07:35 +08:00
|
|
|
IsMobyDriver() bool
|
2020-11-16 12:13:02 +08:00
|
|
|
Config() InitConfig
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:36:24 +08:00
|
|
|
func Boot(ctx, clientContext context.Context, d Driver, pw progress.Writer) (*client.Client, error) {
|
2019-03-26 10:01:31 +08:00
|
|
|
try := 0
|
|
|
|
for {
|
|
|
|
info, err := d.Info(ctx)
|
|
|
|
if err != nil {
|
2019-04-17 08:11:43 +08:00
|
|
|
return nil, err
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
|
|
|
try++
|
|
|
|
if info.Status != Running {
|
|
|
|
if try > 2 {
|
2019-04-17 08:11:43 +08:00
|
|
|
return nil, errors.Errorf("failed to bootstrap %T driver in attempts", d)
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
2020-09-21 10:46:39 +08:00
|
|
|
if err := d.Bootstrap(ctx, pw.Write); err != nil {
|
2019-04-17 08:11:43 +08:00
|
|
|
return nil, err
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 11:36:24 +08:00
|
|
|
c, err := d.Client(clientContext)
|
2019-03-26 10:01:31 +08:00
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == ErrNotRunning && try <= 2 {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-17 08:11:43 +08:00
|
|
|
return nil, err
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
2019-04-17 08:11:43 +08:00
|
|
|
return c, nil
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
2019-03-27 00:55:09 +08:00
|
|
|
}
|