-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstep_create_server.go
More file actions
221 lines (184 loc) · 6.59 KB
/
step_create_server.go
File metadata and controls
221 lines (184 loc) · 6.59 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package upcloud
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
)
// StepCreateServer represents the step that creates a server.
type StepCreateServer struct {
Config *Config
GeneratedData *packerbuilderdata.GeneratedData
}
// Run runs the actual step.
func (s *StepCreateServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui, drv, sshKeyPublic, err := s.validateState(state)
if err != nil {
return stepHaltWithError(state, err)
}
storage, err := s.getStorage(ctx, ui, drv)
if err != nil {
return stepHaltWithError(state, err)
}
response, err := s.createServer(ctx, ui, drv, storage, sshKeyPublic)
if err != nil {
return stepHaltWithError(state, err)
}
addr, err := s.selectIPAddress(ui, response)
if err != nil {
return stepHaltWithError(state, err)
}
s.populateState(state, storage, response, addr)
s.handleBootWait(ui)
return multistep.ActionContinue
}
// validateState validates and extracts required values from the state bag.
func (s *StepCreateServer) validateState(state multistep.StateBag) (packer.Ui, driver.Driver, string, error) {
ui, ok := state.Get("ui").(packer.Ui)
if !ok {
return nil, nil, "", errors.New("UI is not of expected type")
}
drv, ok := state.Get("driver").(driver.Driver)
if !ok {
return nil, nil, "", errors.New("driver is not of expected type")
}
rawSSHKeyPublic, ok := state.GetOk("ssh_key_public")
if !ok {
return nil, nil, "", errors.New("SSH public key is missing")
}
sshKeyPublic, ok := rawSSHKeyPublic.(string)
if !ok {
return nil, nil, "", errors.New("SSH public key is not of expected type")
}
return ui, drv, sshKeyPublic, nil
}
// getStorage retrieves the storage template to use for server creation.
func (s *StepCreateServer) getStorage(ctx context.Context, ui packer.Ui, drv driver.Driver) (*upcloud.Storage, error) {
ui.Say("Getting storage...")
storage, err := drv.GetStorage(ctx, s.Config.StorageUUID, s.Config.StorageName)
if err != nil {
return nil, fmt.Errorf("failed to get storage (UUID: %s, Name: %s): %w", s.Config.StorageUUID, s.Config.StorageName, err)
}
return storage, nil
}
// createServer creates the server with the specified configuration.
func (s *StepCreateServer) createServer(ctx context.Context, ui packer.Ui, drv driver.Driver, storage *upcloud.Storage, sshKeyPublic string) (*upcloud.ServerDetails, error) {
ui.Say(fmt.Sprintf("Creating server based on storage %q...", storage.Title))
networking := defaultNetworking()
if len(s.Config.NetworkInterfaces) > 0 {
networking = convertNetworkTypes(s.Config.NetworkInterfaces)
}
response, err := drv.CreateServer(ctx, &driver.ServerOpts{
ServerPlan: s.Config.ServerPlan,
StorageUUID: storage.UUID,
StorageSize: s.Config.StorageSize,
Zone: s.Config.Zone,
SSHPublicKey: sshKeyPublic,
Networking: networking,
StorageTier: s.Config.StorageTier,
})
if err != nil {
return nil, fmt.Errorf("failed to create server in zone %s: %w", s.Config.Zone, err)
}
ui.Say(fmt.Sprintf("Server %q created and in 'started' state", response.Title))
return response, nil
}
// selectIPAddress selects the appropriate IP address for the server.
func (s *StepCreateServer) selectIPAddress(ui packer.Ui, response *upcloud.ServerDetails) (*IPAddress, error) {
addr, infType := s.Config.DefaultIPaddress()
// Handle case when no default IP address is configured
if addr == nil {
return s.selectAutoIPAddress(ui, response)
}
// Handle case when default IP address needs to be resolved
if addr.Address == "" {
return s.selectDefaultIPAddress(ui, response, infType)
}
// Use the explicitly configured IP address
ui.Say(fmt.Sprintf("Selecting default ip '%s' as Server IP", addr.Address))
return addr, nil
}
// selectAutoIPAddress automatically selects a public IP address.
func (s *StepCreateServer) selectAutoIPAddress(ui packer.Ui, response *upcloud.ServerDetails) (*IPAddress, error) {
addr, err := findIPAddressByType(response.IPAddresses, InterfaceTypePublic)
if err != nil {
return nil, err
}
ui.Say(fmt.Sprintf("Auto-selecting ip '%s' as Server IP", addr.Address))
return addr, nil
}
// selectDefaultIPAddress selects an IP address based on the configured interface type.
func (s *StepCreateServer) selectDefaultIPAddress(ui packer.Ui, response *upcloud.ServerDetails, infType InterfaceType) (*IPAddress, error) {
addr, err := findIPAddressByType(response.IPAddresses, infType)
if err != nil {
return nil, err
}
ui.Say(fmt.Sprintf("Selecting default ip '%s' as Server IP", addr.Address))
return addr, nil
}
// populateState populates the state bag with server and storage information.
func (s *StepCreateServer) populateState(state multistep.StateBag, storage *upcloud.Storage, response *upcloud.ServerDetails, addr *IPAddress) {
state.Put("source_template_uuid", storage.UUID)
state.Put("source_template_title", storage.Title)
state.Put("server_ip_address", addr)
state.Put("server_uuid", response.UUID)
state.Put("server_title", response.Title)
s.GeneratedData.Put("ServerUUID", response.UUID)
s.GeneratedData.Put("ServerTitle", response.Title)
s.GeneratedData.Put("ServerSize", response.Plan)
}
// handleBootWait handles the boot wait period if configured.
func (s *StepCreateServer) handleBootWait(ui packer.Ui) {
if s.Config.BootWait > 0 {
ui.Say(fmt.Sprintf("Waitig boot: %s", s.Config.BootWait.String()))
time.Sleep(s.Config.BootWait)
}
}
// Cleanup stops and destroys the server if server details are found in the state.
func (s *StepCreateServer) Cleanup(state multistep.StateBag) {
ctx, cancel := contextWithDefaultTimeout()
defer cancel()
// Extract server uuid, return if no uuid has been stored
rawServerUUID, ok := state.GetOk("server_uuid")
if !ok {
return
}
serverUUID, ok := rawServerUUID.(string)
if !ok {
return
}
serverTitleRaw := state.Get("server_title")
serverTitle, ok := serverTitleRaw.(string)
if !ok {
return
}
uiRaw := state.Get("ui")
ui, ok := uiRaw.(packer.Ui)
if !ok {
return
}
driverRaw := state.Get("driver")
driver, ok := driverRaw.(driver.Driver)
if !ok {
return
}
// stop server
ui.Say(fmt.Sprintf("Stopping server %q...", serverTitle))
err := driver.StopServer(ctx, serverUUID)
if err != nil {
ui.Error(err.Error())
return
}
// delete server
ui.Say(fmt.Sprintf("Deleting server %q...", serverTitle))
err = driver.DeleteServer(ctx, serverUUID)
if err != nil {
ui.Error(err.Error())
return
}
}