2017-06-08 00:54:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-21 15:49:26 +00:00
|
|
|
"net"
|
2017-06-08 00:54:29 +00:00
|
|
|
"os"
|
2017-06-12 08:49:48 +00:00
|
|
|
"path/filepath"
|
2017-11-21 08:08:36 +00:00
|
|
|
"sort"
|
2017-09-21 15:49:26 +00:00
|
|
|
"strings"
|
2017-06-08 00:54:29 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/sys"
|
2017-09-21 15:49:26 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/cache/cacheimport"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/control"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile"
|
|
|
|
"github.com/moby/buildkit/frontend/gateway"
|
|
|
|
"github.com/moby/buildkit/session"
|
2017-07-03 04:21:40 +00:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
2017-07-12 05:08:53 +00:00
|
|
|
"github.com/moby/buildkit/util/appdefaults"
|
2017-07-12 22:17:23 +00:00
|
|
|
"github.com/moby/buildkit/util/profiler"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
2017-12-15 08:06:54 +00:00
|
|
|
"github.com/moby/buildkit/worker/base"
|
2017-06-08 00:54:29 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-08 00:54:29 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-06-09 01:16:19 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-12-04 03:09:39 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-06-08 18:17:44 +00:00
|
|
|
"google.golang.org/grpc"
|
2017-06-08 00:54:29 +00:00
|
|
|
)
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
type workerInitializerOpt struct {
|
|
|
|
sessionManager *session.Manager
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
|
|
|
type workerInitializer struct {
|
2017-12-15 08:06:54 +00:00
|
|
|
fn func(c *cli.Context, common workerInitializerOpt) ([]worker.Worker, error)
|
2017-11-21 08:08:36 +00:00
|
|
|
// less priority number, more preferred
|
|
|
|
priority int
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
appFlags []cli.Flag
|
|
|
|
workerInitializers []workerInitializer
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerWorkerInitializer(wi workerInitializer, flags ...cli.Flag) {
|
|
|
|
workerInitializers = append(workerInitializers, wi)
|
|
|
|
sort.Slice(workerInitializers,
|
|
|
|
func(i, j int) bool {
|
|
|
|
return workerInitializers[i].priority < workerInitializers[j].priority
|
|
|
|
})
|
|
|
|
appFlags = append(appFlags, flags...)
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:54:29 +00:00
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
2017-12-18 07:33:02 +00:00
|
|
|
app.Name = "buildkitd"
|
2017-06-08 00:54:29 +00:00
|
|
|
app.Usage = "build daemon"
|
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "enable debug output in logs",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "root",
|
|
|
|
Usage: "path to state directory",
|
2017-07-12 05:08:53 +00:00
|
|
|
Value: appdefaults.Root,
|
2017-06-08 00:54:29 +00:00
|
|
|
},
|
2017-12-04 03:09:39 +00:00
|
|
|
cli.StringSliceFlag{
|
2017-09-21 15:49:26 +00:00
|
|
|
Name: "addr",
|
|
|
|
Usage: "listening address (socket or tcp)",
|
2017-12-04 03:09:39 +00:00
|
|
|
Value: &cli.StringSlice{appdefaults.Address},
|
2017-06-08 00:54:29 +00:00
|
|
|
},
|
2017-07-03 06:52:27 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "debugaddr",
|
2017-09-21 15:49:26 +00:00
|
|
|
Usage: "debugging address (eg. 0.0.0.0:6060)",
|
2017-07-03 06:52:27 +00:00
|
|
|
Value: "",
|
|
|
|
},
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
app.Flags = append(app.Flags, appFlags...)
|
2017-06-08 19:00:31 +00:00
|
|
|
|
2017-06-08 00:54:29 +00:00
|
|
|
app.Action = func(c *cli.Context) error {
|
2017-07-03 04:21:40 +00:00
|
|
|
ctx, cancel := context.WithCancel(appcontext.Context())
|
2017-06-08 00:54:29 +00:00
|
|
|
|
2017-07-03 06:52:27 +00:00
|
|
|
if debugAddr := c.GlobalString("debugaddr"); debugAddr != "" {
|
|
|
|
if err := setupDebugHandlers(debugAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 04:21:40 +00:00
|
|
|
server := grpc.NewServer(unaryInterceptor(ctx))
|
2017-06-08 00:54:29 +00:00
|
|
|
|
2017-06-12 08:49:48 +00:00
|
|
|
// relative path does not work with nightlyone/lockfile
|
|
|
|
root, err := filepath.Abs(c.GlobalString("root"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-08 00:54:29 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create %s", root)
|
|
|
|
}
|
|
|
|
|
2017-06-12 08:49:48 +00:00
|
|
|
controller, err := newController(c, root)
|
2017-06-08 00:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.Register(server)
|
|
|
|
|
2017-07-03 04:21:40 +00:00
|
|
|
errCh := make(chan error, 1)
|
2017-12-04 03:09:39 +00:00
|
|
|
addrs := c.GlobalStringSlice("addr")
|
|
|
|
if len(addrs) > 1 {
|
|
|
|
addrs = addrs[1:] // https://github.com/urfave/cli/issues/160
|
|
|
|
}
|
|
|
|
if err := serveGRPC(server, addrs, errCh); err != nil {
|
2017-06-08 00:54:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-03 04:21:40 +00:00
|
|
|
select {
|
|
|
|
case serverErr := <-errCh:
|
|
|
|
err = serverErr
|
|
|
|
cancel()
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("stopping server")
|
|
|
|
server.GracefulStop()
|
|
|
|
|
|
|
|
return err
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
app.Before = func(context *cli.Context) error {
|
|
|
|
if context.GlobalBool("debug") {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-12 22:17:23 +00:00
|
|
|
|
|
|
|
profiler.Attach(app)
|
|
|
|
|
2017-06-08 00:54:29 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
2017-12-18 07:33:02 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "buildkitd: %s\n", err)
|
2017-06-08 00:54:29 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 03:09:39 +00:00
|
|
|
func serveGRPC(server *grpc.Server, addrs []string, errCh chan error) error {
|
|
|
|
if len(addrs) == 0 {
|
2017-09-21 15:49:26 +00:00
|
|
|
return errors.New("--addr cannot be empty")
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
2017-12-04 03:09:39 +00:00
|
|
|
eg, _ := errgroup.WithContext(context.Background())
|
|
|
|
listeners := make([]net.Listener, 0, len(addrs))
|
|
|
|
for _, addr := range addrs {
|
|
|
|
l, err := getListener(addr)
|
|
|
|
if err != nil {
|
|
|
|
for _, l := range listeners {
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
listeners = append(listeners, l)
|
|
|
|
}
|
|
|
|
for _, l := range listeners {
|
|
|
|
func(l net.Listener) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer l.Close()
|
|
|
|
logrus.Infof("running server on %s", l.Addr())
|
|
|
|
return server.Serve(l)
|
|
|
|
})
|
|
|
|
}(l)
|
2017-06-08 00:54:29 +00:00
|
|
|
}
|
|
|
|
go func() {
|
2017-12-04 03:09:39 +00:00
|
|
|
errCh <- eg.Wait()
|
2017-06-08 00:54:29 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-21 15:49:26 +00:00
|
|
|
func getListener(addr string) (net.Listener, error) {
|
|
|
|
addrSlice := strings.SplitN(addr, "://", 2)
|
|
|
|
proto := addrSlice[0]
|
|
|
|
listenAddr := addrSlice[1]
|
|
|
|
switch proto {
|
|
|
|
case "unix", "npipe":
|
|
|
|
return sys.GetLocalListener(listenAddr, os.Getuid(), os.Getgid())
|
|
|
|
case "tcp":
|
|
|
|
return sockets.NewTCPSocket(listenAddr, nil)
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("addr %s not supported", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 04:21:40 +00:00
|
|
|
func unaryInterceptor(globalCtx context.Context) grpc.ServerOption {
|
2017-06-09 01:16:19 +00:00
|
|
|
return grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
2017-07-03 04:21:40 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-globalCtx.Done():
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-06-09 01:16:19 +00:00
|
|
|
resp, err = handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("%s returned error: %+v", info.FullMethod, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
2017-11-21 08:08:36 +00:00
|
|
|
|
|
|
|
func newController(c *cli.Context, root string) (*control.Controller, error) {
|
|
|
|
sessionManager, err := session.NewManager()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wc, err := newWorkerController(c, workerInitializerOpt{
|
|
|
|
sessionManager: sessionManager,
|
|
|
|
root: root,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
frontends := map[string]frontend.Frontend{}
|
|
|
|
frontends["dockerfile.v0"] = dockerfile.NewDockerfileFrontend()
|
|
|
|
frontends["gateway.v0"] = gateway.NewGatewayFrontend()
|
2017-12-15 08:06:54 +00:00
|
|
|
|
|
|
|
// cache exporter and importer are manager concepts but as there is no
|
|
|
|
// way to pull data into specific worker yet we currently set them up
|
|
|
|
// as part of default worker
|
|
|
|
var ce *cacheimport.CacheExporter
|
|
|
|
var ci *cacheimport.CacheImporter
|
|
|
|
|
|
|
|
w, err := wc.GetDefault()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wt := w.(*base.Worker)
|
|
|
|
ce = wt.CacheExporter
|
|
|
|
ci = wt.CacheImporter
|
|
|
|
|
2017-11-21 08:08:36 +00:00
|
|
|
return control.NewController(control.Opt{
|
|
|
|
SessionManager: sessionManager,
|
|
|
|
WorkerController: wc,
|
|
|
|
Frontends: frontends,
|
2017-12-15 08:06:54 +00:00
|
|
|
CacheExporter: ce,
|
|
|
|
CacheImporter: ci,
|
2017-11-21 08:08:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWorkerController(c *cli.Context, wiOpt workerInitializerOpt) (*worker.Controller, error) {
|
|
|
|
wc := &worker.Controller{}
|
2017-12-19 09:34:34 +00:00
|
|
|
nWorkers := 0
|
2017-11-21 08:08:36 +00:00
|
|
|
for _, wi := range workerInitializers {
|
|
|
|
ws, err := wi.fn(c, wiOpt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, w := range ws {
|
2017-12-19 09:34:34 +00:00
|
|
|
logrus.Infof("found worker %q, labels=%v", w.ID(), w.Labels())
|
2017-11-21 08:08:36 +00:00
|
|
|
if err = wc.Add(w); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-19 09:34:34 +00:00
|
|
|
nWorkers++
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if nWorkers == 0 {
|
2017-12-15 07:00:13 +00:00
|
|
|
return nil, errors.New("no worker found, rebuild the buildkit daemon?")
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
defaultWorker, err := wc.GetDefault()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-19 09:34:34 +00:00
|
|
|
logrus.Infof("found %d workers, default=%q", nWorkers, defaultWorker.ID())
|
2017-12-15 08:06:54 +00:00
|
|
|
logrus.Warn("currently, only the default worker can be used.")
|
2017-11-21 08:08:36 +00:00
|
|
|
return wc, nil
|
|
|
|
}
|
2017-12-19 09:34:34 +00:00
|
|
|
|
|
|
|
func attrMap(sl []string) (map[string]string, error) {
|
|
|
|
m := map[string]string{}
|
|
|
|
for _, v := range sl {
|
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, errors.Errorf("invalid value %s", v)
|
|
|
|
}
|
|
|
|
m[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|