2018-11-19 02:25:12 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-04-26 03:12:07 -04:00
|
|
|
"time"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2018-11-19 02:25:12 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateAlicloudSnapshot struct {
|
2019-04-25 22:37:49 -04:00
|
|
|
snapshot *ecs.Snapshot
|
2018-11-25 02:46:03 -05:00
|
|
|
WaitSnapshotReadyTimeout int
|
2018-11-19 02:25:12 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateAlicloudSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-11-19 02:25:12 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2018-11-19 02:25:12 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2018-11-19 02:25:12 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
describeDisksRequest := ecs.CreateDescribeDisksRequest()
|
|
|
|
describeDisksRequest.RegionId = config.AlicloudRegion
|
|
|
|
describeDisksRequest.InstanceId = instance.InstanceId
|
|
|
|
describeDisksRequest.DiskType = DiskTypeSystem
|
|
|
|
disksResponse, err := client.DescribeDisks(describeDisksRequest)
|
2018-11-19 02:25:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Error describe disks")
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
disks := disksResponse.Disks.Disk
|
2018-11-19 02:25:12 -05:00
|
|
|
if len(disks) == 0 {
|
|
|
|
return halt(state, err, "Unable to find system disk of instance")
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
createSnapshotRequest := ecs.CreateCreateSnapshotRequest()
|
|
|
|
createSnapshotRequest.DiskId = disks[0].DiskId
|
|
|
|
snapshot, err := client.CreateSnapshot(createSnapshotRequest)
|
2018-11-19 02:25:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Error creating snapshot")
|
|
|
|
}
|
|
|
|
|
2019-06-11 03:22:06 -04:00
|
|
|
// Create the alicloud snapshot
|
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot from system disk %s: %s", disks[0].DiskId, snapshot.SnapshotId))
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-06-11 03:22:06 -04:00
|
|
|
snapshotsResponse, err := client.WaitForSnapshotStatus(config.AlicloudRegion, snapshot.SnapshotId, SnapshotStatusAccomplished, time.Duration(s.WaitSnapshotReadyTimeout)*time.Second)
|
2018-11-19 02:25:12 -05:00
|
|
|
if err != nil {
|
2019-06-11 03:22:06 -04:00
|
|
|
_, ok := err.(errors.Error)
|
|
|
|
if ok {
|
|
|
|
return halt(state, err, "Error querying created snapshot")
|
|
|
|
}
|
2018-11-19 02:25:12 -05:00
|
|
|
|
2019-06-11 03:22:06 -04:00
|
|
|
return halt(state, err, "Timeout waiting for snapshot to be created")
|
2018-11-19 02:25:12 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-06-11 03:22:06 -04:00
|
|
|
snapshots := snapshotsResponse.(*ecs.DescribeSnapshotsResponse).Snapshots.Snapshot
|
2018-11-19 02:25:12 -05:00
|
|
|
if len(snapshots) == 0 {
|
|
|
|
return halt(state, err, "Unable to find created snapshot")
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
s.snapshot = &snapshots[0]
|
|
|
|
state.Put("alicloudsnapshot", snapshot.SnapshotId)
|
2018-11-19 02:25:12 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateAlicloudSnapshot) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.snapshot == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2018-11-19 02:25:12 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Deleting the snapshot because of cancellation or error...")
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
deleteSnapshotRequest := ecs.CreateDeleteSnapshotRequest()
|
|
|
|
deleteSnapshotRequest.SnapshotId = s.snapshot.SnapshotId
|
|
|
|
if _, err := client.DeleteSnapshot(deleteSnapshotRequest); err != nil {
|
2018-11-19 02:25:12 -05:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting snapshot, it may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|