generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstep_copy_image.go
103 lines (82 loc) · 2.94 KB
/
step_copy_image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cvm
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)
type stepCopyImage struct {
DestinationRegions []string
SourceRegion string
}
func (s *stepCopyImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
image, ok := state.GetOk("image")
// Skip if we don't have an image to copy or no regions to copy to
if !ok || len(s.DestinationRegions) == 0 || (len(s.DestinationRegions) == 1 && s.DestinationRegions[0] == s.SourceRegion) {
return multistep.ActionContinue
}
config := state.Get("config").(*Config)
client := state.Get("cvm_client").(*cvm.Client)
imageId := image.(*cvm.Image).ImageId
Say(state, strings.Join(s.DestinationRegions, ","), "Trying to copy image to")
req := cvm.NewSyncImagesRequest()
req.ImageIds = []*string{imageId}
copyRegions := make([]*string, 0, len(s.DestinationRegions))
for _, region := range s.DestinationRegions {
if region != s.SourceRegion {
copyRegions = append(copyRegions, common.StringPtr(region))
}
}
req.DestinationRegions = copyRegions
err := Retry(ctx, func(ctx context.Context) error {
_, e := client.SyncImages(req)
return e
})
if err != nil {
return Halt(state, err, "Failed to copy image")
}
Message(state, "Waiting for image ready", "")
tencentCloudImages := state.Get("tencentcloudimages").(map[string]string)
cf := &TencentCloudAccessConfig{
SecretId: config.SecretId,
SecretKey: config.SecretKey,
Zone: config.Zone,
CvmEndpoint: config.CvmEndpoint,
SecurityToken: config.SecurityToken,
AssumeRole: TencentCloudAccessRole{
RoleArn: config.AssumeRole.RoleArn,
SessionName: config.AssumeRole.SessionName,
SessionDuration: config.AssumeRole.SessionDuration,
},
Profile: config.Profile,
SharedCredentialsDir: config.SharedCredentialsDir,
}
for _, region := range req.DestinationRegions {
cf.Region = *region
rc, err := NewCvmClient(cf)
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)
Message(state, "Image copied", "")
return multistep.ActionContinue
}
func (s *stepCopyImage) Cleanup(state multistep.StateBag) {}