1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Send registry auth token for service deploy

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
This commit is contained in:
Nishant Totla 2016-07-15 13:17:03 -07:00
parent ea59668046
commit a26bdd8607
No known key found for this signature in database
GPG key ID: 7EA5781C9B3D0C19
4 changed files with 32 additions and 7 deletions

View file

@ -23,6 +23,7 @@ const (
type deployOptions struct { type deployOptions struct {
bundlefile string bundlefile string
namespace string namespace string
sendRegistryAuth bool
} }
func newDeployCommand(dockerCli *client.DockerCli) *cobra.Command { func newDeployCommand(dockerCli *client.DockerCli) *cobra.Command {
@ -41,6 +42,7 @@ func newDeployCommand(dockerCli *client.DockerCli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
addBundlefileFlag(&opts.bundlefile, flags) addBundlefileFlag(&opts.bundlefile, flags)
addRegistryAuthFlag(&opts.sendRegistryAuth, flags)
return cmd return cmd
} }
@ -56,7 +58,7 @@ func runDeploy(dockerCli *client.DockerCli, opts deployOptions) error {
if err := updateNetworks(ctx, dockerCli, networks, opts.namespace); err != nil { if err := updateNetworks(ctx, dockerCli, networks, opts.namespace); err != nil {
return err return err
} }
return deployServices(ctx, dockerCli, bundle.Services, opts.namespace) return deployServices(ctx, dockerCli, bundle.Services, opts.namespace, opts.sendRegistryAuth)
} }
func getUniqueNetworkNames(services map[string]bundlefile.Service) []string { func getUniqueNetworkNames(services map[string]bundlefile.Service) []string {
@ -129,6 +131,7 @@ func deployServices(
dockerCli *client.DockerCli, dockerCli *client.DockerCli,
services map[string]bundlefile.Service, services map[string]bundlefile.Service,
namespace string, namespace string,
sendAuth bool,
) error { ) error {
apiClient := dockerCli.Client() apiClient := dockerCli.Client()
out := dockerCli.Out() out := dockerCli.Out()
@ -181,24 +184,40 @@ func deployServices(
cspec.User = *service.User cspec.User = *service.User
} }
encodedAuth := ""
if sendAuth {
// Retrieve encoded auth token from the image reference
image := serviceSpec.TaskTemplate.ContainerSpec.Image
encodedAuth, err = dockerCli.RetrieveAuthTokenFromImage(ctx, image)
if err != nil {
return err
}
}
if service, exists := existingServiceMap[name]; exists { if service, exists := existingServiceMap[name]; exists {
fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID) fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
// TODO(nishanttotla): Pass auth token updateOpts := types.ServiceUpdateOptions{}
if sendAuth {
updateOpts.EncodedRegistryAuth = encodedAuth
}
if err := apiClient.ServiceUpdate( if err := apiClient.ServiceUpdate(
ctx, ctx,
service.ID, service.ID,
service.Version, service.Version,
serviceSpec, serviceSpec,
types.ServiceUpdateOptions{}, updateOpts,
); err != nil { ); err != nil {
return err return err
} }
} else { } else {
fmt.Fprintf(out, "Creating service %s\n", name) fmt.Fprintf(out, "Creating service %s\n", name)
// TODO(nishanttotla): Pass headers with X-Registry-Auth createOpts := types.ServiceCreateOptions{}
if _, err := apiClient.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{}); err != nil { if sendAuth {
createOpts.EncodedRegistryAuth = encodedAuth
}
if _, err := apiClient.ServiceCreate(ctx, serviceSpec, createOpts); err != nil {
return err return err
} }
} }

View file

@ -18,6 +18,10 @@ func addBundlefileFlag(opt *string, flags *pflag.FlagSet) {
"Path to a Distributed Application Bundle file (Default: STACK.dab)") "Path to a Distributed Application Bundle file (Default: STACK.dab)")
} }
func addRegistryAuthFlag(opt *bool, flags *pflag.FlagSet) {
flags.BoolVar(opt, "registry-auth", false, "Send registry authentication details to Swarm agents")
}
func loadBundlefile(stderr io.Writer, namespace string, path string) (*bundlefile.Bundlefile, error) { func loadBundlefile(stderr io.Writer, namespace string, path string) (*bundlefile.Bundlefile, error) {
defaultPath := fmt.Sprintf("%s.dab", namespace) defaultPath := fmt.Sprintf("%s.dab", namespace)

View file

@ -19,6 +19,7 @@ Create and update a stack from a Distributed Application Bundle (DAB)
Options: Options:
--file string Path to a Distributed Application Bundle file (Default: STACK.dab) --file string Path to a Distributed Application Bundle file (Default: STACK.dab)
--help Print usage --help Print usage
--registry-auth Send registry authentication details to Swarm agents
``` ```
Create and update a stack from a `dab` file. This command has to be Create and update a stack from a `dab` file. This command has to be

View file

@ -46,6 +46,7 @@ Create and update a stack
Options: Options:
--file string Path to a Distributed Application Bundle file (Default: STACK.dab) --file string Path to a Distributed Application Bundle file (Default: STACK.dab)
--help Print usage --help Print usage
--registry-auth Send registry authentication details to Swarm agents
``` ```
Let's deploy the stack created before: Let's deploy the stack created before: