forked from flynn/flynn
/
amis.go
66 lines (56 loc) · 1.63 KB
/
amis.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
package main
import (
"encoding/json"
"log"
"os"
"strings"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/ec2"
"github.com/flynn/flynn/util/release/types"
"github.com/flynn/go-docopt"
)
func amis(args *docopt.Args) {
auth, err := aws.EnvCreds()
if err != nil {
log.Fatal(err)
}
manifest := &release.EC2Manifest{}
if err := json.NewDecoder(os.Stdin).Decode(manifest); err != nil {
log.Fatal(err)
}
for _, s := range strings.Split(args.String["<ids>"], ",") {
regionID := strings.SplitN(s, ":", 2)
svc := ec2.New(auth, regionID[0], nil)
resp, err := svc.DescribeImages(&ec2.DescribeImagesRequest{ImageIDs: []string{regionID[1]}})
if err != nil {
log.Fatal(err)
}
if len(resp.Images) < 1 {
log.Fatalln("Could not find image", regionID[1])
}
image := resp.Images[0]
var snapshotID string
for _, mapping := range image.BlockDeviceMappings {
if *mapping.DeviceName == *image.RootDeviceName {
snapshotID = *mapping.EBS.SnapshotID
}
}
if snapshotID == "" {
log.Fatalln("Could not determine RootDeviceSnapshotID for", regionID[1])
}
manifest.Add(args.String["<version>"], &release.EC2Image{
ID: *image.ImageID,
Name: *image.Name,
Region: regionID[0],
OwnerID: *image.OwnerID,
RootDeviceType: *image.RootDeviceType,
RootDeviceName: *image.RootDeviceName,
RootDeviceSnapshotID: snapshotID,
VirtualizationType: *image.VirtualizationType,
Hypervisor: *image.Hypervisor,
})
}
if err := json.NewEncoder(os.Stdout).Encode(manifest); err != nil {
log.Fatal(err)
}
}