-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagemetadata.go
189 lines (170 loc) · 5.92 KB
/
imagemetadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/juju/cmd"
"github.com/juju/errors"
"launchpad.net/gnuflag"
"github.com/juju/juju/cmd/envcmd"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/configstore"
"github.com/juju/juju/environs/filestorage"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/environs/simplestreams"
"github.com/juju/juju/environs/storage"
"github.com/juju/juju/juju/arch"
)
type ImageMetadataCommandBase struct {
envcmd.EnvCommandBase
}
func (c *ImageMetadataCommandBase) prepare(context *cmd.Context, store configstore.Storage) (environs.Environ, error) {
cfg, err := c.Config(store)
if err != nil {
return nil, errors.Annotate(err, "could not get config from store")
}
// We are preparing an environment to access parameters needed to access
// image metadata. We don't need, nor want, credential verification.
// In most cases, credentials will not be available.
ctx := envcmd.BootstrapContextNoVerify(context)
return environs.Prepare(cfg, ctx, store)
}
// ImageMetadataCommand is used to write out simplestreams image metadata information.
type ImageMetadataCommand struct {
ImageMetadataCommandBase
Dir string
Series string
Arch string
ImageId string
Region string
Endpoint string
privateStorage string
}
var imageMetadataDoc = `
generate-image creates simplestreams image metadata for the specified cloud.
The cloud specification comes from the current Juju environment, as specified in
the usual way from either ~/.juju/environments.yaml, the -e option, or JUJU_ENV.
Using command arguments, it is possible to override cloud attributes region, endpoint, and series.
By default, "amd64" is used for the architecture but this may also be changed.
`
func (c *ImageMetadataCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "generate-image",
Purpose: "generate simplestreams image metadata",
Doc: imageMetadataDoc,
}
}
func (c *ImageMetadataCommand) SetFlags(f *gnuflag.FlagSet) {
f.StringVar(&c.Series, "s", "", "the charm series")
f.StringVar(&c.Arch, "a", arch.AMD64, "the image achitecture")
f.StringVar(&c.Dir, "d", "", "the destination directory in which to place the metadata files")
f.StringVar(&c.ImageId, "i", "", "the image id")
f.StringVar(&c.Region, "r", "", "the region")
f.StringVar(&c.Endpoint, "u", "", "the cloud endpoint (for Openstack, this is the Identity Service endpoint)")
}
// setParams sets parameters based on the environment configuration
// for those which have not been explicitly specified.
func (c *ImageMetadataCommand) setParams(context *cmd.Context) error {
c.privateStorage = "<private storage name>"
var environ environs.Environ
if store, err := configstore.Default(); err == nil {
if environ, err = c.prepare(context, store); err == nil {
logger.Infof("creating image metadata for environment %q", environ.Config().Name())
// If the user has not specified region and endpoint, try and get it from the environment.
if c.Region == "" || c.Endpoint == "" {
var cloudSpec simplestreams.CloudSpec
if inst, ok := environ.(simplestreams.HasRegion); ok {
if cloudSpec, err = inst.Region(); err != nil {
return err
}
} else {
return errors.Errorf("environment %q cannot provide region and endpoint", environ.Config().Name())
}
// If only one of region or endpoint is provided, that is a problem.
if cloudSpec.Region != cloudSpec.Endpoint && (cloudSpec.Region == "" || cloudSpec.Endpoint == "") {
return errors.Errorf("cannot generate metadata without a complete cloud configuration")
}
if c.Region == "" {
c.Region = cloudSpec.Region
}
if c.Endpoint == "" {
c.Endpoint = cloudSpec.Endpoint
}
}
cfg := environ.Config()
if c.Series == "" {
c.Series = config.PreferredSeries(cfg)
}
if v, ok := cfg.AllAttrs()["control-bucket"]; ok {
c.privateStorage = v.(string)
}
} else {
logger.Warningf("environment could not be opened: %v", err)
}
}
if environ == nil {
logger.Infof("no environment found, creating image metadata using user supplied data")
}
if c.Series == "" {
c.Series = config.LatestLtsSeries()
}
if c.ImageId == "" {
return errors.Errorf("image id must be specified")
}
if c.Region == "" {
return errors.Errorf("image region must be specified")
}
if c.Endpoint == "" {
return errors.Errorf("cloud endpoint URL must be specified")
}
if c.Dir == "" {
logger.Infof("no destination directory specified, using current directory")
var err error
if c.Dir, err = os.Getwd(); err != nil {
return err
}
}
return nil
}
var helpDoc = `
image metadata files have been written to:
%s.
For Juju to use this metadata, the files need to be put into the
image metadata search path. There are 2 options:
1. Use the --metadata-source parameter when bootstrapping:
juju bootstrap --metadata-source %s
2. Use image-metadata-url in $JUJU_HOME/environments.yaml
Configure a http server to serve the contents of
%s
and set the value of image-metadata-url accordingly.
"
`
func (c *ImageMetadataCommand) Run(context *cmd.Context) error {
if err := c.setParams(context); err != nil {
return err
}
out := context.Stdout
im := &imagemetadata.ImageMetadata{
Id: c.ImageId,
Arch: c.Arch,
}
cloudSpec := simplestreams.CloudSpec{
Region: c.Region,
Endpoint: c.Endpoint,
}
targetStorage, err := filestorage.NewFileStorageWriter(c.Dir)
if err != nil {
return err
}
err = imagemetadata.MergeAndWriteMetadata(c.Series, []*imagemetadata.ImageMetadata{im}, &cloudSpec, targetStorage)
if err != nil {
return fmt.Errorf("image metadata files could not be created: %v", err)
}
dir := context.AbsPath(c.Dir)
dest := filepath.Join(dir, storage.BaseImagesPath, "streams", "v1")
fmt.Fprintf(out, fmt.Sprintf(helpDoc, dest, dir, dir))
return nil
}