2017-06-08 18:17:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-12-21 05:18:54 +00:00
|
|
|
"net/url"
|
2017-06-08 18:17:44 +00:00
|
|
|
"os"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
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"
|
2018-01-05 23:17:35 +00:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-08 18:17:44 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "buildctl"
|
|
|
|
app.Usage = "build utility"
|
|
|
|
|
2017-10-15 19:13:47 +00:00
|
|
|
defaultAddress := os.Getenv("BUILDKIT_HOST")
|
|
|
|
if defaultAddress == "" {
|
|
|
|
defaultAddress = appdefaults.Address
|
|
|
|
}
|
|
|
|
|
2017-06-08 18:17:44 +00:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "enable debug output in logs",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2017-09-21 15:49:26 +00:00
|
|
|
Name: "addr",
|
2017-12-20 10:45:06 +00:00
|
|
|
Usage: "buildkitd address",
|
2017-10-15 19:13:47 +00:00
|
|
|
Value: defaultAddress,
|
2017-06-08 18:17:44 +00:00
|
|
|
},
|
2017-12-20 10:45:06 +00:00
|
|
|
cli.StringFlag{
|
2017-12-21 05:18:54 +00:00
|
|
|
Name: "tlsservername",
|
2017-12-20 10:45:06 +00:00
|
|
|
Usage: "buildkitd server name for certificate validation",
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2017-12-21 05:18:54 +00:00
|
|
|
Name: "tlscacert",
|
2017-12-20 10:45:06 +00:00
|
|
|
Usage: "CA certificate for validation",
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2017-12-21 05:18:54 +00:00
|
|
|
Name: "tlscert",
|
2017-12-20 10:45:06 +00:00
|
|
|
Usage: "client certificate",
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2017-12-21 05:18:54 +00:00
|
|
|
Name: "tlskey",
|
2017-12-20 10:45:06 +00:00
|
|
|
Usage: "client key",
|
|
|
|
Value: "",
|
|
|
|
},
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Commands = []cli.Command{
|
|
|
|
diskUsageCommand,
|
2017-12-27 01:22:50 +00:00
|
|
|
pruneCommand,
|
2017-06-09 01:16:19 +00:00
|
|
|
buildCommand,
|
2017-06-13 08:09:14 +00:00
|
|
|
debugCommand,
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 07:08:13 +00:00
|
|
|
var debugEnabled bool
|
|
|
|
|
2017-06-08 18:17:44 +00:00
|
|
|
app.Before = func(context *cli.Context) error {
|
2017-12-02 07:08:13 +00:00
|
|
|
debugEnabled = context.GlobalBool("debug")
|
|
|
|
if debugEnabled {
|
2017-06-08 18:17:44 +00:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-12 22:17:23 +00:00
|
|
|
|
2018-01-05 23:17:35 +00:00
|
|
|
attachAppContext(app)
|
|
|
|
|
2017-07-12 22:17:23 +00:00
|
|
|
profiler.Attach(app)
|
|
|
|
|
2017-06-08 18:17:44 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
2017-12-02 07:08:13 +00:00
|
|
|
if debugEnabled {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
}
|
2017-06-08 18:17:44 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveClient(c *cli.Context) (*client.Client, error) {
|
2017-12-21 05:18:54 +00:00
|
|
|
serverName := c.GlobalString("tlsservername")
|
|
|
|
if serverName == "" {
|
|
|
|
// guess servername as hostname of target address
|
|
|
|
uri, err := url.Parse(c.GlobalString("addr"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serverName = uri.Hostname()
|
|
|
|
}
|
|
|
|
caCert := c.GlobalString("tlscacert")
|
|
|
|
cert := c.GlobalString("tlscert")
|
|
|
|
key := c.GlobalString("tlskey")
|
2018-01-05 23:17:35 +00:00
|
|
|
|
2017-12-20 10:45:06 +00:00
|
|
|
opts := []client.ClientOpt{client.WithBlock()}
|
2018-01-05 23:17:35 +00:00
|
|
|
|
|
|
|
if span := opentracing.SpanFromContext(commandContext(c)); span != nil {
|
|
|
|
opts = append(opts, client.WithTracer(span.Tracer()))
|
|
|
|
}
|
|
|
|
|
2017-12-21 05:18:54 +00:00
|
|
|
if caCert != "" || cert != "" || key != "" {
|
2017-12-20 10:45:06 +00:00
|
|
|
opts = append(opts, client.WithCredentials(serverName, caCert, cert, key))
|
|
|
|
}
|
|
|
|
return client.New(c.GlobalString("addr"), opts...)
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|