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"
|
2019-04-26 03:12:07 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common/random"
|
|
|
|
|
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"
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
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 stepCreateAlicloudImage struct {
|
2018-11-19 02:25:12 -05:00
|
|
|
AlicloudImageIgnoreDataDisks bool
|
2018-11-25 02:46:03 -05:00
|
|
|
WaitSnapshotReadyTimeout int
|
2019-04-25 22:37:49 -04:00
|
|
|
image *ecs.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
var createImageRetryErrors = []string{
|
|
|
|
"IdempotentProcessing",
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateAlicloudImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
tempImageName := config.AlicloudImageName
|
|
|
|
if config.ImageEncrypted != nil && *config.ImageEncrypted {
|
|
|
|
tempImageName = fmt.Sprintf("packer_%s", random.AlphaNum(7))
|
|
|
|
ui.Say(fmt.Sprintf("Creating temporary image for encryption: %s", tempImageName))
|
|
|
|
} else {
|
|
|
|
ui.Say(fmt.Sprintf("Creating image: %s", tempImageName))
|
|
|
|
}
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
createImageRequest := s.buildCreateImageRequest(state, tempImageName)
|
2019-04-25 22:37:49 -04:00
|
|
|
createImageResponse, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
return client.CreateImage(createImageRequest)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(createImageRetryErrors, EvalRetryErrorType),
|
|
|
|
})
|
2017-03-03 03:56:17 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2018-11-26 03:13:06 -05:00
|
|
|
return halt(state, err, "Error creating image")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
imageId := createImageResponse.(*ecs.CreateImageResponse).ImageId
|
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
imagesResponse, err := client.WaitForImageStatus(config.AlicloudRegion, imageId, ImageStatusAvailable, time.Duration(s.WaitSnapshotReadyTimeout)*time.Second)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2018-11-26 03:13:06 -05:00
|
|
|
return halt(state, err, "Timeout waiting for image to be created")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
images := imagesResponse.(*ecs.DescribeImagesResponse).Images.Image
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(images) == 0 {
|
2018-11-26 03:13:06 -05:00
|
|
|
return halt(state, err, "Unable to find created image")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2018-11-26 03:13:06 -05:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
s.image = &images[0]
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
var snapshotIds []string
|
2018-11-26 03:13:06 -05:00
|
|
|
for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping {
|
|
|
|
snapshotIds = append(snapshotIds, device.SnapshotId)
|
|
|
|
}
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("alicloudimage", imageId)
|
2018-11-26 03:13:06 -05:00
|
|
|
state.Put("alicloudsnapshots", snapshotIds)
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
alicloudImages := make(map[string]string)
|
|
|
|
alicloudImages[config.AlicloudRegion] = images[0].ImageId
|
|
|
|
state.Put("alicloudimages", alicloudImages)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateAlicloudImage) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.image == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
encryptedSet := config.ImageEncrypted != nil && *config.ImageEncrypted
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2019-04-26 03:12:07 -04:00
|
|
|
|
|
|
|
if !cancelled && !halted && !encryptedSet {
|
2017-03-03 03:56:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
if !cancelled && !halted && encryptedSet {
|
|
|
|
ui.Say(fmt.Sprintf("Deleting temporary image %s(%s) and related snapshots after finishing encryption...", s.image.ImageId, s.image.ImageName))
|
|
|
|
} else {
|
|
|
|
ui.Say("Deleting the image and related snapshots because of cancellation or error...")
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
deleteImageRequest := ecs.CreateDeleteImageRequest()
|
|
|
|
deleteImageRequest.RegionId = config.AlicloudRegion
|
|
|
|
deleteImageRequest.ImageId = s.image.ImageId
|
|
|
|
if _, err := client.DeleteImage(deleteImageRequest); err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting image, it may still be around: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return
|
|
|
|
}
|
2019-04-26 03:12:07 -04:00
|
|
|
|
|
|
|
//Delete the snapshot of this image
|
|
|
|
for _, diskDevices := range s.image.DiskDeviceMappings.DiskDeviceMapping {
|
|
|
|
deleteSnapshotRequest := ecs.CreateDeleteSnapshotRequest()
|
|
|
|
deleteSnapshotRequest.SnapshotId = diskDevices.SnapshotId
|
|
|
|
if _, err := client.DeleteSnapshot(deleteSnapshotRequest); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting snapshot, it may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
func (s *stepCreateAlicloudImage) buildCreateImageRequest(state multistep.StateBag, imageName string) *ecs.CreateImageRequest {
|
2019-04-25 22:37:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
request := ecs.CreateCreateImageRequest()
|
|
|
|
request.ClientToken = uuid.TimeOrderedUUID()
|
|
|
|
request.RegionId = config.AlicloudRegion
|
2019-04-26 03:12:07 -04:00
|
|
|
request.ImageName = imageName
|
2019-04-25 22:37:49 -04:00
|
|
|
request.ImageVersion = config.AlicloudImageVersion
|
|
|
|
request.Description = config.AlicloudImageDescription
|
|
|
|
|
|
|
|
if s.AlicloudImageIgnoreDataDisks {
|
|
|
|
snapshotId := state.Get("alicloudsnapshot").(string)
|
|
|
|
request.SnapshotId = snapshotId
|
|
|
|
} else {
|
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
|
|
|
request.InstanceId = instance.InstanceId
|
|
|
|
}
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|