-
Notifications
You must be signed in to change notification settings - Fork 7
/
step_create_server.go
123 lines (101 loc) · 3.37 KB
/
step_create_server.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
package upcloud
import (
"context"
"fmt"
"time"
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
)
// 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(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
drv := state.Get("driver").(driver.Driver)
rawSshKeyPublic, ok := state.GetOk("ssh_key_public")
if !ok {
return stepHaltWithError(state, fmt.Errorf("SSH public key is missing"))
}
sshKeyPublic := rawSshKeyPublic.(string)
ui.Say("Getting storage...")
storage, err := drv.GetStorage(s.Config.StorageUUID, s.Config.StorageName)
if err != nil {
return stepHaltWithError(state, err)
}
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(&driver.ServerOpts{
StorageUuid: storage.UUID,
StorageSize: s.Config.StorageSize,
Zone: s.Config.Zone,
SshPublicKey: sshKeyPublic,
Networking: networking,
})
if err != nil {
return stepHaltWithError(state, err)
}
ui.Say(fmt.Sprintf("Server %q created and in 'started' state", response.Title))
addr, infType := s.Config.DefaultIPaddress()
if addr != nil {
if addr.Address == "" {
addr, err = findIPAddressByType(response.IPAddresses, infType)
if err != nil {
return stepHaltWithError(state, err)
}
}
ui.Say(fmt.Sprintf("Selecting default ip '%s' as Server IP", addr.Address))
} else {
addr, err = findIPAddressByType(response.IPAddresses, InterfaceTypePublic)
if err != nil {
return stepHaltWithError(state, err)
}
ui.Say(fmt.Sprintf("Auto-selecting ip '%s' as Server IP", addr.Address))
}
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)
if s.Config.BootWait > 0 {
ui.Say(fmt.Sprintf("Waitig boot: %s", s.Config.BootWait.String()))
time.Sleep(s.Config.BootWait)
}
return multistep.ActionContinue
}
// Cleanup stops and destroys the server if server details are found in the state
func (s *StepCreateServer) Cleanup(state multistep.StateBag) {
// Extract server uuid, return if no uuid has been stored
rawServerUuid, ok := state.GetOk("server_uuid")
if !ok {
return
}
serverUuid := rawServerUuid.(string)
serverTitle := state.Get("server_title").(string)
ui := state.Get("ui").(packer.Ui)
driver := state.Get("driver").(driver.Driver)
// stop server
ui.Say(fmt.Sprintf("Stopping server %q...", serverTitle))
err := driver.StopServer(serverUuid)
if err != nil {
ui.Error(err.Error())
return
}
// delete server
ui.Say(fmt.Sprintf("Deleting server %q...", serverTitle))
err = driver.DeleteServer(serverUuid)
if err != nil {
ui.Error(err.Error())
return
}
}