-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstep_capture_image.go
173 lines (148 loc) · 5.33 KB
/
step_capture_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package vpc
import (
"context"
"fmt"
"log"
"regexp"
"github.com/IBM/go-sdk-core/v5/core"
globaltaggingv1 "github.com/IBM/platform-services-go-sdk/globaltaggingv1"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepCaptureImage struct{}
func (s *stepCaptureImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*IBMCloudClient)
config := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
var vpcService *vpcv1.VpcV1
if state.Get("vpcService") != nil {
vpcService = state.Get("vpcService").(*vpcv1.VpcV1)
}
instanceData := state.Get("instance_data").(*vpcv1.Instance)
instanceID := *instanceData.ID
ui.Say(fmt.Sprintf("Stopping instance ID: %s ...", instanceID))
status, err := client.manageInstance(instanceID, "stop", state)
if err != nil {
err := fmt.Errorf("[ERROR] Error stopping the instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
if status != "stopped" {
err := client.waitForResourceDown(instanceID, "instances", config.StateTimeout, state)
if err != nil {
err := fmt.Errorf("[ERROR] Error stopping the instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
}
ui.Say("Instance successfully stopped!")
ui.Say(fmt.Sprintf("Creating an Image from instance ID: %s ...", instanceID))
bootVolumeAttachment := instanceData.BootVolumeAttachment
bootVolume := bootVolumeAttachment.Volume
bootVolumeId := *bootVolume.ID
validName := regexp.MustCompile(`[^a-z0-9\-]+`)
config.ImageName = validName.ReplaceAllString(config.ImageName, "")
options := &vpcv1.CreateImageOptions{}
imagePrototype := &vpcv1.ImagePrototypeImageBySourceVolume{
Name: &config.ImageName,
SourceVolume: &vpcv1.VolumeIdentityByID{
ID: &bootVolumeId,
},
}
// Encryption key to create an encrypted image
if config.EncryptionKeyCRN != "" {
imagePrototype.EncryptionKey = &vpcv1.EncryptionKeyIdentity{
CRN: &config.EncryptionKeyCRN,
}
}
if config.ResourceGroupID != "" {
imagePrototype.ResourceGroup = &vpcv1.ResourceGroupIdentityByID{
ID: &config.ResourceGroupID,
}
} else if config.ResourceGroupName != "" {
derivedResourceGroupId := state.Get("derived_resource_group_id")
if derivedResourceGroupId != nil && derivedResourceGroupId.(string) != "" {
derivedResourceGroupIdStr := derivedResourceGroupId.(string)
imagePrototype.ResourceGroup = &vpcv1.ResourceGroupIdentityByID{
ID: &derivedResourceGroupIdStr,
}
}
}
options.SetImagePrototype(imagePrototype)
imageData, _, err := vpcService.CreateImage(options)
if err != nil {
err := fmt.Errorf("[ERROR] Error sending the HTTP request that creates the image. Error: %s", err)
ui.Error(err.Error())
log.Println(err.Error())
return multistep.ActionHalt
}
if err != nil {
err := fmt.Errorf("[ERROR] Error creating the Image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
imageId := *imageData.ID
state.Put("image_id", imageId)
ui.Say("Image Successfully created!")
ui.Say(fmt.Sprintf("Image's Name: %s", config.ImageName))
ui.Say(fmt.Sprintf("Image's ID: %s", imageId))
if config.ImageTags != nil && len(config.ImageTags) > 0 {
optGlbTag := globaltaggingv1.GlobalTaggingV1Options{
Authenticator: &core.IamAuthenticator{
ApiKey: client.IBMApiKey,
URL: config.IAMEndpoint,
},
}
if config.GhostEndpoint != "" {
optGlbTag.URL = config.GhostEndpoint
}
serviceClientOptions, errOpt := globaltaggingv1.NewGlobalTaggingV1(&optGlbTag)
if errOpt != nil {
err := fmt.Errorf("[ERROR] Error creating global tagging client: %s", errOpt)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
var tagType = new(string)
*tagType = "user"
resources := []globaltaggingv1.Resource{}
r := globaltaggingv1.Resource{ResourceID: imageData.CRN, ResourceType: nil}
resources = append(resources, r)
AttachTagOptions := &globaltaggingv1.AttachTagOptions{}
AttachTagOptions.Resources = resources
AttachTagOptions.TagNames = config.ImageTags
AttachTagOptions.TagType = tagType
_, resp, err := serviceClientOptions.AttachTag(AttachTagOptions)
if err != nil {
errUserTags := fmt.Errorf("[ERROR] Error attaching tags %v : %s\n%s", config.ImageTags, err, resp)
state.Put("error", errUserTags)
ui.Say(errUserTags.Error())
}
}
ui.Say("Waiting for the Image to become AVAILABLE...")
err2 := client.waitForResourceReady(imageId, "images", config.StateTimeout, state)
if err2 != nil {
err := fmt.Errorf("[ERROR] Error waiting for the Image to become AVAILABLE: %s", err2)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return multistep.ActionHalt
}
ui.Say("Image is now AVAILABLE!")
return multistep.ActionContinue
}
func (s *stepCaptureImage) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
ui.Say("")
ui.Say("****************************************************************************")
ui.Say("* Cleaning Up all temporary infrastructure created during packer execution *")
ui.Say("****************************************************************************")
ui.Say("")
}