/
tasks.go
119 lines (110 loc) · 2.55 KB
/
tasks.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
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
Output *string
Find *bool
Stop *bool
Force *bool
Trace *bool
}
func (o TasksOption) taskID() string {
return aws.ToString(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", aws.ToString(opt.Output))
if aws.ToBool(opt.Find) {
return ecstaApp.RunDescribe(ctx, &ecsta.DescribeOption{
ID: opt.taskID(),
})
} else if aws.ToBool(opt.Stop) {
return ecstaApp.RunStop(ctx, &ecsta.StopOption{
ID: opt.taskID(),
Force: aws.ToBool(opt.Force),
})
} else if aws.ToBool(opt.Trace) {
return ecstaApp.RunTrace(ctx, &ecsta.TraceOption{
ID: opt.taskID(),
Duration: time.Minute,
})
} else {
family, err := d.taskDefinitionFamily(ctx)
if err != nil {
return err
}
return ecstaApp.RunList(ctx, &ecsta.ListOption{
Family: family,
})
}
}
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
}