forked from wata727/packer-plugin-amazon-ami-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner.go
227 lines (201 loc) · 5.85 KB
/
cleaner.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
package main
import (
"fmt"
"log"
"sort"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
//go:generate mockgen -source cleaner.go -destination cleaner_mock.go -package main
// AbstractCleaner is an interface of Cleaner
type AbstractCleaner interface {
RetrieveCandidateImages() ([]*ec2.Image, error)
DeleteImage(*ec2.Image) error
IsUsed(*ec2.Image) *Used
}
// Cleaner is a wrapper of aws-sdk client
type Cleaner struct {
ec2conn ec2iface.EC2API
autoscalingconn autoscalingiface.AutoScalingAPI
config Config
now time.Time
used map[string]*Used
}
// Used is metadata about the details of the image being used
type Used struct {
Type string
ID string
}
// NewCleaner returns a new cleaner
func NewCleaner(sess *session.Session, config Config) (*Cleaner, error) {
cleaner := &Cleaner{
ec2conn: ec2.New(sess),
autoscalingconn: autoscaling.New(sess),
config: config,
now: time.Now().UTC(),
used: map[string]*Used{},
}
err := cleaner.setInstanceUsed()
if err != nil {
return nil, err
}
err = cleaner.setLaunchConfigurationUsed()
if err != nil {
return nil, err
}
err = cleaner.setLaunchTemplateUsed()
if err != nil {
return nil, err
}
return cleaner, nil
}
// RetrieveCandidateImages returns the images of candidate to be deleted.
// These images are sorted in descending order by creation date.
// Please be aware that these are candidates. Not all images are deleted due to output to the Packer UI.
func (c *Cleaner) RetrieveCandidateImages() ([]*ec2.Image, error) {
log.Println("Describing images")
output, err := c.ec2conn.DescribeImages(&ec2.DescribeImagesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Amazon_AMI_Management_Identifier"),
Values: []*string{
aws.String(c.config.Identifier),
},
},
},
})
if err != nil {
return nil, err
}
// Sort in descending order by creation date
sort.Slice(output.Images, func(i, j int) bool {
iTime, _ := time.Parse("2006-01-02T15:04:05.000Z", *output.Images[i].CreationDate)
jTime, _ := time.Parse("2006-01-02T15:04:05.000Z", *output.Images[j].CreationDate)
return iTime.After(jTime)
})
images := []*ec2.Image{}
for i, image := range output.Images {
if c.config.KeepReleases != 0 && i < c.config.KeepReleases {
continue
}
if c.config.KeepDays != 0 {
creationDate, err := time.ParseInLocation("2006-01-02T15:04:05.000Z", *image.CreationDate, time.UTC)
if err != nil {
return []*ec2.Image{}, err
}
if creationDate.Add(time.Duration(c.config.KeepDays) * 24 * time.Hour).After(c.now) {
continue
}
}
images = append(images, image)
}
return images, nil
}
// DeleteImage deletes a passed image and related snapshots
func (c *Cleaner) DeleteImage(image *ec2.Image) error {
log.Printf("Deleting AMI (%s)", *image.ImageId)
if _, err := c.ec2conn.DeregisterImage(&ec2.DeregisterImageInput{
ImageId: image.ImageId,
DryRun: aws.Bool(c.config.DryRun),
}); err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "DryRunOperation" {
// noop
} else {
return err
}
}
// DeregisterImage method only performs to AMI
// Because it retains snapshot. Following operation is deleting snapshots.
log.Printf("Deleting snapshot related to AMI (%s)", *image.ImageId)
for _, device := range image.BlockDeviceMappings {
// skip delete if use ephemeral devise
if device.Ebs == nil {
continue
}
log.Printf("Deleting snapshot (%s) related to AMI (%s)", *device.Ebs.SnapshotId, *image.ImageId)
if _, err := c.ec2conn.DeleteSnapshot(&ec2.DeleteSnapshotInput{
SnapshotId: device.Ebs.SnapshotId,
DryRun: aws.Bool(c.config.DryRun),
}); err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "DryRunOperation" {
// noop
} else {
return err
}
}
}
return nil
}
// IsUsed checks whether a passed image is used.
// If used, it returns Used instead of nil.
func (c *Cleaner) IsUsed(image *ec2.Image) *Used {
return c.used[*image.ImageId]
}
func (c *Cleaner) setInstanceUsed() error {
ret, err := c.ec2conn.DescribeInstances(&ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-state-name"),
Values: []*string{
aws.String("pending"),
aws.String("running"),
aws.String("shutting-down"),
aws.String("stopping"),
aws.String("stopped"),
},
},
},
})
if err != nil {
return err
}
for _, reservation := range ret.Reservations {
for _, instance := range reservation.Instances {
c.used[*instance.ImageId] = &Used{
ID: *instance.InstanceId,
Type: "instance",
}
}
}
return nil
}
func (c *Cleaner) setLaunchConfigurationUsed() error {
ret, err := c.autoscalingconn.DescribeLaunchConfigurations(&autoscaling.DescribeLaunchConfigurationsInput{})
if err != nil {
return err
}
for _, lc := range ret.LaunchConfigurations {
c.used[*lc.ImageId] = &Used{
ID: *lc.LaunchConfigurationName,
Type: "launch configuration",
}
}
return nil
}
func (c *Cleaner) setLaunchTemplateUsed() error {
ret, err := c.ec2conn.DescribeLaunchTemplates(&ec2.DescribeLaunchTemplatesInput{})
if err != nil {
return err
}
for _, lt := range ret.LaunchTemplates {
versions, err := c.ec2conn.DescribeLaunchTemplateVersions(&ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: lt.LaunchTemplateId,
})
if err != nil {
return err
}
for _, ltv := range versions.LaunchTemplateVersions {
c.used[*ltv.LaunchTemplateData.ImageId] = &Used{
ID: fmt.Sprintf("%s (%d)", *ltv.LaunchTemplateName, *ltv.VersionNumber),
Type: "launch template",
}
}
}
return nil
}