-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
init.go
242 lines (227 loc) · 7.4 KB
/
init.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
package ecspresso
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Songmu/prompter"
"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/goccy/go-yaml"
"github.com/google/go-jsonnet/formatter"
)
var CreateFileMode = os.FileMode(0644)
type InitOption struct {
Region string `help:"AWS region" env:"AWS_REGION" default:""`
Cluster string `help:"ECS cluster name" default:"default"`
Service string `help:"ECS service name" required:"" xor:"FROM"`
TaskDefinition string `help:"ECS task definition name:revision" required:"" xor:"FROM"`
TaskDefinitionPath string `help:"path to output task definition file" default:"ecs-task-def.json"`
ServiceDefinitionPath string `help:"path to output service definition file" default:"ecs-service-def.json"`
Sort bool `help:"sort elements in task definition" default:"false" negatable:""`
ForceOverwrite bool `help:"overwrite existing files" default:"false"`
Jsonnet bool `help:"output files as jsonnet format" default:"false"`
}
func (opt *InitOption) NewConfig(ctx context.Context, configFilePath string) (*Config, error) {
conf := NewDefaultConfig()
conf.path = configFilePath
conf.Region = opt.Region
conf.Cluster = opt.Cluster
conf.Service = opt.Service
conf.TaskDefinitionPath = opt.TaskDefinitionPath
conf.ServiceDefinitionPath = opt.ServiceDefinitionPath
if err := conf.Restrict(ctx); err != nil {
return nil, err
}
return conf, nil
}
var (
jsonnetExt = ".jsonnet"
jsonExt = ".json"
ymlExt = ".yml"
yamlExt = ".yaml"
)
func (d *App) Init(ctx context.Context, opt InitOption) error {
conf := d.config
// when --task-definition is not empty, --service is empty because these flags are exclusive.
tdOnly := opt.TaskDefinition != ""
d.LogJSON(opt)
if opt.Jsonnet {
if ext := filepath.Ext(conf.ServiceDefinitionPath); ext == jsonExt {
conf.ServiceDefinitionPath = strings.TrimSuffix(conf.ServiceDefinitionPath, ext) + jsonnetExt
}
if ext := filepath.Ext(conf.TaskDefinitionPath); ext == jsonExt {
conf.TaskDefinitionPath = strings.TrimSuffix(conf.TaskDefinitionPath, ext) + jsonnetExt
}
if ext := filepath.Ext(conf.path); ext == ymlExt || ext == yamlExt {
conf.path = strings.TrimSuffix(conf.path, ext) + jsonnetExt
}
}
var sv *Service
var tdArn string
if tdOnly {
tdArn = opt.TaskDefinition
} else {
var err error
sv, tdArn, err = d.initServiceDefinition(ctx, opt)
if err != nil {
return err
}
}
td, err := d.initTaskDefinition(ctx, opt, tdArn)
if err != nil {
return err
}
if err := d.initConfigurationFile(ctx, conf.path, opt, sv, td); err != nil {
return err
}
return nil
}
func (d *App) initConfigurationFile(ctx context.Context, configFilePath string, opt InitOption, sv *Service, td *TaskDefinitionInput) error {
conf := d.config
if sv == nil {
// tdOnly
conf.Service = ""
conf.ServiceDefinitionPath = ""
} else if sv.isCodeDeploy() {
info, err := d.findDeploymentInfo(ctx)
if err != nil {
Log("[WARNING] failed to find CodeDeploy deployment info: %s", err)
Log("[WARNING] you need to set config.codedeploy section manually")
} else {
conf.CodeDeploy = &ConfigCodeDeploy{
ApplicationName: *info.ApplicationName,
DeploymentGroupName: *info.DeploymentGroupName,
}
}
}
{
var b []byte
var err error
if opt.Jsonnet {
b, err = json.MarshalIndent(conf, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal config to JSON: %w", err)
}
out, err := formatter.Format(configFilePath, string(b), formatter.DefaultOptions())
if err != nil {
return fmt.Errorf("unable to format config as Jsonnet: %w", err)
}
b = []byte(out)
} else {
b, err = yaml.Marshal(conf)
if err != nil {
return fmt.Errorf("unable to marshal config to YAML: %w", err)
}
}
d.Log("save the config to %s", configFilePath)
if err := d.saveFile(configFilePath, b, CreateFileMode, opt.ForceOverwrite); err != nil {
return err
}
}
return nil
}
func (d *App) initServiceDefinition(ctx context.Context, opt InitOption) (*Service, string, error) {
conf := d.config
out, err := d.ecs.DescribeServices(ctx, d.DescribeServicesInput())
if err != nil {
return nil, "", fmt.Errorf("failed to describe service: %w", err)
}
if len(out.Services) == 0 {
return nil, "", ErrNotFound("service is not found")
}
sv, err := d.newServiceFromTypes(ctx, out.Services[0])
if err != nil {
return nil, "", fmt.Errorf("failed to describe service: %w", err)
}
svArn := aws.ToString(sv.ServiceArn)
if long, _ := isLongArnFormat(svArn); long {
// Long arn format must be used for tagging operations
lt, 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)
}
sv.Tags = lt.Tags
}
tdArn := *sv.TaskDefinition
treatmentServiceDefinition(sv)
// remove unnecessary fields
if b, err := MarshalJSONForAPI(sv, "del(.runningCount, .pendingCount)"); err != nil {
return nil, "", fmt.Errorf("unable to marshal service definition to JSON: %w", err)
} else {
if opt.Jsonnet {
out, err := formatter.Format(conf.ServiceDefinitionPath, string(b), formatter.DefaultOptions())
if err != nil {
return nil, "", fmt.Errorf("unable to format service definition as Jsonnet: %w", err)
}
b = []byte(out)
}
d.Log("save the service definition %s to %s", svArn, conf.ServiceDefinitionPath)
if err := d.saveFile(conf.ServiceDefinitionPath, b, CreateFileMode, opt.ForceOverwrite); err != nil {
return nil, "", err
}
}
return sv, tdArn, nil
}
func (d *App) initTaskDefinition(ctx context.Context, opt InitOption, tdArn string) (*TaskDefinitionInput, error) {
conf := d.config
td, err := d.DescribeTaskDefinition(ctx, tdArn)
if err != nil {
return nil, err
}
if opt.Sort {
sortTaskDefinition(td)
}
if b, err := MarshalJSONForAPI(td); err != nil {
return nil, fmt.Errorf("unable to marshal task definition to JSON: %w", err)
} else {
if opt.Jsonnet {
out, err := formatter.Format(conf.TaskDefinitionPath, string(b), formatter.DefaultOptions())
if err != nil {
return nil, fmt.Errorf("unable to format task definition as Jsonnet: %w", err)
}
b = []byte(out)
}
d.Log("save the task definition %s to %s", tdArn, conf.TaskDefinitionPath)
if err := d.saveFile(conf.TaskDefinitionPath, b, CreateFileMode, opt.ForceOverwrite); err != nil {
return nil, err
}
}
return td, nil
}
func treatmentServiceDefinition(sv *Service) {
sv.ClusterArn = nil
sv.CreatedAt = nil
sv.CreatedBy = nil
sv.Deployments = nil
sv.Events = nil
sv.PendingCount = 0
sv.RunningCount = 0
sv.Status = nil
sv.TaskDefinition = nil
sv.TaskSets = nil
sv.ServiceArn = nil
sv.RoleArn = nil
sv.ServiceName = nil
if sv.PropagateTags != types.PropagateTagsService && sv.PropagateTags != types.PropagateTagsTaskDefinition {
sv.PropagateTags = types.PropagateTagsNone
}
}
func (d *App) saveFile(path string, b []byte, mode os.FileMode, force bool) error {
if _, err := os.Stat(path); err == nil && !force {
ok := prompter.YN(fmt.Sprintf("Overwrite existing file %s?", path), false)
if !ok {
d.Log("skip %s", path)
return nil
}
}
if err := os.WriteFile(path, b, mode); err != nil {
return fmt.Errorf("failed to write file %s: %w", path, err)
}
return nil
}