/
ebclean.go
202 lines (169 loc) · 5.86 KB
/
ebclean.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 main
import (
"bytes"
"fmt"
"github.com/apcera/termtables"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/codegangsta/cli"
"log"
"os"
"time"
)
func main() {
app := cli.NewApp()
app.Name = "ebclean"
app.Usage = "A command to lean up old Application Versions in AWS Elastic Beanstalk"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "region, r",
Value: "us-east-1",
Usage: "AWS region. Default is us-east-1",
EnvVar: "AWS_REGION",
},
}
app.Commands = []cli.Command{
{
Name: "inspect",
Usage: "inspect an application",
Action: func(c *cli.Context) {
eb := elasticbeanstalk.New(&aws.Config{Region: c.GlobalString("region")})
appName := c.Args().First()
duration := c.Int("duration")
//if flag is not set
if duration == 0 {
duration = 30
}
inspect(eb, appName, duration)
},
Flags: []cli.Flag{
cli.IntFlag{
Name: "duration, d",
Value: 30,
Usage: `number of days from current time that a version is considered obsolete. For example, if set to 30,
all versions older than 30 days from current time will be marked to delete. Default is 30`,
},
},
},
{
Name: "clean",
Usage: "clean up all old versions of an application",
Action: func(c *cli.Context) {
eb := elasticbeanstalk.New(&aws.Config{Region: c.GlobalString("region")})
appName := c.Args().First()
duration := c.Int("duration")
//if flag is not set
if duration == 0 {
duration = 30
}
disposableVersionList := inspect(eb, appName, duration)
if len(disposableVersionList) > 0 {
deleteSourceBundle := c.BoolT("delete-source-bundle")
//if the flag is not set, default is true
if !deleteSourceBundle {
deleteSourceBundle = true
}
clean(eb, appName, disposableVersionList, deleteSourceBundle)
} else {
fmt.Println("No version can be deleted")
}
},
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "delete-source-bundle",
Usage: "if true, this will delete source bundle for a given application version in AWS S3. Default is true.",
},
cli.IntFlag{
Name: "duration, d",
Value: 30,
Usage: `number of days from current time that a version is considered obsolete. For example, if set to 30,
all versions older than 30 days from current time will be marked to delete. Default is 30`,
},
},
},
}
app.Run(os.Args)
}
// inspect and print out details of disposable versions for a given application
func inspect(eb *elasticbeanstalk.ElasticBeanstalk, appName string, duration int) []*elasticbeanstalk.ApplicationVersionDescription {
applicationVersionResp, err := eb.DescribeApplicationVersions(&elasticbeanstalk.DescribeApplicationVersionsInput{ApplicationName: &appName})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
fmt.Println(err.Error())
}
os.Exit(1)
}
environmentResp, err := eb.DescribeEnvironments(&elasticbeanstalk.DescribeEnvironmentsInput{ApplicationName: &appName})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
fmt.Println(err.Error())
}
os.Exit(1)
}
var disposableVersions = make([]*elasticbeanstalk.ApplicationVersionDescription, 0)
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Application Name: %s\n", appName))
buffer.WriteString(fmt.Sprintf("Total version: %d\n", len(applicationVersionResp.ApplicationVersions)))
table := termtables.CreateTable()
table.AddHeaders("Version Label", "Date Created", "Environment")
for _, version := range applicationVersionResp.ApplicationVersions {
var found bool = false
for _, environment := range environmentResp.Environments {
if *version.VersionLabel == *environment.VersionLabel {
table.AddRow(*version.VersionLabel, version.DateCreated, *environment.EnvironmentName)
found = true
}
}
if !found {
if time.Now().After(version.DateCreated.AddDate(0, 0, duration)) {
disposableVersions = append(disposableVersions, version)
table.AddRow(*version.VersionLabel, version.DateCreated, "*Disposable*")
} else {
table.AddRow(*version.VersionLabel, version.DateCreated, "Not disposable")
}
}
}
buffer.WriteString(table.Render())
buffer.WriteString(fmt.Sprintf("Disposable version count: %d\n", len(disposableVersions)))
fmt.Println(buffer.String())
return disposableVersions
}
//clean up all Application Version inside "versions" slice
func clean(eb *elasticbeanstalk.ElasticBeanstalk, appName string, versions []*elasticbeanstalk.ApplicationVersionDescription, deleteSourceBundle bool) {
for _, version := range versions {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Deleting version %s .......... ", *version.VersionLabel))
_, err := eb.DeleteApplicationVersion(&elasticbeanstalk.DeleteApplicationVersionInput{
ApplicationName: &appName,
DeleteSourceBundle: &deleteSourceBundle,
VersionLabel: version.VersionLabel,
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
log.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
buffer.WriteString("Error")
if reqErr, ok := err.(awserr.RequestFailure); ok {
log.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
buffer.WriteString("Error")
}
} else {
log.Println(err.Error())
buffer.WriteString("Error")
}
} else {
buffer.WriteString("OK")
}
fmt.Println(buffer.String())
}
}