/
main.go
179 lines (151 loc) · 3.14 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
package main
import (
"log"
"os"
"sync"
"time"
"flag"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/go-ini/ini"
)
var (
regions = []string{
"us-east-1",
"us-west-1",
"us-west-2",
"eu-west-1",
"ap-southeast-1",
"ap-southeast-2",
"ap-northeast-1",
"sa-east-1",
}
)
//p2s return string value from pointer of string.
//if the pointer is nil, p2s returns empty string.
func p2s(ps *string) string {
if ps == nil {
return ""
}
return *ps
}
func nameTag(tags []*ec2.Tag) string {
var name string
for _, tag := range tags {
if *tag.Key == "Name" {
name = *tag.Value
}
}
return name
}
func getCredentialFile() (string, error) {
var provider credentials.SharedCredentialsProvider
_, err := provider.Retrieve()
if err != nil {
return "", err
}
return provider.Filename, nil
}
func getProfileNames(filePath string) ([]string, error) {
cfg, err := ini.Load(filePath)
if err != nil {
return nil, err
}
sections := cfg.SectionStrings()
return sections, nil
}
func validateCredential(creds *credentials.Credentials) bool {
_, err := creds.Get()
if err != nil {
return false
}
return true
}
func retrieve(cfg *aws.Config, profile, region string) ([]InstanceInfo, error) {
svc := ec2.New(session.New(cfg), &aws.Config{Region: aws.String(region)})
c := make(chan bool, 1)
var resp *ec2.DescribeInstancesOutput
var err error
go func() {
resp, err = svc.DescribeInstances(nil)
if err != nil {
log.Fatalf("profile=%s region=%s err=%v\n", profile, region, err)
}
c <- true
}()
select {
case _ = <-c:
case <-time.After(time.Second * 10):
log.Fatalf("timeout error. profile=%s region=%s\n", profile, region)
}
var infos []InstanceInfo
for idx := range resp.Reservations {
for _, inst := range resp.Reservations[idx].Instances {
name := nameTag(inst.Tags)
info := InstanceInfo{
Name: name,
Profile: profile,
Instance: inst,
}
infos = append(infos, info)
}
}
return infos, nil
}
var (
cachemin int
refresh bool
)
func init() {
flag.IntVar(&cachemin, "cachemin", 0, "Cache expire minutes.")
flag.BoolVar(&refresh, "refresh", false, "Delete and re-create cache.")
}
func main() {
flag.Parse()
cache := NewCache(cachemin)
if refresh {
cache.Delete()
}
if cache.Use() && cache.Available() {
cache.Output(os.Stdout)
return
}
credfile, err := getCredentialFile()
if err != nil {
return
}
profiles, err := getProfileNames(credfile)
if err != nil {
return
}
var list EC2List
var wg sync.WaitGroup
for _, profile := range profiles {
profile := profile
creds := credentials.NewSharedCredentials(credfile, profile)
if ok := validateCredential(creds); !ok {
continue
}
cfg := aws.NewConfig().WithCredentials(creds)
for _, region := range regions {
region := region
wg.Add(1)
go func() {
defer wg.Done()
infos, err := retrieve(cfg, profile, region)
if err != nil {
log.Fatal(err)
}
list.Add(infos...)
}()
}
}
wg.Wait()
list.Sort()
if cache.Use() {
cache.Save(list)
}
list.Output(os.Stdout)
}