/
simplestreams.go
276 lines (245 loc) · 10.5 KB
/
simplestreams.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The imagemetadata package supports locating, parsing, and filtering Ubuntu image metadata in simplestreams format.
// See http://launchpad.net/simplestreams and in particular the doc/README file in that project for more information
// about the file formats.
package imagemetadata
import (
"fmt"
"sort"
"github.com/juju/juju/environs/simplestreams"
"github.com/juju/juju/juju/arch"
"github.com/juju/juju/version"
)
func init() {
simplestreams.RegisterStructTags(ImageMetadata{})
}
const (
// ImageIds is the simplestreams image content type.
ImageIds = "image-ids"
// StreamsVersionV1 is used to construct the path for accessing streams data.
StreamsVersionV1 = "v1"
// IndexFileVersion is used to construct the streams index file.
IndexFileVersion = 1
// currentStreamsVersion is the current version of image simplestreams data.
currentStreamsVersion = StreamsVersionV1
)
// simplestreamsImagesPublicKey is the public key required to
// authenticate the simple streams data on http://cloud-images.ubuntu.com.
// Declared as a var so it can be overidden for testing.
// See http://bazaar.launchpad.net/~smoser/simplestreams/trunk/view/head:/examples/keys/cloud-images.pub
var simplestreamsImagesPublicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.12 (GNU/Linux)
mQINBFCMc9EBEADDKn9mOi9VZhW+0cxmu3aFZWMg0p7NEKuIokkEdd6P+BRITccO
ddDLaBuuamMbt/V1vrxWC5J+UXe33TwgO6KGfH+ECnXD5gYdEOyjVKkUyIzYV5RV
U5BMrxTukHuh+PkcMVUy5vossCk9MivtCRIqM6eRqfeXv6IBV9MFkAbG3x96ZNI/
TqaWTlaHGszz2Axf9JccHCNfb3muLI2uVnUaojtDiZPm9SHTn6O0p7Tz7M7+P8qy
vc6bdn5FYAk+Wbo+zejYVBG/HLLE4+fNZPESGVCWZtbZODBPxppTnNVm3E84CTFt
pmWFBvBE/q2G9e8s5/mP2ATrzLdUKMxr3vcbNX+NY1Uyvn0Z02PjbxThiz1G+4qh
6Ct7gprtwXPOB/bCITZL9YLrchwXiNgLLKcGF0XjlpD1hfELGi0aPZaHFLAa6qq8
Ro9WSJljY/Z0g3woj6sXpM9TdWe/zaWhxBGmteJl33WBV7a1GucN0zF1dHIvev4F
krp13Uej3bMWLKUWCmZ01OHStLASshTqVxIBj2rgsxIcqH66DKTSdZWyBQtgm/kC
qBvuoQLFfUgIlGZihTQ96YZXqn+VfBiFbpnh1vLt24CfnVdKmzibp48KkhfqduDE
Xxx/f/uZENH7t8xCuNd3p+u1zemGNnxuO8jxS6Ico3bvnJaG4DAl48vaBQARAQAB
tG9VYnVudHUgQ2xvdWQgSW1hZ2UgQnVpbGRlciAoQ2Fub25pY2FsIEludGVybmFs
IENsb3VkIEltYWdlIEJ1aWxkZXIpIDx1YnVudHUtY2xvdWRidWlsZGVyLW5vcmVw
bHlAY2Fub25pY2FsLmNvbT6JAjgEEwECACIFAlCMc9ECGwMGCwkIBwMCBhUIAgkK
CwQWAgMBAh4BAhe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9ySST0+OkWXYpbMOIARfBKyrGM3nu
=+MFT
-----END PGP PUBLIC KEY BLOCK-----
`
const (
// The location where Ubuntu cloud image metadata is published for
// public consumption.
UbuntuCloudImagesURL = "http://cloud-images.ubuntu.com"
// The path where released image metadata is found.
ReleasedImagesPath = "releases"
)
// This needs to be a var so we can override it for testing and in bootstrap.
var DefaultBaseURL = UbuntuCloudImagesURL
// PrivateMetadataDir is a directory possibly containing private image
// metadata, used during bootstrap.
var PrivateMetadataDir string
// ImageConstraint defines criteria used to find an image metadata record.
type ImageConstraint struct {
simplestreams.LookupParams
}
func NewImageConstraint(params simplestreams.LookupParams) *ImageConstraint {
if len(params.Series) == 0 {
params.Series = version.SupportedSeries()
}
if len(params.Arches) == 0 {
params.Arches = arch.AllSupportedArches
}
return &ImageConstraint{LookupParams: params}
}
const (
// Used to specify the released image metadata.
ReleasedStream = "released"
)
// idStream returns the string to use in making a product id
// for the given product stream.
func idStream(stream string) string {
idstream := ""
if stream != "" && stream != ReleasedStream {
idstream = "." + stream
}
return idstream
}
// IndexIds generates a string array representing product ids formed similarly to an ISCSI qualified name (IQN).
func (ic *ImageConstraint) IndexIds() []string {
// Image constraints do not filter on index ids.
return nil
}
// ProductIds generates a string array representing product ids formed similarly to an ISCSI qualified name (IQN).
func (ic *ImageConstraint) ProductIds() ([]string, error) {
stream := idStream(ic.Stream)
nrArches := len(ic.Arches)
nrSeries := len(ic.Series)
ids := make([]string, nrArches*nrSeries)
for i, arch := range ic.Arches {
for j, series := range ic.Series {
version, err := version.SeriesVersion(series)
if err != nil {
return nil, err
}
ids[j*nrArches+i] = fmt.Sprintf("com.ubuntu.cloud%s:server:%s:%s", stream, version, arch)
}
}
return ids, nil
}
// ImageMetadata holds information about a particular cloud image.
type ImageMetadata struct {
Id string `json:"id"`
Storage string `json:"root_store,omitempty"`
VirtType string `json:"virt,omitempty"`
Arch string `json:"arch,omitempty"`
Version string `json:"version,omitempty"`
RegionAlias string `json:"crsn,omitempty"`
RegionName string `json:"region,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Stream string `json:"-"`
}
func (im *ImageMetadata) String() string {
return fmt.Sprintf("%#v", im)
}
func (im *ImageMetadata) productId() string {
stream := idStream(im.Stream)
return fmt.Sprintf("com.ubuntu.cloud%s:server:%s:%s", stream, im.Version, im.Arch)
}
// Fetch returns a list of images for the specified cloud matching the constraint.
// The base URL locations are as specified - the first location which has a file is the one used.
// Signed data is preferred, but if there is no signed data available and onlySigned is false,
// then unsigned data is used.
func Fetch(
sources []simplestreams.DataSource, cons *ImageConstraint,
onlySigned bool) ([]*ImageMetadata, *simplestreams.ResolveInfo, error) {
params := simplestreams.GetMetadataParams{
StreamsVersion: currentStreamsVersion,
OnlySigned: onlySigned,
LookupConstraint: cons,
ValueParams: simplestreams.ValueParams{
DataType: ImageIds,
FilterFunc: appendMatchingImages,
ValueTemplate: ImageMetadata{},
PublicKey: simplestreamsImagesPublicKey,
},
}
items, resolveInfo, err := simplestreams.GetMetadata(sources, params)
if err != nil {
return nil, resolveInfo, err
}
metadata := make([]*ImageMetadata, len(items))
for i, md := range items {
metadata[i] = md.(*ImageMetadata)
}
// Sorting the metadata is not strictly necessary, but it ensures consistent ordering for
// all compilers, and it just makes it easier to look at the data.
Sort(metadata)
return metadata, resolveInfo, nil
}
// Sort sorts a slice of ImageMetadata in ascending order of their id
// in order to ensure the results of Fetch are ordered deterministically.
func Sort(metadata []*ImageMetadata) {
sort.Sort(byId(metadata))
}
type byId []*ImageMetadata
func (b byId) Len() int { return len(b) }
func (b byId) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byId) Less(i, j int) bool { return b[i].Id < b[j].Id }
type imageKey struct {
vtype string
arch string
version string
region string
storage string
}
// appendMatchingImages updates matchingImages with image metadata records from images which belong to the
// specified region. If an image already exists in matchingImages, it is not overwritten.
func appendMatchingImages(source simplestreams.DataSource, matchingImages []interface{},
images map[string]interface{}, cons simplestreams.LookupConstraint) ([]interface{}, error) {
imagesMap := make(map[imageKey]*ImageMetadata, len(matchingImages))
for _, val := range matchingImages {
im := val.(*ImageMetadata)
imagesMap[imageKey{im.VirtType, im.Arch, im.Version, im.RegionName, im.Storage}] = im
}
for _, val := range images {
im := val.(*ImageMetadata)
if cons != nil && cons.Params().Region != "" && cons.Params().Region != im.RegionName {
continue
}
if _, ok := imagesMap[imageKey{im.VirtType, im.Arch, im.Version, im.RegionName, im.Storage}]; !ok {
matchingImages = append(matchingImages, im)
}
}
return matchingImages, nil
}
// GetLatestImageIdMetadata is provided so it can be call by tests outside the imagemetadata package.
func GetLatestImageIdMetadata(data []byte, source simplestreams.DataSource, cons *ImageConstraint) ([]*ImageMetadata, error) {
metadata, err := simplestreams.ParseCloudMetadata(data, simplestreams.ProductFormat, "<unknown>", ImageMetadata{})
if err != nil {
return nil, err
}
items, err := simplestreams.GetLatestMetadata(metadata, cons, source, appendMatchingImages)
if err != nil {
return nil, err
}
result := make([]*ImageMetadata, len(items))
for i, md := range items {
result[i] = md.(*ImageMetadata)
}
return result, nil
}