|
| 1 | +package cloudinit |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + "gopkg.in/yaml.v2" |
| 10 | + |
| 11 | + "github.com/weaveworks-liquidmetal/flintlock/client/cloudinit" |
| 12 | + "github.com/weaveworks-liquidmetal/flintlock/client/cloudinit/userdata" |
| 13 | + "github.com/weaveworks-liquidmetal/flintlock/core/models" |
| 14 | + "github.com/weaveworks-liquidmetal/flintlock/pkg/log" |
| 15 | + "github.com/weaveworks-liquidmetal/flintlock/pkg/planner" |
| 16 | +) |
| 17 | + |
| 18 | +func NewDiskMountStep(vm *models.MicroVM) planner.Procedure { |
| 19 | + return &diskMountStep{ |
| 20 | + vm: vm, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +type diskMountStep struct { |
| 25 | + vm *models.MicroVM |
| 26 | +} |
| 27 | + |
| 28 | +// Name is the name of the procedure/operation. |
| 29 | +func (s *diskMountStep) Name() string { |
| 30 | + return "cloudinit_disk_mount" |
| 31 | +} |
| 32 | + |
| 33 | +func (s *diskMountStep) ShouldDo(ctx context.Context) (bool, error) { |
| 34 | + logger := log.GetLogger(ctx).WithFields(logrus.Fields{ |
| 35 | + "step": s.Name(), |
| 36 | + }) |
| 37 | + logger.Debug("checking if procedure should be run") |
| 38 | + |
| 39 | + if !s.vm.Spec.AdditionalVolumes.HasMountableVolumes() { |
| 40 | + return false, nil |
| 41 | + } |
| 42 | + |
| 43 | + for _, vol := range s.vm.Spec.AdditionalVolumes { |
| 44 | + if vol.MountPoint == "" { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + status := s.vm.Status.Volumes[vol.ID] |
| 49 | + |
| 50 | + if status == nil || status.Mount.Source == "" { |
| 51 | + return true, nil |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + vendorData, err := s.getVendorData() |
| 56 | + if err != nil { |
| 57 | + return false, fmt.Errorf("getting vendor data: %w", err) |
| 58 | + } |
| 59 | + if vendorData == nil { |
| 60 | + return true, nil |
| 61 | + } |
| 62 | + |
| 63 | + for _, vol := range s.vm.Spec.AdditionalVolumes { |
| 64 | + if vol.MountPoint == "" { |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + if !vendorData.HasMountByMountPoint(vol.MountPoint) { |
| 69 | + return true, nil |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return false, nil |
| 74 | +} |
| 75 | + |
| 76 | +// Do will perform the operation/procedure. |
| 77 | +func (s *diskMountStep) Do(ctx context.Context) ([]planner.Procedure, error) { |
| 78 | + logger := log.GetLogger(ctx).WithFields(logrus.Fields{ |
| 79 | + "step": s.Name(), |
| 80 | + }) |
| 81 | + logger.Debug("running step to mount additional disks via cloud-init") |
| 82 | + |
| 83 | + vendorData, err := s.getVendorData() |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("getting vendor data: %w", err) |
| 86 | + } |
| 87 | + if vendorData == nil { |
| 88 | + vendorData = &userdata.UserData{} |
| 89 | + } |
| 90 | + |
| 91 | + startingCode := int('b') |
| 92 | + for i, vol := range s.vm.Spec.AdditionalVolumes { |
| 93 | + if vol.MountPoint == "" { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + device := fmt.Sprintf("vd%c", rune(startingCode+i)) // Device number is always +1 as we have the root volume first |
| 98 | + |
| 99 | + if !vendorData.HasMountByName(device) { |
| 100 | + vendorData.Mounts = append(vendorData.Mounts, userdata.Mount{ |
| 101 | + device, |
| 102 | + vol.MountPoint, |
| 103 | + }) |
| 104 | + } |
| 105 | + } |
| 106 | + vendorData.MountDefaultFields = userdata.Mount{"None", "None", "auto", "defaults,nofail", "0", "2"} |
| 107 | + |
| 108 | + data, err := yaml.Marshal(vendorData) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("marshalling vendor-data to yaml: %w", err) |
| 111 | + } |
| 112 | + dataWithHeader := append([]byte("## template: jinja\n#cloud-config\n\n"), data...) |
| 113 | + |
| 114 | + if s.vm.Spec.Metadata == nil { |
| 115 | + s.vm.Spec.Metadata = map[string]string{} |
| 116 | + } |
| 117 | + s.vm.Spec.Metadata[cloudinit.VendorDataKey] = base64.StdEncoding.EncodeToString(dataWithHeader) |
| 118 | + |
| 119 | + return nil, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (s *diskMountStep) Verify(ctx context.Context) error { |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func (s *diskMountStep) getVendorData() (*userdata.UserData, error) { |
| 127 | + vendorDataRaw, ok := s.vm.Spec.Metadata[cloudinit.VendorDataKey] |
| 128 | + if !ok { |
| 129 | + return nil, nil |
| 130 | + } |
| 131 | + |
| 132 | + vendorData := &userdata.UserData{} |
| 133 | + data, err := base64.StdEncoding.DecodeString(vendorDataRaw) |
| 134 | + if err != nil { |
| 135 | + return nil, fmt.Errorf("decoding vendor data: %w", err) |
| 136 | + } |
| 137 | + if marshalErr := yaml.Unmarshal(data, vendorData); marshalErr != nil { |
| 138 | + return nil, fmt.Errorf("unmarshalling vendor-data yaml: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + return vendorData, nil |
| 142 | +} |
0 commit comments