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"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
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
|
2018-11-19 02:25:12 -05:00
|
|
|
image *ecs.ImageType
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepCreateAlicloudImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
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
|
|
|
var imageId string
|
|
|
|
var err error
|
|
|
|
|
2018-11-19 02:25:12 -05: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 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
|
|
|
}
|
2018-11-25 02:46:03 -05:00
|
|
|
err = client.WaitForImageReady(common.Region(config.AlicloudRegion), imageId, s.WaitSnapshotReadyTimeout)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
ImageId: imageId})
|
|
|
|
if err != nil {
|
2018-11-26 03:13:06 -05:00
|
|
|
return halt(state, err, "Error querying created imaged")
|
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]
|
|
|
|
|
2018-11-26 03:13:06 -05:00
|
|
|
var snapshotIds = []string{}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
_, 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 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...")
|
2017-03-03 03:56:17 -05:00
|
|
|
if err := client.DeleteImage(common.Region(config.AlicloudRegion), s.image.ImageId); 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
|
|
|
|
}
|
|
|
|
}
|