2016-06-14 02:56:23 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
2016-07-05 15:20:49 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-06-14 02:56:23 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
opts := newServiceOptions()
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create [OPTIONS] IMAGE [COMMAND] [ARG...]",
|
|
|
|
Short: "Create a new service",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.image = args[0]
|
|
|
|
if len(args) > 1 {
|
|
|
|
opts.args = args[1:]
|
|
|
|
}
|
|
|
|
return runCreate(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
2016-06-17 18:14:36 +00:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVar(&opts.mode, flagMode, "replicated", "Service mode (replicated or global)")
|
2016-06-14 02:56:23 +00:00
|
|
|
addServiceFlags(cmd, opts)
|
2016-06-29 16:28:33 +00:00
|
|
|
|
|
|
|
flags.VarP(&opts.labels, flagLabel, "l", "Service labels")
|
2016-07-25 09:38:01 +00:00
|
|
|
flags.Var(&opts.containerLabels, flagContainerLabel, "Container labels")
|
2016-06-29 16:28:33 +00:00
|
|
|
flags.VarP(&opts.env, flagEnv, "e", "Set environment variables")
|
|
|
|
flags.Var(&opts.mounts, flagMount, "Attach a mount to the service")
|
|
|
|
flags.StringSliceVar(&opts.constraints, flagConstraint, []string{}, "Placement constraints")
|
|
|
|
flags.StringSliceVar(&opts.networks, flagNetwork, []string{}, "Network attachments")
|
|
|
|
flags.VarP(&opts.endpoint.ports, flagPublish, "p", "Publish a port as a node port")
|
|
|
|
|
|
|
|
flags.SetInterspersed(false)
|
2016-06-14 02:56:23 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCreate(dockerCli *client.DockerCli, opts *serviceOptions) error {
|
2016-06-15 18:50:49 +00:00
|
|
|
apiClient := dockerCli.Client()
|
2016-07-05 15:20:49 +00:00
|
|
|
createOpts := types.ServiceCreateOptions{}
|
2016-06-14 02:56:23 +00:00
|
|
|
|
|
|
|
service, err := opts.ToService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:50:49 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-30 00:08:00 +00:00
|
|
|
// only send auth if flag was set
|
|
|
|
if opts.registryAuth {
|
|
|
|
// Retrieve encoded auth token from the image reference
|
|
|
|
encodedAuth, err := dockerCli.RetrieveAuthTokenFromImage(ctx, opts.image)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-05 15:20:49 +00:00
|
|
|
createOpts.EncodedRegistryAuth = encodedAuth
|
2016-06-15 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 15:20:49 +00:00
|
|
|
response, err := apiClient.ServiceCreate(ctx, service, createOpts)
|
2016-06-14 02:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
|
|
|
|
return nil
|
|
|
|
}
|