forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
630 lines (558 loc) · 15.5 KB
/
plugin.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
"github.com/nightlyone/lockfile"
"github.com/spf13/afero"
)
// This example uses terraform as the instance plugin.
// It is very similar to the file instance plugin. When we
// provision an instance, we write a *.tf.json file in the directory
// and call terra apply. For describing instances, we parse the
// result of terra show. Destroying an instance is simply removing a
// tf.json file and call terra apply again.
type plugin struct {
Dir string
fs afero.Fs
lock lockfile.Lockfile
applying bool
applyLock sync.Mutex
pretend bool // true to actually do terraform apply
}
// NewTerraformInstancePlugin returns an instance plugin backed by disk files.
func NewTerraformInstancePlugin(dir string) instance.Plugin {
log.Debugln("terraform instance plugin. dir=", dir)
lock, err := lockfile.New(filepath.Join(dir, "tf-apply.lck"))
if err != nil {
panic(err)
}
return &plugin{
Dir: dir,
fs: afero.NewOsFs(),
lock: lock,
}
}
/*
TFormat models the on disk representation of a terraform resource JSON.
An example of this looks like:
{
"resource" : {
"aws_instance" : {
"web4" : {
"ami" : "${lookup(var.aws_amis, var.aws_region)}",
"instance_type" : "m1.small",
"key_name": "PUBKEY",
"vpc_security_group_ids" : ["${aws_security_group.default.id}"],
"subnet_id": "${aws_subnet.default.id}",
"tags" : {
"Name" : "web4",
"InstancePlugin" : "terraform"
}
"connection" : {
"user" : "ubuntu"
},
"provisioner" : {
"remote_exec" : {
"inline" : [
"sudo apt-get -y update",
"sudo apt-get -y install nginx",
"sudo service nginx start"
]
}
}
}
}
}
}
Note that the JSON above has a name (web4). In general, we do not require names to
be specified. So this means the raw JSON we support needs to omit the name. So the instance.Spec
JSON looks like below, where the value of `value` is the instance body of the TF format JSON.
{
"Properties" : {
"type" : "aws_instance",
"value" : {
"ami" : "${lookup(var.aws_amis, var.aws_region)}",
"instance_type" : "m1.small",
"key_name": "PUBKEY",
"vpc_security_group_ids" : ["${aws_security_group.default.id}"],
"subnet_id": "${aws_subnet.default.id}",
"tags" : {
"Name" : "web4",
"InstancePlugin" : "terraform"
},
"connection" : {
"user" : "ubuntu"
},
"provisioner" : {
"remote_exec" : {
"inline" : [
"sudo apt-get -y update",
"sudo apt-get -y install nginx",
"sudo service nginx start"
]
}
}
}
},
"Tags" : {
"other" : "values",
"to" : "merge",
"with" : "tags"
},
"Init" : "init string"
}
*/
type TFormat struct {
// Resource : resource_type : name : map[string]interface{}
Resource map[string]map[string]map[string]interface{} `json:"resource"`
}
// SpecPropertiesFormat is the schema in the Properties field of the instance.Spec JSON
type SpecPropertiesFormat struct {
Type string `json:"type"`
Value map[string]interface{} `json:"value"`
}
// Validate performs local validation on a provision request.
func (p *plugin) Validate(req *types.Any) error {
log.Debugln("validate", req.String())
parsed := SpecPropertiesFormat{}
err := req.Decode(&parsed)
if err != nil {
return err
}
if parsed.Type == "" {
return fmt.Errorf("no-resource-type:%s", req.String())
}
if len(parsed.Value) == 0 {
return fmt.Errorf("no-value:%s", req.String())
}
return nil
}
func addUserData(m map[string]interface{}, key string, init string) {
if v, has := m[key]; has {
m[key] = fmt.Sprintf("%s\n%s", v, init)
} else {
m[key] = init
}
}
func (p *plugin) terraformApply() error {
if p.pretend {
return nil
}
p.applyLock.Lock()
defer p.applyLock.Unlock()
if p.applying {
return nil
}
go func() {
for {
if err := p.lock.TryLock(); err == nil {
defer p.lock.Unlock()
doTerraformApply(p.Dir)
}
log.Debugln("Can't acquire lock, waiting")
time.Sleep(time.Duration(int64(rand.NormFloat64())%1000) * time.Millisecond)
}
}()
p.applying = true
return nil
}
func doTerraformApply(dir string) error {
log.Infoln(time.Now().Format(time.RFC850) + " Applying plan")
cmd := exec.Command("terraform", "apply")
cmd.Dir = dir
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
output := io.MultiReader(stdout, stderr)
go func() {
reader := bufio.NewReader(output)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
log.WithField("terraform", "apply").Infoln(line)
}
}()
return cmd.Run() // blocks
}
func (p *plugin) terraformShow() (map[string]interface{}, error) {
re := regexp.MustCompile("(^instance-[0-9]+)(.tf.json)")
result := map[string]interface{}{}
fs := &afero.Afero{Fs: p.fs}
// just scan the directory for the instance-*.tf.json files
err := fs.Walk(p.Dir, func(path string, info os.FileInfo, err error) error {
matches := re.FindStringSubmatch(info.Name())
if len(matches) == 3 {
id := matches[1]
parse := map[string]interface{}{}
buff, err := ioutil.ReadFile(filepath.Join(p.Dir, info.Name()))
if err != nil {
log.Warningln("Cannot parse:", err)
return err
}
err = json.Unmarshal(buff, &parse)
if err != nil {
return err
}
if res, has := parse["resource"].(map[string]interface{}); has {
var first map[string]interface{}
res:
for _, r := range res {
if f, ok := r.(map[string]interface{}); ok {
first = f
break res
}
}
if props, has := first[id]; has {
result[id] = props
}
}
}
return nil
})
return result, err
}
func (p *plugin) parseTfStateFile() (map[string]interface{}, error) {
// open the terraform.tfstate file
buff, err := ioutil.ReadFile(filepath.Join(p.Dir, "terraform.tfstate"))
if err != nil {
// The tfstate file is not present this means we have to apply it first.
if os.IsNotExist(err) {
if err = p.terraformApply(); err != nil {
return nil, err
}
return p.terraformShow()
}
return nil, err
}
// tfstate is a JSON so query it
parsed := map[string]interface{}{}
err = json.Unmarshal(buff, &parsed)
if err != nil {
return nil, err
}
if m1, has := parsed["modules"].([]interface{}); has && len(m1) > 0 {
module := m1[0]
if mm, ok := module.(map[string]interface{}); ok {
if resources, ok := mm["resources"].(map[string]interface{}); ok {
// the attributes are wrapped under each resource objects'
// primary.attributes
result := map[string]interface{}{}
for k, rr := range resources {
if r, ok := rr.(map[string]interface{}); ok {
if primary, ok := r["primary"].(map[string]interface{}); ok {
if attributes, ok := primary["attributes"]; ok {
result[k] = attributes
}
}
}
}
return result, nil
}
}
}
return nil, nil
}
func (p *plugin) ensureUniqueFile() string {
for {
if err := p.lock.TryLock(); err == nil {
defer p.lock.Unlock()
return ensureUniqueFile(p.Dir)
}
log.Infoln("Can't acquire lock, waiting")
time.Sleep(time.Duration(int64(rand.NormFloat64())%1000) * time.Millisecond)
}
}
func ensureUniqueFile(dir string) string {
n := fmt.Sprintf("instance-%d", time.Now().Unix())
// if we can open then we have to try again... the file cannot exist currently
if f, err := os.Open(filepath.Join(dir, n) + ".tf.json"); err == nil {
f.Close()
return ensureUniqueFile(dir)
}
return n
}
// Provision creates a new instance based on the spec.
func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
// Simply writes a file and call terraform apply
if spec.Properties == nil {
return nil, fmt.Errorf("no-properties")
}
properties := SpecPropertiesFormat{}
err := spec.Properties.Decode(&properties)
if err != nil {
return nil, err
}
// use timestamp as instance id
name := p.ensureUniqueFile()
id := instance.ID(name)
// set the tags.
// add a name
if spec.Tags != nil {
if _, has := spec.Tags["Name"]; !has {
spec.Tags["Name"] = string(id)
}
}
switch properties.Type {
case "aws_instance", "azurerm_virtual_machine", "digitalocean_droplet", "google_compute_instance":
if t, exists := properties.Value["tags"]; !exists {
properties.Value["tags"] = spec.Tags
} else if mm, ok := t.(map[string]interface{}); ok {
// merge tags
for tt, vv := range spec.Tags {
mm[tt] = vv
}
}
case "softlayer_virtual_guest":
log.Debugln("softlayer_virtual_guest detected, adding hostname to properties: hostname=", name)
properties.Value["hostname"] = name
if _, has := properties.Value["tags"]; !has {
properties.Value["tags"] = []interface{}{}
}
tags, ok := properties.Value["tags"].([]interface{})
if ok {
//softlayer uses a list of tags, instead of a map of tags
properties.Value["tags"] = mergeLabelsIntoTagSlice(tags, spec.Tags)
}
}
// Use tag to store the logical id
if spec.LogicalID != nil {
if m, ok := properties.Value["tags"].(map[string]interface{}); ok {
m["LogicalID"] = string(*spec.LogicalID)
}
}
switch properties.Type {
case "aws_instance":
if p, exists := properties.Value["private_ip"]; exists {
if p == "INSTANCE_LOGICAL_ID" {
if spec.LogicalID != nil {
// set private IP to logical ID
properties.Value["private_ip"] = string(*spec.LogicalID)
} else {
// reset private IP (the tag is not relevant in this context)
delete(properties.Value, "private_ip")
}
}
}
}
// merge the inits
switch properties.Type {
case "aws_instance", "digitalocean_droplet":
addUserData(properties.Value, "user_data", spec.Init)
case "softlayer_virtual_guest":
addUserData(properties.Value, "user_metadata", spec.Init)
case "azurerm_virtual_machine":
// os_profile.custom_data
if m, has := properties.Value["os_profile"]; !has {
properties.Value["os_profile"] = map[string]interface{}{
"custom_data": spec.Init,
}
} else if mm, ok := m.(map[string]interface{}); ok {
addUserData(mm, "custom_data", spec.Init)
}
case "google_compute_instance":
// metadata_startup_script
addUserData(properties.Value, "metadata_startup_script", spec.Init)
}
tfFile := TFormat{
Resource: map[string]map[string]map[string]interface{}{
properties.Type: {
name: properties.Value,
},
},
}
buff, err := json.MarshalIndent(tfFile, " ", " ")
log.Debugln("provision", id, "data=", string(buff), "err=", err)
if err != nil {
return nil, err
}
err = afero.WriteFile(p.fs, filepath.Join(p.Dir, string(id)+".tf.json"), buff, 0644)
if err != nil {
return nil, err
}
return &id, p.terraformApply()
}
// Label labels the instance
func (p *plugin) Label(instance instance.ID, labels map[string]string) error {
buff, err := afero.ReadFile(p.fs, filepath.Join(p.Dir, string(instance)+".tf.json"))
if err != nil {
return err
}
tfFile := map[string]interface{}{}
err = json.Unmarshal(buff, &tfFile)
if err != nil {
return err
}
resources, has := tfFile["resource"].(map[string]interface{})
if !has {
return fmt.Errorf("bad tfile:%v", instance)
}
var resourceType string
var first map[string]interface{} // there should be only one element keyed by the type (e.g. aws_instance)
for k, r := range resources {
if f, ok := r.(map[string]interface{}); ok {
first = f
resourceType = k
break
}
}
if len(first) == 0 {
return fmt.Errorf("no typed properties:%v", instance)
}
props, has := first[string(instance)].(map[string]interface{})
if !has {
return fmt.Errorf("not found:%v", instance)
}
switch resourceType {
case "aws_instance", "azurerm_virtual_machine", "digitalocean_droplet", "google_compute_instance":
if _, has := props["tags"]; !has {
props["tags"] = map[string]interface{}{}
}
if tags, ok := props["tags"].(map[string]interface{}); ok {
for k, v := range labels {
tags[k] = v
}
}
case "softlayer_virtual_guest":
if _, has := props["tags"]; !has {
props["tags"] = []interface{}{}
}
tags, ok := props["tags"].([]interface{})
if !ok {
return fmt.Errorf("bad format:%v", instance)
}
props["tags"] = mergeLabelsIntoTagSlice(tags, labels)
}
buff, err = json.MarshalIndent(tfFile, " ", " ")
if err != nil {
return err
}
err = afero.WriteFile(p.fs, filepath.Join(p.Dir, string(instance)+".tf.json"), buff, 0644)
if err != nil {
return err
}
return p.terraformApply()
}
// Destroy terminates an existing instance.
func (p *plugin) Destroy(instance instance.ID) error {
fp := filepath.Join(p.Dir, string(instance)+".tf.json")
log.Debugln("destroy instance", fp)
err := p.fs.Remove(fp)
if err != nil {
return err
}
return p.terraformApply()
}
// DescribeInstances returns descriptions of all instances matching all of the provided tags.
func (p *plugin) DescribeInstances(tags map[string]string) ([]instance.Description, error) {
log.Debugln("describe-instances", tags)
show, err := p.terraformShow()
if err != nil {
return nil, err
}
re := regexp.MustCompile("(.*)(instance-[0-9]+)")
result := []instance.Description{}
// now we scan for <instance_type.instance-<timestamp> as keys
scan:
for k, v := range show {
matches := re.FindStringSubmatch(k)
if len(matches) == 3 {
id := matches[2]
inst := instance.Description{
Tags: terraformTags(v, "tags"),
ID: instance.ID(id),
LogicalID: terraformLogicalID(v),
}
if len(tags) == 0 {
result = append(result, inst)
} else {
for k, v := range tags {
if inst.Tags[k] != v {
continue scan // we implement AND
}
}
result = append(result, inst)
}
}
}
log.Debugln("describe-instances result=", result)
return result, nil
}
func terraformTags(v interface{}, key string) map[string]string {
log.Debugln("terraformTags", v)
m, ok := v.(map[string]interface{})
if !ok {
log.Debugln("terraformTags: return nil")
return nil
}
tags := map[string]string{}
if mm, ok := m[key].(map[string]interface{}); ok {
for k, v := range mm {
tags[k] = fmt.Sprintf("%v", v)
}
return tags
} else if mm, ok := m[key].([]interface{}); ok {
// add each tag in the list to the tags map
for _, v := range mm {
value := fmt.Sprintf("%v", v)
if strings.Contains(value, ":") {
log.Debugln("terraformTags system tags detected v=", v)
vv := strings.Split(value, ":")
if len(vv) == 2 {
tags[vv[0]] = vv[1]
} else {
log.Errorln("terraformTags: ignore invalid tag detected", value)
}
} else {
tags[value] = "" // for list but no ':"
}
}
log.Debugln("terraformTags return tags", tags)
return tags
} else {
log.Errorln("terraformTags: invalid terraformTags tags value", m[key])
}
for k, v := range m {
if k != "tags.%" && strings.Index(k, "tags.") == 0 {
n := k[len("tags."):]
tags[n] = fmt.Sprintf("%v", v)
}
}
return tags
}
func terraformLogicalID(v interface{}) *instance.LogicalID {
m, ok := v.(map[string]interface{})
if !ok {
return nil
}
tags, ok := m["tags"].(map[string]interface{})
if !ok {
return nil
}
v, exists := tags["LogicalID"]
if exists {
id := instance.LogicalID(fmt.Sprintf("%v", v))
return &id
}
return nil
}