-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
442 lines (401 loc) · 15 KB
/
sync.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package sync
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/juju/errors"
"github.com/juju/loggo"
jujuseries "github.com/juju/os/series"
"github.com/juju/utils"
"github.com/juju/version"
"github.com/juju/juju/environs/filestorage"
"github.com/juju/juju/environs/simplestreams"
"github.com/juju/juju/environs/storage"
envtools "github.com/juju/juju/environs/tools"
"github.com/juju/juju/juju/keys"
coretools "github.com/juju/juju/tools"
jujuversion "github.com/juju/juju/version"
)
var logger = loggo.GetLogger("juju.environs.sync")
// SyncContext describes the context for tool synchronization.
type SyncContext struct {
// TargetToolsFinder is a ToolsFinder provided to find existing
// tools in the target destination.
TargetToolsFinder ToolsFinder
// TargetToolsUploader is a ToolsUploader provided to upload
// tools to the target destination.
TargetToolsUploader ToolsUploader
// AllVersions controls the copy of all versions, not only the latest.
AllVersions bool
// Copy tools with major version, if MajorVersion > 0.
MajorVersion int
// Copy tools with minor version, if MinorVersion > 0.
MinorVersion int
// DryRun controls that nothing is copied. Instead it's logged
// what would be coppied.
DryRun bool
// Stream specifies the simplestreams stream to use (defaults to "Released").
Stream string
// Source, if non-empty, specifies a directory in the local file system
// to use as a source.
Source string
}
// ToolsFinder provides an interface for finding tools of a specified version.
type ToolsFinder interface {
// FindTools returns a list of tools with the specified major version in the specified stream.
FindTools(major int, stream string) (coretools.List, error)
}
// ToolsUploader provides an interface for uploading tools and associated
// metadata.
type ToolsUploader interface {
// UploadTools uploads the tools with the specified version and tarball contents.
UploadTools(toolsDir, stream string, tools *coretools.Tools, data []byte) error
}
// SyncTools copies the Juju tools tarball from the official bucket
// or a specified source directory into the user's environment.
func SyncTools(syncContext *SyncContext) error {
sourceDataSource, err := selectSourceDatasource(syncContext)
if err != nil {
return errors.Trace(err)
}
logger.Infof("listing available agent binaries")
if syncContext.MajorVersion == 0 && syncContext.MinorVersion == 0 {
syncContext.MajorVersion = jujuversion.Current.Major
syncContext.MinorVersion = -1
if !syncContext.AllVersions {
syncContext.MinorVersion = jujuversion.Current.Minor
}
}
toolsDir := syncContext.Stream
// If no stream has been specified, assume "released" for non-devel versions of Juju.
if syncContext.Stream == "" {
// We now store the tools in a directory named after their stream, but the
// legacy behaviour is to store all tools in a single "releases" directory.
toolsDir = envtools.ReleasedStream
// Always use the primary stream here - the user can specify
// to override that decision.
syncContext.Stream = envtools.PreferredStreams(&jujuversion.Current, false, "")[0]
}
// For backwards compatibility with cloud storage, if there are no tools in the specified stream,
// double check the release stream.
// TODO - remove this when we no longer need to support cloud storage upgrades.
streams := []string{syncContext.Stream, envtools.ReleasedStream}
sourceTools, err := envtools.FindToolsForCloud(
[]simplestreams.DataSource{sourceDataSource}, simplestreams.CloudSpec{},
streams, syncContext.MajorVersion, syncContext.MinorVersion, coretools.Filter{})
if err != nil {
return errors.Trace(err)
}
logger.Infof("found %d agent binaries", len(sourceTools))
if !syncContext.AllVersions {
var latest version.Number
latest, sourceTools = sourceTools.Newest()
logger.Infof("found %d recent agent binaries (version %s)", len(sourceTools), latest)
}
for _, tool := range sourceTools {
logger.Debugf("found source agent binary: %v", tool)
}
logger.Infof("listing target agent binaries storage")
targetTools, err := syncContext.TargetToolsFinder.FindTools(syncContext.MajorVersion, syncContext.Stream)
switch err {
case nil, coretools.ErrNoMatches, envtools.ErrNoTools:
default:
return errors.Trace(err)
}
for _, tool := range targetTools {
logger.Debugf("found target agent binary: %v", tool)
}
missing := sourceTools.Exclude(targetTools)
logger.Infof("found %d agent binaries in target; %d agent binaries to be copied", len(targetTools), len(missing))
if syncContext.DryRun {
for _, tools := range missing {
logger.Infof("copying %s from %s", tools.Version, tools.URL)
}
return nil
}
err = copyTools(toolsDir, syncContext.Stream, missing, syncContext.TargetToolsUploader)
if err != nil {
return err
}
logger.Infof("copied %d agent binaries", len(missing))
return nil
}
// selectSourceDatasource returns a storage reader based on the source setting.
func selectSourceDatasource(syncContext *SyncContext) (simplestreams.DataSource, error) {
source := syncContext.Source
if source == "" {
source = envtools.DefaultBaseURL
}
sourceURL, err := envtools.ToolsURL(source)
if err != nil {
return nil, err
}
logger.Infof("source for sync of agent binaries: %v", sourceURL)
config := simplestreams.Config{
Description: "sync agent binaries source",
BaseURL: sourceURL,
PublicSigningKey: keys.JujuPublicKey,
HostnameVerification: utils.VerifySSLHostnames,
Priority: simplestreams.CUSTOM_CLOUD_DATA,
}
if err := config.Validate(); err != nil {
return nil, errors.Annotate(err, "simplestreams config validation failed")
}
return simplestreams.NewDataSource(config), nil
}
// copyTools copies a set of tools from the source to the target.
func copyTools(toolsDir, stream string, tools []*coretools.Tools, u ToolsUploader) error {
for _, tool := range tools {
logger.Infof("copying %s from %s", tool.Version, tool.URL)
if err := copyOneToolsPackage(toolsDir, stream, tool, u); err != nil {
return err
}
}
return nil
}
// copyOneToolsPackage copies one tool from the source to the target.
func copyOneToolsPackage(toolsDir, stream string, tools *coretools.Tools, u ToolsUploader) error {
toolsName := envtools.StorageName(tools.Version, toolsDir)
logger.Infof("downloading %q %v (%v)", stream, toolsName, tools.URL)
resp, err := utils.GetValidatingHTTPClient().Get(tools.URL)
if err != nil {
return err
}
defer resp.Body.Close()
// Verify SHA-256 hash.
var buf bytes.Buffer
sha256, size, err := utils.ReadSHA256(io.TeeReader(resp.Body, &buf))
if err != nil {
return err
}
if tools.SHA256 == "" {
logger.Errorf("no SHA-256 hash for %v", tools.SHA256) // TODO(dfc) can you spot the bug ?
} else if sha256 != tools.SHA256 {
return errors.Errorf("SHA-256 hash mismatch (%v/%v)", sha256, tools.SHA256)
}
sizeInKB := (size + 512) / 1024
logger.Infof("uploading %v (%dkB) to model", toolsName, sizeInKB)
return u.UploadTools(toolsDir, stream, tools, buf.Bytes())
}
// UploadFunc is the type of Upload, which may be
// reassigned to control the behaviour of tools
// uploading.
type UploadFunc func(stor storage.Storage, stream string, forceVersion *version.Number, series ...string) (*coretools.Tools, error)
// Exported for testing.
var Upload UploadFunc = upload
// upload builds whatever version of github.com/juju/juju is in $GOPATH,
// uploads it to the given storage, and returns a Tools instance describing
// them. If forceVersion is not nil, the uploaded tools bundle will report
// the given version number; if any fakeSeries are supplied, additional copies
// of the built tools will be uploaded for use by machines of those series.
// Juju tools built for one series do not necessarily run on another, but this
// func exists only for development use cases.
func upload(stor storage.Storage, stream string, forceVersion *version.Number, fakeSeries ...string) (*coretools.Tools, error) {
builtTools, err := BuildAgentTarball(true, forceVersion, stream)
if err != nil {
return nil, err
}
defer os.RemoveAll(builtTools.Dir)
logger.Debugf("Uploading agent binaries for %v", fakeSeries)
return syncBuiltTools(stor, stream, builtTools, fakeSeries...)
}
// cloneToolsForSeries copies the built tools tarball into a tarball for the specified
// stream and series and generates corresponding metadata.
func cloneToolsForSeries(toolsInfo *BuiltAgent, stream string, series ...string) error {
// Copy the tools to the target storage, recording a Tools struct for each one.
var targetTools coretools.List
targetTools = append(targetTools, &coretools.Tools{
Version: toolsInfo.Version,
Size: toolsInfo.Size,
SHA256: toolsInfo.Sha256Hash,
})
putTools := func(vers version.Binary) (string, error) {
name := envtools.StorageName(vers, stream)
src := filepath.Join(toolsInfo.Dir, toolsInfo.StorageName)
dest := filepath.Join(toolsInfo.Dir, name)
destDir := filepath.Dir(dest)
if err := os.MkdirAll(destDir, 0755); err != nil {
return "", err
}
if err := utils.CopyFile(dest, src); err != nil {
return "", err
}
// Append to targetTools the attributes required to write out tools metadata.
targetTools = append(targetTools, &coretools.Tools{
Version: vers,
Size: toolsInfo.Size,
SHA256: toolsInfo.Sha256Hash,
})
return name, nil
}
logger.Debugf("generating tarballs for %v", series)
for _, series := range series {
_, err := jujuseries.SeriesVersion(series)
if err != nil {
return err
}
if series != toolsInfo.Version.Series {
fakeVersion := toolsInfo.Version
fakeVersion.Series = series
if _, err := putTools(fakeVersion); err != nil {
return err
}
}
}
// The tools have been copied to a temp location from which they will be uploaded,
// now write out the matching simplestreams metadata so that SyncTools can find them.
metadataStore, err := filestorage.NewFileStorageWriter(toolsInfo.Dir)
if err != nil {
return err
}
logger.Debugf("generating agent metadata")
return envtools.MergeAndWriteMetadata(metadataStore, stream, stream, targetTools, false)
}
// BuiltAgent contains metadata for a tools tarball resulting from
// a call to BundleTools.
type BuiltAgent struct {
Version version.Binary
Official bool
Dir string
StorageName string
Sha256Hash string
Size int64
}
// BuildAgentTarballFunc is a function which can build an agent tarball.
type BuildAgentTarballFunc func(build bool, forceVersion *version.Number, stream string) (*BuiltAgent, error)
// Override for testing.
var BuildAgentTarball BuildAgentTarballFunc = buildAgentTarball
// BuildAgentTarball bundles an agent tarball and places it in a temp directory in
// the expected agent path.
func buildAgentTarball(build bool, forceVersion *version.Number, stream string) (_ *BuiltAgent, err error) {
// TODO(rog) find binaries from $PATH when not using a development
// version of juju within a $GOPATH.
logger.Debugf("Making agent binary tarball")
// We create the entire archive before asking the environment to
// start uploading so that we can be sure we have archived
// correctly.
f, err := ioutil.TempFile("", "juju-tgz")
if err != nil {
return nil, err
}
defer f.Close()
defer os.Remove(f.Name())
toolsVersion, official, sha256Hash, err := envtools.BundleTools(build, f, forceVersion)
if err != nil {
return nil, err
}
// Built agent version needs to match the client used to bootstrap.
builtVersion := toolsVersion
builtVersion.Build = 0
clientVersion := jujuversion.Current
clientVersion.Build = 0
if builtVersion.Number.Compare(clientVersion) != 0 {
return nil, errors.Errorf("agent binary %v not compatible with bootstrap client %v", toolsVersion.Number, jujuversion.Current)
}
fileInfo, err := f.Stat()
if err != nil {
return nil, errors.Errorf("cannot stat newly made agent binary archive: %v", err)
}
size := fileInfo.Size()
reportedVersion := toolsVersion
if !official && forceVersion != nil {
reportedVersion.Number = *forceVersion
}
if official {
logger.Infof("using official agent binary %v (%dkB)", toolsVersion, (size+512)/1024)
} else {
logger.Infof("using agent binary %v aliased to %v (%dkB)", toolsVersion, reportedVersion, (size+512)/1024)
}
baseToolsDir, err := ioutil.TempDir("", "juju-tools")
if err != nil {
return nil, err
}
// If we exit with an error, clean up the built tools directory.
defer func() {
if err != nil {
os.RemoveAll(baseToolsDir)
}
}()
err = os.MkdirAll(filepath.Join(baseToolsDir, storage.BaseToolsPath, stream), 0755)
if err != nil {
return nil, err
}
storageName := envtools.StorageName(toolsVersion, stream)
err = utils.CopyFile(filepath.Join(baseToolsDir, storageName), f.Name())
if err != nil {
return nil, err
}
return &BuiltAgent{
Version: toolsVersion,
Official: official,
Dir: baseToolsDir,
StorageName: storageName,
Size: size,
Sha256Hash: sha256Hash,
}, nil
}
// syncBuiltTools copies to storage a tools tarball and cloned copies for each series.
func syncBuiltTools(stor storage.Storage, stream string, builtTools *BuiltAgent, fakeSeries ...string) (*coretools.Tools, error) {
if err := cloneToolsForSeries(builtTools, stream, fakeSeries...); err != nil {
return nil, err
}
syncContext := &SyncContext{
Source: builtTools.Dir,
TargetToolsFinder: StorageToolsFinder{stor},
TargetToolsUploader: StorageToolsUploader{stor, false, false},
AllVersions: true,
Stream: stream,
MajorVersion: builtTools.Version.Major,
MinorVersion: -1,
}
logger.Debugf("uploading agent binaries to cloud storage")
err := SyncTools(syncContext)
if err != nil {
return nil, err
}
url, err := stor.URL(builtTools.StorageName)
if err != nil {
return nil, err
}
return &coretools.Tools{
Version: builtTools.Version,
URL: url,
Size: builtTools.Size,
SHA256: builtTools.Sha256Hash,
}, nil
}
// StorageToolsFinder is an implementation of ToolsFinder
// that searches for tools in the specified storage.
type StorageToolsFinder struct {
Storage storage.StorageReader
}
func (f StorageToolsFinder) FindTools(major int, stream string) (coretools.List, error) {
return envtools.ReadList(f.Storage, stream, major, -1)
}
// StorageToolsUplader is an implementation of ToolsUploader that
// writes tools to the provided storage and then writes merged
// metadata, optionally with mirrors.
type StorageToolsUploader struct {
Storage storage.Storage
WriteMetadata bool
WriteMirrors envtools.ShouldWriteMirrors
}
func (u StorageToolsUploader) UploadTools(toolsDir, stream string, tools *coretools.Tools, data []byte) error {
toolsName := envtools.StorageName(tools.Version, toolsDir)
if err := u.Storage.Put(toolsName, bytes.NewReader(data), int64(len(data))); err != nil {
return err
}
if !u.WriteMetadata {
return nil
}
err := envtools.MergeAndWriteMetadata(u.Storage, toolsDir, stream, coretools.List{tools}, u.WriteMirrors)
if err != nil {
logger.Errorf("error writing agent metadata: %v", err)
return err
}
return nil
}