This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.go
398 lines (348 loc) · 9.66 KB
/
ec2.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
package ec2
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
awsec2 "github.com/aws/aws-sdk-go/service/ec2"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/grid-x/aws-auto-snapshot/pkg/datastore"
)
const (
defaultBackupTag = "backup"
defaultRetentionTag = "retention"
defaultSnapshotSuffix = "auto-snapshot"
defaultDeleteAfterTag = "_DELETE_AFTER"
defaultRetentionDays = 7 // Default are 7 days retention
defaultDescription = "auto snapshot created by grid-x/aws-auto-snapshot"
)
var (
describeVolumesRequets = prometheus.NewCounter(prometheus.CounterOpts{
Name: "ec2_describe_volumes_requests_total",
Help: "Total number of describe volumes requests",
})
describeSnapshotsRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "ec2_describe_snapshots_requests_total",
Help: "Total number of describe snapshots requests",
})
createSnapshotRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "ec2_create_snapshot_requests_total",
Help: "Total number of create snapshot requests",
})
createTagsRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "ec2_create_tags_requests_total",
Help: "Total number of create tags requests",
})
deleteSnapshotRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "ec2_delete_snapshot_requests_total",
Help: "Total number of delete snapshot requests",
})
)
func init() {
prometheus.MustRegister(describeVolumesRequets)
prometheus.MustRegister(describeSnapshotsRequests)
prometheus.MustRegister(createSnapshotRequests)
prometheus.MustRegister(createTagsRequests)
prometheus.MustRegister(deleteSnapshotRequests)
}
// SnapshotManager manages the snapshot creation and pruning of EC2 EBS-based
// snapshots
type SnapshotManager struct {
client *awsec2.EC2
volumeID string
suffix string // snapshot suffix
backupTag string
retentionTag string
deleteAfterTag string
logger log.FieldLogger
datastore datastore.Datastore
}
// Opt is the type for Options of the SnapshotManager
type Opt func(*SnapshotManager)
// WithRetentionTag sets the retention tag key
func WithRetentionTag(t string) Opt {
return func(m *SnapshotManager) {
m.retentionTag = t
}
}
// WithBackupTag sets the backup tag key
func WithBackupTag(t string) Opt {
return func(m *SnapshotManager) {
m.backupTag = t
}
}
// WithSnapshotSuffix sets the automated snapshot suffix
func WithSnapshotSuffix(suf string) Opt {
return func(m *SnapshotManager) {
m.suffix = suf
}
}
// WithDeleteAfterTag sets the tag key to be used for indication the deletion
// date
func WithDeleteAfterTag(tag string) Opt {
return func(m *SnapshotManager) {
m.deleteAfterTag = tag
}
}
// NewSnapshotManager creates a new SnapshotManager given an EC2 client and a
// set of Opts
func NewSnapshotManager(client *awsec2.EC2, datastore datastore.Datastore, opts ...Opt) *SnapshotManager {
smgr := &SnapshotManager{
client: client,
suffix: defaultSnapshotSuffix,
retentionTag: defaultRetentionTag,
backupTag: defaultBackupTag,
deleteAfterTag: defaultDeleteAfterTag,
logger: log.New().WithFields(
log.Fields{
"component": "ec2-snapshot-manager",
},
),
datastore: datastore,
}
for _, o := range opts {
o(smgr)
}
return smgr
}
func (smgr *SnapshotManager) fetchVolumes(ctx context.Context) ([]*awsec2.Volume, error) {
var result []*awsec2.Volume
var token *string
for {
in := &awsec2.DescribeVolumesInput{}
if token != nil {
in.NextToken = token
}
// Filter so we get only volumes that have the Backup tag set
in.SetFilters([]*awsec2.Filter{
{
Name: aws.String("tag-key"),
Values: []*string{
aws.String(smgr.backupTag),
aws.String(strings.ToLower(smgr.backupTag)), // we are not case sensitive
},
},
})
resp, err := smgr.client.DescribeVolumesWithContext(ctx, in)
describeVolumesRequets.Inc()
if err != nil {
return nil, err
}
for _, volume := range resp.Volumes {
if volume.VolumeId == nil {
//skip
continue
}
result = append(result, volume)
}
if resp.NextToken == nil {
break
}
token = resp.NextToken
}
return result, nil
}
func (smgr *SnapshotManager) fetchSnapshots(ctx context.Context) ([]*awsec2.Snapshot, error) {
var result []*awsec2.Snapshot
var token *string
for {
in := &awsec2.DescribeSnapshotsInput{}
if token != nil {
in.NextToken = token
}
// Filter so we get only volumes that have the Backup tag set
in.SetFilters([]*awsec2.Filter{
{
Name: aws.String("tag-key"),
Values: []*string{
aws.String(smgr.deleteAfterTag),
},
},
})
resp, err := smgr.client.DescribeSnapshotsWithContext(ctx, in)
describeSnapshotsRequests.Inc()
if err != nil {
return nil, err
}
for _, snap := range resp.Snapshots {
if snap.SnapshotId == nil {
//skip
continue
}
result = append(result, snap)
}
if resp.NextToken == nil {
break
}
token = resp.NextToken
}
return result, nil
}
// Snapshot creates EBS snapshots for all matching EBS volumes, i.e. all EBS
// volumes having a Backup tag and optionally a retention tag set
func (smgr *SnapshotManager) Snapshot(ctx context.Context) error {
volumes, err := smgr.fetchVolumes(ctx)
if err != nil {
return err
}
for _, volume := range volumes {
// For each volume it should at most take 5 minutes
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
snapshotName := fmt.Sprintf("%s-%d-%s",
*volume.VolumeId,
time.Now().UnixNano(),
smgr.suffix,
)
logger := smgr.logger.WithFields(
log.Fields{
"volume-id": volume.VolumeId,
"snapshot-name": snapshotName,
},
)
var days int64
for _, tag := range volume.Tags {
if tag.Key == nil {
continue
}
if strings.ToLower(*tag.Key) == strings.ToLower(smgr.retentionTag) {
if tag.Value == nil {
logger.Warnf("Retention tag value is nil")
continue
}
days, err = strconv.ParseInt(*tag.Value, 10, 64)
if err != nil {
logger.Warnf("Couldn't parse retention days: %+v. Falling back to default value", err)
days = defaultRetentionDays // if error occurs fall back to default retention time
}
break
}
}
if days == 0 {
days = defaultRetentionDays
}
created := time.Now()
deleteAfter := created.Add(time.Duration(days) * 24 * time.Hour)
logger.Infof("Creating snapshot with name %s", snapshotName)
snapshot, err := smgr.client.CreateSnapshotWithContext(
ctx,
&awsec2.CreateSnapshotInput{
VolumeId: volume.VolumeId,
Description: aws.String(defaultDescription),
},
)
createSnapshotRequests.Inc()
if err != nil {
logger.Error(err)
continue
}
if snapshot.SnapshotId == nil {
logger.Errorf("Snapshot ID is nil.")
continue
}
tags := []*awsec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(snapshotName),
},
{
Key: aws.String(smgr.deleteAfterTag),
Value: aws.String(deleteAfter.Format(time.RFC3339)),
},
}
for _, t := range volume.Tags {
if t.Key != nil && *t.Key == "Name" {
tags = append(tags, &awsec2.Tag{
Key: aws.String("volume-name"),
Value: t.Value,
})
break
}
}
if _, err := smgr.client.CreateTagsWithContext(
ctx,
&awsec2.CreateTagsInput{
Resources: []*string{
snapshot.SnapshotId,
},
Tags: tags,
},
); err != nil {
logger.Error(err)
continue
}
createTagsRequests.Inc()
if err := smgr.datastore.StoreSnapshotInfo(&datastore.SnapshotInfo{
Resource: datastore.SnapshotResource(*volume.VolumeId),
ID: datastore.SnapshotID(*snapshot.SnapshotId),
// The createdAt timestamp is used as a key for ordering
// in the datatstore. Hence we need to ensure it is
// stable. To avoid problems let's truncate it to one
// minute
CreatedAt: (*snapshot.StartTime).Truncate(time.Minute),
}); err != nil {
logger.Error(err)
continue
}
}
return nil
}
// Prune deletes all matching EBS snapshots, i.e. snapshots with a delete after
// tag that is set to a date in the past
func (smgr *SnapshotManager) Prune(ctx context.Context) error {
snaps, err := smgr.fetchSnapshots(ctx)
if err != nil {
return err
}
for _, snap := range snaps {
smgr.logger.Infof("Processing snapshot %s", *snap.SnapshotId)
for _, tag := range snap.Tags {
if tag.Key == nil {
continue
}
if *tag.Key == smgr.deleteAfterTag {
// add context to the logger
logger := smgr.logger.WithFields(log.Fields{
"snapshotID": *snap.SnapshotId,
})
if tag.Value == nil {
logger.Errorf("Delete after tag value is nil")
continue
}
deleteAfter, err := time.Parse(time.RFC3339, *tag.Value)
if err != nil {
logger.Error("Couldn't parse tag value for : %+v", err)
break
}
if time.Now().Before(deleteAfter) {
logger.Info("Snapshot not yet scheduled for deletion")
break
}
if _, err := smgr.client.DeleteSnapshotWithContext(ctx, &awsec2.DeleteSnapshotInput{
SnapshotId: snap.SnapshotId,
}); err != nil {
logger.Error("Couldn't delete snapshot: %+v", err)
break
}
deleteSnapshotRequests.Inc()
logger.Info("Successfully deleted snapshot")
if err := smgr.datastore.DeleteSnapshotInfo(&datastore.SnapshotInfo{
Resource: datastore.SnapshotResource(*snap.VolumeId),
ID: datastore.SnapshotID(*snap.SnapshotId),
// The createdAt timestamp is used as a key for ordering
// in the datatstore. Hence we need to ensure it is
// stable. To avoid problems it was truncated to one
// minute during creation above
CreatedAt: (*snap.StartTime).Truncate(time.Minute),
}); err != nil {
smgr.logger.Error(err)
}
break
}
}
}
return nil
}