This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
874 lines (778 loc) · 23.2 KB
/
file.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package jujuclient provides functionality to support
// connections to Juju such as controllers cache, accounts cache, etc.
package jujuclient
import (
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"reflect"
"time"
"github.com/juju/clock"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/mutex"
cookiejar "github.com/juju/persistent-cookiejar"
"github.com/juju/juju/cloud"
"github.com/juju/juju/core/model"
"github.com/juju/juju/juju/osenv"
)
var (
_ ClientStore = (*store)(nil)
// A second should be enough to write or read any files. But
// some disks are slow when under load, so lets give the disk a
// reasonable time to get the lock.
lockTimeout = 5 * time.Second
)
// NewFileClientStore returns a new filesystem-based client store
// that manages files in $XDG_DATA_HOME/juju.
func NewFileClientStore() ClientStore {
return &store{
lockName: generateStoreLockName(),
}
}
// NewFileCredentialStore returns a new filesystem-based credentials store
// that manages credentials in $XDG_DATA_HOME/juju.
func NewFileCredentialStore() CredentialStore {
return &store{
lockName: generateStoreLockName(),
}
}
type store struct {
lockName string
}
// generateStoreLockName uses part of the hash of the controller path as the
// name of the lock. This is to avoid contention between multiple users on a
// single machine with different controller files, but also helps with
// contention in tests.
func generateStoreLockName() string {
h := sha256.New()
_, _ = h.Write([]byte(JujuControllersPath()))
fullHash := fmt.Sprintf("%x", h.Sum(nil))
return fmt.Sprintf("store-lock-%x", fullHash[:8])
}
func (s *store) acquireLock() (mutex.Releaser, error) {
spec := mutex.Spec{
Name: s.lockName,
Clock: clock.WallClock,
Delay: 20 * time.Millisecond,
Timeout: lockTimeout,
}
releaser, err := mutex.Acquire(spec)
if err != nil {
return nil, errors.Trace(err)
}
return releaser, nil
}
// AllControllers implements ControllersGetter.
func (s *store) AllControllers() (map[string]ControllerDetails, error) {
releaser, err := s.acquireLock()
if err != nil {
return nil, errors.Annotate(err,
"cannot acquire lock file to read all the controllers",
)
}
defer releaser.Release()
controllers, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return nil, errors.Trace(err)
}
return controllers.Controllers, nil
}
// CurrentController implements ControllersGetter.
func (s *store) CurrentController() (string, error) {
releaser, err := s.acquireLock()
if err != nil {
return "", errors.Annotate(err,
"cannot acquire lock file to get the current controller name",
)
}
defer releaser.Release()
controllers, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return "", errors.Trace(err)
}
if controllers.CurrentController == "" {
return "", errors.NotFoundf("current controller")
}
return controllers.CurrentController, nil
}
// ControllerByName implements ControllersGetter.
func (s *store) ControllerByName(name string) (*ControllerDetails, error) {
if err := ValidateControllerName(name); err != nil {
return nil, errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return nil, errors.Annotatef(err,
"cannot acquire lock file to read controller %s", name,
)
}
defer releaser.Release()
controllers, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return nil, errors.Trace(err)
}
if result, ok := controllers.Controllers[name]; ok {
return &result, nil
}
return nil, errors.NotFoundf("controller %s", name)
}
// ControllerByEndpoints implements ControllersGetter.
func (s *store) ControllerByAPIEndpoints(endpoints ...string) (*ControllerDetails, string, error) {
releaser, err := s.acquireLock()
if err != nil {
return nil, "", errors.Annotatef(err, "cannot acquire lock file to read controllers")
}
defer releaser.Release()
controllers, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return nil, "", errors.Trace(err)
}
matchEps := set.NewStrings(endpoints...)
for name, ctrl := range controllers.Controllers {
if matchEps.Intersection(set.NewStrings(ctrl.APIEndpoints...)).IsEmpty() {
continue
}
return &ctrl, name, nil
}
return nil, "", errors.NotFoundf("controller with API endpoints %v", endpoints)
}
// AddController implements ControllerUpdater.
func (s *store) AddController(name string, details ControllerDetails) error {
if err := ValidateControllerName(name); err != nil {
return errors.Trace(err)
}
if err := ValidateControllerDetails(details); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file to add controller %s", name,
)
}
defer releaser.Release()
all, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return errors.Annotate(err, "cannot get controllers")
}
if len(all.Controllers) == 0 {
all.Controllers = make(map[string]ControllerDetails)
}
if _, ok := all.Controllers[name]; ok {
return errors.AlreadyExistsf("controller with name %s", name)
}
for k, v := range all.Controllers {
if v.ControllerUUID == details.ControllerUUID {
return errors.AlreadyExistsf("controller with UUID %s (%s)",
details.ControllerUUID, k)
}
}
all.Controllers[name] = details
return WriteControllersFile(all)
}
// UpdateController implements ControllerUpdater.
func (s *store) UpdateController(name string, details ControllerDetails) error {
if err := ValidateControllerName(name); err != nil {
return errors.Trace(err)
}
if err := ValidateControllerDetails(details); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file to update controller %s", name,
)
}
defer releaser.Release()
all, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return errors.Annotate(err, "cannot get controllers")
}
if len(all.Controllers) == 0 {
return errors.NotFoundf("controllers")
}
for k, v := range all.Controllers {
if v.ControllerUUID == details.ControllerUUID && k != name {
return errors.AlreadyExistsf("controller %s with UUID %s",
k, v.ControllerUUID)
}
}
if _, ok := all.Controllers[name]; !ok {
return errors.NotFoundf("controller %s", name)
}
all.Controllers[name] = details
return WriteControllersFile(all)
}
// SetCurrentController implements ControllerUpdater.
func (s *store) SetCurrentController(name string) error {
if err := ValidateControllerName(name); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotate(err,
"cannot acquire lock file to set the current controller name",
)
}
defer releaser.Release()
controllers, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return errors.Trace(err)
}
if _, ok := controllers.Controllers[name]; !ok {
return errors.NotFoundf("controller %v", name)
}
if controllers.CurrentController == name {
return nil
}
controllers.CurrentController = name
return WriteControllersFile(controllers)
}
// RemoveController implements ControllersRemover
func (s *store) RemoveController(name string) error {
if err := ValidateControllerName(name); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file to remove controller %s", name,
)
}
defer releaser.Release()
controllers, err := ReadControllersFile(JujuControllersPath())
if err != nil {
return errors.Annotate(err, "cannot get controllers")
}
// We remove all controllers with the same UUID as the named one.
namedControllerDetails, ok := controllers.Controllers[name]
if !ok {
return nil
}
var names []string
for name, details := range controllers.Controllers {
if details.ControllerUUID == namedControllerDetails.ControllerUUID {
names = append(names, name)
delete(controllers.Controllers, name)
if controllers.CurrentController == name {
controllers.CurrentController = ""
}
}
}
// Remove models for the controller.
controllerModels, err := ReadModelsFile(JujuModelsPath())
if err != nil {
return errors.Trace(err)
}
for _, name := range names {
if _, ok := controllerModels[name]; ok {
delete(controllerModels, name)
if err := WriteModelsFile(controllerModels); err != nil {
return errors.Trace(err)
}
}
}
// Remove accounts for the controller.
controllerAccounts, err := ReadAccountsFile(JujuAccountsPath())
if err != nil {
return errors.Trace(err)
}
for _, name := range names {
if _, ok := controllerAccounts[name]; ok {
delete(controllerAccounts, name)
if err := WriteAccountsFile(controllerAccounts); err != nil {
return errors.Trace(err)
}
}
}
// Remove bootstrap config for the controller.
bootstrapConfigurations, err := ReadBootstrapConfigFile(JujuBootstrapConfigPath())
if err != nil {
return errors.Trace(err)
}
for _, name := range names {
if _, ok := bootstrapConfigurations[name]; ok {
delete(bootstrapConfigurations, name)
if err := WriteBootstrapConfigFile(bootstrapConfigurations); err != nil {
return errors.Trace(err)
}
}
}
// Remove the controller cookie jars.
for _, name := range names {
err := os.Remove(JujuCookiePath(name))
if err != nil && !os.IsNotExist(err) {
return errors.Trace(err)
}
}
// Finally, remove the controllers. This must be done last
// so we don't end up with dangling entries in other files.
return WriteControllersFile(controllers)
}
// UpdateModel implements ModelUpdater.
func (s *store) UpdateModel(controllerName, modelName string, details ModelDetails) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
if err := ValidateModel(modelName, details); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for updating model %s on controller %s", modelName, controllerName,
)
}
defer releaser.Release()
return errors.Trace(updateModels(
controllerName,
func(models *ControllerModels) (bool, error) {
oldDetails, ok := models.Models[modelName]
if ok && details == oldDetails {
return false, nil
}
if ok && oldDetails.ModelType == "" && details.ModelType != model.IAAS ||
oldDetails.ModelType != "" && oldDetails.ModelType != details.ModelType {
oldModelType := oldDetails.ModelType
if oldModelType == "" {
oldModelType = model.IAAS
}
return false, errors.Errorf(
"model type was %q, cannot change to %q", oldModelType, details.ModelType)
}
models.Models[modelName] = details
return true, nil
},
))
}
// SetCurrentModel implements ModelUpdater.
func (s *store) SetCurrentModel(controllerName, modelName string) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for setting current model %s on controller %s", modelName, controllerName,
)
}
defer releaser.Release()
if modelName != "" {
if err := ValidateModelName(modelName); err != nil {
return errors.Trace(err)
}
}
return errors.Trace(updateModels(
controllerName,
func(models *ControllerModels) (bool, error) {
if modelName == "" {
// We just want to reset
models.CurrentModel = ""
return true, nil
}
if _, ok := models.Models[modelName]; !ok {
return false, errors.NotFoundf(
"model %s:%s",
controllerName,
modelName,
)
}
models.CurrentModel = modelName
return true, nil
},
))
}
// AllModels implements ModelGetter.
func (s *store) AllModels(controllerName string) (map[string]ModelDetails, error) {
if err := ValidateControllerName(controllerName); err != nil {
return nil, errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return nil, errors.Annotatef(err,
"cannot acquire lock file for getting all models for controller %s", controllerName,
)
}
defer releaser.Release()
all, err := ReadModelsFile(JujuModelsPath())
if err != nil {
return nil, errors.Trace(err)
}
controllerModels, ok := all[controllerName]
if !ok {
return nil, errors.NotFoundf(
"models for controller %s",
controllerName,
)
}
return controllerModels.Models, nil
}
// CurrentModel implements ModelGetter.
func (s *store) CurrentModel(controllerName string) (string, error) {
if err := ValidateControllerName(controllerName); err != nil {
return "", errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return "", errors.Annotatef(err,
"cannot acquire lock file for getting current model for controller %s", controllerName,
)
}
defer releaser.Release()
all, err := ReadModelsFile(JujuModelsPath())
if err != nil {
return "", errors.Trace(err)
}
controllerModels, ok := all[controllerName]
if !ok {
return "", errors.NotFoundf(
"current model for controller %s",
controllerName,
)
}
if controllerModels.CurrentModel == "" {
return "", errors.NotFoundf(
"current model for controller %s",
controllerName,
)
}
return controllerModels.CurrentModel, nil
}
// ModelByName implements ModelGetter.
func (s *store) ModelByName(controllerName, modelName string) (*ModelDetails, error) {
if err := ValidateControllerName(controllerName); err != nil {
return nil, errors.Trace(err)
}
if err := ValidateModelName(modelName); err != nil {
return nil, errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return nil, errors.Annotatef(err,
"cannot acquire lock file for getting model %s for controller %s", modelName, controllerName,
)
}
defer releaser.Release()
all, err := ReadModelsFile(JujuModelsPath())
if err != nil {
return nil, errors.Trace(err)
}
controllerModels, ok := all[controllerName]
if !ok {
return nil, errors.NotFoundf(
"model %s:%s",
controllerName,
modelName,
)
}
details, ok := controllerModels.Models[modelName]
if !ok {
return nil, errors.NotFoundf(
"model %s:%s",
controllerName,
modelName,
)
}
return &details, nil
}
// RemoveModel implements ModelRemover.
func (s *store) RemoveModel(controllerName, modelName string) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
if err := ValidateModelName(modelName); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for removing model %s on controller %s", modelName, controllerName,
)
}
defer releaser.Release()
return errors.Trace(updateModels(
controllerName,
func(models *ControllerModels) (bool, error) {
if _, ok := models.Models[modelName]; !ok {
return false, errors.NotFoundf(
"model %s:%s",
controllerName,
modelName,
)
}
delete(models.Models, modelName)
return true, nil
},
))
}
type updateModelFunc func(storedModels *ControllerModels) (bool, error)
func updateModels(controllerName string, update updateModelFunc) error {
all, err := ReadModelsFile(JujuModelsPath())
if err != nil {
return errors.Trace(err)
}
controllerModels, ok := all[controllerName]
if !ok {
if all == nil {
all = make(map[string]*ControllerModels)
}
controllerModels = &ControllerModels{}
all[controllerName] = controllerModels
}
if controllerModels.Models == nil {
controllerModels.Models = make(map[string]ModelDetails)
}
updated, err := update(controllerModels)
if err != nil {
return errors.Trace(err)
}
if updated {
return errors.Trace(WriteModelsFile(all))
}
return nil
}
// SetModels implements ModelUpdater.
func (s *store) SetModels(controllerName string, models map[string]ModelDetails) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
for modelName, details := range models {
if err := ValidateModel(modelName, details); err != nil {
return errors.Trace(err)
}
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for setting models on controller %s", controllerName,
)
}
defer releaser.Release()
err = updateModels(controllerName, func(storedModels *ControllerModels) (bool, error) {
changed := len(storedModels.Models) != len(models)
// Add or update controller models based on a new collection.
for modelName, details := range models {
oldDetails, ok := storedModels.Models[modelName]
if ok && details == oldDetails {
continue
}
if details.ActiveBranch == "" {
if oldDetails.ActiveBranch != "" {
details.ActiveBranch = oldDetails.ActiveBranch
} else {
details.ActiveBranch = model.GenerationMaster
}
}
storedModels.Models[modelName] = details
changed = true
}
// Delete models that are not in the new collection.
for modelName := range storedModels.Models {
if _, ok := models[modelName]; !ok {
delete(storedModels.Models, modelName)
}
}
return changed, nil
})
if err != nil {
return errors.Trace(err)
}
return nil
}
// UpdateAccount implements AccountUpdater.
func (s *store) UpdateAccount(controllerName string, details AccountDetails) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
if err := ValidateAccountDetails(details); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for updating an account on controller %s", controllerName,
)
}
defer releaser.Release()
accounts, err := ReadAccountsFile(JujuAccountsPath())
if err != nil {
return errors.Trace(err)
}
if accounts == nil {
accounts = make(map[string]AccountDetails)
}
if oldDetails, ok := accounts[controllerName]; ok && reflect.DeepEqual(details, oldDetails) {
return nil
} else {
// Only update last known access if it has a value.
if details.LastKnownAccess == "" {
details.LastKnownAccess = oldDetails.LastKnownAccess
}
}
accounts[controllerName] = details
return errors.Trace(WriteAccountsFile(accounts))
}
// AccountByName implements AccountGetter.
func (s *store) AccountDetails(controllerName string) (*AccountDetails, error) {
if err := ValidateControllerName(controllerName); err != nil {
return nil, errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return nil, errors.Annotatef(err,
"cannot acquire lock file for getting an account details on controller %s", controllerName,
)
}
defer releaser.Release()
accounts, err := ReadAccountsFile(JujuAccountsPath())
if err != nil {
return nil, errors.Trace(err)
}
details, ok := accounts[controllerName]
if !ok {
return nil, errors.NotFoundf("account details for controller %s", controllerName)
}
return &details, nil
}
// RemoveAccount implements AccountRemover.
func (s *store) RemoveAccount(controllerName string) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for removing an account on controller %s", controllerName,
)
}
defer releaser.Release()
accounts, err := ReadAccountsFile(JujuAccountsPath())
if err != nil {
return errors.Trace(err)
}
if _, ok := accounts[controllerName]; !ok {
return errors.NotFoundf("account details for controller %s", controllerName)
}
delete(accounts, controllerName)
return errors.Trace(WriteAccountsFile(accounts))
}
// UpdateCredential implements CredentialUpdater.
func (s *store) UpdateCredential(cloudName string, details cloud.CloudCredential) error {
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for updating credentials for %s", cloudName,
)
}
defer releaser.Release()
credentials, err := ReadCredentialsFile(JujuCredentialsPath())
if err != nil {
return errors.Annotate(err, "cannot get credentials")
}
credentials.UpdateCloudCredential(cloudName, details)
return WriteCredentialsFile(credentials)
}
// CredentialForCloud implements CredentialGetter.
func (s *store) CredentialForCloud(cloudName string) (*cloud.CloudCredential, error) {
credentialCollection, err := ReadCredentialsFile(JujuCredentialsPath())
if err != nil {
return nil, errors.Trace(err)
}
credential, err := credentialCollection.CloudCredential(cloudName)
if err != nil {
return nil, errors.Trace(err)
}
return credential, nil
}
// AllCredentials implements CredentialGetter.
func (s *store) AllCredentials() (map[string]cloud.CloudCredential, error) {
credentialCollection, err := ReadCredentialsFile(JujuCredentialsPath())
if err != nil {
return nil, errors.Trace(err)
}
cloudNames := credentialCollection.CloudNames()
cloudCredentials := make(map[string]cloud.CloudCredential)
for _, cloud := range cloudNames {
v, err := credentialCollection.CloudCredential(cloud)
if err != nil {
return nil, errors.Trace(err)
}
cloudCredentials[cloud] = *v
}
return cloudCredentials, nil
}
// UpdateBootstrapConfig implements BootstrapConfigUpdater.
func (s *store) UpdateBootstrapConfig(controllerName string, cfg BootstrapConfig) error {
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
if err := ValidateBootstrapConfig(cfg); err != nil {
return errors.Trace(err)
}
releaser, err := s.acquireLock()
if err != nil {
return errors.Annotatef(err,
"cannot acquire lock file for updating the bootstrap config for controller %s", controllerName,
)
}
defer releaser.Release()
all, err := ReadBootstrapConfigFile(JujuBootstrapConfigPath())
if err != nil {
return errors.Annotate(err, "cannot get bootstrap config")
}
if all == nil {
all = make(map[string]BootstrapConfig)
}
all[controllerName] = cfg
return WriteBootstrapConfigFile(all)
}
// BootstrapConfigForController implements BootstrapConfigGetter.
func (s *store) BootstrapConfigForController(controllerName string) (*BootstrapConfig, error) {
configs, err := ReadBootstrapConfigFile(JujuBootstrapConfigPath())
if err != nil {
return nil, errors.Trace(err)
}
cfg, ok := configs[controllerName]
if !ok {
return nil, errors.NotFoundf("bootstrap config for controller %s", controllerName)
}
return &cfg, nil
}
// CookieJar returns the cookie jar associated with the given controller.
func (s *store) CookieJar(controllerName string) (CookieJar, error) {
if err := ValidateControllerName(controllerName); err != nil {
return nil, errors.Trace(err)
}
path := JujuCookiePath(controllerName)
jar, err := cookiejar.New(&cookiejar.Options{
Filename: path,
})
if err != nil {
return nil, errors.Trace(err)
}
return &cookieJar{
path: path,
Jar: jar,
}, nil
}
type cookieJar struct {
path string
*cookiejar.Jar
}
func (jar *cookieJar) Save() error {
// Ensure that the directory exists before saving.
if err := os.MkdirAll(filepath.Dir(jar.path), 0700); err != nil {
return errors.Annotatef(err, "cannot make cookies directory")
}
return jar.Jar.Save()
}
// JujuCookiePath is the location where cookies associated
// with the given controller are expected to be found.
func JujuCookiePath(controllerName string) string {
return osenv.JujuXDGDataHomePath("cookies", controllerName+".json")
}