-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
deploy.go
560 lines (510 loc) · 16.8 KB
/
deploy.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package ecspresso
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/kayac/ecspresso/v2/appspec"
"github.com/samber/lo"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codedeploy"
cdTypes "github.com/aws/aws-sdk-go-v2/service/codedeploy/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
isatty "github.com/mattn/go-isatty"
)
const (
CodeDeployConsoleURLFmt = "https://%s.console.aws.amazon.com/codesuite/codedeploy/deployments/%s?region=%s"
)
type DeployOption struct {
DryRun bool `help:"dry run" default:"false"`
DesiredCount *int32 `name:"tasks" help:"desired count of tasks" default:"-1"`
SkipTaskDefinition bool `help:"skip register a new task definition" default:"false"`
Revision int64 `help:"revision of the task definition to run when --skip-task-definition" default:"0"`
ForceNewDeployment bool `help:"force a new deployment of the service" default:"false"`
Wait bool `help:"wait for service stable" default:"true" negatable:""`
SuspendAutoScaling *bool `help:"suspend application auto-scaling attached with the ECS service"`
ResumeAutoScaling *bool `help:"resume application auto-scaling attached with the ECS service"`
AutoScalingMin *int32 `help:"set minimum capacity of application auto-scaling attached with the ECS service"`
AutoScalingMax *int32 `help:"set maximum capacity of application auto-scaling attached with the ECS service"`
RollbackEvents string `help:"roll back when specified events happened (DEPLOYMENT_FAILURE,DEPLOYMENT_STOP_ON_ALARM,DEPLOYMENT_STOP_ON_REQUEST,...) CodeDeploy only." default:""`
UpdateService bool `help:"update service attributes by service definition" default:"true" negatable:""`
LatestTaskDefinition bool `help:"deploy with the latest task definition without registering a new task definition" default:"false"`
}
func (opt DeployOption) DryRunString() string {
if opt.DryRun {
return dryRunStr
}
return ""
}
func (opt DeployOption) ModifyAutoScalingParams() *modifyAutoScalingParams {
p := &modifyAutoScalingParams{
Suspend: nil,
MinCapacity: opt.AutoScalingMin,
MaxCapacity: opt.AutoScalingMax,
}
if opt.SuspendAutoScaling != nil && *opt.SuspendAutoScaling {
p.Suspend = aws.Bool(true)
} else if opt.ResumeAutoScaling != nil && *opt.ResumeAutoScaling {
p.Suspend = aws.Bool(false)
}
return p
}
func calcDesiredCount(sv *Service, opt DeployOption) *int32 {
if sv.SchedulingStrategy == types.SchedulingStrategyDaemon {
return nil
}
if oc := opt.DesiredCount; oc != nil {
if *oc == DefaultDesiredCount {
return sv.DesiredCount
}
return oc // --tasks
}
return nil
}
func (d *App) Deploy(ctx context.Context, opt DeployOption) error {
d.Log("[DEBUG] deploy")
d.LogJSON(opt)
ctx, cancel := d.Start(ctx)
defer cancel()
var sv *Service
d.Log("Starting deploy %s", opt.DryRunString())
sv, err := d.DescribeServiceStatus(ctx, 0)
if err != nil {
if errors.As(err, &errNotFound) {
d.Log("Service %s not found. Creating a new service %s", d.Service, opt.DryRunString())
return d.createService(ctx, opt)
}
return err
}
doDeploy, err := d.DeployFunc(sv)
if err != nil {
return err
}
tdArn, err := d.taskDefinitionArnForDeploy(ctx, sv, opt)
if err != nil {
return err
}
doWait, err := d.WaitFunc(sv, d.confirmPrimaryTD(tdArn))
if err != nil {
return err
}
var count *int32
if d.config.ServiceDefinitionPath != "" && opt.UpdateService {
newSv, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return err
}
addedTags, updatedTags, deletedTags := CompareTags(sv.Tags, newSv.Tags)
differ, err := diffServices(ctx, newSv, sv, d.config.ServiceDefinitionPath, &DiffOption{Unified: true, w: io.Discard})
if err != nil {
return fmt.Errorf("failed to diff of service definitions: %w", err)
}
if differ {
if err = d.UpdateServiceAttributes(ctx, newSv, tdArn, opt); err != nil {
return err
}
sv = newSv // updated
} else {
d.Log("service attributes will not change")
}
if err := d.UpdateServiceTags(ctx, sv, addedTags, updatedTags, deletedTags, opt); err != nil {
return err
}
count = calcDesiredCount(newSv, opt)
} else {
count = calcDesiredCount(sv, opt)
}
if count != nil {
d.Log("desired count: %d", *count)
} else {
d.Log("desired count: unchanged")
}
// manage auto scaling
if err := d.modifyAutoScaling(ctx, opt); err != nil {
return err
}
if opt.DryRun {
d.Log("DRY RUN OK")
return nil
}
if err := doDeploy(ctx, tdArn, count, sv, opt); err != nil {
return err
}
if !opt.Wait {
d.Log("Service is deployed.")
return nil
}
if err := doWait(ctx, sv); err != nil {
if errors.As(err, &errNotFound) {
d.Log("[INFO] %s", err)
// no need to wait
return nil
}
return err
}
d.Log("Service is stable now. Completed!")
return nil
}
func (d *App) UpdateServiceTasks(ctx context.Context, taskDefinitionArn string, count *int32, sv *Service, opt DeployOption) error {
in := &ecs.UpdateServiceInput{
Service: sv.ServiceName,
Cluster: aws.String(d.Cluster),
TaskDefinition: aws.String(taskDefinitionArn),
DesiredCount: count,
ForceNewDeployment: opt.ForceNewDeployment,
}
msg := "Updating service tasks"
if opt.ForceNewDeployment {
msg = msg + " with force new deployment"
}
msg = msg + "..."
d.Log(msg)
d.LogJSON(in)
_, err := d.ecs.UpdateService(ctx, in)
if err != nil {
return fmt.Errorf("failed to update service tasks: %w", err)
}
time.Sleep(delayForServiceChanged) // wait for service updated
return nil
}
func svToUpdateServiceInput(sv *Service) *ecs.UpdateServiceInput {
in := &ecs.UpdateServiceInput{
CapacityProviderStrategy: sv.CapacityProviderStrategy,
DeploymentConfiguration: sv.DeploymentConfiguration,
DesiredCount: sv.DesiredCount,
EnableECSManagedTags: &sv.EnableECSManagedTags,
EnableExecuteCommand: &sv.EnableExecuteCommand,
HealthCheckGracePeriodSeconds: sv.HealthCheckGracePeriodSeconds,
LoadBalancers: sv.LoadBalancers,
NetworkConfiguration: sv.NetworkConfiguration,
PlacementConstraints: sv.PlacementConstraints,
PlacementStrategy: sv.PlacementStrategy,
PlatformVersion: sv.PlatformVersion,
PropagateTags: sv.PropagateTags,
ServiceConnectConfiguration: sv.ServiceConnectConfiguration,
ServiceRegistries: sv.ServiceRegistries,
VolumeConfigurations: sv.VolumeConfigurations,
}
if sv.SchedulingStrategy == types.SchedulingStrategyDaemon {
in.PlacementStrategy = nil
}
return in
}
func (d *App) UpdateServiceAttributes(ctx context.Context, sv *Service, taskDefinitionArn string, opt DeployOption) error {
in := svToUpdateServiceInput(sv)
if sv.isCodeDeploy() {
d.Log("[INFO] deployment by CodeDeploy")
// unable to update attributes below with a CODE_DEPLOY deployment controller.
in.NetworkConfiguration = nil
in.PlatformVersion = nil
in.ForceNewDeployment = false
in.LoadBalancers = nil
in.ServiceRegistries = nil
in.TaskDefinition = nil
in.CapacityProviderStrategy = nil
} else {
d.Log("[INFO] deployment by ECS rolling update")
in.ForceNewDeployment = opt.ForceNewDeployment
in.TaskDefinition = aws.String(taskDefinitionArn)
}
in.Service = aws.String(d.Service)
in.Cluster = aws.String(d.Cluster)
if opt.DryRun {
d.Log("[INFO] update service input: %s", MustMarshalJSONStringForAPI(in))
return nil
}
d.Log("Updating service attributes...")
if out, err := d.ecs.UpdateService(ctx, in); err != nil {
return fmt.Errorf("failed to update service attributes: %w", err)
} else {
sv.ServiceArn = out.Service.ServiceArn
}
time.Sleep(delayForServiceChanged) // wait for service updated
return nil
}
func (d *App) DeployByCodeDeploy(ctx context.Context, taskDefinitionArn string, count *int32, sv *Service, opt DeployOption) error {
if count != nil {
d.Log("updating desired count to %d", *count)
}
_, err := d.ecs.UpdateService(
ctx,
&ecs.UpdateServiceInput{
Service: aws.String(d.Service),
Cluster: aws.String(d.Cluster),
DesiredCount: count,
},
)
if err != nil {
return fmt.Errorf("failed to update service: %w", err)
}
if opt.SkipTaskDefinition && !opt.UpdateService && !opt.ForceNewDeployment {
// no need to create new deployment.
return nil
}
return d.createDeployment(ctx, sv, taskDefinitionArn, opt.RollbackEvents)
}
func (d *App) findDeploymentInfo(ctx context.Context) (*cdTypes.DeploymentInfo, error) {
// search deploymentGroup in CodeDeploy
d.Log("[DEBUG] find applications in CodeDeploy")
apps, err := d.findCodeDeployApplications(ctx)
if err != nil {
return nil, err
}
for _, app := range apps {
groups, err := d.findCodeDeployDeploymentGroups(ctx, *app.ApplicationName)
if err != nil {
return nil, err
}
for _, dg := range groups {
d.LogJSON(dg)
for _, ecsService := range dg.EcsServices {
if *ecsService.ClusterName == d.config.Cluster && *ecsService.ServiceName == d.config.Service {
return &cdTypes.DeploymentInfo{
ApplicationName: app.ApplicationName,
DeploymentGroupName: dg.DeploymentGroupName,
DeploymentConfigName: dg.DeploymentConfigName,
}, nil
}
}
}
}
return nil, ErrNotFound(fmt.Sprintf(
"failed to find CodeDeploy Application/DeploymentGroup for ECS service %s on cluster %s",
d.config.Service,
d.config.Cluster,
))
}
func (d *App) findCodeDeployApplications(ctx context.Context) ([]cdTypes.ApplicationInfo, error) {
var appNames []string
if cd := d.config.CodeDeploy; cd != nil && cd.ApplicationName != "" {
appNames = []string{cd.ApplicationName}
} else {
pager := codedeploy.NewListApplicationsPaginator(d.codedeploy, &codedeploy.ListApplicationsInput{})
for pager.HasMorePages() {
p, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list applications: %w", err)
}
appNames = append(appNames, p.Applications...)
}
}
if len(appNames) == 0 {
return nil, ErrNotFound("no CodeDeploy applications found")
}
d.Log("[DEBUG] found CodeDeploy applications: %v", appNames)
var apps []cdTypes.ApplicationInfo
// BatchGetApplications accepts applications less than 100
for _, names := range lo.Chunk(appNames, 100) {
res, err := d.codedeploy.BatchGetApplications(ctx, &codedeploy.BatchGetApplicationsInput{
ApplicationNames: names,
})
if err != nil {
return nil, fmt.Errorf("failed to batch get applications in CodeDeploy: %w", err)
}
for _, info := range res.ApplicationsInfo {
d.Log("[DEBUG] application %s compute platform %s", *info.ApplicationName, info.ComputePlatform)
if info.ComputePlatform != cdTypes.ComputePlatformEcs {
continue
}
apps = append(apps, info)
}
}
if len(apps) == 0 {
return nil, ErrNotFound("no CodeDeploy applications found")
}
return apps, nil
}
func (d *App) findCodeDeployDeploymentGroups(ctx context.Context, appName string) ([]cdTypes.DeploymentGroupInfo, error) {
var groupNames []string
if cd := d.config.CodeDeploy; cd != nil && cd.DeploymentGroupName != "" {
groupNames = []string{cd.DeploymentGroupName}
} else {
pager := codedeploy.NewListDeploymentGroupsPaginator(d.codedeploy, &codedeploy.ListDeploymentGroupsInput{
ApplicationName: &appName,
})
for pager.HasMorePages() {
p, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list deployment groups in CodeDeploy: %w", err)
}
groupNames = append(groupNames, p.DeploymentGroups...)
}
}
d.Log("[DEBUG] CodeDeploy found deploymentGroups: %v", groupNames)
var groups []cdTypes.DeploymentGroupInfo
// BatchGetDeploymentGroups accepts applications less than 100
for _, names := range lo.Chunk(groupNames, 100) {
gs, err := d.codedeploy.BatchGetDeploymentGroups(ctx, &codedeploy.BatchGetDeploymentGroupsInput{
ApplicationName: &appName,
DeploymentGroupNames: names,
})
if err != nil {
return nil, fmt.Errorf("failed to batch get deployment groups in CodeDeploy: %w", err)
}
groups = append(groups, gs.DeploymentGroupsInfo...)
}
return groups, nil
}
func (d *App) createDeployment(ctx context.Context, sv *Service, taskDefinitionArn string, rollbackEvents string) error {
spec, err := appspec.NewWithService(&sv.Service, taskDefinitionArn)
if err != nil {
return fmt.Errorf("failed to create appspec: %w", err)
}
if d.config.AppSpec != nil {
spec.Hooks = d.config.AppSpec.Hooks
}
d.Log("[DEBUG] appSpecContent: %s", spec.String())
// deployment
dp, err := d.findDeploymentInfo(ctx)
if err != nil {
return err
}
dd := &codedeploy.CreateDeploymentInput{
ApplicationName: dp.ApplicationName,
DeploymentGroupName: dp.DeploymentGroupName,
DeploymentConfigName: dp.DeploymentConfigName,
Revision: &cdTypes.RevisionLocation{
RevisionType: cdTypes.RevisionLocationTypeAppSpecContent,
AppSpecContent: &cdTypes.AppSpecContent{
Content: aws.String(spec.String()),
},
},
}
if rollbackEvents != "" {
var events []cdTypes.AutoRollbackEvent
for _, ev := range strings.Split(rollbackEvents, ",") {
switch ev {
case "DEPLOYMENT_FAILURE":
events = append(events, cdTypes.AutoRollbackEventDeploymentFailure)
case "DEPLOYMENT_STOP_ON_ALARM":
events = append(events, cdTypes.AutoRollbackEventDeploymentStopOnAlarm)
case "DEPLOYMENT_STOP_ON_REQUEST":
events = append(events, cdTypes.AutoRollbackEventDeploymentStopOnRequest)
default:
return fmt.Errorf("invalid rollback event: %s", ev)
}
}
dd.AutoRollbackConfiguration = &cdTypes.AutoRollbackConfiguration{
Enabled: true,
Events: events,
}
}
d.Log("[DEBUG] creating a deployment to CodeDeploy %v", dd)
res, err := d.codedeploy.CreateDeployment(ctx, dd)
if err != nil {
return fmt.Errorf("failed to create deployment: %w", err)
}
id := *res.DeploymentId
u := fmt.Sprintf(
CodeDeployConsoleURLFmt,
d.config.Region,
id,
d.config.Region,
)
d.Log("Deployment %s is created on CodeDeploy:", id)
d.Log(u)
if isatty.IsTerminal(os.Stdout.Fd()) {
if err := exec.Command("open", u).Start(); err != nil {
d.Log("Couldn't open URL %s", u)
}
}
return nil
}
type deployFunc func(ctx context.Context, taskDefinitionArn string, count *int32, sv *Service, opt DeployOption) error
func (d *App) DeployFunc(sv *Service) (deployFunc, error) {
defaultFunc := d.UpdateServiceTasks
if sv == nil || sv.DeploymentController == nil {
return defaultFunc, nil
}
if dc := sv.DeploymentController; dc != nil {
switch dc.Type {
case types.DeploymentControllerTypeCodeDeploy:
return d.DeployByCodeDeploy, nil
case types.DeploymentControllerTypeEcs:
return d.UpdateServiceTasks, nil
default:
return nil, fmt.Errorf("unsupported deployment controller type: %s", dc.Type)
}
}
return defaultFunc, nil
}
func (d *App) UpdateServiceTags(ctx context.Context, sv *Service, added, updated, deleted []types.Tag, opt DeployOption) error {
if len(added) == 0 && len(updated) == 0 && len(deleted) == 0 {
d.Log("[DEBUG] no service tags to update")
return nil
}
var tags []types.Tag
tags = append(tags, added...)
tags = append(tags, updated...)
var untagKeys []string
for _, t := range deleted {
untagKeys = append(untagKeys, *t.Key)
}
if len(tags) > 0 {
for _, t := range tags {
d.Log("[INFO] updating service tags: %s=%s", aws.ToString(t.Key), aws.ToString(t.Value))
}
}
if len(untagKeys) > 0 {
d.Log("[INFO] deleting service tags: %v", untagKeys)
}
if opt.DryRun {
return nil
}
if len(tags) > 0 {
if _, err := d.ecs.TagResource(ctx, &ecs.TagResourceInput{
ResourceArn: sv.ServiceArn,
Tags: tags,
}); err != nil {
return fmt.Errorf("failed to tag service: %w", err)
}
}
if len(untagKeys) > 0 {
if _, err := d.ecs.UntagResource(ctx, &ecs.UntagResourceInput{
ResourceArn: sv.ServiceArn,
TagKeys: untagKeys,
}); err != nil {
return fmt.Errorf("failed to untag service: %w", err)
}
}
return nil
}
func (d *App) taskDefinitionArnForDeploy(ctx context.Context, sv *Service, opt DeployOption) (string, error) {
if opt.Revision > 0 {
if opt.LatestTaskDefinition {
return "", ErrConflictOptions("revision and latest-task-definition are exclusive")
}
family := strings.Split(arnToName(*sv.TaskDefinition), ":")[0]
return fmt.Sprintf("%s:%d", family, opt.Revision), nil
}
if opt.LatestTaskDefinition {
family := strings.Split(arnToName(*sv.TaskDefinition), ":")[0]
tdArn, err := d.findLatestTaskDefinitionArn(ctx, family)
if err != nil {
return "", err
}
return tdArn, nil
}
if opt.SkipTaskDefinition {
return *sv.TaskDefinition, nil
}
td, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return "", err
}
if opt.DryRun {
d.Log("[INFO] task definition:")
d.OutputJSONForAPI(os.Stderr, td)
return "", nil
}
newTd, err := d.RegisterTaskDefinition(ctx, td)
if err != nil {
return "", err
}
return *newTd.TaskDefinitionArn, nil
}