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 {
|
|
|
|
image *ecs.ImageType
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepCreateAlicloudImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
config := state.Get("config").(Config)
|
|
|
|
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
|
|
|
|
|
|
|
|
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})
|
|
|
|
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Error creating image: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
err = client.WaitForImageReady(common.Region(config.AlicloudRegion),
|
|
|
|
imageId, ALICLOUD_DEFAULT_LONG_TIMEOUT)
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Timeout waiting for image to be created: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
ImageId: imageId})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Error querying created image: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(images) == 0 {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Unable to find created image: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
s.image = &images[0]
|
|
|
|
|
|
|
|
state.Put("alicloudimage", imageId)
|
|
|
|
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)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|