forked from juju/juju
/
charmstore.go
442 lines (384 loc) · 13 KB
/
charmstore.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 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package application
import (
"fmt"
"io"
"net/url"
"os"
"github.com/juju/errors"
"github.com/juju/utils"
"github.com/juju/version"
"gopkg.in/juju/charm.v6"
"gopkg.in/juju/charmrepo.v3"
"gopkg.in/juju/charmrepo.v3/csclient"
csparams "gopkg.in/juju/charmrepo.v3/csclient/params"
"gopkg.in/macaroon-bakery.v2-unstable/httpbakery"
"gopkg.in/macaroon.v2-unstable"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/controller"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/state"
"github.com/juju/juju/state/storage"
jujuversion "github.com/juju/juju/version"
)
//go:generate mockgen -package mocks -destination mocks/storage_mock.go github.com/juju/juju/state/storage Storage
//go:generate mockgen -package mocks -destination mocks/interface_mock.go gopkg.in/juju/charmrepo.v3 Interface
//go:generate mockgen -package mocks -destination mocks/charm_mock.go github.com/juju/juju/apiserver/facades/client/application StateCharm
//go:generate mockgen -package mocks -destination mocks/model_mock.go github.com/juju/juju/apiserver/facades/client/application StateModel
//go:generate mockgen -package mocks -destination mocks/charmstore_mock.go github.com/juju/juju/apiserver/facades/client/application State
// TODO - we really want to avoid this, which we can do by refactoring code requiring this
// to use interfaces.
// NewCharmStoreRepo instantiates a new charm store repository.
// It is exported for testing purposes.
var NewCharmStoreRepo = newCharmStoreFromClient
var newStateStorage = storage.NewStorage
func newCharmStoreFromClient(csClient *csclient.Client) charmrepo.Interface {
return charmrepo.NewCharmStoreFromClient(csClient)
}
// StateCharm represents a Charm from the state package
type StateCharm interface {
IsUploaded() bool
}
// StateModel represents a Model from the state package
type StateModel interface {
ModelConfig() (*config.Config, error)
}
// CharmState represents directives for accessing charm methods
type CharmState interface {
UpdateUploadedCharm(info state.CharmInfo) (*state.Charm, error)
PrepareStoreCharmUpload(curl *charm.URL) (StateCharm, error)
}
// ModelState represents methods for accessing model definitions
type ModelState interface {
Model() (StateModel, error)
ModelUUID() string
}
// ControllerState represents information defined for accessing controller
// configuration
type ControllerState interface {
ControllerConfig() (controller.Config, error)
}
// State represents the access patterns for the charm store methods.
type State interface {
CharmState
ModelState
ControllerState
state.MongoSessioner
}
// AddCharmWithAuthorizationAndRepo adds the given charm URL (which must include
// revision) to the environment, if it does not exist yet.
// Local charms are not supported, only charm store URLs.
// See also AddLocalCharm().
// Additionally a Repo (See charmrepo.Interface) function factory can be
// provided to help with overriding the source of downloading charms. The main
// benefit of this indirection is to help with testing (mocking)
//
// The authorization macaroon, args.CharmStoreMacaroon, may be
// omitted, in which case this call is equivalent to AddCharm.
func AddCharmWithAuthorizationAndRepo(st State, args params.AddCharmWithAuthorization, repoFn func() (charmrepo.Interface, error)) error {
charmURL, err := charm.ParseURL(args.URL)
if err != nil {
return err
}
if charmURL.Schema != "cs" {
return fmt.Errorf("only charm store charm URLs are supported, with cs: schema")
}
if charmURL.Revision < 0 {
return fmt.Errorf("charm URL must include revision")
}
// First, check if a pending or a real charm exists in state.
stateCharm, err := st.PrepareStoreCharmUpload(charmURL)
if err != nil {
return err
}
if stateCharm.IsUploaded() {
// Charm already in state (it was uploaded already).
return nil
}
// Get the repo from the constructor
repo, err := repoFn()
// Get the charm and its information from the store.
downloadedCharm, err := repo.Get(charmURL)
if err != nil {
cause := errors.Cause(err)
if httpbakery.IsDischargeError(cause) || httpbakery.IsInteractionError(cause) {
return errors.NewUnauthorized(err, "")
}
return errors.Trace(err)
}
if err := checkMinVersion(downloadedCharm); err != nil {
return errors.Trace(err)
}
// Open it and calculate the SHA256 hash.
downloadedBundle, ok := downloadedCharm.(*charm.CharmArchive)
if !ok {
return errors.Errorf("expected a charm archive, got %T", downloadedCharm)
}
// Validate the charm lxd profile once we've downloaded it.
if err := lxdprofile.ValidateCharmLXDProfile(downloadedCharm); err != nil {
if !args.Force {
return errors.Annotate(err, "cannot add charm")
}
}
// Clean up the downloaded charm - we don't need to cache it in
// the filesystem as well as in blob storage.
defer os.Remove(downloadedBundle.Path)
archive, err := os.Open(downloadedBundle.Path)
if err != nil {
return errors.Annotate(err, "cannot read downloaded charm")
}
defer archive.Close()
bundleSHA256, size, err := utils.ReadSHA256(archive)
if err != nil {
return errors.Annotate(err, "cannot calculate SHA256 hash of charm")
}
if _, err := archive.Seek(0, 0); err != nil {
return errors.Annotate(err, "cannot rewind charm archive")
}
ca := CharmArchive{
ID: charmURL,
Charm: downloadedCharm,
Data: archive,
Size: size,
SHA256: bundleSHA256,
CharmVersion: downloadedBundle.Version(),
}
if args.CharmStoreMacaroon != nil {
ca.Macaroon = macaroon.Slice{args.CharmStoreMacaroon}
}
// Store the charm archive in environment storage.
return StoreCharmArchive(st, ca)
}
// AddCharmWithAuthorization adds the given charm URL (which must include revision) to
// the environment, if it does not exist yet. Local charms are not
// supported, only charm store URLs. See also AddLocalCharm().
//
// The authorization macaroon, args.CharmStoreMacaroon, may be
// omitted, in which case this call is equivalent to AddCharm.
func AddCharmWithAuthorization(st State, args params.AddCharmWithAuthorization) error {
return AddCharmWithAuthorizationAndRepo(st, args, func() (charmrepo.Interface, error) {
// determine which charmstore api url to use.
controllerCfg, err := st.ControllerConfig()
if err != nil {
return nil, err
}
repo, err := openCSRepo(controllerCfg.CharmStoreURL(), args)
if err != nil {
return nil, err
}
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
modelConfig, err := model.ModelConfig()
if err != nil {
return nil, errors.Trace(err)
}
repo = config.SpecializeCharmRepo(repo, modelConfig).(*charmrepo.CharmStore)
return repo, nil
})
}
func openCSRepo(csURL string, args params.AddCharmWithAuthorization) (charmrepo.Interface, error) {
csClient, err := openCSClient(csURL, args)
if err != nil {
return nil, err
}
repo := NewCharmStoreRepo(csClient)
return repo, nil
}
func openCSClient(csAPIURL string, args params.AddCharmWithAuthorization) (*csclient.Client, error) {
csURL, err := url.Parse(csAPIURL)
if err != nil {
return nil, err
}
csParams := csclient.Params{
URL: csURL.String(),
HTTPClient: httpbakery.NewHTTPClient(),
}
if args.CharmStoreMacaroon != nil {
// Set the provided charmstore authorizing macaroon
// as a cookie in the HTTP client.
// TODO(cmars) discharge any third party caveats in the macaroon.
ms := []*macaroon.Macaroon{args.CharmStoreMacaroon}
httpbakery.SetCookie(csParams.HTTPClient.Jar, csURL, ms)
}
csClient := csclient.New(csParams)
channel := csparams.Channel(args.Channel)
if channel != csparams.NoChannel {
csClient = csClient.WithChannel(channel)
}
return csClient, nil
}
func checkMinVersion(ch charm.Charm) error {
minver := ch.Meta().MinJujuVersion
if minver != version.Zero && minver.Compare(jujuversion.Current) > 0 {
return minVersionError(minver, jujuversion.Current)
}
return nil
}
type minJujuVersionErr struct {
*errors.Err
}
func minVersionError(minver, jujuver version.Number) error {
err := errors.NewErr("charm's min version (%s) is higher than this juju model's version (%s)",
minver, jujuver)
err.SetLocation(1)
return minJujuVersionErr{&err}
}
// CharmArchive is the data that needs to be stored for a charm archive in
// state.
type CharmArchive struct {
// ID is the charm URL for which we're storing the archive.
ID *charm.URL
// Charm is the metadata about the charm for the archive.
Charm charm.Charm
// Data contains the bytes of the archive.
Data io.Reader
// Size is the number of bytes in Data.
Size int64
// SHA256 is the hash of the bytes in Data.
SHA256 string
// Macaroon is the authorization macaroon for accessing the charmstore.
Macaroon macaroon.Slice
// Charm Version contains semantic version of charm, typically the output of git describe.
CharmVersion string
}
// StoreCharmArchive stores a charm archive in environment storage.
func StoreCharmArchive(st State, archive CharmArchive) error {
storage := newStateStorage(st.ModelUUID(), st.MongoSession())
storagePath, err := charmArchiveStoragePath(archive.ID)
if err != nil {
return errors.Annotate(err, "cannot generate charm archive name")
}
if err := storage.Put(storagePath, archive.Data, archive.Size); err != nil {
return errors.Annotate(err, "cannot add charm to storage")
}
info := state.CharmInfo{
Charm: archive.Charm,
ID: archive.ID,
StoragePath: storagePath,
SHA256: archive.SHA256,
Macaroon: archive.Macaroon,
Version: archive.CharmVersion,
}
// Now update the charm data in state and mark it as no longer pending.
_, err = st.UpdateUploadedCharm(info)
if err != nil {
alreadyUploaded := err == state.ErrCharmRevisionAlreadyModified ||
errors.Cause(err) == state.ErrCharmRevisionAlreadyModified ||
state.IsCharmAlreadyUploadedError(err)
if err := storage.Remove(storagePath); err != nil {
if alreadyUploaded {
logger.Errorf("cannot remove duplicated charm archive from storage: %v", err)
} else {
logger.Errorf("cannot remove unsuccessfully recorded charm archive from storage: %v", err)
}
}
if alreadyUploaded {
// Somebody else managed to upload and update the charm in
// state before us. This is not an error.
return nil
}
return errors.Trace(err)
}
return nil
}
// charmArchiveStoragePath returns a string that is suitable as a
// storage path, using a random UUID to avoid colliding with concurrent
// uploads.
func charmArchiveStoragePath(curl *charm.URL) (string, error) {
uuid, err := utils.NewUUID()
if err != nil {
return "", err
}
return fmt.Sprintf("charms/%s-%s", curl.String(), uuid), nil
}
// ResolveCharm resolves the best available charm URLs with series, for charm
// locations without a series specified.
func ResolveCharms(st State, args params.ResolveCharms) (params.ResolveCharmResults, error) {
var results params.ResolveCharmResults
model, err := st.Model()
if err != nil {
return params.ResolveCharmResults{}, errors.Trace(err)
}
envConfig, err := model.ModelConfig()
if err != nil {
return params.ResolveCharmResults{}, err
}
controllerCfg, err := st.ControllerConfig()
if err != nil {
return params.ResolveCharmResults{}, err
}
csParams := csclient.Params{
URL: controllerCfg.CharmStoreURL(),
}
repo := config.SpecializeCharmRepo(
NewCharmStoreRepo(csclient.New(csParams)),
envConfig)
for _, ref := range args.References {
result := params.ResolveCharmResult{}
curl, err := charm.ParseURL(ref)
if err != nil {
result.Error = err.Error()
} else {
curl, err := resolveCharm(curl, repo)
if err != nil {
result.Error = err.Error()
} else {
result.URL = curl.String()
}
}
results.URLs = append(results.URLs, result)
}
return results, nil
}
func resolveCharm(ref *charm.URL, repo charmrepo.Interface) (*charm.URL, error) {
if ref.Schema != "cs" {
return nil, errors.New("only charm store charm references are supported, with cs: schema")
}
// Resolve the charm location with the repository.
resolved, _, err := repo.Resolve(ref)
if err != nil {
return nil, err
}
if resolved.Series == "" {
return nil, errors.Errorf("no series found in charm URL %q", resolved)
}
return resolved.WithRevision(ref.Revision), nil
}
type csStateShim struct {
*state.State
}
func NewStateShim(st *state.State) State {
return csStateShim{
State: st,
}
}
func (s csStateShim) PrepareStoreCharmUpload(curl *charm.URL) (StateCharm, error) {
charm, err := s.State.PrepareStoreCharmUpload(curl)
if err != nil {
return nil, errors.Trace(err)
}
return csStateCharmShim{Charm: charm}, nil
}
func (s csStateShim) Model() (StateModel, error) {
model, err := s.State.Model()
if err != nil {
return nil, errors.Trace(err)
}
return csStateModelShim{Model: model}, nil
}
type csStateCharmShim struct {
*state.Charm
}
func (s csStateCharmShim) IsUploaded() bool {
return s.Charm.IsUploaded()
}
type csStateModelShim struct {
*state.Model
}
func (s csStateModelShim) ModelConfig() (*config.Config, error) {
return s.Model.ModelConfig()
}