1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
"launchpad.net/gnuflag"
14
"launchpad.net/juju-core/cmd"
15
"launchpad.net/juju-core/environs"
16
"launchpad.net/juju-core/environs/simplestreams"
17
"launchpad.net/juju-core/environs/tools"
18
"launchpad.net/juju-core/version"
21
// ValidateToolsMetadataCommand
22
type ValidateToolsMetadataCommand struct {
32
var validateToolsMetadataDoc = `
33
validate-tools loads simplestreams metadata and validates the contents by looking for tools
34
belonging to the specified series, version, and architecture, for the specified cloud.
36
The cloud specificaton comes from the current Juju environment, as specified in the usual way
37
from either ~/.juju/environments.yaml, the -e option, or JUJU_ENV. Series, Region, and Endpoint
38
are the key attributes.
40
The key environment attributes may be overridden using command arguments, so that the validation
41
may be peformed on arbitary metadata.
45
- validate using the current environment settings but with series raring
46
juju metadata validate-tools -s raring
48
- validate using the current environment settings but with Juju version 1.11.4
49
juju metadata validate-tools -j 1.11.4
51
- validate using the current environment settings and list all tools found for any series
52
juju metadata validate-tools --series=
54
- validate using the current environment settings but with series raring and using metadata from local directory
55
juju metadata validate-images -s raring -d <some directory>
57
A key use case is to validate newly generated metadata prior to deployment to production.
58
In this case, the metadata is placed in a local directory, a cloud provider type is specified (ec2, openstack etc),
59
and the validation is performed for each supported series, version, and arcgitecture.
65
juju metadata validate-tools -p ec2 -r us-east-1 -s precise --juju-version 1.12.0 -d <some directory>
67
[ $RETVAL -eq 0 ] && echo Success
68
[ $RETVAL -ne 0 ] && echo Failure
71
func (c *ValidateToolsMetadataCommand) Info() *cmd.Info {
73
Name: "validate-tools",
74
Purpose: "validate tools metadata and ensure tools tarball(s) exist for Juju version(s)",
75
Doc: validateToolsMetadataDoc,
79
func (c *ValidateToolsMetadataCommand) SetFlags(f *gnuflag.FlagSet) {
80
c.EnvCommandBase.SetFlags(f)
81
f.StringVar(&c.providerType, "p", "", "the provider type eg ec2, openstack")
82
f.StringVar(&c.metadataDir, "d", "", "directory where metadata files are found")
83
f.StringVar(&c.series, "s", "", "the series for which to validate (overrides env config series)")
84
f.StringVar(&c.series, "series", "", "")
85
f.StringVar(&c.region, "r", "", "the region for which to validate (overrides env config region)")
86
f.StringVar(&c.endpoint, "u", "", "the cloud endpoint URL for which to validate (overrides env config endpoint)")
87
f.StringVar(&c.version, "j", "current", "the Juju version (use 'current' for current version)")
88
f.StringVar(&c.version, "juju-version", "", "")
91
func (c *ValidateToolsMetadataCommand) Init(args []string) error {
92
if c.providerType != "" {
94
return fmt.Errorf("region required if provider type is specified")
96
if c.metadataDir == "" {
97
return fmt.Errorf("metadata directory required if provider type is specified")
100
if c.version == "current" {
101
c.version = version.CurrentNumber().String()
103
return c.EnvCommandBase.Init(args)
106
func (c *ValidateToolsMetadataCommand) Run(context *cmd.Context) error {
107
var params *simplestreams.MetadataLookupParams
109
if c.providerType == "" {
110
environ, err := environs.NewFromName(c.EnvName)
114
mdLookup, ok := environ.(simplestreams.MetadataValidator)
116
return fmt.Errorf("%s provider does not support tools metadata validation", environ.Config().Type())
118
params, err = mdLookup.MetadataLookupParams(c.region)
123
prov, err := environs.Provider(c.providerType)
127
mdLookup, ok := prov.(simplestreams.MetadataValidator)
129
return fmt.Errorf("%s provider does not support tools metadata validation", c.providerType)
131
params, err = mdLookup.MetadataLookupParams(c.region)
138
params.Series = c.series
141
params.Region = c.region
143
if c.endpoint != "" {
144
params.Endpoint = c.endpoint
146
// If the metadata files are to be loaded from a directory, we need to register
147
// a file http transport.
148
if c.metadataDir != "" {
149
if _, err := os.Stat(c.metadataDir); err != nil {
153
params.BaseURLs = []string{"file://" + c.metadataDir}
154
t := &http.Transport{}
155
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
156
c := &http.Client{Transport: t}
157
simplestreams.SetHttpClient(c)
160
versions, err := tools.ValidateToolsMetadata(&tools.ToolsMetadataLookupParams{
161
MetadataLookupParams: *params,
168
if len(versions) > 0 {
169
fmt.Fprintf(context.Stdout, "matching tools versions:\n%s\n", strings.Join(versions, "\n"))
171
return fmt.Errorf("no matching tools using URLs:\n%s", strings.Join(params.BaseURLs, "\n"))