-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
302 lines (252 loc) · 6.89 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
)
var sess *session.Session
var cfSvc *cloudformation.CloudFormation
// ChangeSet is public struct to store changesets
type ChangeSet struct {
changeSetID string
changeSetName string
creationTime time.Time
description string
executionStatus string
stackID string
stackName string
status string
statusReason string
}
// ChangeSets is public collection of ChangeSets
type ChangeSets struct {
sets []ChangeSet
}
// Stack to store information about the cloudformation stack
type Stack struct {
creationTime time.Time
deletionTime time.Time
driftInformation cloudformation.StackDriftInformationSummary
lastUpdatedTime time.Time
stackID string
stackName string
stackStatus string
templateDescription string
}
// Stacks is public collection of Stacks
type Stacks struct {
stacks []Stack
}
// initializes the client for cloudformation
func createClient(profile *string, verbose bool) {
config := aws.Config{Region: aws.String("eu-central-1"), MaxRetries: aws.Int(15)}
if verbose == true {
config.WithLogLevel(aws.LogDebugWithRequestRetries)
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: config,
Profile: *profile,
})
if err != nil {
fmt.Println("Session not created: ", err)
os.Exit(127)
}
cfSvc = cloudformation.New(sess)
}
// fetches all the stacks
func fetchChangeSets(cfSvc *cloudformation.CloudFormation, stackName *string) (ChangeSets, error) {
lcsInput := cloudformation.ListChangeSetsInput{
StackName: stackName,
}
ntoken := "1"
var changeset ChangeSet
var changesets ChangeSets
for ntoken != "" {
output, err := cfSvc.ListChangeSets(&lcsInput)
if err != nil {
fmt.Println("Error: ", err)
return changesets, err
}
if output.NextToken != nil {
ntoken = *output.NextToken
lcsInput.NextToken = &ntoken
} else {
ntoken = ""
}
for _, v := range output.Summaries {
changeset.changeSetID = *v.ChangeSetId
changeset.changeSetName = *v.ChangeSetName
if v.CreationTime != nil {
changeset.creationTime = *v.CreationTime
}
if v.Description != nil {
changeset.description = *v.Description
}
changeset.executionStatus = *v.ExecutionStatus
changeset.stackID = *v.StackId
changeset.stackName = *v.StackName
changeset.status = *v.Status
if v.StatusReason != nil {
changeset.statusReason = *v.StatusReason
}
changesets.sets = append(changesets.sets, changeset)
}
}
fmt.Printf("%s: %v changesets found.\n", *stackName, len(changesets.sets))
return changesets, nil
}
// deletes all the failed changeSets for a given stackName
func deleteChangeSets(cfSvc *cloudformation.CloudFormation, stackName string) {
lcsInput := cloudformation.ListChangeSetsInput{
StackName: aws.String(stackName),
}
ntoken := "1"
for ntoken != "" {
output, err := cfSvc.ListChangeSets(&lcsInput)
if err != nil {
fmt.Println("Error", err)
} else {
if output.NextToken != nil {
ntoken = *output.NextToken
lcsInput.NextToken = &ntoken
} else {
ntoken = ""
}
for i := range output.Summaries {
if *output.Summaries[i].Status == "FAILED" {
csName := *output.Summaries[i].ChangeSetName
fmt.Println(csName)
dcsInput := cloudformation.DeleteChangeSetInput{
ChangeSetName: &csName,
StackName: aws.String("opal-inventory-ecr-live"),
}
req, err := cfSvc.DeleteChangeSet(&dcsInput)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(req)
}
}
}
}
}
}
// deletes changeset in a given collection but keeping those newer than the given time
func deleteChangeSetsTimeGap(cfSvc *cloudformation.CloudFormation, sets *ChangeSets, limit *time.Time) error {
for k := range sets.sets {
fmt.Println(sets.sets[k].creationTime)
}
fmt.Println(limit.Format("15:04:05"))
return nil
}
// deletes changesets in a given collection but keeping an also given number
func deleteChangeSetsKeep(cfSvc *cloudformation.CloudFormation, sets *ChangeSets, keep *int) {
for index := 0; index < len(sets.sets)-*keep; index++ {
if sets.sets[index].status == "FAILED" {
csName := sets.sets[index].changeSetName
stack := sets.sets[index].stackName
csTime := sets.sets[index].creationTime
fmt.Printf("Deleting changeset %s (%s) on stack %s.\n", csName, csTime, stack)
dcsInput := cloudformation.DeleteChangeSetInput{
ChangeSetName: &csName,
StackName: &stack,
}
_, err := cfSvc.DeleteChangeSet(&dcsInput)
if err != nil {
fmt.Println(err)
}
}
}
}
func fetchStacks(cfSvc *cloudformation.CloudFormation) (Stacks, error) {
lsInput := cloudformation.ListStacksInput{}
ntoken := "1"
var stack Stack
var stacks Stacks
for ntoken != "" {
output, err := cfSvc.ListStacks(&lsInput)
if err != nil {
fmt.Println("Error", err)
return stacks, err
}
if output.NextToken != nil {
ntoken = *output.NextToken
lsInput.NextToken = &ntoken
} else {
ntoken = ""
}
for _, v := range output.StackSummaries {
//fmt.Println(*v)
stack.creationTime = *v.CreationTime
if *v.StackStatus == "DELETE_COMPLETE" {
stack.deletionTime = *v.DeletionTime
}
stack.driftInformation = *v.DriftInformation
if *v.StackStatus == "UPDATE_COMPLETE" {
stack.lastUpdatedTime = *v.LastUpdatedTime
}
stack.stackID = *v.StackId
stack.stackName = *v.StackName
stack.stackStatus = *v.StackStatus
stacks.stacks = append(stacks.stacks, stack)
}
}
fmt.Printf("%v stacks found.\n", len(stacks.stacks))
return stacks, nil
}
func cleanUpAllStacks(keep *int) error {
stacks, err := fetchStacks(cfSvc)
if err != nil {
return err
}
for _, v := range stacks.stacks {
if v.stackStatus != "DELETE_COMPLETE" {
sets, err := fetchChangeSets(cfSvc, aws.String(v.stackName))
if err != nil {
return err
}
go deleteChangeSetsKeep(cfSvc, &sets, keep)
}
}
return nil
}
// the main function
func main() {
config := NewCleanerConfig()
config.parseCLIArguments()
// check user input on command line flags
if err := config.validate(); err != nil {
fmt.Fprintln(os.Stderr, err)
flag.Usage()
os.Exit(1)
}
if config.processAll && !config.yesyesyes {
reader := bufio.NewReader(os.Stdin)
fmt.Println()
fmt.Print("Processing on all stacks. Deleting all failed changesets on _all_ stacks. Continue (y/n)? ")
text, _ := reader.ReadString('\n')
if text != "y\n" {
fmt.Println("Coward.")
os.Exit(3)
}
}
createClient(&config.profile, config.verbose)
if config.processAll == true {
err := cleanUpAllStacks(&config.keep)
if err != nil {
fmt.Println(err)
}
} else {
sets, err := fetchChangeSets(cfSvc, &config.stackToClean)
if err != nil {
fmt.Println(err)
} else {
deleteChangeSetsKeep(cfSvc, &sets, &config.keep)
}
}
}