forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iaas.go
350 lines (328 loc) · 9.11 KB
/
iaas.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ec2
import (
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/tsuru/monsterqueue"
"github.com/tsuru/tsuru/iaas"
"github.com/tsuru/tsuru/log"
"github.com/tsuru/tsuru/queue"
)
const defaultRegion = "us-east-1"
func init() {
iaas.RegisterIaasProvider("ec2", newEC2IaaS)
}
type EC2IaaS struct {
base iaas.UserDataIaaS
}
func newEC2IaaS(name string) iaas.IaaS {
return &EC2IaaS{base: iaas.UserDataIaaS{NamedIaaS: iaas.NamedIaaS{BaseIaaSName: "ec2", IaaSName: name}}}
}
func (i *EC2IaaS) createEC2Handler(regionOrEndpoint string) (*ec2.EC2, error) {
keyId, err := i.base.GetConfigString("key-id")
if err != nil {
return nil, err
}
secretKey, err := i.base.GetConfigString("secret-key")
if err != nil {
return nil, err
}
var region, endpoint string
if strings.HasPrefix(regionOrEndpoint, "http") {
endpoint = regionOrEndpoint
region = defaultRegion
} else {
region = regionOrEndpoint
}
config := aws.Config{
Credentials: credentials.NewStaticCredentials(keyId, secretKey, ""),
Region: aws.String(region),
Endpoint: aws.String(endpoint),
}
return ec2.New(session.New(&config)), nil
}
func (i *EC2IaaS) waitForDnsName(ec2Inst *ec2.EC2, instance *ec2.Instance) (*ec2.Instance, error) {
rawWait, _ := i.base.GetConfigString("wait-timeout")
maxWaitTime, _ := strconv.Atoi(rawWait)
if maxWaitTime == 0 {
maxWaitTime = 300
}
q, err := queue.Queue()
if err != nil {
return nil, err
}
taskName := fmt.Sprintf("ec2-wait-machine-%s", i.base.IaaSName)
waitDuration := time.Duration(maxWaitTime) * time.Second
job, err := q.EnqueueWait(taskName, monsterqueue.JobParams{
"region": ec2Inst.Config.Region,
"endpoint": ec2Inst.Config.Endpoint,
"machineId": *instance.InstanceId,
"timeout": maxWaitTime,
}, waitDuration)
if err != nil {
if err == monsterqueue.ErrQueueWaitTimeout {
return nil, fmt.Errorf("ec2: time out after %v waiting for instance %s to start", waitDuration, *instance.InstanceId)
}
return nil, err
}
result, err := job.Result()
if err != nil {
return nil, err
}
instance.PublicDnsName = aws.String(result.(string))
return instance, nil
}
func (i *EC2IaaS) Initialize() error {
q, err := queue.Queue()
if err != nil {
return err
}
return q.RegisterTask(&ec2WaitTask{iaas: i})
}
func (i *EC2IaaS) Describe() string {
return `EC2 IaaS required params:
image=<image id> Image AMI ID
type=<instance type> Your template uuid
Optional params:
region=<region> Chosen region, defaults to us-east-1
securityGroup=<group> Chosen security group
keyName=<key name> Key name for machine
`
}
func (i *EC2IaaS) DeleteMachine(m *iaas.Machine) error {
regionOrEndpoint := getRegionOrEndpoint(m.CreationParams, false)
if regionOrEndpoint == "" {
return fmt.Errorf("region or endpoint creation param required")
}
ec2Inst, err := i.createEC2Handler(regionOrEndpoint)
if err != nil {
return err
}
input := ec2.TerminateInstancesInput{InstanceIds: []*string{&m.Id}}
_, err = ec2Inst.TerminateInstances(&input)
return err
}
type invalidFieldError struct {
fieldName string
convertError error
}
func (err *invalidFieldError) Error() string {
return fmt.Sprintf("invalid value for the field %q: %s", err.fieldName, err.convertError)
}
func (i *EC2IaaS) buildRunInstancesOptions(params map[string]string) (ec2.RunInstancesInput, error) {
result := ec2.RunInstancesInput{
MaxCount: aws.Int64(1),
MinCount: aws.Int64(1),
}
forbiddenFields := []string{
"maxcount", "mincount", "dryrun", "monitoring",
}
aliases := map[string]string{
"image": "imageid",
"type": "instancetype",
"securitygroup": "securitygroups",
"ebs-optimized": "ebsoptimized",
}
refType := reflect.TypeOf(result)
refValue := reflect.ValueOf(&result)
for key, value := range params {
field, ok := refType.FieldByNameFunc(func(name string) bool {
lowerName := strings.ToLower(name)
for _, field := range forbiddenFields {
if lowerName == field {
return false
}
}
lowerKey := strings.ToLower(key)
if aliased, ok := aliases[lowerKey]; ok {
lowerKey = aliased
}
return lowerName == lowerKey
})
if !ok {
continue
}
fieldType := field.Type
fieldValue := refValue.Elem().FieldByIndex(field.Index)
if !fieldValue.IsValid() || !fieldValue.CanSet() {
continue
}
switch fieldType.Kind() {
case reflect.Ptr:
switch fieldType.Elem().Kind() {
case reflect.String:
copy := value
fieldValue.Set(reflect.ValueOf(©))
case reflect.Int64:
intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return result, &invalidFieldError{
fieldName: key,
convertError: err,
}
}
fieldValue.Set(reflect.ValueOf(&intValue))
case reflect.Bool:
boolValue, err := strconv.ParseBool(value)
if err != nil {
return result, &invalidFieldError{
fieldName: key,
convertError: err,
}
}
fieldValue.Set(reflect.ValueOf(&boolValue))
case reflect.Struct:
err := i.loadStruct(fieldValue, fieldType, []byte(value))
if err != nil {
return result, &invalidFieldError{
fieldName: key,
convertError: err,
}
}
}
case reflect.Slice:
switch fieldType.Elem().Elem().Kind() {
case reflect.String:
parts := strings.Split(value, ",")
values := make([]*string, len(parts))
for i, part := range parts {
values[i] = aws.String(part)
}
fieldValue.Set(reflect.ValueOf(values))
case reflect.Struct:
var raw []map[string]interface{}
err := json.Unmarshal([]byte(value), &raw)
if err != nil {
return result, &invalidFieldError{
fieldName: key,
convertError: err,
}
}
val, err := i.translateSlice(raw, fieldType)
if err != nil {
return result, &invalidFieldError{
fieldName: key,
convertError: err,
}
}
fieldValue.Set(val)
}
}
}
// Manual configuration
if monitoring, ok := params["monitoring-enabled"]; ok {
value, _ := strconv.ParseBool(monitoring)
result.Monitoring = &ec2.RunInstancesMonitoringEnabled{
Enabled: aws.Bool(value),
}
}
return result, nil
}
func (i *EC2IaaS) translateSlice(in []map[string]interface{}, t reflect.Type) (reflect.Value, error) {
result := reflect.MakeSlice(t, len(in), len(in))
for idx, value := range in {
data, _ := json.Marshal(value)
err := i.loadStruct(result.Index(idx), t.Elem(), data)
if err != nil {
return result, err
}
}
return result, nil
}
func (i *EC2IaaS) loadStruct(value reflect.Value, t reflect.Type, data []byte) error {
raw := value.Interface()
if value.IsNil() {
raw = reflect.New(t.Elem()).Interface()
}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
value.Set(reflect.ValueOf(raw))
return nil
}
func (i *EC2IaaS) CreateMachine(params map[string]string) (*iaas.Machine, error) {
regionOrEndpoint := getRegionOrEndpoint(params, true)
userData, err := i.base.ReadUserData()
if err != nil {
return nil, err
}
options, err := i.buildRunInstancesOptions(params)
if err != nil {
return nil, err
}
options.UserData = aws.String(base64.StdEncoding.EncodeToString([]byte(userData)))
if options.ImageId == nil || *options.ImageId == "" {
return nil, fmt.Errorf("the parameter %q is required", "imageid")
}
if options.InstanceType == nil || *options.InstanceType == "" {
return nil, fmt.Errorf("the parameter %q is required", "instancetype")
}
ec2Inst, err := i.createEC2Handler(regionOrEndpoint)
if err != nil {
return nil, err
}
resp, err := ec2Inst.RunInstances(&options)
if err != nil {
return nil, err
}
if len(resp.Instances) == 0 {
return nil, fmt.Errorf("no instance created")
}
runInst := resp.Instances[0]
if tags, ok := params["tags"]; ok {
var ec2Tags []*ec2.Tag
tagList := strings.Split(tags, ",")
ec2Tags = make([]*ec2.Tag, 0, len(tagList))
for _, tag := range tagList {
if strings.Contains(tag, ":") {
parts := strings.SplitN(tag, ":", 2)
ec2Tags = append(ec2Tags, &ec2.Tag{
Key: aws.String(parts[0]),
Value: aws.String(parts[1]),
})
}
}
if len(ec2Tags) > 0 {
input := ec2.CreateTagsInput{
Resources: []*string{runInst.InstanceId},
Tags: ec2Tags,
}
_, err = ec2Inst.CreateTags(&input)
if err != nil {
log.Errorf("failed to tag EC2 instance: %s", err)
}
}
}
instance, err := i.waitForDnsName(ec2Inst, runInst)
if err != nil {
return nil, err
}
machine := iaas.Machine{
Id: *instance.InstanceId,
Status: *instance.State.Name,
Address: *instance.PublicDnsName,
}
return &machine, nil
}
func getRegionOrEndpoint(params map[string]string, useDefault bool) string {
regionOrEndpoint := params["endpoint"]
if regionOrEndpoint == "" {
regionOrEndpoint = params["region"]
if regionOrEndpoint == "" && useDefault {
regionOrEndpoint = defaultRegion
}
}
return regionOrEndpoint
}