-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.go
202 lines (189 loc) · 5.39 KB
/
generate.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
package ecrm
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/goccy/go-yaml"
)
var nameToPatternRe = regexp.MustCompile(`^.*?[/_-]`)
func nameToPattern(s string) string {
p := nameToPatternRe.FindAllString(s, 1)
if len(p) > 0 {
return p[0] + "*"
}
return s
}
type Generator struct {
awsCfg aws.Config
}
func NewGenerator(cfg aws.Config) *Generator {
return &Generator{
awsCfg: cfg,
}
}
func (g *Generator) GenerateConfig(ctx context.Context, configFile string) error {
config := Config{}
if err := g.generateClusterConfig(ctx, &config); err != nil {
return err
}
if err := g.generateTaskdefConfig(ctx, &config); err != nil {
return err
}
if err := g.generateLambdaConfig(ctx, &config); err != nil {
return err
}
if err := g.generateRepositoryConfig(ctx, &config); err != nil {
return err
}
buf := bytes.NewBuffer(nil)
if err := yaml.NewEncoder(buf, yaml.IndentSequence(true)).Encode(config); err != nil {
return err
}
log.Println("[notice] Generated config:")
os.Stderr.Write(buf.Bytes())
if _, err := os.Stat(configFile); err == nil {
if !prompter.YN(fmt.Sprintf("%s file already exists. Overwrite?", configFile), false) {
return errors.New("aborted")
}
}
if err := os.WriteFile(configFile, buf.Bytes(), 0644); err != nil {
return err
}
log.Println("[notice] Saved", configFile)
return nil
}
func (g *Generator) generateClusterConfig(ctx context.Context, config *Config) error {
clusters, err := clusterArns(ctx, ecs.NewFromConfig(g.awsCfg))
if err != nil {
return err
}
clusterNames := newSet()
for _, c := range clusters {
name := clusterArnToName(c)
pattern := nameToPattern(name)
clusterNames.add(pattern)
log.Printf("[debug] cluster %s -> %s", name, pattern)
}
for _, name := range clusterNames.members() {
cfg := ClusterConfig{}
if strings.Contains(name, "*") {
cfg.NamePattern = name
} else {
cfg.Name = name
}
config.Clusters = append(config.Clusters, &cfg)
}
sort.Slice(config.Clusters, func(i, j int) bool {
if config.Clusters[i].Name != config.Clusters[j].Name {
return config.Clusters[i].Name < config.Clusters[j].Name
}
return config.Clusters[i].NamePattern < config.Clusters[j].NamePattern
})
return nil
}
func (g *Generator) generateTaskdefConfig(ctx context.Context, config *Config) error {
taskdefs, err := taskDefinitionFamilies(ctx, ecs.NewFromConfig(g.awsCfg))
if err != nil {
return err
}
taskdefNames := newSet()
for _, n := range taskdefs {
name := arnToName(n, "")
pattern := nameToPattern(name)
taskdefNames.add(pattern)
log.Printf("[debug] taskdef %s -> %s", name, pattern)
}
for _, name := range taskdefNames.members() {
cfg := TaskdefConfig{
KeepCount: int64(DefaultKeepCount),
}
if strings.Contains(name, "*") {
cfg.NamePattern = name
} else {
cfg.Name = name
}
config.TaskDefinitions = append(config.TaskDefinitions, &cfg)
}
sort.Slice(config.TaskDefinitions, func(i, j int) bool {
if config.TaskDefinitions[i].Name != config.TaskDefinitions[j].Name {
return config.TaskDefinitions[i].Name < config.TaskDefinitions[j].Name
}
return config.TaskDefinitions[i].NamePattern < config.TaskDefinitions[j].NamePattern
})
return nil
}
func (g *Generator) generateLambdaConfig(ctx context.Context, config *Config) error {
lambdas, err := lambdaFunctions(ctx, lambda.NewFromConfig(g.awsCfg))
if err != nil {
return err
}
lambdaNames := newSet()
for _, c := range lambdas {
name := arnToName(*c.FunctionName, "")
pattern := nameToPattern(name)
lambdaNames.add(pattern)
log.Printf("[debug] lambda %s -> %s", name, pattern)
}
for _, name := range lambdaNames.members() {
cfg := LambdaConfig{
KeepCount: int64(DefaultKeepCount),
}
if strings.Contains(name, "*") {
cfg.NamePattern = name
} else {
cfg.Name = name
}
config.LambdaFunctions = append(config.LambdaFunctions, &cfg)
}
sort.Slice(config.LambdaFunctions, func(i, j int) bool {
if config.LambdaFunctions[i].Name != config.LambdaFunctions[j].Name {
return config.LambdaFunctions[i].Name < config.LambdaFunctions[j].Name
}
return config.LambdaFunctions[i].NamePattern < config.LambdaFunctions[j].NamePattern
})
return nil
}
func (g *Generator) generateRepositoryConfig(ctx context.Context, config *Config) error {
repos, err := ecrRepositories(ctx, ecr.NewFromConfig(g.awsCfg))
if err != nil {
return err
}
repoNames := newSet()
for _, r := range repos {
name := arnToName(*r.RepositoryName, "")
pattern := nameToPattern(name)
repoNames.add(pattern)
log.Printf("[debug] ECR %s -> %s", name, pattern)
}
for _, name := range repoNames.members() {
cfg := RepositoryConfig{
KeepCount: int64(DefaultKeepCount),
Expires: DefaultExpiresStr,
KeepTagPatterns: DefaultKeepTagPatterns,
}
if strings.Contains(name, "*") {
cfg.NamePattern = name
} else {
cfg.Name = RepositoryName(name)
}
config.Repositories = append(config.Repositories, &cfg)
}
sort.Slice(config.Repositories, func(i, j int) bool {
if config.Repositories[i].Name != config.Repositories[j].Name {
return config.Repositories[i].Name < config.Repositories[j].Name
}
return config.Repositories[i].NamePattern < config.Repositories[j].NamePattern
})
return nil
}