OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /* | 5 /* |
6 Client side of for Chrome Infra Package Deployer. | 6 Client side of for Chrome Infra Package Deployer. |
7 | 7 |
8 TODO: write more. | 8 TODO: write more. |
9 | 9 |
10 Subcommand starting with 'pkg-' are low level commands operating on package | 10 Subcommand starting with 'pkg-' are low level commands operating on package |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 } | 215 } |
216 return out, nil | 216 return out, nil |
217 } | 217 } |
218 | 218 |
219 // All command line options are missing. | 219 // All command line options are missing. |
220 reportError("-pkg-def or -name/-in are required") | 220 reportError("-pkg-def or -name/-in are required") |
221 return out, cmdErr | 221 return out, cmdErr |
222 } | 222 } |
223 | 223 |
224 //////////////////////////////////////////////////////////////////////////////// | 224 //////////////////////////////////////////////////////////////////////////////// |
| 225 // TagsOptions mixin. |
| 226 |
| 227 // Tags holds array of '-tag' command line options. |
| 228 type Tags []string |
| 229 |
| 230 func (tags *Tags) String() string { |
| 231 // String() for empty vars used in -help output. |
| 232 if len(*tags) == 0 { |
| 233 return "key:value" |
| 234 } |
| 235 return strings.Join(*tags, " ") |
| 236 } |
| 237 |
| 238 // Set is called by 'flag' package when parsing command line options. |
| 239 func (tags *Tags) Set(value string) error { |
| 240 err := cipd.ValidateInstanceTag(value) |
| 241 if err != nil { |
| 242 return err |
| 243 } |
| 244 *tags = append(*tags, value) |
| 245 return nil |
| 246 } |
| 247 |
| 248 // TagsOptions defines command line arguments for commands that accept a set |
| 249 // of tags. |
| 250 type TagsOptions struct { |
| 251 // Set of tags to attach to the package instance. |
| 252 tags Tags |
| 253 } |
| 254 |
| 255 func (opts *TagsOptions) registerFlags(f *flag.FlagSet) { |
| 256 opts.tags = []string{} |
| 257 f.Var(&opts.tags, "tag", "tag to attach to the package instance") |
| 258 } |
| 259 |
| 260 //////////////////////////////////////////////////////////////////////////////// |
225 // JSONOutputOptions mixin. | 261 // JSONOutputOptions mixin. |
226 | 262 |
227 // PackageInfo is put into JSON output by subcommands. It describes a built | 263 // PackageInfo is put into JSON output by subcommands. It describes a built |
228 // package instance. | 264 // package instance. |
229 type PackageInfo struct { | 265 type PackageInfo struct { |
230 Package string `json:"package"` | 266 Package string `json:"package"` |
231 InstanceID string `json:"instance_id"` | 267 InstanceID string `json:"instance_id"` |
232 } | 268 } |
233 | 269 |
234 // JSONOutputOptions define -json-output option that is used to return | 270 // JSONOutputOptions define -json-output option that is used to return |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 //////////////////////////////////////////////////////////////////////////////// | 318 //////////////////////////////////////////////////////////////////////////////// |
283 // 'create' subcommand. | 319 // 'create' subcommand. |
284 | 320 |
285 var cmdCreate = &subcommands.Command{ | 321 var cmdCreate = &subcommands.Command{ |
286 UsageLine: "create [options]", | 322 UsageLine: "create [options]", |
287 ShortDesc: "builds and uploads a package instance file", | 323 ShortDesc: "builds and uploads a package instance file", |
288 LongDesc: "Builds and uploads a package instance file.", | 324 LongDesc: "Builds and uploads a package instance file.", |
289 CommandRun: func() subcommands.CommandRun { | 325 CommandRun: func() subcommands.CommandRun { |
290 c := &createRun{} | 326 c := &createRun{} |
291 c.InputOptions.registerFlags(&c.Flags) | 327 c.InputOptions.registerFlags(&c.Flags) |
| 328 c.TagsOptions.registerFlags(&c.Flags) |
292 c.ServiceOptions.registerFlags(&c.Flags) | 329 c.ServiceOptions.registerFlags(&c.Flags) |
293 c.JSONOutputOptions.registerFlags(&c.Flags) | 330 c.JSONOutputOptions.registerFlags(&c.Flags) |
294 return c | 331 return c |
295 }, | 332 }, |
296 } | 333 } |
297 | 334 |
298 type createRun struct { | 335 type createRun struct { |
299 subcommands.CommandRunBase | 336 subcommands.CommandRunBase |
300 InputOptions | 337 InputOptions |
| 338 TagsOptions |
301 ServiceOptions | 339 ServiceOptions |
302 JSONOutputOptions | 340 JSONOutputOptions |
303 } | 341 } |
304 | 342 |
305 func (c *createRun) Run(a subcommands.Application, args []string) int { | 343 func (c *createRun) Run(a subcommands.Application, args []string) int { |
306 if !checkCommandLine(args, c.GetFlags(), 0) { | 344 if !checkCommandLine(args, c.GetFlags(), 0) { |
307 return 1 | 345 return 1 |
308 } | 346 } |
309 » info, err := buildAndUploadInstance(c.InputOptions, c.ServiceOptions) | 347 » info, err := buildAndUploadInstance(c.InputOptions, c.TagsOptions, c.Ser
viceOptions) |
310 err = c.writeJSONOutput(&info, err) | 348 err = c.writeJSONOutput(&info, err) |
311 if err != nil { | 349 if err != nil { |
312 reportError("Error while uploading the package: %s", err) | 350 reportError("Error while uploading the package: %s", err) |
313 return 1 | 351 return 1 |
314 } | 352 } |
315 return 0 | 353 return 0 |
316 } | 354 } |
317 | 355 |
318 func buildAndUploadInstance(inputOpts InputOptions, serviceOpts ServiceOptions)
(PackageInfo, error) { | 356 func buildAndUploadInstance(inputOpts InputOptions, tagsOpts TagsOptions, servic
eOpts ServiceOptions) (PackageInfo, error) { |
319 f, err := ioutil.TempFile("", "cipd_pkg") | 357 f, err := ioutil.TempFile("", "cipd_pkg") |
320 if err != nil { | 358 if err != nil { |
321 return PackageInfo{}, err | 359 return PackageInfo{}, err |
322 } | 360 } |
323 defer func() { | 361 defer func() { |
324 f.Close() | 362 f.Close() |
325 os.Remove(f.Name()) | 363 os.Remove(f.Name()) |
326 }() | 364 }() |
327 err = buildInstanceFile(f.Name(), inputOpts) | 365 err = buildInstanceFile(f.Name(), inputOpts) |
328 if err != nil { | 366 if err != nil { |
329 return PackageInfo{}, err | 367 return PackageInfo{}, err |
330 } | 368 } |
331 » return registerInstanceFile(f.Name(), serviceOpts) | 369 » return registerInstanceFile(f.Name(), tagsOpts, serviceOpts) |
332 } | 370 } |
333 | 371 |
334 //////////////////////////////////////////////////////////////////////////////// | 372 //////////////////////////////////////////////////////////////////////////////// |
335 // 'ensure' subcommand. | 373 // 'ensure' subcommand. |
336 | 374 |
337 var cmdEnsure = &subcommands.Command{ | 375 var cmdEnsure = &subcommands.Command{ |
338 UsageLine: "ensure [options]", | 376 UsageLine: "ensure [options]", |
339 ShortDesc: "installs, removes and updates packages", | 377 ShortDesc: "installs, removes and updates packages", |
340 LongDesc: "Installs, removes and updates packages.", | 378 LongDesc: "Installs, removes and updates packages.", |
341 CommandRun: func() subcommands.CommandRun { | 379 CommandRun: func() subcommands.CommandRun { |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 | 858 |
821 //////////////////////////////////////////////////////////////////////////////// | 859 //////////////////////////////////////////////////////////////////////////////// |
822 // 'pkg-register' subcommand. | 860 // 'pkg-register' subcommand. |
823 | 861 |
824 var cmdRegister = &subcommands.Command{ | 862 var cmdRegister = &subcommands.Command{ |
825 UsageLine: "pkg-register <package instance file>", | 863 UsageLine: "pkg-register <package instance file>", |
826 ShortDesc: "uploads and registers package instance in the package reposi
tory", | 864 ShortDesc: "uploads and registers package instance in the package reposi
tory", |
827 LongDesc: "Uploads and registers package instance in the package reposi
tory.", | 865 LongDesc: "Uploads and registers package instance in the package reposi
tory.", |
828 CommandRun: func() subcommands.CommandRun { | 866 CommandRun: func() subcommands.CommandRun { |
829 c := ®isterRun{} | 867 c := ®isterRun{} |
| 868 c.TagsOptions.registerFlags(&c.Flags) |
830 c.ServiceOptions.registerFlags(&c.Flags) | 869 c.ServiceOptions.registerFlags(&c.Flags) |
831 c.JSONOutputOptions.registerFlags(&c.Flags) | 870 c.JSONOutputOptions.registerFlags(&c.Flags) |
832 return c | 871 return c |
833 }, | 872 }, |
834 } | 873 } |
835 | 874 |
836 type registerRun struct { | 875 type registerRun struct { |
837 subcommands.CommandRunBase | 876 subcommands.CommandRunBase |
| 877 TagsOptions |
838 ServiceOptions | 878 ServiceOptions |
839 JSONOutputOptions | 879 JSONOutputOptions |
840 } | 880 } |
841 | 881 |
842 func (c *registerRun) Run(a subcommands.Application, args []string) int { | 882 func (c *registerRun) Run(a subcommands.Application, args []string) int { |
843 if !checkCommandLine(args, c.GetFlags(), 1) { | 883 if !checkCommandLine(args, c.GetFlags(), 1) { |
844 return 1 | 884 return 1 |
845 } | 885 } |
846 » info, err := registerInstanceFile(args[0], c.ServiceOptions) | 886 » info, err := registerInstanceFile(args[0], c.TagsOptions, c.ServiceOptio
ns) |
847 err = c.writeJSONOutput(&info, err) | 887 err = c.writeJSONOutput(&info, err) |
848 if err != nil { | 888 if err != nil { |
849 reportError("Error while registering the package: %s", err) | 889 reportError("Error while registering the package: %s", err) |
850 return 1 | 890 return 1 |
851 } | 891 } |
852 return 0 | 892 return 0 |
853 } | 893 } |
854 | 894 |
855 func registerInstanceFile(instanceFile string, serviceOpts ServiceOptions) (Pack
ageInfo, error) { | 895 func registerInstanceFile(instanceFile string, tagsOpts TagsOptions, serviceOpts
ServiceOptions) (PackageInfo, error) { |
856 inst, err := cipd.OpenInstanceFile(instanceFile, "") | 896 inst, err := cipd.OpenInstanceFile(instanceFile, "") |
857 if err != nil { | 897 if err != nil { |
858 return PackageInfo{}, err | 898 return PackageInfo{}, err |
859 } | 899 } |
860 defer inst.Close() | 900 defer inst.Close() |
861 client, err := serviceOpts.makeClient() | 901 client, err := serviceOpts.makeClient() |
862 if err != nil { | 902 if err != nil { |
863 return PackageInfo{}, err | 903 return PackageInfo{}, err |
864 } | 904 } |
865 logging.Infof("Registering package %s:%s", inst.PackageName(), inst.Inst
anceID()) | 905 logging.Infof("Registering package %s:%s", inst.PackageName(), inst.Inst
anceID()) |
866 info := inspectInstance(inst, false) | 906 info := inspectInstance(inst, false) |
867 return info, cipd.RegisterInstance(cipd.RegisterInstanceOptions{ | 907 return info, cipd.RegisterInstance(cipd.RegisterInstanceOptions{ |
868 PackageInstance: inst, | 908 PackageInstance: inst, |
| 909 Tags: tagsOpts.tags, |
869 UploadOptions: cipd.UploadOptions{ | 910 UploadOptions: cipd.UploadOptions{ |
870 ServiceURL: serviceOpts.serviceURL, | 911 ServiceURL: serviceOpts.serviceURL, |
871 Client: client, | 912 Client: client, |
872 }, | 913 }, |
873 }) | 914 }) |
874 } | 915 } |
875 | 916 |
876 //////////////////////////////////////////////////////////////////////////////// | 917 //////////////////////////////////////////////////////////////////////////////// |
877 // Main. | 918 // Main. |
878 | 919 |
(...skipping 21 matching lines...) Expand all Loading... |
900 cmdDeploy, | 941 cmdDeploy, |
901 cmdFetch, | 942 cmdFetch, |
902 cmdInspect, | 943 cmdInspect, |
903 cmdRegister, | 944 cmdRegister, |
904 }, | 945 }, |
905 } | 946 } |
906 | 947 |
907 func main() { | 948 func main() { |
908 os.Exit(subcommands.Run(application, nil)) | 949 os.Exit(subcommands.Run(application, nil)) |
909 } | 950 } |
OLD | NEW |