2020-04-29 22:12:58 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-05-02 18:54:03 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-26 10:27:18 +02:00
|
|
|
"github.com/spf13/cobra"
|
2020-05-02 18:54:03 +02:00
|
|
|
|
2020-05-25 14:58:22 +02:00
|
|
|
containersv1 "github.com/docker/api/protos/containers/v1"
|
2020-05-26 10:20:13 +02:00
|
|
|
contextsv1 "github.com/docker/api/protos/contexts/v1"
|
2020-04-29 22:12:58 +02:00
|
|
|
"github.com/docker/api/server"
|
2020-04-29 23:44:01 +02:00
|
|
|
"github.com/docker/api/server/proxy"
|
2020-04-29 22:12:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type serveOpts struct {
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// ServeCommand returns the command to serve the API
|
2020-04-29 22:12:58 +02:00
|
|
|
func ServeCommand() *cobra.Command {
|
2020-05-20 15:56:07 +02:00
|
|
|
// FIXME(chris-crone): Should warn that specified context is ignored
|
2020-04-29 22:12:58 +02:00
|
|
|
var opts serveOpts
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Start an api server",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runServe(cmd.Context(), opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().StringVar(&opts.address, "address", "", "The address to listen to")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runServe(ctx context.Context, opts serveOpts) error {
|
2020-05-20 15:56:07 +02:00
|
|
|
s := server.New(ctx)
|
2020-04-29 22:12:58 +02:00
|
|
|
|
2020-05-16 16:39:08 +02:00
|
|
|
listener, err := server.CreateListener(opts.address)
|
2020-04-29 22:12:58 +02:00
|
|
|
if err != nil {
|
2020-05-16 16:39:08 +02:00
|
|
|
return errors.Wrap(err, "listen address "+opts.address)
|
2020-04-29 22:12:58 +02:00
|
|
|
}
|
2020-05-04 23:50:00 +02:00
|
|
|
// nolint errcheck
|
2020-04-29 23:44:01 +02:00
|
|
|
defer listener.Close()
|
2020-04-29 22:12:58 +02:00
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
p := proxy.NewContainerAPI()
|
2020-05-26 10:27:18 +02:00
|
|
|
contextsService := server.NewContexts()
|
2020-04-29 22:12:58 +02:00
|
|
|
|
|
|
|
containersv1.RegisterContainersServer(s, p)
|
2020-05-26 10:27:18 +02:00
|
|
|
contextsv1.RegisterContextsServer(s, contextsService)
|
2020-04-29 22:12:58 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
logrus.Info("stopping server")
|
|
|
|
s.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
logrus.WithField("address", opts.address).Info("serving daemon API")
|
|
|
|
|
|
|
|
// start the GRPC server to serve on the listener
|
2020-04-29 23:44:01 +02:00
|
|
|
return s.Serve(listener)
|
2020-04-29 22:12:58 +02:00
|
|
|
}
|