2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepMountAlicloudDisk struct {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepMountAlicloudDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
|
|
|
alicloudDiskDevices := config.ECSImagesDiskMappings
|
|
|
|
if len(config.ECSImagesDiskMappings) == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Mounting disks.")
|
2017-05-25 22:59:53 -04:00
|
|
|
disks, _, err := client.DescribeDisks(&ecs.DescribeDisksArgs{InstanceId: instance.InstanceId,
|
|
|
|
RegionId: instance.RegionId})
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Error querying disks: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
for _, disk := range disks {
|
|
|
|
if disk.Status == ecs.DiskStatusAvailable {
|
|
|
|
if err := client.AttachDisk(&ecs.AttachDiskArgs{DiskId: disk.DiskId,
|
|
|
|
InstanceId: instance.InstanceId,
|
|
|
|
Device: getDevice(&disk, alicloudDiskDevices),
|
|
|
|
}); err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Error mounting disks: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, disk := range disks {
|
|
|
|
if err := client.WaitForDisk(instance.RegionId, disk.DiskId, ecs.DiskStatusInUse, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Timeout waiting for mount: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Finished mounting disks.")
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepMountAlicloudDisk) Cleanup(state multistep.StateBag) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDevice(disk *ecs.DiskItemType, diskDevices []AlicloudDiskDevice) string {
|
|
|
|
if disk.Device != "" {
|
|
|
|
return disk.Device
|
|
|
|
}
|
|
|
|
for _, alicloudDiskDevice := range diskDevices {
|
|
|
|
if alicloudDiskDevice.DiskName == disk.DiskName || alicloudDiskDevice.SnapshotId == disk.SourceSnapshotId {
|
|
|
|
return alicloudDiskDevice.Device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|