2017-03-03 16:56:17 +08:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2017-03-03 16:56:17 +08:00
|
|
|
"fmt"
|
2017-05-25 18:49:35 -07:00
|
|
|
|
2017-03-03 16:56:17 +08:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 21:04:52 +08:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 16:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateAlicloudImage struct {
|
2018-11-19 15:25:12 +08:00
|
|
|
AlicloudImageIgnoreDataDisks bool
|
2018-11-25 15:46:03 +08:00
|
|
|
WaitSnapshotReadyTimeout int
|
2018-11-19 15:25:12 +08:00
|
|
|
image *ecs.ImageType
|
2017-03-03 16:56:17 +08:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:31:41 -08:00
|
|
|
func (s *stepCreateAlicloudImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 21:40:57 +08:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 16:56:17 +08:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
// Create the alicloud image
|
2017-05-25 18:49:35 -07:00
|
|
|
ui.Say(fmt.Sprintf("Creating image: %s", config.AlicloudImageName))
|
2017-03-03 16:56:17 +08:00
|
|
|
var imageId string
|
|
|
|
var err error
|
|
|
|
|
2018-11-19 15:25:12 +08:00
|
|
|
if s.AlicloudImageIgnoreDataDisks {
|
|
|
|
snapshotId := state.Get("alicloudsnapshot").(string)
|
|
|
|
imageId, err = client.CreateImage(&ecs.CreateImageArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
SnapshotId: snapshotId,
|
|
|
|
ImageName: config.AlicloudImageName,
|
|
|
|
ImageVersion: config.AlicloudImageVersion,
|
|
|
|
Description: config.AlicloudImageDescription})
|
|
|
|
} else {
|
|
|
|
instance := state.Get("instance").(*ecs.InstanceAttributesType)
|
|
|
|
imageId, err = client.CreateImage(&ecs.CreateImageArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
InstanceId: instance.InstanceId,
|
|
|
|
ImageName: config.AlicloudImageName,
|
|
|
|
ImageVersion: config.AlicloudImageVersion,
|
|
|
|
Description: config.AlicloudImageDescription})
|
|
|
|
}
|
2017-03-03 16:56:17 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-11-26 16:13:06 +08:00
|
|
|
return halt(state, err, "Error creating image")
|
2017-03-03 16:56:17 +08:00
|
|
|
}
|
2018-11-25 15:46:03 +08:00
|
|
|
err = client.WaitForImageReady(common.Region(config.AlicloudRegion), imageId, s.WaitSnapshotReadyTimeout)
|
2017-03-03 16:56:17 +08:00
|
|
|
if err != nil {
|
2018-11-26 16:13:06 +08:00
|
|
|
return halt(state, err, "Timeout waiting for image to be created")
|
2017-03-03 16:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
ImageId: imageId})
|
|
|
|
if err != nil {
|
2018-11-26 16:13:06 +08:00
|
|
|
return halt(state, err, "Error querying created imaged")
|
2017-03-03 16:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(images) == 0 {
|
2018-11-26 16:13:06 +08:00
|
|
|
return halt(state, err, "Unable to find created image")
|
2017-03-03 16:56:17 +08:00
|
|
|
}
|
2018-11-26 16:13:06 +08:00
|
|
|
|
2017-03-03 16:56:17 +08:00
|
|
|
s.image = &images[0]
|
|
|
|
|
2018-11-26 16:13:06 +08:00
|
|
|
var snapshotIds = []string{}
|
|
|
|
for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping {
|
|
|
|
snapshotIds = append(snapshotIds, device.SnapshotId)
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:56:17 +08:00
|
|
|
state.Put("alicloudimage", imageId)
|
2018-11-26 16:13:06 +08:00
|
|
|
state.Put("alicloudsnapshots", snapshotIds)
|
|
|
|
|
2017-03-03 16:56:17 +08: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
|
|
|
|
}
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-18 21:40:57 +08:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 16:56:17 +08:00
|
|
|
|
2017-05-25 18:49:35 -07:00
|
|
|
ui.Say("Deleting the image because of cancellation or error...")
|
2017-03-03 16:56:17 +08:00
|
|
|
if err := client.DeleteImage(common.Region(config.AlicloudRegion), s.image.ImageId); err != nil {
|
2017-05-25 18:49:35 -07:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting image, it may still be around: %s", err))
|
2017-03-03 16:56:17 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|