/
filesystems.go
785 lines (746 loc) · 26.3 KB
/
filesystems.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storageprovisioner
import (
"path/filepath"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/instance"
"github.com/juju/juju/storage"
)
// filesystemsChanged is called when the lifecycle states of the filesystems
// with the provided IDs have been seen to have changed.
func filesystemsChanged(ctx *context, changes []string) error {
tags := make([]names.Tag, len(changes))
for i, change := range changes {
tags[i] = names.NewFilesystemTag(change)
}
alive, dying, dead, err := storageEntityLife(ctx, tags)
if err != nil {
return errors.Trace(err)
}
logger.Debugf("filesystems alive: %v, dying: %v, dead: %v", alive, dying, dead)
if len(alive)+len(dying)+len(dead) == 0 {
return nil
}
// Get filesystem information for filesystems, so we can provision,
// deprovision, attach and detach.
filesystemTags := make([]names.FilesystemTag, 0, len(alive)+len(dying)+len(dead))
for _, tag := range alive {
filesystemTags = append(filesystemTags, tag.(names.FilesystemTag))
}
for _, tag := range dying {
filesystemTags = append(filesystemTags, tag.(names.FilesystemTag))
}
for _, tag := range dead {
filesystemTags = append(filesystemTags, tag.(names.FilesystemTag))
}
filesystemResults, err := ctx.filesystemAccessor.Filesystems(filesystemTags)
if err != nil {
return errors.Annotatef(err, "getting filesystem information")
}
aliveFilesystemTags := filesystemTags[:len(alive)]
dyingFilesystemTags := filesystemTags[len(alive) : len(alive)+len(dying)]
deadFilesystemTags := filesystemTags[len(alive)+len(dying):]
aliveFilesystemResults := filesystemResults[:len(alive)]
dyingFilesystemResults := filesystemResults[len(alive) : len(alive)+len(dying)]
deadFilesystemResults := filesystemResults[len(alive)+len(dying):]
if err := processDeadFilesystems(ctx, deadFilesystemTags, deadFilesystemResults); err != nil {
return errors.Annotate(err, "deprovisioning filesystems")
}
if err := processDyingFilesystems(ctx, dyingFilesystemTags, dyingFilesystemResults); err != nil {
return errors.Annotate(err, "processing dying filesystems")
}
if err := processAliveFilesystems(ctx, aliveFilesystemTags, aliveFilesystemResults); err != nil {
return errors.Annotate(err, "provisioning filesystems")
}
return nil
}
// filesystemAttachmentsChanged is called when the lifecycle states of the filesystem
// attachments with the provided IDs have been seen to have changed.
func filesystemAttachmentsChanged(ctx *context, ids []params.MachineStorageId) error {
alive, dying, dead, err := attachmentLife(ctx, ids)
if err != nil {
return errors.Trace(err)
}
logger.Debugf("filesystem attachment alive: %v, dying: %v, dead: %v", alive, dying, dead)
if len(dead) != 0 {
// We should not see dead filesystem attachments;
// attachments go directly from Dying to removed.
logger.Debugf("unexpected dead filesystem attachments: %v", dead)
}
if len(alive)+len(dying) == 0 {
return nil
}
// Get filesystem information for alive and dying filesystem attachments, so
// we can attach/detach.
ids = append(alive, dying...)
filesystemAttachmentResults, err := ctx.filesystemAccessor.FilesystemAttachments(ids)
if err != nil {
return errors.Annotatef(err, "getting filesystem attachment information")
}
// Deprovision Dying filesystem attachments.
dyingFilesystemAttachmentResults := filesystemAttachmentResults[len(alive):]
if err := processDyingFilesystemAttachments(ctx, dying, dyingFilesystemAttachmentResults); err != nil {
return errors.Annotate(err, "destroying filesystem attachments")
}
// Provision Alive filesystem attachments.
aliveFilesystemAttachmentResults := filesystemAttachmentResults[:len(alive)]
if err := processAliveFilesystemAttachments(ctx, alive, aliveFilesystemAttachmentResults); err != nil {
return errors.Annotate(err, "creating filesystem attachments")
}
return nil
}
// processDyingFilesystems processes the FilesystemResults for Dying filesystems,
// removing them from provisioning-pending as necessary, and storing the current
// filesystem info for provisioned filesystems so that attachments may be destroyed.
func processDyingFilesystems(ctx *context, tags []names.FilesystemTag, filesystemResults []params.FilesystemResult) error {
for _, tag := range tags {
delete(ctx.pendingFilesystems, tag)
}
for i, result := range filesystemResults {
tag := tags[i]
if result.Error == nil {
filesystem, err := filesystemFromParams(result.Result)
if err != nil {
return errors.Annotate(err, "getting filesystem info")
}
ctx.filesystems[tag] = filesystem
} else if !params.IsCodeNotProvisioned(result.Error) {
return errors.Annotatef(result.Error, "getting information for filesystem %s", tag.Id())
}
}
return nil
}
// processDeadFilesystems processes the FilesystemResults for Dead filesystems,
// deprovisioning filesystems and removing from state as necessary.
func processDeadFilesystems(ctx *context, tags []names.FilesystemTag, filesystemResults []params.FilesystemResult) error {
for _, tag := range tags {
delete(ctx.pendingFilesystems, tag)
}
var destroy []names.FilesystemTag
var remove []names.Tag
for i, result := range filesystemResults {
tag := tags[i]
if result.Error == nil {
logger.Debugf("filesystem %s is provisioned, queuing for deprovisioning", tag.Id())
filesystem, err := filesystemFromParams(result.Result)
if err != nil {
return errors.Annotate(err, "getting filesystem info")
}
ctx.filesystems[tag] = filesystem
destroy = append(destroy, tag)
continue
}
if params.IsCodeNotProvisioned(result.Error) {
logger.Debugf("filesystem %s is not provisioned, queuing for removal", tag.Id())
remove = append(remove, tag)
continue
}
return errors.Annotatef(result.Error, "getting filesystem information for filesystem %s", tag.Id())
}
if len(destroy)+len(remove) == 0 {
return nil
}
if len(destroy) > 0 {
errorResults, err := destroyFilesystems(ctx, destroy)
if err != nil {
return errors.Annotate(err, "destroying filesystems")
}
for i, tag := range destroy {
if err := errorResults[i]; err != nil {
return errors.Annotatef(err, "destroying %s", names.ReadableString(tag))
}
remove = append(remove, tag)
}
}
if err := removeEntities(ctx, remove); err != nil {
return errors.Annotate(err, "removing filesystems from state")
}
return nil
}
// processDyingFilesystemAttachments processes the FilesystemAttachmentResults for
// Dying filesystem attachments, detaching filesystems and updating state as necessary.
func processDyingFilesystemAttachments(
ctx *context,
ids []params.MachineStorageId,
filesystemAttachmentResults []params.FilesystemAttachmentResult,
) error {
if len(ids) == 0 {
return nil
}
for _, id := range ids {
delete(ctx.pendingFilesystemAttachments, id)
}
detach := make([]params.MachineStorageId, 0, len(ids))
remove := make([]params.MachineStorageId, 0, len(ids))
for i, result := range filesystemAttachmentResults {
id := ids[i]
if result.Error == nil {
detach = append(detach, id)
continue
}
if params.IsCodeNotProvisioned(result.Error) {
remove = append(remove, id)
continue
}
return errors.Annotatef(result.Error, "getting information for filesystem attachment %v", id)
}
if len(detach) > 0 {
attachmentParams, err := filesystemAttachmentParams(ctx, detach)
if err != nil {
return errors.Trace(err)
}
for i, params := range attachmentParams {
ctx.pendingDyingFilesystemAttachments[detach[i]] = params
}
}
if len(remove) > 0 {
if err := removeAttachments(ctx, remove); err != nil {
return errors.Annotate(err, "removing attachments from state")
}
}
return nil
}
// processAliveFilesystems processes the FilesystemResults for Alive filesystems,
// provisioning filesystems and setting the info in state as necessary.
func processAliveFilesystems(ctx *context, tags []names.FilesystemTag, filesystemResults []params.FilesystemResult) error {
// Filter out the already-provisioned filesystems.
pending := make([]names.FilesystemTag, 0, len(tags))
for i, result := range filesystemResults {
tag := tags[i]
if result.Error == nil {
// Filesystem is already provisioned: skip.
logger.Debugf("filesystem %q is already provisioned, nothing to do", tag.Id())
filesystem, err := filesystemFromParams(result.Result)
if err != nil {
return errors.Annotate(err, "getting filesystem info")
}
ctx.filesystems[tag] = filesystem
if filesystem.Volume != (names.VolumeTag{}) {
// Ensure that volume-backed filesystems' block
// devices are present even after creating the
// filesystem, so that attachments can be made.
maybeAddPendingVolumeBlockDevice(ctx, filesystem.Volume)
}
continue
}
if !params.IsCodeNotProvisioned(result.Error) {
return errors.Annotatef(
result.Error, "getting filesystem information for filesystem %q", tag.Id(),
)
}
// The filesystem has not yet been provisioned, so record its tag
// to enquire about parameters below.
pending = append(pending, tag)
}
if len(pending) == 0 {
return nil
}
paramsResults, err := ctx.filesystemAccessor.FilesystemParams(pending)
if err != nil {
return errors.Annotate(err, "getting filesystem params")
}
for i, result := range paramsResults {
if result.Error != nil {
return errors.Annotate(result.Error, "getting filesystem parameters")
}
params, err := filesystemParamsFromParams(result.Result)
if err != nil {
return errors.Annotate(err, "getting filesystem parameters")
}
ctx.pendingFilesystems[pending[i]] = params
if params.Volume != (names.VolumeTag{}) {
// The filesystem is volume-backed: we must watch for
// the corresponding block device. This will trigger a
// one-time (for the volume) forced update of block
// devices. If the block device is not immediately
// available, then we rely on the watcher. The forced
// update is necessary in case the block device was
// added to state already, and we didn't observe it.
maybeAddPendingVolumeBlockDevice(ctx, params.Volume)
}
}
return nil
}
func maybeAddPendingVolumeBlockDevice(ctx *context, v names.VolumeTag) {
if _, ok := ctx.volumeBlockDevices[v]; !ok {
ctx.pendingVolumeBlockDevices.Add(v)
}
}
// processPendingFilesystems creates as many of the pending filesystems
// as possible, first ensuring that their prerequisites have been met.
func processPendingFilesystems(ctx *context) error {
if len(ctx.pendingFilesystems) == 0 {
logger.Tracef("no pending filesystems")
return nil
}
ready := make([]storage.FilesystemParams, 0, len(ctx.pendingFilesystems))
for tag, filesystemParams := range ctx.pendingFilesystems {
if filesystemParams.Volume != (names.VolumeTag{}) {
// The filesystem is backed by a volume; ensure that
// the volume is attached by virtue of there being a
// matching block device on the machine.
if _, ok := ctx.volumeBlockDevices[filesystemParams.Volume]; !ok {
logger.Debugf(
"filesystem %v backing-volume %v is not attached yet",
filesystemParams.Tag.Id(),
filesystemParams.Volume.Id(),
)
continue
}
}
ready = append(ready, filesystemParams)
delete(ctx.pendingFilesystems, tag)
}
if len(ready) == 0 {
return nil
}
filesystems, err := createFilesystems(ctx, ready)
if err != nil {
return errors.Annotate(err, "creating filesystems")
}
if err := setFilesystemInfo(ctx, filesystems); err != nil {
return errors.Trace(err)
}
return nil
}
func setFilesystemInfo(ctx *context, filesystems []storage.Filesystem) error {
if len(filesystems) == 0 {
return nil
}
// TODO(axw) we need to be able to list filesystems in the provider,
// by environment, so that we can "harvest" them if they're
// unknown. This will take care of killing filesystems that we fail
// to record in state.
errorResults, err := ctx.filesystemAccessor.SetFilesystemInfo(
filesystemsFromStorage(filesystems),
)
if err != nil {
return errors.Annotate(err, "publishing filesystems to state")
}
for i, result := range errorResults {
if result.Error != nil {
return errors.Annotatef(
result.Error, "publishing filesystem %s to state",
filesystems[i].Tag.Id(),
)
}
ctx.filesystems[filesystems[i].Tag] = filesystems[i]
}
return nil
}
// processAliveFilesystemAttachments processes the FilesystemAttachmentResults
// for Alive filesystem attachments, attaching filesystems and setting the info
// in state as necessary.
func processAliveFilesystemAttachments(
ctx *context,
ids []params.MachineStorageId,
filesystemAttachmentResults []params.FilesystemAttachmentResult,
) error {
// Filter out the already-attached.
pending := make([]params.MachineStorageId, 0, len(ids))
for i, result := range filesystemAttachmentResults {
if result.Error == nil {
delete(ctx.pendingFilesystemAttachments, ids[i])
// Filesystem attachment is already provisioned: if we
// didn't (re)attach in this session, then we must do
// so now.
action := "nothing to do"
if _, ok := ctx.filesystemAttachments[ids[i]]; !ok {
// Not yet (re)attached in this session.
pending = append(pending, ids[i])
action = "will reattach"
}
logger.Debugf(
"%s is already attached to %s, %s",
ids[i].AttachmentTag, ids[i].MachineTag, action,
)
continue
}
if !params.IsCodeNotProvisioned(result.Error) {
return errors.Annotatef(
result.Error, "getting information for attachment %v", ids[i],
)
}
// The filesystem has not yet been attached, so
// record its tag to enquire about parameters below.
pending = append(pending, ids[i])
}
if len(pending) == 0 {
return nil
}
params, err := filesystemAttachmentParams(ctx, pending)
if err != nil {
return errors.Trace(err)
}
for i, params := range params {
if params.InstanceId == "" {
watchMachine(ctx, params.Machine)
}
ctx.pendingFilesystemAttachments[pending[i]] = params
}
return nil
}
// filesystemAttachmentParams obtains the specified attachments' parameters.
func filesystemAttachmentParams(
ctx *context, ids []params.MachineStorageId,
) ([]storage.FilesystemAttachmentParams, error) {
paramsResults, err := ctx.filesystemAccessor.FilesystemAttachmentParams(ids)
if err != nil {
return nil, errors.Annotate(err, "getting filesystem attachment params")
}
attachmentParams := make([]storage.FilesystemAttachmentParams, len(ids))
for i, result := range paramsResults {
if result.Error != nil {
return nil, errors.Annotate(result.Error, "getting filesystem attachment parameters")
}
params, err := filesystemAttachmentParamsFromParams(result.Result)
if err != nil {
return nil, errors.Annotate(err, "getting filesystem attachment parameters")
}
attachmentParams[i] = params
}
return attachmentParams, nil
}
func processPendingFilesystemAttachments(ctx *context) error {
if len(ctx.pendingFilesystemAttachments) == 0 {
logger.Tracef("no pending filesystem attachments")
return nil
}
ready := make([]storage.FilesystemAttachmentParams, 0, len(ctx.pendingFilesystemAttachments))
for id, params := range ctx.pendingFilesystemAttachments {
filesystem, ok := ctx.filesystems[params.Filesystem]
if !ok {
logger.Debugf("filesystem %v has not been provisioned yet", params.Filesystem.Id())
continue
}
if filesystem.Volume != (names.VolumeTag{}) {
// The filesystem is volume-backed: if the filesystem
// was created in another session, then the block device
// may not have been seen yet. We must wait for the block
// device watcher to trigger.
if _, ok := ctx.volumeBlockDevices[filesystem.Volume]; !ok {
logger.Debugf(
"filesystem %v backing-volume %v is not attached yet",
filesystem.Tag.Id(),
filesystem.Volume.Id(),
)
continue
}
}
if params.InstanceId == "" {
logger.Debugf("machine %v has not been provisioned yet", params.Machine.Id())
continue
}
if params.Path == "" {
params.Path = filepath.Join(ctx.storageDir, params.Filesystem.Id())
}
params.FilesystemId = filesystem.FilesystemId
ready = append(ready, params)
delete(ctx.pendingFilesystemAttachments, id)
}
if len(ready) == 0 {
return nil
}
filesystemAttachments, err := createFilesystemAttachments(ctx, ready)
if err != nil {
return errors.Annotate(err, "creating filesystem attachments")
}
if err := setFilesystemAttachmentInfo(ctx, filesystemAttachments); err != nil {
return errors.Trace(err)
}
return nil
}
func processPendingDyingFilesystemAttachments(ctx *context) error {
if len(ctx.pendingDyingFilesystemAttachments) == 0 {
logger.Tracef("no pending, dying filesystem attachments")
return nil
}
var detach []storage.FilesystemAttachmentParams
var remove []params.MachineStorageId
for id, params := range ctx.pendingDyingFilesystemAttachments {
if _, ok := ctx.filesystems[params.Filesystem]; !ok {
// Wait until the filesystem info is known.
continue
}
delete(ctx.pendingDyingFilesystemAttachments, id)
detach = append(detach, params)
remove = append(remove, id)
}
if len(detach) == 0 {
return nil
}
if err := detachFilesystems(ctx, detach); err != nil {
return errors.Annotate(err, "detaching filesystems")
}
if err := removeAttachments(ctx, remove); err != nil {
return errors.Annotate(err, "removing attachments from state")
}
return nil
}
func setFilesystemAttachmentInfo(ctx *context, filesystemAttachments []storage.FilesystemAttachment) error {
if len(filesystemAttachments) == 0 {
return nil
}
// TODO(axw) we need to be able to list filesystem attachments in the
// provider, by environment, so that we can "harvest" them if they're
// unknown. This will take care of killing filesystems that we fail to
// record in state.
errorResults, err := ctx.filesystemAccessor.SetFilesystemAttachmentInfo(
filesystemAttachmentsFromStorage(filesystemAttachments),
)
if err != nil {
return errors.Annotate(err, "publishing filesystems to state")
}
for i, result := range errorResults {
if result.Error != nil {
return errors.Annotatef(
result.Error, "publishing attachment of %s to %s to state",
names.ReadableString(filesystemAttachments[i].Filesystem),
names.ReadableString(filesystemAttachments[i].Machine),
)
}
// Record the filesystem attachment in the context.
ctx.filesystemAttachments[params.MachineStorageId{
MachineTag: filesystemAttachments[i].Machine.String(),
AttachmentTag: filesystemAttachments[i].Filesystem.String(),
}] = filesystemAttachments[i]
}
return nil
}
// createFilesystems creates filesystems with the specified parameters.
func createFilesystems(ctx *context, params []storage.FilesystemParams) ([]storage.Filesystem, error) {
// TODO(axw) later we may have multiple instantiations (sources)
// for a storage provider, e.g. multiple Ceph installations. For
// now we assume a single source for each provider type, with no
// configuration.
// Create filesystem sources.
filesystemSources := make(map[string]storage.FilesystemSource)
for _, params := range params {
sourceName := string(params.Provider)
if _, ok := filesystemSources[sourceName]; ok {
continue
}
if params.Volume != (names.VolumeTag{}) {
filesystemSources[sourceName] = ctx.managedFilesystemSource
continue
}
filesystemSource, err := filesystemSource(
ctx.environConfig, ctx.storageDir, sourceName, params.Provider,
)
if err != nil {
return nil, errors.Annotate(err, "getting filesystem source")
}
filesystemSources[sourceName] = filesystemSource
}
// Validate and gather filesystem parameters.
paramsBySource := make(map[string][]storage.FilesystemParams)
for _, params := range params {
sourceName := string(params.Provider)
filesystemSource := filesystemSources[sourceName]
err := filesystemSource.ValidateFilesystemParams(params)
if err != nil {
// TODO(axw) we should set an error status for params.Tag
// here, and we should retry periodically.
logger.Errorf("ignoring invalid filesystem: %v", err)
continue
}
paramsBySource[sourceName] = append(paramsBySource[sourceName], params)
}
var allFilesystems []storage.Filesystem
for sourceName, params := range paramsBySource {
logger.Debugf("creating filesystems: %v", params)
filesystemSource := filesystemSources[sourceName]
filesystems, err := filesystemSource.CreateFilesystems(params)
if err != nil {
return nil, errors.Annotatef(err, "creating filesystems from source %q", sourceName)
}
allFilesystems = append(allFilesystems, filesystems...)
}
return allFilesystems, nil
}
// createFilesystemAttachments creates filesystem attachments with the specified parameters.
func createFilesystemAttachments(
ctx *context,
params []storage.FilesystemAttachmentParams,
) ([]storage.FilesystemAttachment, error) {
paramsBySource, filesystemSources, err := filesystemAttachmentParamsBySource(ctx, params)
if err != nil {
return nil, errors.Trace(err)
}
var allFilesystemAttachments []storage.FilesystemAttachment
for sourceName, params := range paramsBySource {
logger.Debugf("attaching filesystems: %v", params)
filesystemSource := filesystemSources[sourceName]
filesystemAttachments, err := filesystemSource.AttachFilesystems(params)
if err != nil {
return nil, errors.Annotatef(err, "attaching filesystems from source %q", sourceName)
}
allFilesystemAttachments = append(allFilesystemAttachments, filesystemAttachments...)
}
return allFilesystemAttachments, nil
}
func destroyFilesystems(ctx *context, tags []names.FilesystemTag) ([]error, error) {
// TODO(axw) add storage.FilesystemSource.DestroyFilesystems
return make([]error, len(tags)), nil
}
func detachFilesystems(ctx *context, attachments []storage.FilesystemAttachmentParams) error {
paramsBySource, filesystemSources, err := filesystemAttachmentParamsBySource(ctx, attachments)
if err != nil {
return errors.Trace(err)
}
for sourceName, params := range paramsBySource {
logger.Debugf("detaching filesystems: %v", params)
filesystemSource := filesystemSources[sourceName]
if err := filesystemSource.DetachFilesystems(params); err != nil {
return errors.Annotatef(err, "detaching filesystems from source %q", sourceName)
}
}
return nil
}
func filesystemAttachmentParamsBySource(
ctx *context, params []storage.FilesystemAttachmentParams,
) (map[string][]storage.FilesystemAttachmentParams, map[string]storage.FilesystemSource, error) {
// TODO(axw) later we may have multiple instantiations (sources)
// for a storage provider, e.g. multiple Ceph installations. For
// now we assume a single source for each provider type, with no
// configuration.
filesystemSources := make(map[string]storage.FilesystemSource)
paramsBySource := make(map[string][]storage.FilesystemAttachmentParams)
for _, params := range params {
sourceName := string(params.Provider)
paramsBySource[sourceName] = append(paramsBySource[sourceName], params)
if _, ok := filesystemSources[sourceName]; ok {
continue
}
filesystem := ctx.filesystems[params.Filesystem]
if filesystem.Volume != (names.VolumeTag{}) {
filesystemSources[sourceName] = ctx.managedFilesystemSource
continue
}
filesystemSource, err := filesystemSource(
ctx.environConfig, ctx.storageDir, sourceName, params.Provider,
)
if err != nil {
return nil, nil, errors.Annotate(err, "getting filesystem source")
}
filesystemSources[sourceName] = filesystemSource
}
return paramsBySource, filesystemSources, nil
}
func filesystemsFromStorage(in []storage.Filesystem) []params.Filesystem {
out := make([]params.Filesystem, len(in))
for i, f := range in {
paramsFilesystem := params.Filesystem{
f.Tag.String(),
"",
params.FilesystemInfo{
f.FilesystemId,
f.Size,
},
}
if f.Volume != (names.VolumeTag{}) {
paramsFilesystem.VolumeTag = f.Volume.String()
}
out[i] = paramsFilesystem
}
return out
}
func filesystemAttachmentsFromStorage(in []storage.FilesystemAttachment) []params.FilesystemAttachment {
out := make([]params.FilesystemAttachment, len(in))
for i, f := range in {
out[i] = params.FilesystemAttachment{
f.Filesystem.String(),
f.Machine.String(),
params.FilesystemAttachmentInfo{
f.Path,
f.ReadOnly,
},
}
}
return out
}
func filesystemFromParams(in params.Filesystem) (storage.Filesystem, error) {
filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag)
if err != nil {
return storage.Filesystem{}, errors.Trace(err)
}
var volumeTag names.VolumeTag
if in.VolumeTag != "" {
volumeTag, err = names.ParseVolumeTag(in.VolumeTag)
if err != nil {
return storage.Filesystem{}, errors.Trace(err)
}
}
return storage.Filesystem{
filesystemTag,
volumeTag,
storage.FilesystemInfo{
in.Info.FilesystemId,
in.Info.Size,
},
}, nil
}
func filesystemAttachmentFromParams(in params.FilesystemAttachment) (storage.FilesystemAttachment, error) {
filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag)
if err != nil {
return storage.FilesystemAttachment{}, errors.Trace(err)
}
machineTag, err := names.ParseMachineTag(in.MachineTag)
if err != nil {
return storage.FilesystemAttachment{}, errors.Trace(err)
}
return storage.FilesystemAttachment{
filesystemTag,
machineTag,
storage.FilesystemAttachmentInfo{
in.Info.MountPoint,
in.Info.ReadOnly,
},
}, nil
}
func filesystemParamsFromParams(in params.FilesystemParams) (storage.FilesystemParams, error) {
filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag)
if err != nil {
return storage.FilesystemParams{}, errors.Trace(err)
}
var volumeTag names.VolumeTag
if in.VolumeTag != "" {
volumeTag, err = names.ParseVolumeTag(in.VolumeTag)
if err != nil {
return storage.FilesystemParams{}, errors.Trace(err)
}
}
providerType := storage.ProviderType(in.Provider)
return storage.FilesystemParams{
filesystemTag,
volumeTag,
in.Size,
providerType,
in.Attributes,
in.Tags,
}, nil
}
func filesystemAttachmentParamsFromParams(in params.FilesystemAttachmentParams) (storage.FilesystemAttachmentParams, error) {
machineTag, err := names.ParseMachineTag(in.MachineTag)
if err != nil {
return storage.FilesystemAttachmentParams{}, errors.Trace(err)
}
filesystemTag, err := names.ParseFilesystemTag(in.FilesystemTag)
if err != nil {
return storage.FilesystemAttachmentParams{}, errors.Trace(err)
}
return storage.FilesystemAttachmentParams{
AttachmentParams: storage.AttachmentParams{
Provider: storage.ProviderType(in.Provider),
Machine: machineTag,
InstanceId: instance.Id(in.InstanceId),
ReadOnly: in.ReadOnly,
},
Filesystem: filesystemTag,
FilesystemId: in.FilesystemId,
Path: in.MountPoint,
}, nil
}