-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
183 lines (164 loc) · 4.37 KB
/
s3.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
package pkg
import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"os"
"sort"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
//GetInstanceID returns the EC2 instance name
func GetInstanceID(svc *ec2metadata.EC2Metadata) (string, error) {
doc, err := svc.GetInstanceIdentityDocument()
if err != nil {
return "", err
}
return doc.InstanceID, nil
}
//GetRegion returns the region the instance is running in
func GetRegion(svc *ec2metadata.EC2Metadata) (string, error) {
doc, err := svc.GetInstanceIdentityDocument()
if err != nil {
return "", err
}
return doc.Region, nil
}
//GetAutoscalingGroupName gets the autoscaling group name the instance is belonging to
func GetAutoscalingGroupName(svc autoscalingiface.AutoScalingAPI, instanceID string) (string, error) {
autoInstance, err := svc.DescribeAutoScalingInstances(
&autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: []*string{
aws.String(instanceID),
},
},
)
if err != nil {
return "", err
}
return *autoInstance.AutoScalingInstances[0].AutoScalingGroupName, nil
}
//GetAutoscalingGroup gets the autoscaling group of the instance from the name of the autoscaling group
func GetAutoscalingGroup(svc autoscalingiface.AutoScalingAPI, groupName string) (*autoscaling.Group, error) {
groups, err := svc.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(groupName),
},
},
)
if err != nil {
return nil, err
}
return groups.AutoScalingGroups[0], nil
}
//WaitTillCapacityReached waits until the autoscaling group is up and running
func WaitTillCapacityReached(group *autoscaling.Group, timeout time.Duration) error {
c1 := make(chan bool, 1)
go func() {
for {
if int64(len(group.Instances)) == *group.DesiredCapacity {
c1 <- true
break
}
time.Sleep(time.Second * 5)
}
}()
select {
case <-c1:
return nil
case <-time.After(timeout * time.Second):
return errors.New("AutoScalingGroup did not reach capacity")
}
}
//GetAutoscalingInstances gets all instances in an autoscaling group
func GetAutoscalingInstances(group *autoscaling.Group) []string {
instances := make([]string, len(group.Instances))
for i, v := range group.Instances {
instances[i] = *v.InstanceId
}
sort.Strings(instances)
return instances
}
//ExistsOnS3 determines if the kube pki is on s3
func ExistsOnS3(svc s3iface.S3API, bucket string, keyPath *map[string]string) (bool, error) {
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucket)})
if err != nil {
return false, err
}
mapObj := map[string]bool{}
for _, item := range resp.Contents {
mapObj[*item.Key] = true
}
for f := range *keyPath {
if _, ok := mapObj[f]; !ok {
return false, nil
}
}
return true, nil
}
//DownloadMapFromS3 gets a map describing keys from s3 and downloads them to a path
func DownloadMapFromS3(svc s3iface.S3API, bucket string, keyPath *map[string]string) error {
for k, p := range *keyPath {
err := DownloadFromS3(svc, bucket, k, p)
if err != nil {
return err
}
}
return nil
}
//DownloadFromS3 gets the kube pki from s3
func DownloadFromS3(svc s3iface.S3API, bucket string, key string, path string) error {
result, err := svc.GetObject(
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
},
)
if err != nil {
return err
}
outfile, err := os.Create(path)
if err != nil {
return err
}
defer func() {
err := outfile.Close()
if err != nil {
log.Fatal(err)
}
}()
_, err = io.Copy(outfile, result.Body)
if err != nil {
return err
}
return nil
}
//UploadMapToS3 puts a map of files to S3
func UploadMapToS3(svc s3iface.S3API, bucket string, keyPath *map[string]string) error {
for k, p := range *keyPath {
err := UploadToS3(svc, bucket, k, p)
if err != nil {
return err
}
}
return nil
}
//UploadToS3 puts a file to s3
func UploadToS3(svc s3iface.S3API, bucket string, key string, path string) error {
dat, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if _, err := svc.PutObject(&s3.PutObjectInput{Body: bytes.NewReader(dat), Bucket: aws.String(bucket), Key: aws.String(key)}); err != nil {
return err
}
return nil
}