-
Notifications
You must be signed in to change notification settings - Fork 2
/
instance.go
291 lines (241 loc) · 7.02 KB
/
instance.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/1and1/oneandone-cloudserver-sdk-go"
log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/spf13/afero"
)
// Provisioner is instance provisioner type for 1&1 Cloud Server.
type Provisioner struct {
client *oneandone.API
prop properties
Dir string
fs afero.Fs
wait bool
}
type properties struct {
apiKey string
Datacenter string
Appliance string
FixedServerSize string
Password string
SSHKey string
SSHKeyPath string
FirewallID string
LoadBalancerID string
MonitorPolicyID string
}
// NewInstancePlugin creates a new plugin that creates 1&1 Cloud Server instances.
func NewInstancePlugin(apiKey string, dir string) instance.Plugin {
log.Infof("oneandone instance plugin. dir=%s", dir)
return &Provisioner{
client: oneandone.New(apiKey, oneandone.BaseUrl),
prop: properties{
apiKey: apiKey,
},
Dir: dir,
fs: afero.NewOsFs(),
wait: true,
}
}
func (p *Provisioner) validateAppliance(prop properties) error {
serverApp := strings.ToLower(prop.Appliance)
if serverApp == "" {
serverApp = "ubuntu1404-64std" // Default server appliance
}
appliances, err := p.client.ListServerAppliances(0, 0, "", serverApp, "")
if err != nil {
return fmt.Errorf("Validating server appliance '%s' failed. Error: %s", serverApp, err.Error())
}
for _, sa := range appliances {
if serverApp == strings.ToLower(sa.Name) || serverApp == strings.ToLower(sa.Id) {
p.prop.Appliance = sa.Id
}
}
if p.prop.Appliance == "" {
return fmt.Errorf("Server appliance '%s' could not be found.", serverApp)
}
return nil
}
func (p *Provisioner) validateDatacenter(prop properties) error {
dc := strings.ToUpper(prop.Datacenter)
if dc == "" {
dc = "US" // Default data center
}
datacenters, err := p.client.ListDatacenters()
if err != nil {
return fmt.Errorf("Validating data center '%s' failed. Error: %s", dc, err.Error())
}
for _, loc := range datacenters {
if dc == strings.ToUpper(loc.CountryCode) || dc == strings.ToUpper(loc.Id) {
p.prop.Datacenter = loc.Id
}
}
if p.prop.Datacenter == "" {
return fmt.Errorf("Data center '%s' could not be found.", dc)
}
return nil
}
func (p *Provisioner) validateServerSize(prop properties) error {
size := strings.ToUpper(prop.FixedServerSize)
if size == "" {
size = "M" // Default server size
}
fixSizes, err := p.client.ListFixedInstanceSizes()
if err != nil {
return fmt.Errorf("Validating fixed-instance size '%s' failed. Error: %s", size, err.Error())
}
for _, s := range fixSizes {
if size == strings.ToUpper(s.Name) || size == strings.ToUpper(s.Id) {
p.prop.FixedServerSize = s.Id
}
}
if p.prop.FixedServerSize == "" {
return fmt.Errorf("Fixed-instance size '%s' could not be found.", size)
}
return nil
}
// Validate the provisioner and 1&1 Cloud Server properties
func (p *Provisioner) Validate(req json.RawMessage) error {
if p.client == nil {
return fmt.Errorf("1&1 API client is not created. Please use the appropriate methods to instantiate the provisioner.")
}
if p.prop.apiKey == "" {
return fmt.Errorf("1&1 API key could not be found.")
}
log.Debugf("Validate : %s", string(req))
prop := properties{}
err := json.Unmarshal([]byte(req), &prop)
if err != nil {
return fmt.Errorf("Invalid instance properties: %s", err)
}
if err = p.validateAppliance(prop); err != nil {
return err
}
if err = p.validateDatacenter(prop); err != nil {
return err
}
if err = p.validateServerSize(prop); err != nil {
return err
}
if prop.SSHKey != "" {
p.prop.SSHKey = prop.SSHKey
} else if prop.SSHKeyPath != "" {
key, err := ioutil.ReadFile(prop.SSHKeyPath)
if err != nil {
log.Errorf("Cannot read SSH key from file '%s'. Error: %s", prop.SSHKeyPath, err.Error())
} else {
p.prop.SSHKey = string(key)
}
}
log.Debugln("Validated:", prop)
p.prop.Password = prop.Password
p.prop.FirewallID = prop.FirewallID
p.prop.LoadBalancerID = prop.LoadBalancerID
p.prop.MonitorPolicyID = prop.MonitorPolicyID
return nil
}
// Provision creates a new instance.
func (p *Provisioner) Provision(spec instance.Spec) (*instance.ID, error) {
rand.Seed(time.Now().UTC().UnixNano())
name := fmt.Sprintf("instance-%d", rand.Int63())
req := oneandone.ServerRequest{
Name: name,
ApplianceId: p.prop.Appliance,
DatacenterId: p.prop.Datacenter,
Password: p.prop.Password,
SSHKey: p.prop.SSHKey,
PowerOn: true,
FirewallPolicyId: p.prop.FirewallID,
LoadBalancerId: p.prop.LoadBalancerID,
MonitoringPolicyId: p.prop.MonitorPolicyID,
Hardware: oneandone.Hardware{
FixedInsSizeId: p.prop.FixedServerSize,
},
}
_, server, err := p.client.CreateServer(&req)
if err != nil {
log.Errorln("Creating server failed")
return nil, err
}
if p.wait {
p.client.WaitForState(server, "POWERED_ON", 10, 360)
server, err = p.client.GetServer(server.Id)
if err != nil {
return nil, err
}
}
ip := ""
if server.Ips != nil {
ip = server.Ips[0].Ip
}
id := instance.ID(name)
logicalID := instance.LogicalID(ip)
description := instance.Description{
Tags: spec.Tags,
ID: id,
LogicalID: &logicalID,
}
description.Tags["serverID"] = server.Id
buff, err := json.MarshalIndent(description, " ", " ")
if err != nil {
return nil, err
}
return &id, afero.WriteFile(p.fs, filepath.Join(p.Dir, string(id)+".1and1.spec"), buff, 0644)
}
// Destroy terminates an existing instance.
func (p *Provisioner) Destroy(id instance.ID) error {
servers, err := p.client.ListServers(0, 0, "", string(id), "")
if err != nil {
log.Warningf("Server '%s' could not be found, assuming it is already deleted", string(id))
} else {
for _, s := range servers {
if s.Name == string(id) {
_, err = p.client.DeleteServer(s.Id, false)
if err != nil {
log.Errorf("Cannot delete server '%s'", string(id))
return err
}
break
}
}
}
fp := filepath.Join(p.Dir, string(id)+".1and1.spec")
return p.fs.Remove(fp)
}
// DescribeInstances returns descriptions of all instances matching all of the provided tags.
func (p *Provisioner) DescribeInstances(tags map[string]string) ([]instance.Description, error) {
log.Debugln("describe-instances", tags)
result := []instance.Description{}
re := regexp.MustCompile("(^instance-[0-9]+)(.1and1.spec)")
fs := &afero.Afero{Fs: p.fs}
err := fs.Walk(p.Dir, func(path string, info os.FileInfo, err error) error {
matches := re.FindStringSubmatch(info.Name())
if len(matches) == 3 {
fName := filepath.Join(p.Dir, info.Name())
buff, err := ioutil.ReadFile(fName)
if err != nil {
log.Errorf("Cannot read file '%s'.", fName)
return err
}
description := instance.Description{}
err = json.Unmarshal(buff, &description)
if err != nil {
log.Errorln("Instance description unmarshal error")
return err
}
result = append(result, description)
}
return nil
})
return result, err
}