forked from hashicorp/packer-plugin-tencentcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_create_image.go
More file actions
158 lines (131 loc) · 3.88 KB
/
step_create_image.go
File metadata and controls
158 lines (131 loc) · 3.88 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cvm
import (
"context"
"fmt"
"github.com/hashicorp/packer-plugin-sdk/multistep"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
)
type stepCreateImage struct {
imageId string
SkipCreateImage bool
}
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("cvm_client").(*cvm.Client)
tagClient := state.Get("tag_client").(*tag.Client)
config := state.Get("config").(*Config)
instance := state.Get("instance").(*cvm.Instance)
// Optionally skip this step
if s.SkipCreateImage {
Say(state, "Skipping image creation step", "")
return multistep.ActionContinue
}
Say(state, config.ImageName, "Trying to create a new image")
req := cvm.NewCreateImageRequest()
req.ImageName = &config.ImageName
req.ImageDescription = &config.ImageDescription
req.ImageFamily = &config.ImageFamily
req.InstanceId = instance.InstanceId
// TODO: We should allow user to specify which data disk should be
// included into created image.
var dataDiskIds []*string
for _, disk := range instance.DataDisks {
dataDiskIds = append(dataDiskIds, disk.DiskId)
}
if len(dataDiskIds) > 0 {
req.DataDiskIds = dataDiskIds
}
True := "True"
False := "False"
if config.ForcePoweroff {
req.ForcePoweroff = &True
} else {
req.ForcePoweroff = &False
}
if config.Sysprep {
req.Sysprep = &True
} else {
req.Sysprep = &False
}
var tags []*cvm.Tag
for k, v := range config.ImageTags {
k := k
v := v
tags = append(tags, &cvm.Tag{
Key: &k,
Value: &v,
})
}
resourceType := "image"
if len(tags) > 0 {
req.TagSpecification = []*cvm.TagSpecification{
{
ResourceType: &resourceType,
Tags: tags,
},
}
}
err := Retry(ctx, func(ctx context.Context) error {
_, e := client.CreateImage(req)
return e
})
if err != nil {
return Halt(state, err, "Failed to create image")
}
Message(state, "Waiting for image ready", "")
err = WaitForImageReady(ctx, client, config.ImageName, "NORMAL", 3600)
if err != nil {
return Halt(state, err, "Failed to wait for image ready")
}
image, err := GetImageByName(ctx, client, config.ImageName)
if err != nil {
return Halt(state, err, "Failed to get image")
}
if image == nil {
return Halt(state, fmt.Errorf("No image return"), "Failed to crate image")
}
s.imageId = *image.ImageId
state.Put("image", image)
Message(state, s.imageId, "Image created")
snapshotTags := config.SnapshotTags
if len(snapshotTags) > 0 {
for _, snapshot := range image.SnapshotSet {
if snapshot == nil || snapshot.SnapshotId == nil {
return Halt(state, err, "snapshot or snapshotId is nil")
}
resourceName := BuildTagResourceName("cvm", "snapshot", config.Region, *snapshot.SnapshotId)
err := AddResourceTag(ctx, tagClient, resourceName, snapshotTags)
if err != nil {
return Halt(state, err, fmt.Sprintf("Failed to set tag for snapshot(%s)", *snapshot.SnapshotId))
}
}
}
tencentCloudImages := make(map[string]string)
tencentCloudImages[config.Region] = s.imageId
state.Put("tencentcloudimages", tencentCloudImages)
return multistep.ActionContinue
}
func (s *stepCreateImage) Cleanup(state multistep.StateBag) {
if s.imageId == "" {
return
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
ctx := context.TODO()
client := state.Get("cvm_client").(*cvm.Client)
SayClean(state, "image")
req := cvm.NewDeleteImagesRequest()
req.ImageIds = []*string{&s.imageId}
err := Retry(ctx, func(ctx context.Context) error {
_, e := client.DeleteImages(req)
return e
})
if err != nil {
Error(state, err, fmt.Sprintf("Failed to delete image(%s), please delete it manually", s.imageId))
}
}