forked from mumoshu/concourse-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
192 lines (152 loc) · 3.96 KB
/
util.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
180
181
182
183
184
185
186
187
188
189
190
191
192
package cmd
import (
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func ListRegions() []string {
svc := ec2.New(session.New(), &aws.Config{Region: aws.String("ap-northeast-1")})
params := &ec2.DescribeRegionsInput{
DryRun: aws.Bool(false),
}
resp, err := svc.DescribeRegions(params)
regions := []string{}
for _, r := range resp.Regions {
regions = append(regions, *r.RegionName)
}
if err != nil {
fmt.Println(err.Error())
return nil
}
return regions
}
func ListKeys(region string) []string {
svc := ec2.New(session.New(), &aws.Config{Region: aws.String(region)})
params := &ec2.DescribeKeyPairsInput{
DryRun: aws.Bool(false),
}
resp, err := svc.DescribeKeyPairs(params)
keyPairs := []string{}
for _, k := range resp.KeyPairs {
keyPairs = append(keyPairs, *k.KeyName)
}
if err != nil {
fmt.Println(err.Error())
return []string{}
}
return keyPairs
}
func ListVPCs(region string) []string {
svc := ec2.New(session.New(), &aws.Config{Region: aws.String(region)})
params := &ec2.DescribeVpcsInput{
DryRun: aws.Bool(false),
}
resp, err := svc.DescribeVpcs(params)
if err != nil {
panic(err)
}
vpcs := []string{}
for _, c := range resp.Vpcs {
vpcs = append(vpcs, *c.VpcId)
}
return vpcs
}
func ListAvailabilityZones(region string) []string {
svc := ec2.New(session.New(), &aws.Config{Region: aws.String(region)})
params := &ec2.DescribeAvailabilityZonesInput{
DryRun: aws.Bool(false),
}
resp, err := svc.DescribeAvailabilityZones(params)
if err != nil {
panic(err)
}
azs := []string{}
for _, z := range resp.AvailabilityZones {
azs = append(azs, *z.ZoneName)
}
return azs
}
func ListSubnets(region string, vpcId string, az string) []string {
svc := ec2.New(session.New(), &aws.Config{Region: aws.String(region)})
params := &ec2.DescribeSubnetsInput{
DryRun: aws.Bool(false),
Filters: []*ec2.Filter{
{
Name: aws.String("availabilityZone"),
Values: []*string{
aws.String(az),
},
},
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcId),
},
},
},
}
resp, err := svc.DescribeSubnets(params)
if err != nil {
panic(err)
}
subnets := []string{}
for _, s := range resp.Subnets {
subnets = append(subnets, *s.SubnetId)
}
return subnets
}
func PutFilesToS3(region string, bucketName string, path string, filenames []string) {
svc := s3.New(session.New(), &aws.Config{Region: aws.String(region)})
for _, filename := range filenames {
file, err := os.Open(fmt.Sprintf("%s%s", path, filename))
if err != nil {
log.Panic(err.Error())
}
defer file.Close()
resp, err := svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(filename),
Body: file,
})
if err != nil {
log.Panic(err.Error())
}
log.Println(awsutil.StringValue(resp))
}
}
func GetFilesFromS3(region string, bucketName string, path string, filenames []string) {
downloader := s3manager.NewDownloader(session.New(&aws.Config{Region: aws.String(region)}))
for _, filename := range filenames {
file, err := os.Create(fmt.Sprintf("%s%s", path, filename))
if err != nil {
log.Panic("Failed to create file", err)
}
defer file.Close()
numBytes, err := downloader.Download(file, &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(filename),
})
if err != nil {
log.Panic(err.Error())
}
log.Println("Downloaded file: ", file.Name(), " (", numBytes, "bytes)")
}
}
func prefixConfigDir(filename string) string {
return fmt.Sprintf("%s%s", cfgDir, filename)
}
func makeCfgDirIfNotExists() {
if _, err := os.Stat(cfgDir); os.IsNotExist(err) {
// permission should be 0700
// becuat the config dir contains ssh keys.
if err := os.Mkdir(cfgDir, 0777); err != nil {
log.Panic(err)
}
}
}