forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_create_server.go
131 lines (103 loc) · 2.85 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
124
125
126
127
128
129
130
131
package oneandone
import (
"fmt"
"github.com/1and1/oneandone-cloudserver-sdk-go"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"strings"
"time"
)
type stepCreateServer struct{}
func (s *stepCreateServer) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
if sshkey, ok := state.GetOk("publicKey"); ok {
c.SSHKey = sshkey.(string)
}
token := oneandone.SetToken(c.Token)
//Create an API client
api := oneandone.New(token, c.Url)
// List server appliances
saps, _ := api.ListServerAppliances()
time.Sleep(time.Second * 10)
var sa oneandone.ServerAppliance
for _, a := range saps {
if a.Type == "IMAGE" && strings.Contains(strings.ToLower(a.Name), strings.ToLower(c.Image)) {
sa = a
break
}
}
if c.DiskSize < sa.MinHddSize {
ui.Error(fmt.Sprintf("Minimum required disk size %d", sa.MinHddSize))
}
ui.Say("Creating Server...")
// Create a server
req := oneandone.ServerRequest{
Name: c.SnapshotName,
Description: "Example server description.",
ApplianceId: sa.Id,
PowerOn: true,
Hardware: oneandone.Hardware{
Vcores: 1,
CoresPerProcessor: 1,
Ram: 2,
Hdds: []oneandone.Hdd{
{
Size: c.DiskSize,
IsMain: true,
},
},
},
}
if c.DataCenterId != "" {
req.DatacenterId = c.DataCenterId
}
if c.Comm.SSHPassword != "" {
req.Password = c.Comm.SSHPassword
}
if c.SSHKey != "" {
req.SSHKey = c.SSHKey
}
server_id, server, err := api.CreateServer(&req)
if err == nil {
// Wait until server is created and powered on for at most 60 x 10 seconds
err = api.WaitForState(server, "POWERED_ON", 10, c.Retries)
} else {
ui.Error(err.Error())
return multistep.ActionHalt
}
// Get a server
server, err = api.GetServer(server_id)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("server_id", server_id)
state.Put("server_ip", server.Ips[0].Ip)
return multistep.ActionContinue
}
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
c := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
ui.Say("Removing Server...")
token := oneandone.SetToken(c.Token)
//Create an API client
api := oneandone.New(token, oneandone.BaseUrl)
var serverId string
if temp, ok := state.GetOk("server_id"); ok {
serverId = temp.(string)
}
if serverId != "" {
server, err := api.ShutdownServer(serverId, false)
if err != nil {
ui.Error(fmt.Sprintf("Error shutting down 1and1 server. Please destroy it manually: %s", serverId))
ui.Error(err.Error())
}
err = api.WaitForState(server, "POWERED_OFF", 10, c.Retries)
server, err = api.DeleteServer(server.Id, false)
if err != nil {
ui.Error(fmt.Sprintf("Error deleting 1and1 server. Please destroy it manually: %s", serverId))
ui.Error(err.Error())
}
}
}