-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
run.go
319 lines (293 loc) · 9.78 KB
/
run.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
package ecspresso
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
)
type RunOption struct {
DryRun bool `help:"dry run" default:"false"`
TaskDefinition string `name:"task-def" help:"task definition file for run task" default:""`
Wait bool `help:"wait for task to complete" default:"true" negatable:""`
TaskOverrideStr string `name:"overrides" help:"task override JSON string" default:""`
TaskOverrideFile string `name:"overrides-file" help:"task override JSON file path" default:""`
SkipTaskDefinition bool `help:"skip register a new task definition" default:"false"`
Count int32 `help:"number of tasks to run (max 10)" default:"1"`
WatchContainer string `help:"container name for watching exit code" default:""`
LatestTaskDefinition bool `help:"use the latest task definition without registering a new task definition" default:"false"`
PropagateTags string `help:"propagate the tags for the task (SERVICE or TASK_DEFINITION)" default:""`
Tags string `help:"tags for the task: format is KeyFoo=ValueFoo,KeyBar=ValueBar" default:""`
WaitUntil string `help:"wait until invoked tasks status reached to (running or stopped)" default:"stopped" enum:"running,stopped"`
Revision *int64 `help:"revision of the task definition to run when --skip-task-definition" default:"0"`
ClientToken *string `help:"unique token that identifies a request, useful for idempotency"`
EBSDeleteOnTermination *bool `help:"whether to delete the EBS volume when the task is stopped" default:"true" negatable:""`
}
func (opt RunOption) waitUntilRunning() bool {
return opt.WaitUntil == "running"
}
func (opt RunOption) DryRunString() string {
if opt.DryRun {
return ""
}
return ""
}
func (d *App) Run(ctx context.Context, opt RunOption) error {
ctx, cancel := d.Start(ctx)
defer cancel()
d.Log("Running task %s", opt.DryRunString())
ov := types.TaskOverride{}
if opt.TaskOverrideStr != "" {
if err := json.Unmarshal([]byte(opt.TaskOverrideStr), &ov); err != nil {
return fmt.Errorf("invalid overrides: %w", err)
}
} else if ovFile := opt.TaskOverrideFile; ovFile != "" {
src, err := d.readDefinitionFile(ovFile)
if err != nil {
return fmt.Errorf("failed to read overrides-file %s: %w", ovFile, err)
}
if err := unmarshalJSON(src, &ov, ovFile); err != nil {
return fmt.Errorf("failed to read overrides-file %s: %w", ovFile, err)
}
}
d.Log("[DEBUG] Overrides")
d.LogJSON(ov)
tdArn, err := d.taskDefinitionArnForRun(ctx, opt)
if err != nil {
return err
}
d.Log("Task definition ARN: %s", tdArn)
if opt.DryRun {
d.Log("DRY RUN OK")
return nil
}
td, err := d.DescribeTaskDefinition(ctx, tdArn)
if err != nil {
return err
}
watchContainer := containerOf(td, &opt.WatchContainer)
d.Log("Watch container: %s", *watchContainer.Name)
task, err := d.RunTask(ctx, tdArn, &ov, &opt)
if err != nil {
return err
}
if !opt.Wait {
d.Log("Run task invoked")
return nil
}
if err := d.WaitRunTask(ctx, task, watchContainer, time.Now(), opt.waitUntilRunning()); err != nil {
return err
}
if err := d.DescribeTaskStatus(ctx, task, watchContainer); err != nil {
return err
}
d.Log("Run task completed!")
return nil
}
func (d *App) RunTask(ctx context.Context, tdArn string, ov *types.TaskOverride, opt *RunOption) (*types.Task, error) {
d.Log("Running task with %s", tdArn)
sv, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return nil, err
}
tags, err := parseTags(opt.Tags)
if err != nil {
return nil, fmt.Errorf("failed to run task. invalid tags: %w", err)
}
in := &ecs.RunTaskInput{
Cluster: aws.String(d.Cluster),
TaskDefinition: aws.String(tdArn),
NetworkConfiguration: sv.NetworkConfiguration,
LaunchType: sv.LaunchType,
Overrides: ov,
Count: &opt.Count,
CapacityProviderStrategy: sv.CapacityProviderStrategy,
PlacementConstraints: sv.PlacementConstraints,
PlacementStrategy: sv.PlacementStrategy,
PlatformVersion: sv.PlatformVersion,
Tags: tags,
EnableECSManagedTags: sv.EnableECSManagedTags,
EnableExecuteCommand: sv.EnableExecuteCommand,
ClientToken: opt.ClientToken,
VolumeConfigurations: serviceVolumeConfigurationsToTask(
sv.VolumeConfigurations,
opt.EBSDeleteOnTermination,
),
}
switch opt.PropagateTags {
case "SERVICE":
out, err := d.ecs.ListTagsForResource(ctx, &ecs.ListTagsForResourceInput{
ResourceArn: sv.ServiceArn,
})
if err != nil {
return nil, fmt.Errorf("failed to list tags for service: %w", err)
}
d.Log("[DEBUG] propagate tags from service %s", *sv.ServiceArn)
d.LogJSON(out)
in.Tags = append(in.Tags, out.Tags...)
case "", "NONE":
// XXX ECS says > InvalidParameterException: Invalid value for propagateTags
// in.PropagateTags = types.PropagateTagsNone
in.PropagateTags = ""
default:
in.PropagateTags = types.PropagateTagsTaskDefinition
}
d.Log("[DEBUG] run task input")
d.LogJSON(in)
out, err := d.ecs.RunTask(ctx, in)
if err != nil {
return nil, fmt.Errorf("failed to run task: %w", err)
}
if len(out.Failures) > 0 {
f := out.Failures[0]
if f.Arn != nil {
d.Log("Task ARN: %s", *f.Arn)
}
return nil, fmt.Errorf("failed to run task: %s %s", aws.ToString(f.Reason), aws.ToString(f.Detail))
}
if len(out.Tasks) == 0 {
return nil, fmt.Errorf("failed to run task: no tasks run")
}
task := out.Tasks[0]
d.Log("Task ARN: %s", aws.ToString(task.TaskArn))
return &task, nil
}
func (d *App) WaitRunTask(ctx context.Context, task *types.Task, watchContainer *types.ContainerDefinition, startedAt time.Time, untilRunning bool) error {
d.Log("Waiting for run task...(it may take a while)")
waitCtx, cancel := context.WithCancel(ctx)
defer cancel()
lc := watchContainer.LogConfiguration
if lc == nil || lc.LogDriver != types.LogDriverAwslogs || lc.Options["awslogs-stream-prefix"] == "" {
d.Log("awslogs not configured")
if err := d.waitTask(ctx, task, untilRunning); err != nil {
return err
}
return nil
}
d.Log("Watching container: %s", *watchContainer.Name)
logGroup, logStream := d.GetLogInfo(task, watchContainer)
time.Sleep(3 * time.Second) // wait for log stream
go func() {
ticker := time.NewTicker(5 * time.Second)
var nextToken *string
for {
select {
case <-waitCtx.Done():
return
case <-ticker.C:
nextToken, _ = d.GetLogEvents(waitCtx, logGroup, logStream, startedAt, nextToken)
}
}
}()
if err := d.waitTask(ctx, task, untilRunning); err != nil {
return err
}
return nil
}
func (d *App) waitTask(ctx context.Context, task *types.Task, untilRunning bool) error {
id := arnToName(*task.TaskArn)
if untilRunning {
d.Log("Waiting for task ID %s until running", id)
waiter := ecs.NewTasksRunningWaiter(d.ecs, func(o *ecs.TasksRunningWaiterOptions) {
o.MaxDelay = waiterMaxDelay
})
if err := waiter.Wait(ctx, d.DescribeTasksInput(task), d.Timeout()); err != nil {
return err
}
d.Log("Task ID %s is running", id)
return nil
}
d.Log("Waiting for task ID %s until stopped", id)
waiter := ecs.NewTasksStoppedWaiter(d.ecs, func(o *ecs.TasksStoppedWaiterOptions) {
o.MaxDelay = waiterMaxDelay
})
if err := waiter.Wait(ctx, d.DescribeTasksInput(task), d.Timeout()); err != nil {
return fmt.Errorf("failed to wait task: %w", err)
}
return nil
}
func (d *App) taskDefinitionArnForRun(ctx context.Context, opt RunOption) (string, error) {
switch {
case *opt.Revision > 0:
if opt.LatestTaskDefinition {
return "", ErrConflictOptions("revision and latest-task-definition are exclusive")
}
family, _, err := d.resolveTaskdefinition(ctx)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%d", family, *opt.Revision), nil
case opt.LatestTaskDefinition:
family, _, err := d.resolveTaskdefinition(ctx)
if err != nil {
return "", err
}
d.Log("Revision is not specified. Use latest task definition family " + family)
latestTdArn, err := d.findLatestTaskDefinitionArn(ctx, family)
if err != nil {
return "", err
}
return latestTdArn, nil
case opt.SkipTaskDefinition:
family, rev, err := d.resolveTaskdefinition(ctx)
if err != nil {
return "", err
}
if rev != "" {
return fmt.Sprintf("%s:%s", family, rev), nil
}
d.Log("Revision is not specified. Use latest task definition family " + family)
latestTdArn, err := d.findLatestTaskDefinitionArn(ctx, family)
if err != nil {
return "", err
}
return latestTdArn, nil
default:
tdPath := opt.TaskDefinition
if tdPath == "" {
tdPath = d.config.TaskDefinitionPath
}
in, err := d.LoadTaskDefinition(tdPath)
if err != nil {
return "", err
}
{
b, _ := MarshalJSONForAPI(in)
d.Log("[DEBUG] task definition: %s", string(b))
}
if opt.DryRun {
return fmt.Sprintf("family %s will be registered", *in.Family), nil
}
newTd, err := d.RegisterTaskDefinition(ctx, in)
if err != nil {
return "", err
}
return *newTd.TaskDefinitionArn, nil
}
}
func (d *App) resolveTaskdefinition(ctx context.Context) (family string, revision string, err error) {
if d.config.Service != "" {
d.Log("[DEBUG] loading service")
sv, err := d.DescribeService(ctx)
if err != nil {
return "", "", err
}
tdArn := *sv.TaskDefinition
p := strings.SplitN(arnToName(tdArn), ":", 2)
if len(p) != 2 {
return "", "", fmt.Errorf("invalid task definition arn: %s", tdArn)
}
return p[0], p[1], nil
} else {
d.Log("[DEBUG] loading task definition")
td, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return "", "", err
}
family = *td.Family
return family, "", nil
}
}