forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
563 lines (462 loc) · 13.7 KB
/
resource.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package aws
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/convox/rack/api/structs"
)
// ResourceCreate creates a new resource.
// Note: see also createResource() below.
func (p *AWSProvider) ResourceCreate(name, kind string, params map[string]string) (*structs.Resource, error) {
_, err := p.ResourceGet(name)
if awsError(err) != "ValidationError" {
return nil, fmt.Errorf("resource named %s already exists", name)
}
s := &structs.Resource{
Name: name,
Parameters: cfParams(params),
Type: kind,
}
s.Parameters["CustomTopic"] = customTopic
s.Parameters["NotificationTopic"] = notificationTopic
var req *cloudformation.CreateStackInput
switch s.Type {
case "memcached", "mysql", "postgres", "redis", "sqs":
req, err = p.createResource(s)
case "fluentd":
req, err = p.createResourceURL(s, "tcp")
case "s3":
if s.Parameters["Topic"] != "" {
s.Parameters["Topic"] = fmt.Sprintf("%s-%s", p.Rack, s.Parameters["Topic"])
}
req, err = p.createResource(s)
case "sns":
if s.Parameters["Queue"] != "" {
s.Parameters["Queue"] = fmt.Sprintf("%s-%s", p.Rack, s.Parameters["Queue"])
}
req, err = p.createResource(s)
case "syslog":
s.Parameters["Private"] = fmt.Sprintf("%t", p.SubnetsPrivate != "")
req, err = p.createResourceURL(s, "tcp", "tcp+tls", "udp")
case "webhook":
s.Parameters["Url"] = fmt.Sprintf("http://%s/sns?endpoint=%s", p.NotificationHost, url.QueryEscape(s.Parameters["Url"]))
req, err = p.createResourceURL(s, "http", "https")
default:
err = fmt.Errorf("Invalid resource type: %s", s.Type)
}
if err != nil {
return s, err
}
keys := []string{}
for key := range s.Parameters {
keys = append(keys, key)
}
// sort keys for easier testing
sort.Strings(keys)
// pass through resource parameters as Cloudformation Parameters
for _, key := range keys {
req.Parameters = append(req.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String(key),
ParameterValue: aws.String(s.Parameters[key]),
})
}
// tag the resource
tags := map[string]string{
"Name": s.Name,
"Rack": p.Rack,
"Resource": s.Type,
"System": "convox",
"Type": "resource",
}
tagKeys := []string{}
for key := range tags {
tagKeys = append(tagKeys, key)
}
// sort keys for easier testing
sort.Strings(tagKeys)
for _, key := range tagKeys {
req.Tags = append(req.Tags, &cloudformation.Tag{Key: aws.String(key), Value: aws.String(tags[key])})
}
_, err = p.cloudformation().CreateStack(req)
p.EventSend(&structs.Event{
Action: "resource:create",
Data: map[string]string{
"name": s.Name,
"type": s.Type,
},
}, err)
return s, err
}
// ResourceDelete deletes a resource.
func (p *AWSProvider) ResourceDelete(name string) (*structs.Resource, error) {
s, err := p.ResourceGet(name)
if err != nil {
return nil, err
}
apps, err := p.resourceApps(*s)
if err != nil {
return nil, err
}
if len(apps) > 0 {
return nil, fmt.Errorf("resource is linked to %s", apps[0].Name)
}
_, err = p.cloudformation().DeleteStack(&cloudformation.DeleteStackInput{
StackName: aws.String(s.Stack),
})
p.EventSend(&structs.Event{
Action: "resource:delete",
Data: map[string]string{
"name": s.Name,
"type": s.Type,
},
}, err)
return s, err
}
// ResourceGet retrieves a resource.
func (p *AWSProvider) ResourceGet(name string) (*structs.Resource, error) {
stacks, err := p.describeStacks(&cloudformation.DescribeStacksInput{
StackName: aws.String(p.Rack + "-" + name),
})
if err != nil {
return nil, err
}
if len(stacks) != 1 {
return nil, fmt.Errorf("could not load stack for resource: %s", name)
}
s := resourceFromStack(stacks[0])
if s.Tags["Rack"] != "" && s.Tags["Rack"] != p.Rack {
return nil, fmt.Errorf("no such stack on this rack: %s", name)
}
if s.Status == "failed" {
eres, err := p.describeStackEvents(&cloudformation.DescribeStackEventsInput{
StackName: aws.String(*stacks[0].StackName),
})
if err != nil {
return &s, err
}
for _, event := range eres.StackEvents {
if *event.ResourceStatus == cloudformation.ResourceStatusCreateFailed {
s.StatusReason = *event.ResourceStatusReason
break
}
}
}
if len(s.Tags["Service"]) > 0 && s.Tags["Resource"] != s.Tags["Service"] {
s.Tags["Resource"] = s.Tags["Service"]
}
if s.Tags["Type"] == "service" {
s.Tags["Type"] = "resource"
}
switch s.Tags["Resource"] {
case "memcached":
s.Exports["URL"] = fmt.Sprintf("%s:%s", s.Outputs["Port11211TcpAddr"], s.Outputs["Port11211TcpPort"])
case "mysql":
s.Exports["URL"] = fmt.Sprintf("mysql://%s:%s@%s:%s/%s", s.Outputs["EnvMysqlUsername"], s.Outputs["EnvMysqlPassword"], s.Outputs["Port3306TcpAddr"], s.Outputs["Port3306TcpPort"], s.Outputs["EnvMysqlDatabase"])
case "papertrail":
s.Exports["URL"] = s.Parameters["Url"]
case "postgres":
s.Exports["URL"] = fmt.Sprintf("postgres://%s:%s@%s:%s/%s", s.Outputs["EnvPostgresUsername"], s.Outputs["EnvPostgresPassword"], s.Outputs["Port5432TcpAddr"], s.Outputs["Port5432TcpPort"], s.Outputs["EnvPostgresDatabase"])
case "redis":
s.Exports["URL"] = fmt.Sprintf("redis://%s:%s/%s", s.Outputs["Port6379TcpAddr"], s.Outputs["Port6379TcpPort"], s.Outputs["EnvRedisDatabase"])
case "s3":
s.Exports["URL"] = fmt.Sprintf("s3://%s:%s@%s", s.Outputs["AccessKey"], s.Outputs["SecretAccessKey"], s.Outputs["Bucket"])
case "sns":
s.Exports["URL"] = fmt.Sprintf("sns://%s:%s@%s", s.Outputs["AccessKey"], s.Outputs["SecretAccessKey"], s.Outputs["Topic"])
case "sqs":
if u, err := url.Parse(s.Outputs["Queue"]); err == nil {
u.Scheme = "sqs"
u.User = url.UserPassword(s.Outputs["AccessKey"], s.Outputs["SecretAccessKey"])
s.Exports["URL"] = u.String()
}
case "webhook":
if parsedURL, err := url.Parse(s.Parameters["Url"]); err == nil {
s.Exports["URL"] = parsedURL.Query().Get("endpoint")
}
}
// Populate linked apps
apps, err := p.resourceApps(s)
if err != nil {
return nil, err
}
s.Apps = apps
return &s, nil
}
//resourceApps returns the apps that have been linked with a resource (ignoring apps that have been delete out of band)
func (p *AWSProvider) resourceApps(s structs.Resource) (structs.Apps, error) {
apps := structs.Apps(make([]structs.App, 0))
for key, value := range s.Outputs {
if strings.HasSuffix(key, "Link") {
// Extract app name from log group
index := strings.Index(value, "-LogGroup")
// avoid runtime panic
if index == -1 {
continue
}
r := strings.NewReplacer(fmt.Sprintf("%s-", p.Rack), "", value[index:], "")
app := r.Replace(value)
a, err := p.AppGet(app)
if err != nil {
if err.Error() == fmt.Sprintf("%s not found", app) {
continue
}
return nil, err
}
apps = append(apps, *a)
}
}
return apps, nil
}
// ResourceList lists the resources.
func (p *AWSProvider) ResourceList() (structs.Resources, error) {
stacks, err := p.describeStacks(&cloudformation.DescribeStacksInput{})
if err != nil {
return nil, err
}
resources := structs.Resources{}
for _, stack := range stacks {
tags := stackTags(stack)
// if it's a resource and the Rack tag is either the current rack or blank
if tags["System"] == "convox" && (tags["Type"] == "resource" || tags["Type"] == "service") {
if tags["Rack"] == p.Rack || tags["Rack"] == "" {
resources = append(resources, resourceFromStack(stack))
}
}
}
for _, s := range resources {
apps, err := p.resourceApps(s)
if err != nil {
return nil, err
}
s.Apps = apps
}
return resources, nil
}
// ResourceLink creates a link between the provided app and resource.
func (p *AWSProvider) ResourceLink(name, app, process string) (*structs.Resource, error) {
a, err := p.AppGet(app)
if err != nil {
return nil, err
}
s, err := p.ResourceGet(name)
if err != nil {
return nil, err
}
// already linked
for _, linkedApp := range s.Apps {
if a.Name == linkedApp.Name {
return nil, fmt.Errorf("resource %s is already linked to app %s", s.Name, a.Name)
}
}
// Update Resource and/or App stacks
switch s.Type {
case "fluentd", "syslog":
err = p.linkResource(a, s) // Update resource to know about App
default:
err = fmt.Errorf("resource type %s does not have a link strategy", s.Type)
}
return s, err
}
// ResourceUnlink removes a link between the provided app and resource.
func (p *AWSProvider) ResourceUnlink(name, app, process string) (*structs.Resource, error) {
a, err := p.AppGet(app)
if err != nil {
return nil, err
}
s, err := p.ResourceGet(name)
if err != nil {
return nil, err
}
// already linked
linked := false
for _, linkedApp := range s.Apps {
if a.Name == linkedApp.Name {
linked = true
break
}
}
if !linked {
return nil, fmt.Errorf("resource %s is not linked to app %s", s.Name, a.Name)
}
// Update Resource and/or App stacks
switch s.Type {
case "fluentd", "syslog":
err = p.unlinkResource(a, s) // Update resource to forget about App
default:
err = fmt.Errorf("resource type %s does not have an unlink strategy", s.Type)
}
return s, err
}
// ResourceUpdate updates a resource with new params.
func (p *AWSProvider) ResourceUpdate(name string, params map[string]string) (*structs.Resource, error) {
s, err := p.ResourceGet(name)
if err != nil {
return nil, err
}
for key, value := range cfParams(params) {
s.Parameters[key] = value
}
err = p.updateResource(s)
return s, err
}
// createResource creates a Resource.
// Note: see also ResourceCreate() above.
// This should probably be renamed to createResourceStack to be in conformity with createResourceURL below.
func (p *AWSProvider) createResource(s *structs.Resource) (*cloudformation.CreateStackInput, error) {
formation, err := resourceFormation(s.Type, nil)
if err != nil {
return nil, err
}
if err := p.appendSystemParameters(s); err != nil {
return nil, err
}
if err := filterFormationParameters(s, formation); err != nil {
return nil, err
}
req := &cloudformation.CreateStackInput{
Capabilities: []*string{aws.String("CAPABILITY_IAM")},
StackName: aws.String(fmt.Sprintf("%s-%s", p.Rack, s.Name)),
TemplateBody: aws.String(formation),
}
return req, nil
}
func (p *AWSProvider) createResourceURL(s *structs.Resource, allowedProtocols ...string) (*cloudformation.CreateStackInput, error) {
if s.Parameters["Url"] == "" {
return nil, fmt.Errorf("must specify a URL")
}
u, err := url.Parse(s.Parameters["Url"])
if err != nil {
return nil, err
}
valid := false
for _, p := range allowedProtocols {
if u.Scheme == p {
valid = true
break
}
}
if !valid {
return nil, fmt.Errorf("invalid URL scheme: %s. Allowed schemes are: %s", u.Scheme, strings.Join(allowedProtocols, ", "))
}
return p.createResource(s)
}
func (p *AWSProvider) updateResource(s *structs.Resource) error {
formation, err := resourceFormation(s.Type, s)
if err != nil {
return err
}
if err := p.appendSystemParameters(s); err != nil {
return err
}
if err := filterFormationParameters(s, formation); err != nil {
return err
}
req := &cloudformation.UpdateStackInput{
Capabilities: []*string{aws.String("CAPABILITY_IAM")},
StackName: aws.String(s.Stack),
TemplateBody: aws.String(formation),
}
for key, value := range s.Parameters {
req.Parameters = append(req.Parameters, &cloudformation.Parameter{
ParameterKey: aws.String(key),
ParameterValue: aws.String(value),
})
}
_, err = p.cloudformation().UpdateStack(req)
return err
}
// add to links
func (p *AWSProvider) linkResource(a *structs.App, s *structs.Resource) error {
s.Apps = append(s.Apps, *a)
return p.updateResource(s)
}
// delete from links
func (p *AWSProvider) unlinkResource(a *structs.App, s *structs.Resource) error {
apps := structs.Apps{}
for _, linkedApp := range s.Apps {
if a.Name != linkedApp.Name {
apps = append(apps, linkedApp)
}
}
s.Apps = apps
return p.updateResource(s)
}
func (p *AWSProvider) appendSystemParameters(s *structs.Resource) error {
password, err := generatePassword()
if err != nil {
return err
}
s.Parameters["Password"] = password
s.Parameters["SecurityGroups"] = p.SecurityGroup
s.Parameters["Subnets"] = p.Subnets
s.Parameters["SubnetsPrivate"] = coalesceString(p.SubnetsPrivate, p.Subnets)
s.Parameters["Vpc"] = p.Vpc
s.Parameters["VpcCidr"] = p.VpcCidr
return nil
}
func coalesceString(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}
func filterFormationParameters(s *structs.Resource, formation string) error {
var params struct {
Parameters map[string]interface{}
}
if err := json.Unmarshal([]byte(formation), ¶ms); err != nil {
return err
}
for key := range s.Parameters {
if _, ok := params.Parameters[key]; !ok {
delete(s.Parameters, key)
}
}
return nil
}
func generatePassword() (string, error) {
data := make([]byte, 1024)
if _, err := rand.Read(data); err != nil {
return "", err
}
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])[0:30], nil
}
func resourceFormation(kind string, data interface{}) (string, error) {
d, err := buildTemplate(fmt.Sprintf("resource/%s", kind), "resource", data)
if err != nil {
return "", err
}
return d, nil
}
func resourceFromStack(stack *cloudformation.Stack) structs.Resource {
params := stackParameters(stack)
tags := stackTags(stack)
name := coalesceString(tags["Name"], *stack.StackName)
exports := map[string]string{}
if url, ok := params["Url"]; ok {
exports["URL"] = url
}
rtype := tags["Resource"]
if rtype == "" {
rtype = tags["Service"]
}
return structs.Resource{
Name: name,
Stack: *stack.StackName,
Type: rtype,
Status: humanStatus(*stack.StackStatus),
Outputs: stackOutputs(stack),
Parameters: params,
Tags: tags,
Exports: exports,
}
}