-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
75 lines (62 loc) · 2.13 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
package main
import (
"flag"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
var (
useEBS bool
ebsVolumeName string
mountPoint string
blockDevice string
awsRegion string
fileSystemFormatType string
fileSystemFormatArguments string
)
func init() {
flag.StringVar(&awsRegion, "aws-region", "eu-west-1", "AWS region this instance is on")
flag.StringVar(&ebsVolumeName, "ebs-volume-name", "", "EBS volume to attach to this node")
flag.StringVar(&mountPoint, "mount-point", "/var/lib/etcd", "EBS volume mount point")
flag.StringVar(&blockDevice, "block-device", "/dev/xvdf", "Block device to attach as")
flag.StringVar(&fileSystemFormatType, "filesystem-type", "ext4", "Linux filesystem format type")
flag.StringVar(&fileSystemFormatArguments, "filesystem-arguments", "", "Linux filesystem format arguments")
flag.BoolVar(&useEBS, "use-ebs", true, "Use EBS instead of instance store")
flag.Parse()
}
func main() {
// Initialize AWS session
awsSession := session.Must(session.NewSession())
// Create ec2 and metadata svc clients with specified region
ec2SVC := ec2.New(awsSession, aws.NewConfig().WithRegion(awsRegion))
metadataSVC := ec2metadata.New(awsSession, aws.NewConfig().WithRegion(awsRegion))
// obtain current AZ, required for finding volume
availabilityZone, err := metadataSVC.GetMetadata("placement/availability-zone")
if err != nil {
panic(err)
}
if useEBS {
volume, err := volumeFromName(ec2SVC, ebsVolumeName, availabilityZone)
if err != nil {
panic(err)
}
instanceID, err := metadataSVC.GetMetadata("instance-id")
if err != nil {
panic(err)
}
err = attachVolume(ec2SVC, instanceID, volume)
if err != nil {
panic(err)
}
}
if err := ensureVolumeInited(blockDevice, fileSystemFormatType, fileSystemFormatArguments); err != nil {
panic(err)
}
if err := ensureVolumeMounted(blockDevice, mountPoint); err != nil {
panic(err)
}
if err := ensureVolumeWriteable(mountPoint); err != nil {
panic(err)
}
}