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-26 03:12:07 -04:00
|
|
|
"time"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2019-08-22 16:52:29 -04:00
|
|
|
confighelper "github.com/hashicorp/packer/helper/config"
|
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
|
|
|
)
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
type stepRegionCopyAlicloudImage struct {
|
2017-03-03 03:56:17 -05:00
|
|
|
AlicloudImageDestinationRegions []string
|
|
|
|
AlicloudImageDestinationNames []string
|
|
|
|
RegionId string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-26 03:12:07 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
2019-08-22 16:52:29 -04:00
|
|
|
if config.ImageEncrypted != confighelper.TriUnset {
|
2019-04-26 03:12:07 -04:00
|
|
|
s.AlicloudImageDestinationRegions = append(s.AlicloudImageDestinationRegions, s.RegionId)
|
|
|
|
s.AlicloudImageDestinationNames = append(s.AlicloudImageDestinationNames, config.AlicloudImageName)
|
|
|
|
}
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(s.AlicloudImageDestinationRegions) == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2019-04-26 03:12:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
srcImageId := state.Get("alicloudimage").(string)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
2017-03-03 03:56:17 -05:00
|
|
|
numberOfName := len(s.AlicloudImageDestinationNames)
|
2019-04-26 03:12:07 -04:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Coping image %s from %s...", srcImageId, s.RegionId))
|
2017-03-03 03:56:17 -05:00
|
|
|
for index, destinationRegion := range s.AlicloudImageDestinationRegions {
|
2019-08-22 16:52:29 -04:00
|
|
|
if destinationRegion == s.RegionId && config.ImageEncrypted == confighelper.TriUnset {
|
2017-03-03 03:56:17 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
ecsImageName := ""
|
|
|
|
if numberOfName > 0 && index < numberOfName {
|
|
|
|
ecsImageName = s.AlicloudImageDestinationNames[index]
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
copyImageRequest := ecs.CreateCopyImageRequest()
|
|
|
|
copyImageRequest.RegionId = s.RegionId
|
2019-04-26 03:12:07 -04:00
|
|
|
copyImageRequest.ImageId = srcImageId
|
2019-04-25 22:37:49 -04:00
|
|
|
copyImageRequest.DestinationRegionId = destinationRegion
|
|
|
|
copyImageRequest.DestinationImageName = ecsImageName
|
2019-08-22 16:52:29 -04:00
|
|
|
if config.ImageEncrypted != confighelper.TriUnset {
|
|
|
|
copyImageRequest.Encrypted = requests.NewBoolean(config.ImageEncrypted.True())
|
2019-04-26 03:12:07 -04:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
imageResponse, err := client.CopyImage(copyImageRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error copying images")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
alicloudImages[destinationRegion] = imageResponse.ImageId
|
|
|
|
ui.Message(fmt.Sprintf("Copy image from %s(%s) to %s(%s)", s.RegionId, srcImageId, destinationRegion, imageResponse.ImageId))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-26 03:12:07 -04:00
|
|
|
|
2019-08-22 16:52:29 -04:00
|
|
|
if config.ImageEncrypted != confighelper.TriUnset {
|
2019-04-26 03:12:07 -04:00
|
|
|
if _, err := client.WaitForImageStatus(s.RegionId, alicloudImages[s.RegionId], ImageStatusAvailable, time.Duration(ALICLOUD_DEFAULT_LONG_TIMEOUT)*time.Second); err != nil {
|
|
|
|
return halt(state, err, fmt.Sprintf("Timeout waiting image %s finish copying", alicloudImages[s.RegionId]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
func (s *stepRegionCopyAlicloudImage) Cleanup(state multistep.StateBag) {
|
2017-03-03 03:56:17 -05:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2019-04-26 03:12:07 -04:00
|
|
|
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say(fmt.Sprintf("Stopping copy image because cancellation or error..."))
|
|
|
|
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
|
|
|
srcImageId := state.Get("alicloudimage").(string)
|
|
|
|
|
|
|
|
for copiedRegionId, copiedImageId := range alicloudImages {
|
|
|
|
if copiedImageId == srcImageId {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelCopyImageRequest := ecs.CreateCancelCopyImageRequest()
|
|
|
|
cancelCopyImageRequest.RegionId = copiedRegionId
|
|
|
|
cancelCopyImageRequest.ImageId = copiedImageId
|
|
|
|
if _, err := client.CancelCopyImage(cancelCopyImageRequest); err != nil {
|
|
|
|
|
|
|
|
ui.Error(fmt.Sprintf("Error cancelling copy image: %v", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|