2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type setpRegionCopyAlicloudImage struct {
|
|
|
|
AlicloudImageDestinationRegions []string
|
|
|
|
AlicloudImageDestinationNames []string
|
|
|
|
RegionId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *setpRegionCopyAlicloudImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
if len(s.AlicloudImageDestinationRegions) == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
imageId := state.Get("alicloudimage").(string)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
|
|
|
region := common.Region(s.RegionId)
|
|
|
|
|
|
|
|
numberOfName := len(s.AlicloudImageDestinationNames)
|
|
|
|
for index, destinationRegion := range s.AlicloudImageDestinationRegions {
|
|
|
|
if destinationRegion == s.RegionId {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ecsImageName := ""
|
|
|
|
if numberOfName > 0 && index < numberOfName {
|
|
|
|
ecsImageName = s.AlicloudImageDestinationNames[index]
|
|
|
|
}
|
|
|
|
imageId, err := client.CopyImage(
|
|
|
|
&ecs.CopyImageArgs{
|
|
|
|
RegionId: region,
|
|
|
|
ImageId: imageId,
|
|
|
|
DestinationRegionId: common.Region(destinationRegion),
|
|
|
|
DestinationImageName: ecsImageName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Error copying images: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
alicloudImages[destinationRegion] = imageId
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *setpRegionCopyAlicloudImage) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if cancelled || halted {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say(fmt.Sprintf("Stopping copy image because cancellation or error..."))
|
2017-10-26 08:42:49 -04:00
|
|
|
for copiedRegionId, copiedImageId := range alicloudImages {
|
|
|
|
if copiedRegionId == s.RegionId {
|
2017-03-03 03:56:17 -05:00
|
|
|
continue
|
|
|
|
}
|
2017-10-26 08:42:49 -04:00
|
|
|
if err := client.CancelCopyImage(common.Region(copiedRegionId), copiedImageId); err != nil {
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say(fmt.Sprintf("Error cancelling copy image: %v", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|