-
Notifications
You must be signed in to change notification settings - Fork 18
/
task.go
73 lines (61 loc) 路 2.02 KB
/
task.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
package api
import (
"context"
"sort"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
)
// Equivalent to
// aws ecs list-tasks --cluster ${cluster} --service ${service}
// aws ecs describe-tasks --cluster ${cluster} --tasks ${taskID}
func (store *Store) ListTasks(clusterName, serviceName *string) ([]types.Task, error) {
limit := int32(100)
listTasksOutput, err := store.ecs.ListTasks(context.Background(), &ecs.ListTasksInput{
Cluster: clusterName,
ServiceName: serviceName,
MaxResults: &limit,
})
if err != nil {
logger.Warnf("Failed to run aws api to list tasks, err: %v", err)
return []types.Task{}, err
}
if len(listTasksOutput.TaskArns) == 0 {
return nil, nil
}
include := []types.TaskField{
types.TaskFieldTags,
}
resultTasks := []types.Task{}
describeTasksOutput, err := store.ecs.DescribeTasks(context.Background(), &ecs.DescribeTasksInput{
Cluster: clusterName,
Tasks: listTasksOutput.TaskArns,
Include: include,
})
if err != nil {
logger.Warnf("Failed to run aws api to describe tasks, error: %v", err)
return []types.Task{}, err
}
resultTasks = append(resultTasks, describeTasksOutput.Tasks...)
// sort tasks by task name
sort.Slice(resultTasks, func(i, j int) bool {
return *resultTasks[i].TaskArn > *resultTasks[j].TaskArn
})
// sort containers by health status
for _, t := range resultTasks {
sort.Slice(t.Containers, func(i, j int) bool {
return t.Containers[i].HealthStatus < t.Containers[j].HealthStatus
})
}
return resultTasks, nil
}
// aws ecs register-task-definition --family ${{family}} --...
// return registered task definition revision
func (store *Store) RegisterTaskDefinition(input *ecs.RegisterTaskDefinitionInput) (string, int32, error) {
registeredTdOutput, err := store.ecs.RegisterTaskDefinition(context.Background(), input)
if err != nil {
return "", 0, err
}
family := *registeredTdOutput.TaskDefinition.Family
revision := registeredTdOutput.TaskDefinition.Revision
return family, revision, nil
}