-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
497 lines (431 loc) · 14.4 KB
/
main.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
package main // import "github.com/Jimdo/asg-ebs"
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"gopkg.in/alecthomas/kingpin.v2"
log "github.com/Sirupsen/logrus"
)
type createFileSystemOnVolumeTimeout struct{}
func (e createFileSystemOnVolumeTimeout) Error() string {
return "Volume Timeout"
}
type ByStartTime []*ec2.Snapshot
func (s ByStartTime) Len() int { return len(s) }
func (s ByStartTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByStartTime) Less(i, j int) bool { return (*s[i].StartTime).Before(*s[j].StartTime) }
func waitForFile(file string, timeout time.Duration) error {
startTime := time.Now()
if _, err := os.Stat(file); err == nil {
return nil
}
newTimeout := timeout - time.Since(startTime)
if newTimeout > 0 {
return waitForFile(file, newTimeout)
} else {
return errors.New("File " + file + " not found")
}
}
func run(cmd string, args ...string) error {
log.WithFields(log.Fields{"cmd": cmd, "args": args}).Info("Running command")
out, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
log.WithFields(log.Fields{"cmd": cmd, "args": args, "err": err, "out": string(out)}).Info("Error running command")
return err
}
return nil
}
func slurpFile(file string) string {
v, err := ioutil.ReadFile(file)
if err != nil {
log.WithFields(log.Fields{"err": err, "file": file}).Info("Failed to read file")
}
return string(v)
}
type AsgEbs interface {
checkDevice(device string) error
checkMountPoint(mountPoint string) error
findVolume(tagKey string, tagValue string) (*string, error)
attachVolume(volumeId string, attachAs string, deleteOnTermination bool) error
findSnapshot(tagKey string, tagValue string) (*string, error)
createVolume(createSize int64, createName string, createVolumeType string, createTags map[string]string, snapshotId *string) (*string, error)
mountVolume(device string, mountPoint string) error
makeFileSystem(device string, mkfsInodeRatio int64, volumeId string) error
waitUntilVolumeAvailable(volumeId string) error
}
type AwsAsgEbs struct {
AwsConfig *aws.Config
Region string
AvailabilityZone string
InstanceId string
}
func NewAwsAsgEbs(maxRetries int) *AwsAsgEbs {
awsAsgEbs := &AwsAsgEbs{}
metadata := ec2metadata.New(session.New())
region, err := metadata.Region()
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to get region from instance metadata")
}
log.WithFields(log.Fields{"region": region}).Info("Setting region")
awsAsgEbs.Region = region
availabilityZone, err := metadata.GetMetadata("placement/availability-zone")
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to get availability zone from instance metadata")
}
log.WithFields(log.Fields{"az": availabilityZone}).Info("Setting availability zone")
awsAsgEbs.AvailabilityZone = availabilityZone
instanceId, err := metadata.GetMetadata("instance-id")
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to get instance id from instance metadata")
}
log.WithFields(log.Fields{"instance_id": instanceId}).Info("Setting instance id")
awsAsgEbs.InstanceId = instanceId
awsAsgEbs.AwsConfig = aws.NewConfig().
WithRegion(region).
WithCredentials(ec2rolecreds.NewCredentials(session.New())).
WithMaxRetries(maxRetries)
return awsAsgEbs
}
func (awsAsgEbs *AwsAsgEbs) findVolume(tagKey string, tagValue string) (*string, error) {
svc := ec2.New(session.New(awsAsgEbs.AwsConfig))
params := &ec2.DescribeVolumesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:" + tagKey),
Values: []*string{
aws.String(tagValue),
},
},
{
Name: aws.String("tag:filesystem"),
Values: []*string{
aws.String("true"),
},
},
{
Name: aws.String("status"),
Values: []*string{
aws.String("available"),
},
},
{
Name: aws.String("availability-zone"),
Values: []*string{
aws.String(awsAsgEbs.AvailabilityZone),
},
},
},
}
describeVolumesOutput, err := svc.DescribeVolumes(params)
if err != nil {
return nil, err
}
if len(describeVolumesOutput.Volumes) == 0 {
return nil, nil
}
return describeVolumesOutput.Volumes[0].VolumeId, nil
}
func (awsAsgEbs *AwsAsgEbs) findSnapshot(tagKey string, tagValue string) (*string, error) {
svc := ec2.New(session.New(awsAsgEbs.AwsConfig))
describeSnapshotsInput := &ec2.DescribeSnapshotsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:" + tagKey),
Values: []*string{
aws.String(tagValue),
},
},
{
Name: aws.String("status"),
Values: []*string{
aws.String("completed"),
},
},
},
}
describeSnapshotsOutput, err := svc.DescribeSnapshots(describeSnapshotsInput)
if err != nil {
return nil, err
}
snapshots := describeSnapshotsOutput.Snapshots
sort.Sort(sort.Reverse(ByStartTime(snapshots)))
if len(snapshots) == 0 {
return nil, nil
}
return snapshots[0].SnapshotId, nil
}
func (awsAsgEbs *AwsAsgEbs) createVolume(createSize int64, createName string, createVolumeType string, createTags map[string]string, snapshotId *string) (*string, error) {
svc := ec2.New(session.New(awsAsgEbs.AwsConfig))
filesystem := "false"
createVolumeInput := &ec2.CreateVolumeInput{
AvailabilityZone: &awsAsgEbs.AvailabilityZone,
Size: aws.Int64(createSize),
VolumeType: aws.String(createVolumeType),
}
if snapshotId != nil {
createVolumeInput.SnapshotId = aws.String(*snapshotId)
filesystem = "true"
}
vol, err := svc.CreateVolume(createVolumeInput)
if err != nil {
return nil, err
}
tags := []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(createName),
},
{
Key: aws.String("filesystem"),
Value: aws.String(filesystem),
},
}
for k, v := range createTags {
tags = append(tags,
&ec2.Tag{
Key: aws.String(k),
Value: aws.String(v),
},
)
}
createTagsInput := &ec2.CreateTagsInput{
Resources: []*string{vol.VolumeId},
Tags: tags,
}
_, err = svc.CreateTags(createTagsInput)
if err != nil {
return vol.VolumeId, err
}
return vol.VolumeId, nil
}
func (awsAsgEbs *AwsAsgEbs) waitUntilVolumeAvailable(volumeId string) error {
svc := ec2.New(session.New(awsAsgEbs.AwsConfig))
describeVolumeInput := &ec2.DescribeVolumesInput{
VolumeIds: []*string{aws.String(volumeId)},
}
err := svc.WaitUntilVolumeAvailable(describeVolumeInput)
if err != nil {
return &createFileSystemOnVolumeTimeout{}
}
return nil
}
func (awsAsgEbs *AwsAsgEbs) attachVolume(volumeId string, attachAs string, deleteOnTermination bool) error {
svc := ec2.New(session.New(awsAsgEbs.AwsConfig))
attachVolumeInput := &ec2.AttachVolumeInput{
VolumeId: aws.String(volumeId),
Device: aws.String(attachAs),
InstanceId: aws.String(awsAsgEbs.InstanceId),
}
_, err := svc.AttachVolume(attachVolumeInput)
if err != nil {
return err
}
describeVolumeInput := &ec2.DescribeVolumesInput{
VolumeIds: []*string{aws.String(volumeId)},
}
err = svc.WaitUntilVolumeInUse(describeVolumeInput)
if err != nil {
return err
}
if deleteOnTermination {
modifyInstanceAttributeInput := &ec2.ModifyInstanceAttributeInput{
Attribute: aws.String("blockDeviceMapping"),
InstanceId: aws.String(awsAsgEbs.InstanceId),
BlockDeviceMappings: []*ec2.InstanceBlockDeviceMappingSpecification{
{
DeviceName: aws.String(attachAs),
Ebs: &ec2.EbsInstanceBlockDeviceSpecification{
DeleteOnTermination: aws.Bool(true),
VolumeId: aws.String(volumeId),
},
},
},
}
_, err = svc.ModifyInstanceAttribute(modifyInstanceAttributeInput)
if err != nil {
return err
}
}
err = waitForFile("/dev/"+attachAs, 60*time.Second)
if err != nil {
return err
}
return nil
}
func (awsAsgEbs *AwsAsgEbs) makeFileSystem(device string, mkfsInodeRatio int64, volumeId string) error {
svc := ec2.New(session.New(awsAsgEbs.AwsConfig))
err := run("/usr/sbin/mkfs.ext4", "-i", fmt.Sprintf("%d", mkfsInodeRatio), device)
if err != nil {
return err
}
tags := []*ec2.Tag{
{
Key: aws.String("filesystem"),
Value: aws.String("true"),
},
}
createTagsInput := &ec2.CreateTagsInput{
Resources: []*string{aws.String(volumeId)},
Tags: tags,
}
_, err = svc.CreateTags(createTagsInput)
if err != nil {
return err
}
return nil
}
func (awsAsgEbs *AwsAsgEbs) mountVolume(device string, mountPoint string) error {
err := os.MkdirAll(mountPoint, 0755)
if err != nil {
return err
}
return run("/bin/mount", device, mountPoint)
}
func (awsAsgEbs *AwsAsgEbs) checkDevice(device string) error {
if _, err := os.Stat(device); !os.IsNotExist(err) {
return errors.New("Device exists")
}
return nil
}
func (awsAsgEbs *AwsAsgEbs) checkMountPoint(mountPoint string) error {
if strings.Contains(slurpFile("/proc/mounts"), mountPoint) {
return errors.New("Already mounted")
}
return nil
}
type CreateTagsValue map[string]string
func (v CreateTagsValue) Set(str string) error {
parts := strings.SplitN(str, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("expected KEY=VALUE got '%s'", str)
}
key := parts[0]
value := parts[1]
v[key] = value
return nil
}
func (v CreateTagsValue) String() string {
return ""
}
func CreateTags(s kingpin.Settings) (target *map[string]string) {
newMap := make(map[string]string)
target = &newMap
s.SetValue((*CreateTagsValue)(target))
return
}
func runAsgEbs(asgEbs AsgEbs, cfg Config) {
createFileSystemOnVolume := false
var volumeId *string
var snapshotId *string
attachAsDevice := "/dev/" + *cfg.attachAs
// Precondition checks
err := asgEbs.checkDevice(attachAsDevice)
if err != nil {
log.WithFields(log.Fields{"device": attachAsDevice}).Fatal("Device already exists")
}
err = asgEbs.checkMountPoint(*cfg.mountPoint)
if err != nil {
log.WithFields(log.Fields{"mount_point": *cfg.mountPoint}).Fatal("Already mounted")
}
if *cfg.snapshotName == "" {
for i := 1; i <= 10; i++ {
volumeId, err = asgEbs.findVolume(*cfg.tagKey, *cfg.tagValue)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to find volume")
}
if volumeId == nil {
break
} else {
log.WithFields(log.Fields{"volume": *volumeId, "device": attachAsDevice, "attempt": i}).Info("Trying to attach existing volume")
err = asgEbs.attachVolume(*volumeId, *cfg.attachAs, *cfg.deleteOnTermination)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("Failed to attach volume")
} else {
break
}
}
}
} else {
snapshotId, err = asgEbs.findSnapshot("Name", *cfg.snapshotName)
if err != nil {
log.WithFields(log.Fields{"error": err, "snapshot_name": *cfg.snapshotName}).Fatal("Failed to find snapshot")
}
}
if volumeId == nil {
log.Info("Creating new volume")
volumeId, err = asgEbs.createVolume(*cfg.createSize, *cfg.createName, *cfg.createVolumeType, *cfg.createTags, snapshotId)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to create new volume")
}
log.WithFields(log.Fields{"volume": *volumeId}).Info("Waiting until new volume is available")
err = asgEbs.waitUntilVolumeAvailable(*volumeId)
if err != nil {
log.WithFields(log.Fields{"error": err, "volume": *volumeId}).Fatal("Waiting for volume timed out")
}
if snapshotId == nil {
createFileSystemOnVolume = true
}
log.WithFields(log.Fields{"volume": *volumeId, "device": attachAsDevice}).Info("Attaching volume")
err = asgEbs.attachVolume(*volumeId, *cfg.attachAs, *cfg.deleteOnTermination)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to attach volume")
}
}
if createFileSystemOnVolume {
log.WithFields(log.Fields{"device": attachAsDevice}).Info("Creating file system on new volume")
err = asgEbs.makeFileSystem(attachAsDevice, *cfg.mkfsInodeRatio, *volumeId)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to create file system")
}
}
log.WithFields(log.Fields{"device": attachAsDevice, "mount_point": *cfg.mountPoint}).Info("Mounting volume")
err = asgEbs.mountVolume(attachAsDevice, *cfg.mountPoint)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed to mount volume")
}
}
type Config struct {
tagKey *string
tagValue *string
attachAs *string
mountPoint *string
createSize *int64
mkfsInodeRatio *int64
createName *string
createVolumeType *string
createTags *map[string]string
deleteOnTermination *bool
snapshotName *string
maxRetries *int
}
func main() {
cfg := &Config{
tagKey: kingpin.Flag("tag-key", "The tag key to search for").Required().PlaceHolder("KEY").String(),
tagValue: kingpin.Flag("tag-value", "The tag value to search for").Required().PlaceHolder("VALUE").String(),
attachAs: kingpin.Flag("attach-as", "device name e.g. xvdb").Required().PlaceHolder("DEVICE").String(),
mountPoint: kingpin.Flag("mount-point", "Directory where the volume will be mounted").Required().PlaceHolder("DIR").String(),
createSize: kingpin.Flag("create-size", "The size of the created volume, in GiBs").Required().PlaceHolder("SIZE").Int64(),
mkfsInodeRatio: kingpin.Flag("mkfs-inode-ratio", "mkfs.ext4 inode ratio (-i)").Default("16384").Int64(),
createName: kingpin.Flag("create-name", "The name of the created volume").Required().PlaceHolder("NAME").String(),
createVolumeType: kingpin.Flag("create-volume-type", "The volume type of the created volume. This can be `gp2` for General Purpose (SSD) volumes or `standard` for Magnetic volumes").Required().PlaceHolder("TYPE").Enum("standard", "gp2"),
createTags: CreateTags(kingpin.Flag("create-tags", "Tag to use for the new volume, can be specified multiple times").PlaceHolder("KEY=VALUE")),
deleteOnTermination: kingpin.Flag("delete-on-termination", "Delete volume when instance is terminated").Bool(),
snapshotName: kingpin.Flag("snapshot-name", "Name of snapshot to use for new volume").String(),
maxRetries: kingpin.Flag("max-retries", "Maximum number of retries for AWS requests").Default("20").Int(),
}
kingpin.UsageTemplate(kingpin.CompactUsageTemplate)
kingpin.CommandLine.Help = "Script to create, attach, format and mount an EBS Volume to an EC2 instance"
kingpin.Parse()
awsAsgEbs := NewAwsAsgEbs(*cfg.maxRetries)
runAsgEbs(awsAsgEbs, *cfg)
}