Browse code

Send registry auth token for service deploy

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>

Nishant Totla authored on 2016/07/16 05:17:03
Showing 4 changed files
... ...
@@ -21,8 +21,9 @@ const (
21 21
 )
22 22
 
23 23
 type deployOptions struct {
24
-	bundlefile string
25
-	namespace  string
24
+	bundlefile       string
25
+	namespace        string
26
+	sendRegistryAuth bool
26 27
 }
27 28
 
28 29
 func newDeployCommand(dockerCli *client.DockerCli) *cobra.Command {
... ...
@@ -41,6 +42,7 @@ func newDeployCommand(dockerCli *client.DockerCli) *cobra.Command {
41 41
 
42 42
 	flags := cmd.Flags()
43 43
 	addBundlefileFlag(&opts.bundlefile, flags)
44
+	addRegistryAuthFlag(&opts.sendRegistryAuth, flags)
44 45
 	return cmd
45 46
 }
46 47
 
... ...
@@ -56,7 +58,7 @@ func runDeploy(dockerCli *client.DockerCli, opts deployOptions) error {
56 56
 	if err := updateNetworks(ctx, dockerCli, networks, opts.namespace); err != nil {
57 57
 		return err
58 58
 	}
59
-	return deployServices(ctx, dockerCli, bundle.Services, opts.namespace)
59
+	return deployServices(ctx, dockerCli, bundle.Services, opts.namespace, opts.sendRegistryAuth)
60 60
 }
61 61
 
62 62
 func getUniqueNetworkNames(services map[string]bundlefile.Service) []string {
... ...
@@ -129,6 +131,7 @@ func deployServices(
129 129
 	dockerCli *client.DockerCli,
130 130
 	services map[string]bundlefile.Service,
131 131
 	namespace string,
132
+	sendAuth bool,
132 133
 ) error {
133 134
 	apiClient := dockerCli.Client()
134 135
 	out := dockerCli.Out()
... ...
@@ -181,24 +184,40 @@ func deployServices(
181 181
 			cspec.User = *service.User
182 182
 		}
183 183
 
184
+		encodedAuth := ""
185
+		if sendAuth {
186
+			// Retrieve encoded auth token from the image reference
187
+			image := serviceSpec.TaskTemplate.ContainerSpec.Image
188
+			encodedAuth, err = dockerCli.RetrieveAuthTokenFromImage(ctx, image)
189
+			if err != nil {
190
+				return err
191
+			}
192
+		}
193
+
184 194
 		if service, exists := existingServiceMap[name]; exists {
185 195
 			fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
186 196
 
187
-			// TODO(nishanttotla): Pass auth token
197
+			updateOpts := types.ServiceUpdateOptions{}
198
+			if sendAuth {
199
+				updateOpts.EncodedRegistryAuth = encodedAuth
200
+			}
188 201
 			if err := apiClient.ServiceUpdate(
189 202
 				ctx,
190 203
 				service.ID,
191 204
 				service.Version,
192 205
 				serviceSpec,
193
-				types.ServiceUpdateOptions{},
206
+				updateOpts,
194 207
 			); err != nil {
195 208
 				return err
196 209
 			}
197 210
 		} else {
198 211
 			fmt.Fprintf(out, "Creating service %s\n", name)
199 212
 
200
-			// TODO(nishanttotla): Pass headers with X-Registry-Auth
201
-			if _, err := apiClient.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{}); err != nil {
213
+			createOpts := types.ServiceCreateOptions{}
214
+			if sendAuth {
215
+				createOpts.EncodedRegistryAuth = encodedAuth
216
+			}
217
+			if _, err := apiClient.ServiceCreate(ctx, serviceSpec, createOpts); err != nil {
202 218
 				return err
203 219
 			}
204 220
 		}
... ...
@@ -18,6 +18,10 @@ func addBundlefileFlag(opt *string, flags *pflag.FlagSet) {
18 18
 		"Path to a Distributed Application Bundle file (Default: STACK.dab)")
19 19
 }
20 20
 
21
+func addRegistryAuthFlag(opt *bool, flags *pflag.FlagSet) {
22
+	flags.BoolVar(opt, "registry-auth", false, "Send registry authentication details to Swarm agents")
23
+}
24
+
21 25
 func loadBundlefile(stderr io.Writer, namespace string, path string) (*bundlefile.Bundlefile, error) {
22 26
 	defaultPath := fmt.Sprintf("%s.dab", namespace)
23 27
 
... ...
@@ -19,6 +19,7 @@ Create and update a stack from a Distributed Application Bundle (DAB)
19 19
 Options:
20 20
       --file   string   Path to a Distributed Application Bundle file (Default: STACK.dab)
21 21
       --help            Print usage
22
+      --registry-auth   Send registry authentication details to Swarm agents
22 23
 ```
23 24
 
24 25
 Create and update a stack from a `dab` file. This command has to be
... ...
@@ -46,6 +46,7 @@ Create and update a stack
46 46
 Options:
47 47
       --file   string   Path to a Distributed Application Bundle file (Default: STACK.dab)
48 48
       --help            Print usage
49
+      --registry-auth   Send registry authentication details to Swarm agents
49 50
 ```
50 51
 
51 52
 Let's deploy the stack created before: