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-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"
|
2019-04-25 22:37:49 -04:00
|
|
|
"time"
|
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)
|
|
|
|
|
|
|
|
// Create the alicloud image
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating image: %s", config.AlicloudImageName))
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
createImageRequest := s.buildCreateImageRequest(state)
|
|
|
|
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
|
|
|
|
|
|
|
|
_, 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-25 22:37:49 -04:00
|
|
|
describeImagesRequest := ecs.CreateDescribeImagesRequest()
|
|
|
|
describeImagesRequest.ImageId = imageId
|
|
|
|
describeImagesRequest.RegionId = config.AlicloudRegion
|
|
|
|
imagesResponse, err := client.DescribeImages(describeImagesRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
images := imagesResponse.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
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
_, 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)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say("Deleting the image 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-25 22:37:49 -04:00
|
|
|
|
|
|
|
func (s *stepCreateAlicloudImage) buildCreateImageRequest(state multistep.StateBag) *ecs.CreateImageRequest {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
request := ecs.CreateCreateImageRequest()
|
|
|
|
request.ClientToken = uuid.TimeOrderedUUID()
|
|
|
|
request.RegionId = config.AlicloudRegion
|
|
|
|
request.ImageName = config.AlicloudImageName
|
|
|
|
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
|
|
|
|
}
|