-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
rollback.go
292 lines (263 loc) · 8.59 KB
/
rollback.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
package ecspresso
import (
"context"
"errors"
"fmt"
"strings"
"time"
"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"
"github.com/kayac/ecspresso/v2/appspec"
"github.com/shogo82148/go-retry"
)
type RollbackOption struct {
DryRun bool `help:"dry run" default:"false"`
DeregisterTaskDefinition bool `help:"deregister the rolled-back task definition. not works with --no-wait" default:"true" negatable:""`
Wait bool `help:"wait for the service stable" default:"true" negatable:""`
RollbackEvents string `help:"roll back when specified events happened (DEPLOYMENT_FAILURE,DEPLOYMENT_STOP_ON_ALARM,DEPLOYMENT_STOP_ON_REQUEST,...) CodeDeploy only." default:""`
}
func (opt RollbackOption) DryRunString() string {
if opt.DryRun {
return dryRunStr
}
return ""
}
func (d *App) Rollback(ctx context.Context, opt RollbackOption) error {
ctx, cancel := d.Start(ctx)
defer cancel()
if opt.DeregisterTaskDefinition && !opt.Wait {
return fmt.Errorf("--deregister-task-definition not works with --no-wait together. Please use --no-deregister-task-definition with --no-wait")
}
d.Log("Starting rollback %s", opt.DryRunString())
sv, err := d.DescribeServiceStatus(ctx, 0)
if err != nil {
return err
}
d.Log("deployment controller: %s", sv.DeploymentController.Type)
doRollback, err := d.RollbackFunc(sv)
if err != nil {
return err
}
targetArn, err := d.FindRollbackTarget(ctx, *sv.TaskDefinition)
if err != nil {
return err
}
doWait, err := d.WaitFunc(sv, d.confirmPrimaryTD(targetArn))
if err != nil {
return err
}
// doRollback returns the task definition arn to be rolled back
rollbackedTdArn, err := doRollback(ctx, sv, targetArn, opt)
if err != nil {
return err
}
if opt.DryRun {
if err := d.rollbackTaskDefinition(ctx, rollbackedTdArn, opt); err != nil {
return err
}
d.Log("DRY RUN OK")
return nil
}
if !opt.Wait {
d.Log("Service is rolled back.")
return nil
}
time.Sleep(delayForServiceChanged) // wait for service updated
if err := doWait(ctx, sv); err != nil {
if errors.As(err, &errNotFound) {
d.Log("[INFO] %s", err)
return d.rollbackTaskDefinition(ctx, rollbackedTdArn, opt)
}
return err
}
d.Log("Service is stable now. Completed!")
return d.rollbackTaskDefinition(ctx, rollbackedTdArn, opt)
}
func (d *App) rollbackTaskDefinition(ctx context.Context, rollbackedTdArn string, opt RollbackOption) error {
if !opt.DeregisterTaskDefinition {
return nil
}
if opt.DryRun {
d.Log("task definition %s will be deregistered", arnToName(rollbackedTdArn))
return nil
}
d.Log("Deregistering the rolled-back task definition %s", arnToName(rollbackedTdArn))
_, err := d.ecs.DeregisterTaskDefinition(
ctx,
&ecs.DeregisterTaskDefinitionInput{
TaskDefinition: &rollbackedTdArn,
},
)
if err != nil {
return fmt.Errorf("failed to deregister task definition: %w", err)
}
d.Log("%s was deregistered successfully", arnToName(rollbackedTdArn))
return nil
}
func (d *App) RollbackServiceTasks(ctx context.Context, sv *Service, targetArn string, opt RollbackOption) (string, error) {
currentArn := *sv.TaskDefinition
d.Log("Rolling back to %s %s", arnToName(targetArn), opt.DryRunString())
if opt.DryRun {
return currentArn, nil
}
if err := d.UpdateServiceTasks(
ctx,
targetArn,
nil,
sv,
DeployOption{
ForceNewDeployment: false,
UpdateService: false,
},
); err != nil {
return "", err
}
return currentArn, nil
}
func (d *App) RollbackByCodeDeploy(ctx context.Context, sv *Service, targetArn string, opt RollbackOption) (string, error) {
dp, err := d.findDeploymentInfo(ctx)
if err != nil {
return "", err
}
ld, err := d.codedeploy.ListDeployments(ctx, &codedeploy.ListDeploymentsInput{
ApplicationName: dp.ApplicationName,
DeploymentGroupName: dp.DeploymentGroupName,
})
if err != nil {
return "", fmt.Errorf("failed to list deployments: %w", err)
}
if len(ld.Deployments) == 0 {
return "", ErrNotFound("no deployments are found")
}
out, err := d.codedeploy.GetDeployment(ctx, &codedeploy.GetDeploymentInput{
DeploymentId: &ld.Deployments[0], // latest deployment
})
if err != nil {
return "", fmt.Errorf("failed to get deployment: %w", err)
}
currentDeployment := out.DeploymentInfo
d.Log("current deployment id: %s", *currentDeployment.DeploymentId)
switch currentDeployment.Status {
case cdTypes.DeploymentStatusSucceeded, cdTypes.DeploymentStatusFailed, cdTypes.DeploymentStatusStopped:
currentTdArn := *sv.TaskDefinition
d.Log("the deployment in progress is not found, creating a new deployment with %s %s", targetArn, opt.DryRunString())
if opt.DryRun {
return currentTdArn, nil
}
if err := d.createDeployment(ctx, sv, targetArn, opt.RollbackEvents); err != nil {
return "", err
}
return currentTdArn, nil
default: // If the deployment is not yet complete
d.Log("the deployment in progress found, stopping the deployment %s %s", *currentDeployment.DeploymentId, opt.DryRunString())
tdArn, err := d.findTaskDefinitionOfDeployment(ctx, currentDeployment)
if err != nil {
return "", err
}
if opt.DryRun {
return tdArn, nil
}
if _, err := d.codedeploy.StopDeployment(ctx, &codedeploy.StopDeploymentInput{
DeploymentId: currentDeployment.DeploymentId,
AutoRollbackEnabled: aws.Bool(true),
}); err != nil {
return "", fmt.Errorf("failed to roll back the deployment: %w", err)
}
if err := d.waitForDeploymentRollbacked(ctx, *currentDeployment.DeploymentId); err != nil {
return "", err
}
return tdArn, nil
}
}
func (d *App) FindRollbackTarget(ctx context.Context, taskDefinitionArn string) (string, error) {
var found bool
var nextToken *string
family := strings.Split(arnToName(taskDefinitionArn), ":")[0]
for {
out, err := d.ecs.ListTaskDefinitions(ctx,
&ecs.ListTaskDefinitionsInput{
NextToken: nextToken,
FamilyPrefix: aws.String(family),
MaxResults: aws.Int32(100),
Sort: types.SortOrderDesc,
},
)
if err != nil {
return "", fmt.Errorf("failed to list task definitions: %w", err)
}
if len(out.TaskDefinitionArns) == 0 {
return "", ErrNotFound(fmt.Sprintf("rollback target is not found: %s", err))
}
for _, tdArn := range out.TaskDefinitionArns {
if found {
return tdArn, nil
}
if tdArn == taskDefinitionArn {
found = true
}
}
nextToken = out.NextToken
if nextToken == nil {
break
}
}
return "", ErrNotFound("rollback target is not found")
}
type rollbackFunc func(ctx context.Context, sv *Service, targetArn string, opt RollbackOption) (string, error)
func (d *App) RollbackFunc(sv *Service) (rollbackFunc, error) {
defaultFunc := d.RollbackServiceTasks
if sv == nil || sv.DeploymentController == nil {
return defaultFunc, nil
}
if dc := sv.DeploymentController; dc != nil {
switch dc.Type {
case types.DeploymentControllerTypeCodeDeploy:
return d.RollbackByCodeDeploy, nil
case types.DeploymentControllerTypeEcs:
return d.RollbackServiceTasks, nil
default:
return nil, fmt.Errorf("unsupported deployment controller type: %s", dc.Type)
}
}
return defaultFunc, nil
}
func (d *App) findTaskDefinitionOfDeployment(ctx context.Context, dp *cdTypes.DeploymentInfo) (string, error) {
resRev, err := d.codedeploy.GetApplicationRevision(ctx, &codedeploy.GetApplicationRevisionInput{
ApplicationName: dp.ApplicationName,
Revision: dp.Revision,
})
if err != nil {
return "", fmt.Errorf("failed to get application revision: %w", err)
}
spec, err := appspec.Unmarsal([]byte(*resRev.Revision.AppSpecContent.Content))
if err != nil {
return "", fmt.Errorf("failed to unmarshal appspec: %w", err)
}
return *spec.Resources[0].TargetService.Properties.TaskDefinition, nil
}
func (d *App) waitForDeploymentRollbacked(ctx context.Context, id string) error {
p := retry.Policy{
MinDelay: time.Second,
MaxDelay: 10 * time.Second,
MaxCount: 10,
}
return p.Do(ctx, func() error {
out, err := d.codedeploy.GetDeployment(ctx, &codedeploy.GetDeploymentInput{
DeploymentId: aws.String(id),
})
if err != nil {
return fmt.Errorf("failed to get deployment: %w", err)
}
status := out.DeploymentInfo.Status
rbinfo := out.DeploymentInfo.RollbackInfo
if status == cdTypes.DeploymentStatusStopped && rbinfo != nil && rbinfo.RollbackDeploymentId != nil {
d.Log("Deployment %s is stopped", id)
d.Log("Rollback deployment created: %s", *rbinfo.RollbackDeploymentId)
return nil
}
return fmt.Errorf("deployment %s is not stopped yet", id)
})
}