-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
diff.go
422 lines (388 loc) · 12.5 KB
/
diff.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package ecspresso
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"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/fatih/color"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/kylelemons/godebug/diff"
"github.com/mattn/go-shellwords"
)
type DiffOption struct {
Unified bool `help:"unified diff format" default:"true" negatable:""`
External string `help:"external command to format diff" env:"ECSPRESSO_DIFF_COMMAND"`
w io.Writer `kong:"-"`
}
func (d *App) Diff(ctx context.Context, opt DiffOption) error {
ctx, cancel := d.Start(ctx)
defer cancel()
if opt.w == nil {
opt.w = os.Stdout
}
var remoteTaskDefArn string
// diff for services only when service defined
if d.config.Service != "" {
d.Log("[DEBUG] diff service compare with %s", d.config.Service)
newSv, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return fmt.Errorf("failed to load service definition: %w", err)
}
remoteSv, err := d.DescribeService(ctx)
if err != nil {
if errors.As(err, &errNotFound) {
d.Log("[INFO] service not found, will create a new service")
} else {
return fmt.Errorf("failed to describe service: %w", err)
}
}
if _, err := diffServices(ctx, newSv, remoteSv, d.config.ServiceDefinitionPath, &opt); err != nil {
return err
}
if remoteSv != nil {
remoteTaskDefArn = *remoteSv.TaskDefinition
}
}
// task definition
newTd, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return err
}
if remoteTaskDefArn == "" {
arn, err := d.findLatestTaskDefinitionArn(ctx, *newTd.Family)
if err != nil {
if errors.As(err, &errNotFound) {
d.Log("[INFO] task definition not found, will register a new task definition")
} else {
return err
}
}
remoteTaskDefArn = arn
}
var remoteTd *TaskDefinitionInput
if remoteTaskDefArn != "" {
d.Log("[DEBUG] diff task definition compare with %s", remoteTaskDefArn)
remoteTd, err = d.DescribeTaskDefinition(ctx, remoteTaskDefArn)
if err != nil {
return err
}
}
if _, err := diffTaskDefs(ctx, newTd, remoteTd, d.config.TaskDefinitionPath, remoteTaskDefArn, &opt); err != nil {
return err
}
return nil
}
type ServiceForDiff struct {
*ecs.UpdateServiceInput
Tags []types.Tag
}
func diffServices(ctx context.Context, local, remote *Service, localPath string, opt *DiffOption) (bool, error) {
var remoteArn string
if remote != nil {
remoteArn = aws.ToString(remote.ServiceArn)
}
localSvForDiff := ServiceDefinitionForDiff(local)
remoteSvForDiff := ServiceDefinitionForDiff(remote)
newSvBytes, err := MarshalJSONForAPI(localSvForDiff)
if err != nil {
return false, fmt.Errorf("failed to marshal new service definition: %w", err)
}
if local.DesiredCount == nil && remoteSvForDiff != nil {
// ignore DesiredCount when it in local is not defined.
remoteSvForDiff.UpdateServiceInput.DesiredCount = nil
}
remoteSvBytes, err := MarshalJSONForAPI(remoteSvForDiff)
if err != nil {
return false, fmt.Errorf("failed to marshal remote service definition: %w", err)
}
remoteSv := toDiffString(remoteSvBytes)
newSv := toDiffString(newSvBytes)
if remoteSv == newSv {
return false, nil
}
switch {
case opt.External != "":
return true, diffExternal(ctx, opt.External, "service", remoteSv, newSv, opt)
case opt.Unified:
edits := myers.ComputeEdits(span.URIFromPath(remoteArn), remoteSv, newSv)
ds := fmt.Sprint(gotextdiff.ToUnified(remoteArn, localPath, remoteSv, edits))
fmt.Fprint(opt.w, coloredDiff(ds))
return true, nil
default:
ds := diff.Diff(remoteSv, newSv)
fmt.Fprint(opt.w, coloredDiff(fmt.Sprintf("--- %s\n+++ %s\n%s", remoteArn, localPath, ds)))
return true, nil
}
}
func diffTaskDefs(ctx context.Context, local, remote *TaskDefinitionInput, localPath, remoteArn string, opt *DiffOption) (bool, error) {
sortTaskDefinition(local)
sortTaskDefinition(remote)
newTdBytes, err := MarshalJSONForAPI(local)
if err != nil {
return false, fmt.Errorf("failed to marshal new task definition: %w", err)
}
remoteTdBytes, err := MarshalJSONForAPI(remote)
if err != nil {
return false, fmt.Errorf("failed to marshal remote task definition: %w", err)
}
remoteTd := toDiffString(remoteTdBytes)
newTd := toDiffString(newTdBytes)
if remoteTd == newTd {
return false, nil
}
switch {
case opt.External != "":
return true, diffExternal(ctx, opt.External, "taskdef", remoteTd, newTd, opt)
case opt.Unified:
edits := myers.ComputeEdits(span.URIFromPath(remoteArn), remoteTd, newTd)
ds := fmt.Sprint(gotextdiff.ToUnified(remoteArn, localPath, remoteTd, edits))
fmt.Fprint(opt.w, coloredDiff(ds))
return true, nil
default:
ds := diff.Diff(remoteTd, newTd)
fmt.Fprint(opt.w, coloredDiff(fmt.Sprintf("--- %s\n+++ %s\n%s", remoteArn, localPath, ds)))
return true, nil
}
}
func diffExternal(ctx context.Context, diffCmd string, target, remote, local string, opt *DiffOption) error {
args, err := shellwords.Parse(diffCmd)
if err != nil {
return fmt.Errorf("failed to parse diff command: %w", err)
}
tmpDir, err := os.MkdirTemp("", "ecspresso-diff-")
if err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to getwd: %w", err)
}
if err := os.Chdir(tmpDir); err != nil {
return fmt.Errorf("failed to chdir: %w", err)
}
defer os.Chdir(pwd)
for _, name := range []string{"remote", "local"} {
if err := os.Mkdir(name, 0755); err != nil {
return fmt.Errorf("failed to create temp dir: %w", err)
}
}
remoteFile := filepath.Join("remote", target+".json")
localFile := filepath.Join("local", target+".json")
if err := os.WriteFile(remoteFile, []byte(remote), 0644); err != nil {
return fmt.Errorf("failed to write remote file: %w", err)
}
if err := os.WriteFile(localFile, []byte(local), 0644); err != nil {
return fmt.Errorf("failed to write local file: %w", err)
}
args = append(args, remoteFile, localFile)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Stdout = opt.w
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run diff command: %w", err)
}
return nil
}
func coloredDiff(src string) string {
if color.NoColor {
// disable color
return src
}
var b strings.Builder
for _, line := range strings.Split(src, "\n") {
if strings.HasPrefix(line, "-") {
b.WriteString(color.RedString(line) + "\n")
} else if strings.HasPrefix(line, "+") {
b.WriteString(color.GreenString(line) + "\n")
} else {
b.WriteString(line + "\n")
}
}
return b.String()
}
func tdToTaskDefinitionInput(td *TaskDefinition, tdTags []types.Tag) *TaskDefinitionInput {
tdi := &TaskDefinitionInput{
ContainerDefinitions: td.ContainerDefinitions,
Cpu: td.Cpu,
EphemeralStorage: td.EphemeralStorage,
ExecutionRoleArn: td.ExecutionRoleArn,
Family: td.Family,
IpcMode: td.IpcMode,
Memory: td.Memory,
NetworkMode: td.NetworkMode,
PlacementConstraints: td.PlacementConstraints,
PidMode: td.PidMode,
RequiresCompatibilities: td.RequiresCompatibilities,
RuntimePlatform: td.RuntimePlatform,
TaskRoleArn: td.TaskRoleArn,
ProxyConfiguration: td.ProxyConfiguration,
Volumes: td.Volumes,
}
if len(tdTags) > 0 {
tdi.Tags = tdTags
}
return tdi
}
func jsonStr(v interface{}) string {
s, _ := json.Marshal(v)
return string(s)
}
func ServiceDefinitionForDiff(sv *Service) *ServiceForDiff {
if sv == nil {
return nil
}
sort.SliceStable(sv.PlacementConstraints, func(i, j int) bool {
return jsonStr(sv.PlacementConstraints[i]) < jsonStr(sv.PlacementConstraints[j])
})
sort.SliceStable(sv.PlacementStrategy, func(i, j int) bool {
return jsonStr(sv.PlacementStrategy[i]) < jsonStr(sv.PlacementStrategy[j])
})
sort.SliceStable(sv.Tags, func(i, j int) bool {
return aws.ToString(sv.Tags[i].Key) < aws.ToString(sv.Tags[j].Key)
})
if sv.LaunchType == types.LaunchTypeFargate && sv.PlatformVersion == nil {
sv.PlatformVersion = aws.String("LATEST")
}
if sv.SchedulingStrategy == "" || sv.SchedulingStrategy == types.SchedulingStrategyReplica {
sv.SchedulingStrategy = types.SchedulingStrategyReplica
if sv.DeploymentConfiguration == nil {
sv.DeploymentConfiguration = &types.DeploymentConfiguration{
DeploymentCircuitBreaker: &types.DeploymentCircuitBreaker{
Enable: false,
Rollback: false,
},
MaximumPercent: aws.Int32(200),
MinimumHealthyPercent: aws.Int32(100),
}
} else if sv.DeploymentConfiguration.DeploymentCircuitBreaker == nil {
sv.DeploymentConfiguration.DeploymentCircuitBreaker = &types.DeploymentCircuitBreaker{
Enable: false,
Rollback: false,
}
}
} else if sv.SchedulingStrategy == types.SchedulingStrategyDaemon && sv.DeploymentConfiguration == nil {
sv.DeploymentConfiguration = &types.DeploymentConfiguration{
MaximumPercent: aws.Int32(100),
MinimumHealthyPercent: aws.Int32(0),
}
}
if nc := sv.NetworkConfiguration; nc != nil {
if ac := nc.AwsvpcConfiguration; ac != nil {
if ac.AssignPublicIp == "" {
ac.AssignPublicIp = types.AssignPublicIpDisabled
}
sort.SliceStable(ac.SecurityGroups, func(i, j int) bool {
return ac.SecurityGroups[i] < ac.SecurityGroups[j]
})
sort.SliceStable(ac.Subnets, func(i, j int) bool {
return ac.Subnets[i] < ac.Subnets[j]
})
}
}
return &ServiceForDiff{
UpdateServiceInput: svToUpdateServiceInput(sv),
Tags: sv.Tags,
}
}
func sortTaskDefinition(td *TaskDefinitionInput) {
if td == nil {
return
}
for i, cd := range td.ContainerDefinitions {
sort.SliceStable(cd.Environment, func(i, j int) bool {
return aws.ToString(cd.Environment[i].Name) < aws.ToString(cd.Environment[j].Name)
})
sort.SliceStable(cd.MountPoints, func(i, j int) bool {
return jsonStr(cd.MountPoints[i]) < jsonStr(cd.MountPoints[j])
})
sort.SliceStable(cd.PortMappings, func(i, j int) bool {
return jsonStr(cd.PortMappings[i]) < jsonStr(cd.PortMappings[j])
})
// fill hostPort only when networkMode is awsvpc
if td.NetworkMode == types.NetworkModeAwsvpc {
for i, pm := range cd.PortMappings {
if pm.HostPort == nil {
pm.HostPort = pm.ContainerPort
}
cd.PortMappings[i] = pm
}
}
sort.SliceStable(cd.VolumesFrom, func(i, j int) bool {
return jsonStr(cd.VolumesFrom[i]) < jsonStr(cd.VolumesFrom[j])
})
sort.SliceStable(cd.Secrets, func(i, j int) bool {
return aws.ToString(cd.Secrets[i].Name) < aws.ToString(cd.Secrets[j].Name)
})
td.ContainerDefinitions[i] = cd // set sorted value
}
sort.SliceStable(td.PlacementConstraints, func(i, j int) bool {
return jsonStr(td.PlacementConstraints[i]) < jsonStr(td.PlacementConstraints[j])
})
sort.SliceStable(td.RequiresCompatibilities, func(i, j int) bool {
return td.RequiresCompatibilities[i] < td.RequiresCompatibilities[j]
})
sort.SliceStable(td.Volumes, func(i, j int) bool {
return jsonStr(td.Volumes[i]) < jsonStr(td.Volumes[j])
})
sort.SliceStable(td.Tags, func(i, j int) bool {
return aws.ToString(td.Tags[i].Key) < aws.ToString(td.Tags[j].Key)
})
// containerDefinitions are sorted by name
sort.SliceStable(td.ContainerDefinitions, func(i, j int) bool {
return aws.ToString(td.ContainerDefinitions[i].Name) < aws.ToString(td.ContainerDefinitions[j].Name)
})
if td.Cpu != nil {
td.Cpu = toNumberCPU(*td.Cpu)
}
if td.Memory != nil {
td.Memory = toNumberMemory(*td.Memory)
}
if td.ProxyConfiguration != nil && len(td.ProxyConfiguration.Properties) > 0 {
p := td.ProxyConfiguration.Properties
sort.SliceStable(p, func(i, j int) bool {
return aws.ToString(p[i].Name) < aws.ToString(p[j].Name)
})
}
}
func toNumberCPU(cpu string) *string {
if i := strings.Index(strings.ToLower(cpu), "vcpu"); i > 0 {
if ns, err := strconv.ParseFloat(strings.Trim(cpu[0:i], " "), 64); err != nil {
return nil
} else {
nn := fmt.Sprintf("%d", int(ns*1024))
return &nn
}
}
return &cpu
}
func toNumberMemory(memory string) *string {
if i := strings.Index(memory, "GB"); i > 0 {
if ns, err := strconv.ParseFloat(strings.Trim(memory[0:i], " "), 64); err != nil {
return nil
} else {
nn := fmt.Sprintf("%d", int(ns*1024))
return &nn
}
}
return &memory
}
func toDiffString(b []byte) string {
if bytes.Equal(b, []byte("null")) || bytes.Equal(b, []byte("null\n")) {
return ""
}
return string(b)
}