-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtasks.go
More file actions
197 lines (181 loc) · 5.1 KB
/
tasks.go
File metadata and controls
197 lines (181 loc) · 5.1 KB
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
package ecspresso
import (
"context"
"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"
"github.com/fujiwara/ecsta"
)
type TasksOption struct {
ID string `help:"task ID" default:""`
Output string `help:"output format" enum:"table,json,tsv" default:"table"`
List *TasksListOption `cmd:"" default:"withargs" help:"list tasks"`
Find *TasksFindOption `cmd:"" help:"find a task from tasks list and dump it as JSON"`
Stop *TasksStopOption `cmd:"" help:"stop a task"`
Trace *TasksTraceOption `cmd:"" help:"trace a task"`
Logs *TasksLogsOption `cmd:"" help:"show logs of a task"`
}
// TasksListOption is the default subcommand for tasks.
// Deprecated flags are kept as hidden for backward compatibility.
type TasksListOption struct {
DeprecatedFind bool `name:"find" hidden:"" default:"false"`
DeprecatedStop bool `name:"stop" hidden:"" default:"false"`
DeprecatedForce bool `name:"force" hidden:"" default:"false"`
DeprecatedTrace bool `name:"trace" hidden:"" default:"false"`
}
type TasksFindOption struct{}
type TasksStopOption struct {
Force bool `help:"stop the task without confirmation" default:"false"`
}
type TasksTraceOption struct{}
type TasksLogsOption struct {
Follow bool `help:"follow logs" short:"f" default:"false"`
Duration time.Duration `help:"duration of logs" short:"d" default:"1m"`
StartTime string `help:"start time of logs" short:"s" default:""`
Container string `help:"container name" default:""`
}
func (o TasksOption) taskID() string {
return o.ID
}
func (d *App) Tasks(ctx context.Context, opt TasksOption) error {
ctx, cancel := d.Start(ctx)
defer cancel()
ecstaApp, err := d.NewEcsta(ctx)
if err != nil {
return err
}
ecstaApp.Config.Set("output", opt.Output)
family, err := d.taskDefinitionFamily(ctx)
if err != nil {
return err
}
var service *string
if d.config.Service != "" {
service = &d.config.Service
}
switch {
case opt.Find != nil:
return ecstaApp.RunDescribe(ctx, &ecsta.DescribeOption{
ID: opt.taskID(),
Family: &family,
Service: service,
})
case opt.Stop != nil:
return ecstaApp.RunStop(ctx, &ecsta.StopOption{
ID: opt.taskID(),
Force: opt.Stop.Force,
Family: &family,
Service: service,
})
case opt.Trace != nil:
return ecstaApp.RunTrace(ctx, &ecsta.TraceOption{
ID: opt.taskID(),
Duration: time.Minute,
Family: &family,
Service: service,
})
case opt.Logs != nil:
return ecstaApp.RunLogs(ctx, &ecsta.LogsOption{
ID: opt.taskID(),
Follow: opt.Logs.Follow,
Duration: opt.Logs.Duration,
StartTime: opt.Logs.StartTime,
Container: opt.Logs.Container,
Family: &family,
Service: service,
JSON: logFormat == logFormatJSON,
})
case opt.List != nil:
list := opt.List
switch {
case list.DeprecatedFind:
LogWarn("--find flag is deprecated, use 'tasks find' subcommand instead")
return ecstaApp.RunDescribe(ctx, &ecsta.DescribeOption{
ID: opt.taskID(),
Family: &family,
Service: service,
})
case list.DeprecatedStop:
LogWarn("--stop flag is deprecated, use 'tasks stop' subcommand instead")
return ecstaApp.RunStop(ctx, &ecsta.StopOption{
ID: opt.taskID(),
Force: list.DeprecatedForce,
Family: &family,
Service: service,
})
case list.DeprecatedTrace:
LogWarn("--trace flag is deprecated, use 'tasks trace' subcommand instead")
return ecstaApp.RunTrace(ctx, &ecsta.TraceOption{
ID: opt.taskID(),
Duration: time.Minute,
Family: &family,
Service: service,
})
default:
return ecstaApp.RunList(ctx, &ecsta.ListOption{
Family: &family,
Service: service,
})
}
}
return nil
}
func (d *App) taskDefinitionFamily(ctx context.Context) (string, error) {
var family string
if d.config.Service != "" {
sv, err := d.DescribeService(ctx)
if err != nil {
return "", err
}
tdArn := sv.TaskDefinition
td, err := d.DescribeTaskDefinition(ctx, *tdArn)
if err != nil {
return "", err
}
family = aws.ToString(td.Family)
} else {
td, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return "", err
}
family = aws.ToString(td.Family)
}
return family, nil
}
func (d *App) listTasks(ctx context.Context) ([]types.Task, error) {
tasks := []types.Task{}
family, err := d.taskDefinitionFamily(ctx)
if err != nil {
return nil, err
}
for _, status := range []types.DesiredStatus{types.DesiredStatusRunning, types.DesiredStatusStopped} {
tp := ecs.NewListTasksPaginator(
d.ecs,
&ecs.ListTasksInput{
Cluster: &d.config.Cluster,
Family: &family,
DesiredStatus: status,
},
)
for tp.HasMorePages() {
to, err := tp.NextPage(ctx)
if err != nil {
return nil, err
}
if len(to.TaskArns) == 0 {
continue
}
out, err := d.ecs.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &d.config.Cluster,
Tasks: to.TaskArns,
Include: []types.TaskField{"TAGS"},
})
if err != nil {
return nil, err
}
tasks = append(tasks, out.Tasks...)
}
}
return tasks, nil
}