-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
123 lines (105 loc) · 2.52 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
package ecsceed
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
)
type RunOption struct {
NoWait bool
AdditionalParams Params
Count int64
TaskDefinitionPath string
Command []string
Overrides string
ContainerName string
}
func containerOf(td *ecs.TaskDefinition, name *string) *ecs.ContainerDefinition {
if name == nil || *name == "" {
return td.ContainerDefinitions[0]
}
for _, c := range td.ContainerDefinitions {
if *c.Name == *name {
c := c
return c
}
}
return nil
}
func (a *App) Run(ctx context.Context, name string, opt RunOption) error {
a.Log("base service", LogTarget(name))
err := a.ResolveConfigStack(opt.AdditionalParams)
if err != nil {
return err
}
if _, ok := a.def.nameToSrv[name]; !ok {
return fmt.Errorf("service %s is undefined", name)
}
fullname := a.resolveFullName(name)
srv, err := a.DescribeService(ctx, &fullname)
if err != nil {
return err
}
var tdArn *string
var container *ecs.ContainerDefinition
if len(opt.TaskDefinitionPath) > 0 {
// extend
var td ecs.TaskDefinition
path, err := filepath.Abs(opt.TaskDefinitionPath)
if err != nil {
return err
}
err = loadAndMatchTmpl(path, a.def.params, &td)
if err != nil {
return err
}
newTd, err := a.RegisterTaskDefinition(ctx, &td)
if err != nil {
return err
}
tdArn = newTd.TaskDefinitionArn
container = containerOf(newTd, &opt.ContainerName)
} else {
tdArn = srv.TaskDefinition
td, err := a.DescribeTaskDefinition(ctx, *tdArn)
if err != nil {
return err
}
container = containerOf(td, &opt.ContainerName)
}
a.Log("container", LogTarget(*container.Name))
count := opt.Count
var ov ecs.TaskOverride
if ovStr := opt.Overrides; ovStr != "" {
if err := json.Unmarshal([]byte(ovStr), &ov); err != nil {
return fmt.Errorf("invalid overrides: %w", err)
}
}
if opt.Command != nil {
a.Log("command", LogTarget(opt.Command))
cmd := aws.StringSlice(opt.Command)
ov.ContainerOverrides = []*ecs.ContainerOverride{
{
Name: container.Name,
Command: cmd,
},
}
}
task, err := a.RunTask(ctx, *srv, *tdArn, count, &ov)
if err != nil {
return err
}
if !opt.NoWait {
if err := a.WaitRunTask(ctx, task, container, time.Now()); err != nil {
return fmt.Errorf("failed to run task: %w", err)
}
}
if err := a.DescribeTaskStatus(ctx, task, container); err != nil {
return err
}
a.Log("Run task completed!")
return nil
}