2018-11-19 02:25:12 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-04-26 03:12:07 -04:00
|
|
|
"time"
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
|
|
|
|
"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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the alicloud snapshot
|
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot from system disk: %s", disks[0].DiskId))
|
|
|
|
|
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-04-25 22:37:49 -04:00
|
|
|
_, err = client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
request := ecs.CreateDescribeSnapshotsRequest()
|
|
|
|
request.RegionId = config.AlicloudRegion
|
|
|
|
request.SnapshotIds = snapshot.SnapshotId
|
|
|
|
return client.DescribeSnapshots(request)
|
|
|
|
},
|
|
|
|
EvalFunc: func(response responses.AcsResponse, err error) WaitForExpectEvalResult {
|
|
|
|
if err != nil {
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshotsResponse := response.(*ecs.DescribeSnapshotsResponse)
|
|
|
|
snapshots := snapshotsResponse.Snapshots.Snapshot
|
|
|
|
for _, snapshot := range snapshots {
|
|
|
|
if snapshot.Status == SnapshotStatusAccomplished {
|
|
|
|
return WaitForExpectSuccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
},
|
|
|
|
RetryTimeout: time.Duration(s.WaitSnapshotReadyTimeout) * time.Second,
|
|
|
|
})
|
|
|
|
|
2018-11-19 02:25:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Timeout waiting for snapshot to be created")
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
describeSnapshotsRequest := ecs.CreateDescribeSnapshotsRequest()
|
|
|
|
describeSnapshotsRequest.RegionId = config.AlicloudRegion
|
|
|
|
describeSnapshotsRequest.SnapshotIds = snapshot.SnapshotId
|
2018-11-19 02:25:12 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
snapshotsResponse, err := client.DescribeSnapshots(describeSnapshotsRequest)
|
2018-11-19 02:25:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Error querying created snapshot")
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
snapshots := snapshotsResponse.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
|
|
|
|
}
|
|
|
|
}
|