forked from pwaller/associate-ebs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (73 loc) · 2.12 KB
/
main.go
File metadata and controls
94 lines (73 loc) · 2.12 KB
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
package main
import (
"context"
"io"
"log"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
func main() {
if len(os.Args) != 3 {
log.Fatalf("usage: associate-ebs <volume-id> </dev/xvd*>")
}
volumeID, deviceName := os.Args[1], os.Args[2]
if exists(deviceName) {
log.Printf("Device %q already present, exiting", deviceName)
return
}
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatalf("associate-ebs: unable to load config: %v", err)
}
imdsClient := imds.NewFromConfig(cfg)
regionOutput, err := imdsClient.GetRegion(context.Background(), &imds.GetRegionInput{})
if err != nil {
log.Fatalf("associate-ebs: unable to determine region failed: %v", err)
}
cfg.Region = regionOutput.Region
instanceIDOutput, err := imdsClient.GetMetadata(context.Background(), &imds.GetMetadataInput{Path: "instance-id"})
if err != nil {
log.Fatalf("associate-ebs: unable to determine instance id: %v", err)
}
defer instanceIDOutput.Content.Close()
instanceIDBytes, err := io.ReadAll(instanceIDOutput.Content)
if err != nil {
log.Fatalf("associate-ebs: unable to read instance id bytes: %v", err)
}
instanceID := string(instanceIDBytes)
svc := ec2.NewFromConfig(cfg)
args := &ec2.AttachVolumeInput{
InstanceId: aws.String(instanceID),
VolumeId: aws.String(volumeID),
Device: aws.String(deviceName),
}
attachment, err := svc.AttachVolume(context.Background(), args)
if err != nil {
log.Fatalf("associate-ebs: AttachVolume failed: %v", err)
}
log.Printf("Attachment State: %q", attachment.State)
tick := time.NewTicker(100 * time.Millisecond).C
timeout := 1 * time.Minute
deadline := time.After(timeout)
start := time.Now()
for {
select {
case <-tick:
case <-deadline:
log.Fatalf("associate-ebs: device did not attach after %v", timeout)
}
if exists(deviceName) {
log.Printf("Attached in %v", time.Since(start))
// Success
return
}
}
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}