Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pwaller committed May 4, 2016
0 parents commit 64a8233
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"log"
"os"
"time"

"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"
)

func main() {

if len(os.Args) != 3 {
log.Fatalf("usage: associate-ebs <volume-id> </dev/xvd*>")
}

volumeID, deviceName := os.Args[1], os.Args[2]

s := session.New()

meta := ec2metadata.New(s)

region, err := meta.Region()
if err != nil {
log.Fatalf("associate-ebs: unable to determine region failed: %v", err)
}

instanceID, err := meta.GetMetadata("instance-id")
if err != nil {
log.Fatalf("associate-ebs: unable to determine instance id: %v", err)
}

svc := ec2.New(s, &aws.Config{Region: aws.String(region)})

args := &ec2.AttachVolumeInput{
InstanceId: aws.String(instanceID),
VolumeId: aws.String(volumeID),
Device: aws.String(deviceName),
}

attachment, err := svc.AttachVolume(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 := 5 * time.Second
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 _, err := os.Stat(deviceName); err == nil {
log.Printf("Attached in %v", time.Since(start))
// Success
return
}
}
}

0 comments on commit 64a8233

Please sign in to comment.