forked from askholme/packer-builder-vultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_create.go
81 lines (66 loc) · 1.82 KB
/
step_create.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
package main
import (
"fmt"
"github.com/DarkDNA/vultr"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type stepCreateServer struct {
serverId string
}
func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*vultr.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
var err error
var keyId string
if c.SSHPrivateKey != "" {
ui.Say("Uploading SSH key")
keyId, err = client.CreateSSHKey(name, c.SSHPrivateKey)
if err != nil {
err := fmt.Errorf("Error uploading ssh key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
ui.Say("Creating server...")
// Create the droplet based on configuration
opts := client.CreateOpts()
opts.Region = c.Region
opts.Plan = c.Plan
opts.Os = c.Os
opts.PrivateNet = c.PrivateNetworking
opts.IpV6 = c.IPv6
opts.IpxeUrl = c.IpxeUrl
if c.SSHPrivateKey != "" {
opts.SSHKeyId = keyId
}
serverId, err := client.CreateServer(&opts)
if err != nil {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// We use this in cleanup
s.serverId = serverId
// Store the droplet id for later
state.Put("server_id", serverId)
return multistep.ActionContinue
}
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
// If the id isn't there, we probably never created it
if s.serverId == "" {
return
}
client := state.Get("client").(*vultr.Client)
ui := state.Get("ui").(packer.Ui)
// Destroy the server we just created
ui.Say("Destroying server...")
err := client.DeleteServer(s.serverId)
if err != nil {
ui.Error(fmt.Sprintf(
"Error destroying server. Please destroy it manually, id is: %v - error was %s", s.serverId, err))
}
}