2021-03-03 01:14:53 +01:00
|
|
|
package buildkitd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-17 22:54:51 +01:00
|
|
|
"errors"
|
2021-03-03 01:14:53 +01:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2021-07-19 16:26:18 +02:00
|
|
|
"runtime/debug"
|
2021-03-03 01:14:53 +01:00
|
|
|
"strings"
|
2021-03-17 22:54:51 +01:00
|
|
|
"time"
|
2021-03-03 01:14:53 +01:00
|
|
|
|
2021-03-17 22:54:51 +01:00
|
|
|
bk "github.com/moby/buildkit/client"
|
|
|
|
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the container connection driver
|
2021-03-03 01:14:53 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2021-07-19 16:26:18 +02:00
|
|
|
var (
|
|
|
|
// vendoredVersion is filled in by init()
|
|
|
|
vendoredVersion string
|
|
|
|
)
|
|
|
|
|
2021-03-03 01:14:53 +01:00
|
|
|
const (
|
|
|
|
image = "moby/buildkit"
|
|
|
|
containerName = "dagger-buildkitd"
|
2021-03-03 03:53:15 +01:00
|
|
|
volumeName = "dagger-buildkitd"
|
2021-03-03 01:14:53 +01:00
|
|
|
)
|
|
|
|
|
2021-07-19 16:26:18 +02:00
|
|
|
func init() {
|
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
2021-07-30 08:02:03 +02:00
|
|
|
return
|
2021-07-19 16:26:18 +02:00
|
|
|
}
|
2021-07-30 08:02:03 +02:00
|
|
|
|
2021-07-19 16:26:18 +02:00
|
|
|
for _, d := range bi.Deps {
|
|
|
|
if d.Path == "github.com/moby/buildkit" {
|
|
|
|
vendoredVersion = d.Version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 01:14:53 +01:00
|
|
|
func Start(ctx context.Context) (string, error) {
|
2021-07-30 08:02:03 +02:00
|
|
|
if vendoredVersion == "" {
|
|
|
|
return "", fmt.Errorf("vendored version is empty")
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:47:27 +02:00
|
|
|
if err := checkBuildkit(ctx); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("docker-container://%s", containerName), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure the buildkit is active and properly set up (e.g. connected to host and last version with moby/buildkit)
|
|
|
|
func checkBuildkit(ctx context.Context) error {
|
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
|
|
|
config, err := getBuildkitInformation(ctx)
|
2021-03-03 01:14:53 +01:00
|
|
|
if err != nil {
|
2021-09-21 08:47:27 +02:00
|
|
|
// If that failed, it might be because the docker CLI is out of service.
|
2021-03-03 01:14:53 +01:00
|
|
|
if err := checkDocker(ctx); err != nil {
|
2021-09-21 08:47:27 +02:00
|
|
|
return err
|
2021-03-03 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lg.Debug().Msg("no buildkit daemon detected")
|
2021-09-21 08:47:27 +02:00
|
|
|
|
|
|
|
if err := removeBuildkit(ctx); err != nil {
|
2022-01-28 10:56:33 +01:00
|
|
|
lg.Debug().Err(err).Msg("error while removing buildkit")
|
2021-09-21 08:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := installBuildkit(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-03 01:14:53 +01:00
|
|
|
} else {
|
2021-09-21 08:47:27 +02:00
|
|
|
lg.
|
|
|
|
Debug().
|
|
|
|
Str("version", config.Version).
|
|
|
|
Bool("isActive", config.IsActive).
|
|
|
|
Bool("haveHostNetwork", config.HaveHostNetwork).
|
|
|
|
Msg("detected buildkit config")
|
2021-03-03 01:14:53 +01:00
|
|
|
|
2021-09-21 08:47:27 +02:00
|
|
|
if config.Version != vendoredVersion || !config.HaveHostNetwork {
|
2021-03-03 01:14:53 +01:00
|
|
|
lg.
|
|
|
|
Info().
|
2021-07-19 16:26:18 +02:00
|
|
|
Str("version", vendoredVersion).
|
2021-09-21 08:47:27 +02:00
|
|
|
Bool("have host network", config.HaveHostNetwork).
|
2021-03-03 01:14:53 +01:00
|
|
|
Msg("upgrading buildkit")
|
2021-09-21 08:47:27 +02:00
|
|
|
|
2021-07-30 08:02:03 +02:00
|
|
|
if err := removeBuildkit(ctx); err != nil {
|
2021-09-21 08:47:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := installBuildkit(ctx); err != nil {
|
|
|
|
return err
|
2021-03-03 01:14:53 +01:00
|
|
|
}
|
2021-09-21 08:47:27 +02:00
|
|
|
}
|
|
|
|
if !config.IsActive {
|
2021-03-03 01:14:53 +01:00
|
|
|
lg.
|
|
|
|
Info().
|
2021-07-19 16:26:18 +02:00
|
|
|
Str("version", vendoredVersion).
|
2021-03-03 01:14:53 +01:00
|
|
|
Msg("starting buildkit")
|
2021-09-21 08:47:27 +02:00
|
|
|
|
|
|
|
if err := startBuildkit(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-03 01:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:47:27 +02:00
|
|
|
return nil
|
2021-03-03 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure the docker CLI is available and properly set up (e.g. permissions to
|
|
|
|
// communicate with the daemon, etc)
|
|
|
|
func checkDocker(ctx context.Context) error {
|
|
|
|
cmd := exec.CommandContext(ctx, "docker", "info")
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.
|
|
|
|
Ctx(ctx).
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Bytes("output", output).
|
|
|
|
Msg("failed to run docker")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:47:27 +02:00
|
|
|
// Start the buildkit daemon
|
2021-03-03 01:14:53 +01:00
|
|
|
func startBuildkit(ctx context.Context) error {
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
2021-07-19 16:26:18 +02:00
|
|
|
Str("version", vendoredVersion).
|
2021-03-03 01:14:53 +01:00
|
|
|
Logger()
|
|
|
|
|
2021-09-21 08:47:27 +02:00
|
|
|
lg.Debug().Msg("starting buildkit image")
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx,
|
|
|
|
"docker",
|
|
|
|
"start",
|
|
|
|
containerName,
|
|
|
|
)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Bytes("output", output).
|
|
|
|
Msg("failed to start buildkit container")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return waitBuildkit(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull and run the buildkit daemon with a proper configuration
|
|
|
|
// If the buildkit daemon is already configured, use startBuildkit
|
|
|
|
func installBuildkit(ctx context.Context) error {
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("version", vendoredVersion).
|
|
|
|
Logger()
|
|
|
|
|
2021-03-03 01:14:53 +01:00
|
|
|
lg.Debug().Msg("pulling buildkit image")
|
2021-07-20 13:11:20 +02:00
|
|
|
// #nosec
|
2021-03-03 01:14:53 +01:00
|
|
|
cmd := exec.CommandContext(ctx,
|
|
|
|
"docker",
|
|
|
|
"pull",
|
2021-07-19 16:26:18 +02:00
|
|
|
image+":"+vendoredVersion,
|
2021-03-03 01:14:53 +01:00
|
|
|
)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Bytes("output", output).
|
|
|
|
Msg("failed to pull buildkit image")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-09 12:28:02 +02:00
|
|
|
// FIXME: buildkitd currently runs without network isolation (--net=host)
|
|
|
|
// in order for containers to be able to reach localhost.
|
2021-04-09 16:46:25 +02:00
|
|
|
// This is required for things such as kubectl being able to
|
|
|
|
// reach a KinD/minikube cluster locally
|
2021-07-20 13:11:20 +02:00
|
|
|
// #nosec
|
2021-03-03 01:14:53 +01:00
|
|
|
cmd = exec.CommandContext(ctx,
|
|
|
|
"docker",
|
|
|
|
"run",
|
2021-04-09 12:28:02 +02:00
|
|
|
"--net=host",
|
2021-03-03 01:14:53 +01:00
|
|
|
"-d",
|
|
|
|
"--restart", "always",
|
2021-03-03 03:53:15 +01:00
|
|
|
"-v", volumeName+":/var/lib/buildkit",
|
2021-03-03 01:14:53 +01:00
|
|
|
"--name", containerName,
|
|
|
|
"--privileged",
|
2021-07-19 16:26:18 +02:00
|
|
|
image+":"+vendoredVersion,
|
2021-03-03 01:14:53 +01:00
|
|
|
)
|
|
|
|
output, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2021-04-15 10:15:01 +02:00
|
|
|
// If the daemon failed to start because it's already running,
|
|
|
|
// chances are another dagger instance started it. We can just ignore
|
|
|
|
// the error.
|
|
|
|
if !strings.Contains(string(output), "Error response from daemon: Conflict.") {
|
|
|
|
log.
|
|
|
|
Ctx(ctx).
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Bytes("output", output).
|
|
|
|
Msg("unable to start buildkitd")
|
|
|
|
return err
|
|
|
|
}
|
2021-03-03 01:14:53 +01:00
|
|
|
}
|
2021-03-17 22:54:51 +01:00
|
|
|
return waitBuildkit(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitBuildkit waits for the buildkit daemon to be responsive.
|
|
|
|
func waitBuildkit(ctx context.Context) error {
|
|
|
|
c, err := bk.New(ctx, "docker-container://"+containerName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-21 08:47:27 +02:00
|
|
|
|
|
|
|
// FIXME Does output "failed to wait: signal: broken pipe"
|
2021-03-17 22:54:51 +01:00
|
|
|
defer c.Close()
|
|
|
|
|
2021-07-19 16:26:18 +02:00
|
|
|
// Try to connect every 100ms up to 100 times (10 seconds total)
|
2021-03-17 22:54:51 +01:00
|
|
|
const (
|
|
|
|
retryPeriod = 100 * time.Millisecond
|
2021-07-19 16:26:18 +02:00
|
|
|
retryAttempts = 100
|
2021-03-17 22:54:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
for retry := 0; retry < retryAttempts; retry++ {
|
|
|
|
_, err = c.ListWorkers(ctx)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(retryPeriod)
|
|
|
|
}
|
|
|
|
return errors.New("buildkit failed to respond")
|
2021-03-03 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2021-07-30 08:02:03 +02:00
|
|
|
func removeBuildkit(ctx context.Context) error {
|
2021-03-03 01:14:53 +01:00
|
|
|
lg := log.
|
|
|
|
Ctx(ctx)
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx,
|
|
|
|
"docker",
|
|
|
|
"rm",
|
|
|
|
"-fv",
|
|
|
|
containerName,
|
|
|
|
)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Bytes("output", output).
|
|
|
|
Msg("failed to stop buildkit")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|