-
Notifications
You must be signed in to change notification settings - Fork 17
/
ecs.go
300 lines (268 loc) · 7.18 KB
/
ecs.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/juju/loggo"
"errors"
"math"
"os"
"strings"
)
// logging
var ecsLogger = loggo.GetLogger("ecs")
// ECS struct
type ECS struct {
clusterArns []*string
serviceArns []*string
taskArns []string
clusterNames []string
serviceNames []string
taskNames []string
containerInstances map[string]string
selectedClusterName string
selectedServiceName string
ipAddr *string
keyName *string
svc *ecs.ECS
}
func newECS() *ECS {
e := ECS{}
// set default region if no region is set
if os.Getenv("AWS_REGION") == "" {
os.Setenv("AWS_REGION", "us-east-1")
}
e.svc = ecs.New(session.New())
e.listCluster()
e.getClusterNames()
return &e
}
// Creates ECS repository
func (e *ECS) listCluster() error {
input := &ecs.ListClustersInput{}
pageNum := 0
err := e.svc.ListClustersPages(input,
func(page *ecs.ListClustersOutput, lastPage bool) bool {
pageNum++
e.clusterArns = append(e.clusterArns, page.ClusterArns...)
return pageNum <= 20
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return err
}
return nil
}
func (e *ECS) getClusterNames() error {
input := &ecs.DescribeClustersInput{
Clusters: e.clusterArns,
}
result, err := e.svc.DescribeClusters(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return err
}
for _, cluster := range result.Clusters {
e.clusterNames = append(e.clusterNames, *cluster.ClusterName)
}
return nil
}
func (e *ECS) listServiceArns(clusterName string) error {
e.serviceArns = []*string{}
input := &ecs.ListServicesInput{
Cluster: aws.String(clusterName),
}
pageNum := 0
err := e.svc.ListServicesPages(input,
func(page *ecs.ListServicesOutput, lastPage bool) bool {
pageNum++
e.serviceArns = append(e.serviceArns, page.ServiceArns...)
return pageNum <= 20
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return err
}
return nil
}
// gets service Arns and returns service names
func (e *ECS) getServices(clusterName string) ([]string, error) {
e.selectedClusterName = clusterName
err := e.listServiceArns(clusterName)
if err != nil {
return []string{}, err
}
// fetch per 10
var y float64 = float64(len(e.serviceArns)) / 10
for i := 0; i < int(math.Ceil(y)); i++ {
f := i * 10
t := int(math.Min(float64(10+10*i), float64(len(e.serviceArns))))
input := &ecs.DescribeServicesInput{
Cluster: aws.String(clusterName),
Services: e.serviceArns[f:t],
}
result, err := e.svc.DescribeServices(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return []string{}, err
}
for _, service := range result.Services {
e.serviceNames = append(e.serviceNames, *service.ServiceName)
}
}
return e.serviceNames, nil
}
// lists task arns
func (e *ECS) listTaskArns(serviceName string) error {
e.taskArns = []string{}
input := &ecs.ListTasksInput{
Cluster: aws.String(e.selectedClusterName),
ServiceName: aws.String(serviceName),
}
pageNum := 0
err := e.svc.ListTasksPages(input,
func(page *ecs.ListTasksOutput, lastPage bool) bool {
pageNum++
e.taskArns = append(e.taskArns, aws.StringValueSlice(page.TaskArns)...)
return pageNum <= 20
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return err
}
return nil
}
// list all task ARNs for a cluster
func (e *ECS) listAllTaskArns() error {
e.taskArns = []string{}
input := &ecs.ListTasksInput{
Cluster: aws.String(e.selectedClusterName),
}
pageNum := 0
err := e.svc.ListTasksPages(input,
func(page *ecs.ListTasksOutput, lastPage bool) bool {
pageNum++
e.taskArns = append(e.taskArns, aws.StringValueSlice(page.TaskArns)...)
return pageNum <= 20
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return err
}
return nil
}
func (e *ECS) getTasks(serviceName string) ([]string, error) {
e.selectedServiceName = serviceName
e.containerInstances = make(map[string]string)
err := e.listTaskArns(serviceName)
if err != nil {
return []string{}, err
}
return e.outputTaskNames()
}
func (e *ECS) getAllTasks() ([]string, error) {
e.containerInstances = make(map[string]string)
err := e.listAllTaskArns()
if err != nil {
return []string{}, err
}
return e.outputTaskNames()
}
func (e *ECS) outputTaskNames() ([]string, error) {
// fetch per 100
var y float64 = float64(len(e.taskArns)) / 100
for i := 0; i < int(math.Ceil(y)); i++ {
f := i * 100
t := int(math.Min(float64(100+100*i), float64(len(e.taskArns))))
input := &ecs.DescribeTasksInput{
Cluster: aws.String(e.selectedClusterName),
Tasks: aws.StringSlice(e.taskArns[f:t]),
}
result, err := e.svc.DescribeTasks(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return []string{}, err
}
for _, task := range result.Tasks {
s := strings.Split(*task.TaskArn, "/")
if len(s) > 1 {
e.taskNames = append(e.taskNames, s[1])
e.containerInstances[s[1]] = aws.StringValue(task.ContainerInstanceArn)
}
}
}
return e.taskNames, nil
}
func (e *ECS) getContainerInstanceIP(taskArn string) (*string, error) {
if e.containerInstances[taskArn] == "" {
return nil, errors.New("Task has no container instance assigned (task might not be running)")
}
input := &ecs.DescribeContainerInstancesInput{
Cluster: aws.String(e.selectedClusterName),
ContainerInstances: []*string{aws.String(e.containerInstances[taskArn])},
}
result, err := e.svc.DescribeContainerInstances(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return nil, err
}
if len(result.ContainerInstances) == 0 {
return nil, errors.New("Container instance not found")
}
inputInstances := &ec2.DescribeInstancesInput{
InstanceIds: []*string{result.ContainerInstances[0].Ec2InstanceId},
}
svcEc2 := ec2.New(session.New())
resultInstances, err := svcEc2.DescribeInstances(inputInstances)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
ecsLogger.Errorf(aerr.Error())
} else {
ecsLogger.Errorf(err.Error())
}
return nil, err
}
if len(resultInstances.Reservations) == 0 {
return nil, errors.New("EC2 instance not found")
}
if len(resultInstances.Reservations[0].Instances) == 0 {
return nil, errors.New("EC2 instance not found")
}
e.ipAddr = resultInstances.Reservations[0].Instances[0].PrivateIpAddress
e.keyName = resultInstances.Reservations[0].Instances[0].KeyName
return e.ipAddr, nil
}