2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-18 05:17:19 -05:00
|
|
|
"fmt"
|
2019-10-21 01:21:21 -04:00
|
|
|
"strings"
|
2018-12-20 11:09:44 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2019-11-07 05:12:19 -05:00
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
2018-12-20 11:09:44 -05:00
|
|
|
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCopyImage struct {
|
|
|
|
DesinationRegions []string
|
|
|
|
SourceRegion string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-12-20 11:09:44 -05:00
|
|
|
if len(s.DesinationRegions) == 0 || (len(s.DesinationRegions) == 1 && s.DesinationRegions[0] == s.SourceRegion) {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-02-18 05:17:19 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2018-12-20 11:09:44 -05:00
|
|
|
client := state.Get("cvm_client").(*cvm.Client)
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
imageId := state.Get("image").(*cvm.Image).ImageId
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
Say(state, strings.Join(s.DesinationRegions, ","), "Trying to copy image to")
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
req := cvm.NewSyncImagesRequest()
|
|
|
|
req.ImageIds = []*string{imageId}
|
|
|
|
copyRegions := make([]*string, 0, len(s.DesinationRegions))
|
|
|
|
for _, region := range s.DesinationRegions {
|
|
|
|
if region != s.SourceRegion {
|
2019-11-07 05:12:19 -05:00
|
|
|
copyRegions = append(copyRegions, common.StringPtr(region))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
req.DestinationRegions = copyRegions
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := client.SyncImages(req)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, err, "Failed to copy image")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2020-02-18 05:17:19 -05:00
|
|
|
Message(state, "Waiting for image ready", "")
|
|
|
|
tencentCloudImages := state.Get("tencentcloudimages").(map[string]string)
|
|
|
|
|
|
|
|
for _, region := range req.DestinationRegions {
|
|
|
|
rc, err := NewCvmClient(config.SecretId, config.SecretKey, *region)
|
|
|
|
if err != nil {
|
|
|
|
return Halt(state, err, "Failed to init client")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = WaitForImageReady(ctx, rc, config.ImageName, "NORMAL", 1800)
|
|
|
|
if err != nil {
|
|
|
|
return Halt(state, err, "Failed to wait for image ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err := GetImageByName(ctx, rc, config.ImageName)
|
|
|
|
if err != nil {
|
|
|
|
return Halt(state, err, "Failed to get image")
|
|
|
|
}
|
|
|
|
|
|
|
|
if image == nil {
|
|
|
|
return Halt(state, err, "Failed to wait for image ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
tencentCloudImages[*region] = *image.ImageId
|
|
|
|
Message(state, fmt.Sprintf("Copy image from %s(%s) to %s(%s)", s.SourceRegion, *imageId, *region, *image.ImageId), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("tencentcloudimages", tencentCloudImages)
|
2019-10-21 01:21:21 -04:00
|
|
|
Message(state, "Image copied", "")
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
func (s *stepCopyImage) Cleanup(state multistep.StateBag) {}
|