forked from awslabs/fargatecli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
307 lines (255 loc) · 7.83 KB
/
service.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
301
302
303
304
305
306
307
package ecs
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
awsecs "github.com/aws/aws-sdk-go/service/ecs"
"github.com/jpignata/fargate/console"
)
type CreateServiceInput struct {
Cluster string
DesiredCount int64
Name string
Port int64
SecurityGroupIds []string
SubnetIds []string
TargetGroupArn string
TaskDefinitionArn string
}
type Service struct {
Cluster string
Cpu string
Deployments []Deployment
DesiredCount int64
EnvVars []EnvVar
Events []Event
Image string
Memory string
Name string
PendingCount int64
RunningCount int64
SecurityGroupIds []string
TargetGroupArn string
TaskDefinitionArn string
TaskRole string
SubnetIds []string
Status string
}
type Event struct {
CreatedAt time.Time
Message string
}
type Deployment struct {
CreatedAt time.Time
DesiredCount int64
Id string
Image string
PendingCount int64
RunningCount int64
Status string
}
func (s *Service) AddEvent(e Event) {
s.Events = append(s.Events, e)
}
func (s *Service) AddDeployment(d Deployment) {
s.Deployments = append(s.Deployments, d)
}
func (ecs *ECS) CreateService(input *CreateServiceInput) {
console.Debug("Creating ECS service")
createServiceInput := &awsecs.CreateServiceInput{
Cluster: aws.String(input.Cluster),
DesiredCount: aws.Int64(input.DesiredCount),
ServiceName: aws.String(input.Name),
TaskDefinition: aws.String(input.TaskDefinitionArn),
LaunchType: aws.String(awsecs.CompatibilityFargate),
NetworkConfiguration: &awsecs.NetworkConfiguration{
AwsvpcConfiguration: &awsecs.AwsVpcConfiguration{
AssignPublicIp: aws.String(awsecs.AssignPublicIpEnabled),
Subnets: aws.StringSlice(input.SubnetIds),
SecurityGroups: aws.StringSlice(input.SecurityGroupIds),
},
},
}
if input.TargetGroupArn != "" && input.Port > 0 {
createServiceInput.SetLoadBalancers(
[]*awsecs.LoadBalancer{
&awsecs.LoadBalancer{
TargetGroupArn: aws.String(input.TargetGroupArn),
ContainerPort: aws.Int64(input.Port),
ContainerName: aws.String(input.Name),
},
},
)
}
_, err := ecs.svc.CreateService(createServiceInput)
if err != nil {
console.ErrorExit(err, "Couldn't create ECS service")
}
console.Debug("Created ECS service [%s]", input.Name)
return
}
func (ecs *ECS) DescribeService(serviceName string) Service {
services := ecs.DescribeServices([]string{serviceName})
if len(services) == 0 {
console.ErrorExit(fmt.Errorf("Could not find %s", serviceName), "Could not describe ECS service")
}
return services[0]
}
func (ecs *ECS) GetDesiredCount(serviceName string) int64 {
service := ecs.DescribeService(serviceName)
return service.DesiredCount
}
func (ecs *ECS) SetDesiredCount(serviceName string, desiredCount int64) {
_, err := ecs.svc.UpdateService(
&awsecs.UpdateServiceInput{
Cluster: aws.String(ecs.ClusterName),
Service: aws.String(serviceName),
DesiredCount: aws.Int64(desiredCount),
},
)
if err != nil {
console.ErrorExit(err, "Could not scale ECS service")
}
}
func (ecs *ECS) DestroyService(serviceName string) {
_, err := ecs.svc.DeleteService(
&awsecs.DeleteServiceInput{
Cluster: aws.String(ecs.ClusterName),
Service: aws.String(serviceName),
},
)
if err != nil {
console.ErrorExit(err, "Could not destroy ECS service")
}
}
func (ecs *ECS) ListServices() []Service {
var services []Service
var serviceArnBatches [][]string
err := ecs.svc.ListServicesPages(
&awsecs.ListServicesInput{
Cluster: aws.String(ecs.ClusterName),
LaunchType: aws.String(awsecs.CompatibilityFargate),
},
func(resp *awsecs.ListServicesOutput, lastPage bool) bool {
if len(resp.ServiceArns) > 0 {
serviceArnBatches = append(serviceArnBatches, aws.StringValueSlice(resp.ServiceArns))
}
return true
},
)
if err != nil {
console.ErrorExit(err, "Could not list ECS services")
}
if len(serviceArnBatches) > 0 {
for _, serviceArnBatch := range serviceArnBatches {
for _, service := range ecs.DescribeServices(serviceArnBatch) {
services = append(services, service)
}
}
}
return services
}
func (ecs *ECS) DescribeServices(serviceArns []string) []Service {
var services []Service
resp, err := ecs.svc.DescribeServices(
&awsecs.DescribeServicesInput{
Cluster: aws.String(ecs.ClusterName),
Services: aws.StringSlice(serviceArns),
},
)
if err != nil {
console.ErrorExit(err, "Could not describe ECS services")
}
for _, service := range resp.Services {
var securityGroupIds, subnetIds []*string
if config := service.NetworkConfiguration.AwsvpcConfiguration; config != nil {
securityGroupIds = config.SecurityGroups
subnetIds = config.Subnets
}
s := Service{
DesiredCount: aws.Int64Value(service.DesiredCount),
Name: aws.StringValue(service.ServiceName),
PendingCount: aws.Int64Value(service.PendingCount),
RunningCount: aws.Int64Value(service.RunningCount),
SecurityGroupIds: aws.StringValueSlice(securityGroupIds),
Status: aws.StringValue(service.Status),
SubnetIds: aws.StringValueSlice(subnetIds),
TaskDefinitionArn: aws.StringValue(service.TaskDefinition),
}
taskDefinition := ecs.DescribeTaskDefinition(aws.StringValue(service.TaskDefinition))
s.Cpu = aws.StringValue(taskDefinition.Cpu)
s.Memory = aws.StringValue(taskDefinition.Memory)
s.TaskRole = aws.StringValue(taskDefinition.TaskRoleArn)
if len(service.LoadBalancers) > 0 {
s.TargetGroupArn = aws.StringValue(service.LoadBalancers[0].TargetGroupArn)
}
if len(taskDefinition.ContainerDefinitions) > 0 {
s.Image = aws.StringValue(taskDefinition.ContainerDefinitions[0].Image)
for _, env := range taskDefinition.ContainerDefinitions[0].Environment {
s.EnvVars = append(
s.EnvVars,
EnvVar{
Key: aws.StringValue(env.Name),
Value: aws.StringValue(env.Value),
},
)
}
}
for _, event := range service.Events {
s.AddEvent(
Event{
CreatedAt: aws.TimeValue(event.CreatedAt),
Message: aws.StringValue(event.Message),
},
)
}
for _, d := range service.Deployments {
deployment := Deployment{
Status: aws.StringValue(d.Status),
DesiredCount: aws.Int64Value(d.DesiredCount),
PendingCount: aws.Int64Value(d.PendingCount),
RunningCount: aws.Int64Value(d.RunningCount),
CreatedAt: aws.TimeValue(d.CreatedAt),
Id: ecs.getDeploymentId(aws.StringValue(d.TaskDefinition)),
}
deploymentTaskDefinition := ecs.DescribeTaskDefinition(aws.StringValue(d.TaskDefinition))
deployment.Image = aws.StringValue(deploymentTaskDefinition.ContainerDefinitions[0].Image)
s.AddDeployment(deployment)
}
services = append(services, s)
}
return services
}
func (ecs *ECS) UpdateServiceTaskDefinition(serviceName, taskDefinitionArn string) {
_, err := ecs.svc.UpdateService(
&awsecs.UpdateServiceInput{
Cluster: aws.String(ecs.ClusterName),
Service: aws.String(serviceName),
TaskDefinition: aws.String(taskDefinitionArn),
},
)
if err != nil {
console.ErrorExit(err, "Could not update ECS service task definition")
}
}
func (ecs *ECS) RestartService(serviceName string) {
_, err := ecs.svc.UpdateService(
&awsecs.UpdateServiceInput{
Cluster: aws.String(ecs.ClusterName),
Service: aws.String(serviceName),
ForceNewDeployment: aws.Bool(true),
},
)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "ServiceNotFoundException":
console.IssueExit("Service %s not found", serviceName)
default:
console.ErrorExit(err, "Could not restart service")
}
}
console.ErrorExit(err, "Could not restart service")
}
}