-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeployer.go
770 lines (680 loc) · 19 KB
/
deployer.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/cloudfoundry/cli/plugin"
)
func isMissing(e error) bool {
return strings.Contains(e.Error(), "not found")
}
func parseService(s string) (string, string) {
x := strings.SplitN(s, "/", 2)
return x[0], x[1]
}
func boolify(s string) bool {
s = strings.ToLower(s)
return s == "yes" || s == "y" || s == "on" || s == "enabled"
}
type Deployer struct {
manifest *Manifest
cf plugin.CliConnection
}
func (d *Deployer) run(args ...string) error {
if os.Getenv("DEBUG") != "" {
fmt.Printf(">> %s\n", strings.Join(args, " "))
}
if os.Getenv("DRYRUN") != "" {
return nil
}
_, err := d.cf.CliCommandWithoutTerminalOutput(args...)
return err
}
func (d *Deployer) runWithOutput(args ...string) ([]string, error) {
if os.Getenv("DEBUG") != "" {
fmt.Printf(">> %s\n", strings.Join(args, " "))
}
if os.Getenv("DRYRUN") != "" {
return nil, nil
}
result, err := d.cf.CliCommandWithoutTerminalOutput(args...)
if os.Getenv("DEBUG") != "" {
for _, l := range result {
fmt.Printf("%s\n", l)
}
}
return result, err
}
func (d *Deployer) createUser(user string) error {
for _, u := range d.manifest.Users {
if u.Name == user {
/* if we have a username and password, let's set them!
(note that this fails miserably if the user exists but has a different
password. oh well.) */
d.run("create-user", u.Name, u.Password)
return nil
}
}
/* since we can't query cf to see if the user exists (yet), let's
just assume that they do exist, and not create them if there is
no entry in the top-level `users` map from the manifest, mmmkay? */
return nil
}
func (d *Deployer) createSharedDomain(domain string) error {
/* there is currently no good way to determine if we failed to create
the shared domain because it already existed, or if there was some
other failure (auth / perms / bad input / etc.)
so for now, we just ignore *all* the errors and pretend everything
is going to be just fine thank you very much. */
d.run("create-shared-domain", domain)
return nil
}
func (d *Deployer) createOrg(org string) error {
o, _ := d.cf.GetOrg(org)
if o.Guid != "" {
return nil
}
if err := d.run("create-org", org); err != nil {
return err
}
return nil
}
func (d *Deployer) createOrgDomain(org, domain string) error {
o, err := d.cf.GetOrg(org)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
for _, d := range o.Domains {
if d.Name == domain {
return nil
}
}
if err := d.run("share-private-domain", org, domain); err != nil {
if err := d.run("create-domain", org, domain); err != nil {
return err
}
}
return nil
}
func (d *Deployer) grantOrgRole(org, user, role string) error {
_, err := d.cf.GetOrg(org)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
users, err := d.cf.GetOrgUsers(org)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
for _, u := range users {
if u.Username == user {
for _, r := range u.Roles {
if r == role {
return nil
}
}
}
}
return d.run("set-org-role", user, org, role)
}
func (d *Deployer) createSpace(org, space string) error {
o, err := d.cf.GetOrg(org)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
for _, s := range o.Spaces {
if s.Name == space {
return nil
}
}
if err := d.run("target", "-o", org); err != nil {
return err
}
if err := d.run("create-space", space); err != nil {
return err
}
return nil
}
func (d *Deployer) enableSSH(space string, on bool) error {
if on {
return d.run("allow-space-ssh", space)
}
return d.run("disallow-space-ssh", space)
}
func (d *Deployer) grantSpaceRole(org, space, user, role string) error {
_, err := d.cf.GetOrg(org)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
if err := d.run("target", "-o", org); err != nil {
return err
}
_, err = d.cf.GetSpace(space)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
users, err := d.cf.GetSpaceUsers(org, space)
if err != nil && os.Getenv("DRYRUN") == "" {
return err
}
for _, u := range users {
if u.Username == user {
for _, r := range u.Roles {
if r == role {
return nil
}
}
}
}
return d.run("set-space-role", user, org, space, role)
}
func (d *Deployer) stageApp(app *Application) error {
args := []string{"push", app.Name, "--no-start", "-i", fmt.Sprintf("%v", app.Instances)}
if app.Hostname != "" {
args = append(args, "-n", app.Hostname)
}
if app.Domain != "" {
args = append(args, "-d", app.Domain)
}
if app.Disk != "" {
args = append(args, "-k", app.Disk)
}
if app.Memory != "" {
args = append(args, "-m", app.Memory)
}
if app.Buildpack != "" {
args = append(args, "-b", app.Buildpack)
}
if app.Image != "" {
args = append(args, "-o", app.Image)
} else if app.Repository != "" {
wd, _ := os.Getwd()
path := wd + "/apps/" + app.Name
os.MkdirAll(path, 0777)
files, _ := ioutil.ReadDir(path)
if len(files) == 0 {
gitPath, err := exec.LookPath("git")
if err != nil {
return err
}
if err := exec.Command(gitPath, "clone", app.Repository, path).Run(); err != nil {
return err
}
}
if app.Path != "" {
path = fmt.Sprintf("%s/%s", path, app.Path)
}
args = append(args, "-p", path)
} else if app.Path != "" {
args = append(args, "-p", app.Path)
} else {
return fmt.Errorf("No image, repository or path supplied for '%s' app", app.Name)
}
return d.run(args...)
}
func (d *Deployer) mapURLs(app *Application) error {
a, err := d.cf.GetApp(app.Name)
if err != nil {
return err
}
want := map[string]URL{}
for _, s := range app.URLs {
url := ParseURL(s, app.Domain)
want[url.String()] = url
}
have := map[string]URL{}
for _, r := range a.Routes {
url := URL{
Host: r.Host,
Domain: r.Domain.Name,
}
if _, ok := want[url.String()]; ok {
delete(want, url.String())
} else {
have[url.String()] = url
}
}
for u, url := range have {
fmt.Printf(" unmapping route %s\n", u)
if err := d.run("unmap-route", app.Name, url.Domain, "--hostname", url.Host); err != nil {
return err
}
}
for u, url := range want {
fmt.Printf(" mapping route %s\n", u)
if err := d.run("map-route", app.Name, url.Domain, "--hostname", url.Host); err != nil {
return err
}
}
return nil
}
func (d *Deployer) setEnvVar(name, value, app string) error {
return d.run("set-env", app, name, value)
}
func (d *Deployer) startApp(app *Application) error {
return d.run("start", app.Name)
}
func (d *Deployer) createService(name, broker, plan string) error {
s, err := d.cf.GetServices()
if err != nil {
return err
}
for _, svc := range s {
if svc.Name == name {
/* FIXME: check configuration */
return nil
}
}
return d.run("create-service", broker, plan, name)
}
func (d *Deployer) bindService(service, app string) error {
return d.run("bind-service", app, service)
}
func (d *Deployer) userProvidedService(name, cred, route, syslog string) error {
args := []string{"create-user-provided-service", name}
if cred != "" {
args = append(args, "-p", cred)
}
if route != "" {
args = append(args, "-r", route)
}
if syslog != "" {
args = append(args, "-l", syslog)
}
if err := d.run(args...); err != nil {
args[0] = "update-user-provided-service"
return d.run(args...)
}
return nil
}
func (d *Deployer) setQuotaArgs(quota *Quota) []string {
var args []string
if quota.Memory["total"] != "" {
args = append(args, "-m", quota.Memory["total"])
}
if quota.Memory["per-app-instance"] != "" {
perAppInstance := quota.Memory["per-app-instance"]
if perAppInstance == "unlimited" {
perAppInstance = "-1"
}
args = append(args, "-i", perAppInstance)
}
if quota.TotalAppInstances != "" {
appInstances := quota.TotalAppInstances
if appInstances == "unlimited" {
appInstances = "-1"
}
args = append(args, "-a", quota.TotalAppInstances)
}
if quota.ServiceInstances != "" {
args = append(args, "-s", quota.ServiceInstances)
}
if quota.Routes != "" {
args = append(args, "-r", quota.Routes)
}
if quota.PaidPlans {
args = append(args, "--allow-paid-service-plans")
}
if quota.NumRoutesWithResPorts != "" {
args = append(args, "--reserved-route-ports", quota.NumRoutesWithResPorts)
}
return args
}
func (d *Deployer) createUpdateSpaceQuota(qname string, quota *Quota, oname string) error {
org, _ := d.cf.GetOrg(oname)
if org.Guid == "" {
return nil
}
if err := d.run("target", "-o", oname); err != nil {
return err
}
args := []string{"create-space-quota", qname}
args = append(args, d.setQuotaArgs(quota)...)
for _, cname := range org.SpaceQuotas {
if cname.Name == qname {
args[0] = "update-space-quota"
}
}
return d.run(args...)
}
func (d *Deployer) createOrgQuota(qname string) error {
return d.run("create-quota", qname)
}
func (d *Deployer) updateOrgQuota(qname string, quota *Quota) error {
args := []string{"update-quota", qname}
args = append(args, d.setQuotaArgs(quota)...)
return d.run(args...)
}
func (d *Deployer) setQuota(name, quota string, space bool) error {
cmd := "set-quota"
if space {
cmd = "set-space-quota"
}
return d.run(cmd, name, quota)
}
func (d *Deployer) getSecurityGroupFile(sgname string, sgrule *SecurityGroup) (sgFileName string, cleanup bool, err error) {
sgFileName = ""
cleanup = false
if sgrule.SecurityGroupFile == "" {
rules := dynamicYamlHelper(sgrule.Rules)
var rulesJson []byte
rulesJson, err = json.Marshal(rules)
if err != nil {
return
}
var prettyJson bytes.Buffer
json.Indent(&prettyJson, rulesJson, "", " ")
var fp *os.File
fp, err = ioutil.TempFile("", sgname)
sgFileName = fp.Name()
cleanup = true
if err != nil {
return
}
_, err = fp.Write(prettyJson.Bytes())
if err != nil {
return
}
_, err = fp.WriteString("\n")
if err != nil {
return
}
fp.Close()
if os.Getenv("DEBUG") != "" {
fmt.Printf("security group rule %s\n%s\n", sgname, prettyJson.String())
}
} else {
sgFileName = sgrule.SecurityGroupFile
}
if os.Getenv("DEBUG") != "" {
fmt.Printf("security group %s file %s\n", sgname, sgFileName)
}
return
}
// TBD Do we need to more specific on testing existence by inspecting err?
func (d *Deployer) testSecurityGroup(sgname string) error {
return d.run("security-group", sgname)
}
func (d *Deployer) createSecurityGroup(sgname, file string) error {
return d.run("create-security-group", sgname, file)
}
func (d *Deployer) updateSecurityGroup(sgname, file string) error {
return d.run("update-security-group", sgname, file)
}
func (d *Deployer) bindRunningSecurityGroup(sgname string) error {
return d.run("bind-running-security-group", sgname)
}
func (d *Deployer) bindStagingSecurityGroup(sgname string) error {
return d.run("bind-staging-security-group", sgname)
}
func (d *Deployer) bindSecurityGroup(sgname, org, space, lifecycle string) error {
args := []string{"bind-security-group", sgname, org}
if space != "" {
args = append(args, space)
}
if lifecycle != "" {
args = append(args, "--lifecycle", lifecycle)
}
return d.run(args...)
}
func (d *Deployer) Deploy() error {
for _, domain := range d.manifest.Domains {
fmt.Printf("setting up shared (global) domain '%s'\n", domain)
if err := d.createSharedDomain(domain); err != nil {
return err
}
}
for qname, quota := range d.manifest.Quotas {
fmt.Printf("creating/updating org quota '%s'\n", qname)
// NOTE: create and update are separated because there is currently no way
// to pull existing top-level quota information out. This method
// avoids errors/failures.
if err := d.createOrgQuota(qname); err != nil {
return err
}
if err := d.updateOrgQuota(qname, quota); err != nil {
return err
}
}
for sgname, sgrule := range d.manifest.SecurityGroups {
file, cleanup, err := d.getSecurityGroupFile(sgname, sgrule)
if err != nil {
return err
}
if err := d.testSecurityGroup(sgname); err != nil {
fmt.Printf("creating security group '%s'\n", sgname)
if err := d.createSecurityGroup(sgname, file); err != nil {
return err
}
} else {
fmt.Printf("updating security group '%s'\n", sgname)
if err := d.updateSecurityGroup(sgname, file); err != nil {
return err
}
}
if cleanup {
os.Remove(file)
}
}
if d.manifest.SecurityGroupSets != nil {
for _, sgname := range d.manifest.SecurityGroupSets.Running {
fmt.Printf("bind running security group %s\n", sgname)
if err := d.bindRunningSecurityGroup(sgname); err != nil {
return err
}
}
for _, sgname := range d.manifest.SecurityGroupSets.Staging {
fmt.Printf("bind staging security group %s\n", sgname)
if err := d.bindStagingSecurityGroup(sgname); err != nil {
return err
}
}
}
for oname, org := range d.manifest.Organizations {
fmt.Printf("creating organization '%s'\n", oname)
if err := d.createOrg(oname); err != nil {
return err
}
for _, domain := range org.Domains {
fmt.Printf(" setting up organization domain '%s'\n", domain)
if err := d.createOrgDomain(oname, domain); err != nil {
return err
}
}
if org.Quota != "" {
fmt.Printf(" applying organization quota '%s'\n", org.Quota)
if err := d.setQuota(oname, org.Quota, false); err != nil {
return err
}
}
for sqname, squota := range org.Quotas {
fmt.Printf(" creating/updating space quota '%s'\n", sqname)
if err := d.createUpdateSpaceQuota(sqname, squota, oname); err != nil {
return err
}
}
if org.SecurityGroupSets != nil {
lifecycle := "staging"
for _, sgname := range org.SecurityGroupSets.Staging {
fmt.Printf("bind organization staging security group %s\n", sgname)
if err := d.bindSecurityGroup(sgname, oname, "", lifecycle); err != nil {
return err
}
}
lifecycle = ""
for _, sgname := range org.SecurityGroupSets.Running {
fmt.Printf("bind organization running security group %s\n", sgname)
if err := d.bindSecurityGroup(sgname, oname, "", lifecycle); err != nil {
return err
}
}
}
for uname, roles := range org.Users {
fmt.Printf(" granting org-level access to user '%s'\n", uname)
if err := d.createUser(uname); err != nil {
return err
}
for _, role := range roles {
fmt.Printf(" granting role '%s' to %s\n", role, uname)
if err := d.grantOrgRole(oname, uname, role); err != nil {
return err
}
}
}
for sname, space := range org.Spaces {
fmt.Printf(" creating space '%s'\n", sname)
if err := d.createSpace(oname, sname); err != nil {
return err
}
if err := d.run("target", "-o", oname, "-s", sname); err != nil {
return err
}
if space.SSH != "" {
fmt.Printf(" setting ssh-enabled to '%s'\n", space.SSH)
if err := d.enableSSH(sname, boolify(space.SSH)); err != nil {
return err
}
}
if space.Domain != "" {
fmt.Printf(" using default domain of '%s'\n", space.Domain)
}
if space.Quota != "" {
fmt.Printf(" applying space quota '%s'\n", space.Quota)
if err := d.setQuota(sname, space.Quota, true); err != nil {
return err
}
}
if space.SecurityGroupSets != nil {
lifecycle := "staging"
for _, sgname := range space.SecurityGroupSets.Staging {
fmt.Printf("bind space staging security group %s\n", sgname)
if err := d.bindSecurityGroup(sgname, oname, sname, lifecycle); err != nil {
return err
}
}
lifecycle = ""
for _, sgname := range space.SecurityGroupSets.Running {
fmt.Printf("bind space running security group %s\n", sgname)
if err := d.bindSecurityGroup(sgname, oname, sname, lifecycle); err != nil {
return err
}
}
}
for uname, roles := range space.Users {
fmt.Printf(" granting space-level access to user '%s'\n", uname)
if err := d.createUser(uname); err != nil {
return err
}
for _, role := range roles {
fmt.Printf(" granting role '%s' to %s\n", role, uname)
if err := d.grantSpaceRole(oname, sname, uname, role); err != nil {
return err
}
}
}
for svname, service := range space.SharedServices {
fmt.Printf(" setting up shared service instance '%s' (from %s)\n", svname, service)
broker, plan := parseService(service)
if err := d.createService(svname, broker, plan); err != nil {
return err
}
}
for _, cups := range space.UserProvidedServices {
if cups.Name != "" {
fmt.Printf(" creating/updating a user provided service %s\n", cups.Name)
var cred string
if cups.Credentials != nil {
obj := dynamicYamlHelper(cups.Credentials)
c, err := json.Marshal(obj)
if err != nil {
return err
}
cred = string(c)
}
if err := d.userProvidedService(cups.Name, cred, cups.RouteServiceUrl, cups.SyslogDrainUrl); err != nil {
return err
}
}
}
for _, app := range space.Applications {
fmt.Printf(" staging application '%s'\n", app.Name)
fmt.Printf(" spinning up %d instances\n", app.Instances)
if app.Hostname != "" {
fmt.Printf(" using hostname '%s'\n", app.Hostname)
}
if app.Domain != "" {
fmt.Printf(" using domain '%s'\n", app.Domain)
}
if app.Disk != "" {
fmt.Printf(" provisioning with %s disk\n", app.Disk)
}
if app.Memory != "" {
fmt.Printf(" provisioning with %s memory\n", app.Memory)
}
if app.Image != "" {
fmt.Printf(" deploying image '%s'\n", app.Image)
} else if app.Repository != "" {
fmt.Printf(" deploying remote codebase from '%s'\n", app.Repository)
} else if app.Path != "" {
fmt.Printf(" deploying local codebase from '%s'\n", app.Path)
}
if app.Buildpack != "" {
fmt.Printf(" using the '%s' buildpack\n", app.Buildpack)
}
if err := d.stageApp(app); err != nil {
return err
}
if len(app.URLs) > 0 {
if err := d.mapURLs(app); err != nil {
return err
}
}
for ename, value := range app.Environment {
fmt.Printf(" setting environment variable $%s\n", ename)
if err := d.setEnvVar(ename, value, app.Name); err != nil {
return err
}
}
for svname, service := range app.BoundServices {
fmt.Printf(" binding service instance '%s' (from %s)\n", svname, service)
broker, plan := parseService(service)
if err := d.createService(svname, broker, plan); err != nil {
return err
}
if err := d.bindService(svname, app.Name); err != nil {
return err
}
}
fmt.Printf(" starting application '%s'\n", app.Name)
if err := d.startApp(app); err != nil {
return err
}
}
}
}
return nil
}
/*
* This method iterates through the interface object recursively and
* converts each map[interface{}]interface{} to map[string]interface{}.
* This solves the problem of converting an unknown depth of a YAML object
* to a JSON object.
*/
func dynamicYamlHelper(data interface{}) interface{} {
switch res := data.(type) {
case map[interface{}]interface{}:
temp := map[string]interface{}{}
for key, val := range res {
temp[key.(string)] = dynamicYamlHelper(val)
}
return temp
case []interface{}:
for i, val := range res {
res[i] = dynamicYamlHelper(val)
}
}
return data
}