This repository is currently being migrated. It's locked while the migration is in progress.
forked from awslabs/amazon-redshift-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshotManager.js
337 lines (293 loc) · 9.74 KB
/
snapshotManager.js
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
/* jshint node: true */
/* jshint -W097 */
/*
* Copyright 2014, Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
'use strict';
var constants = require("./constants");
var setRegion = process.env['AWS_REGION'];
if (!setRegion) {
console.log("Setting default region us-east-1");
setRegion = "us-east-1";
}
var AWS = require("aws-sdk");
AWS.config.apiVersions = {
redshift : '2012-12-01'
};
AWS.config.update({
region : setRegion
});
var redshift = new AWS.Redshift();
var async = require('async');
var moment = require('moment');
function getFriendlyDate(t) {
return t.format(constants.dateFormat);
}
function getSnapshotId(config) {
return config.namespace + "-" + config.targetResource + "-" + getFriendlyDate(moment());
}
function getTags(config, forDate) {
var tags = [ {
Key : constants.createdByName,
Value : constants.createdByValue
}, {
Key : constants.namespaceTagName,
Value : config.namespace
} ];
if (forDate) {
tags.push({
Key : constants.createdAtName,
Value : getFriendlyDate(forDate)
});
}
return tags;
}
// function to query for snapshots created since the configuration start time
function getSnapshots(config, callback) {
var snapStartTime;
var intervalExpression;
if (config.snapshotInterval) {
snapStartTime = moment().subtract(config.snapshotInterval.duration, config.snapshotInterval.units);
intervalExpression = config.snapshotInterval.duration + " " + config.snapshotInterval.units;
} else {
snapStartTime = moment().subtract(config.snapshotIntervalHours, 'hours');
intervalExpression = config.snapshotIntervalHours + " hours";
}
exports.getSnapshotsInInterval(config.targetResource, config.namespace, snapStartTime, undefined, undefined,
function(err, snapshots) {
if (err) {
callback(err);
} else {
console.log("Resolved " + snapshots.length + " Snapshots in the last " + intervalExpression);
callback(null, config, snapshots);
}
});
}
exports.getSnapshots = getSnapshots;
function getSnapshotsInInterval(targetResource, namespace, startTime, endTime, extraSearchCriteria, callback) {
var startSearchLabel = startTime ? startTime.format() : "any";
var endSearchLabel = endTime ? endTime.format() : "any";
console.log("Requesting '" + namespace + "' Snapshots for " + targetResource + " in interval (" + startSearchLabel
+ " - " + endSearchLabel + ")");
var describeParams = {
ClusterIdentifier : targetResource,
// only search for snapshots in the current namespace
TagKeys : [ constants.namespaceTagName ],
TagValues : [ namespace ]
};
// add time search criteria
if (startTime) {
describeParams.StartTime = startTime.toDate();
}
if (endTime) {
describeParams.EndTime = endTime.toDate();
}
// add any provided extra search items
if (extraSearchCriteria) {
Object.keys(extraSearchCriteria).map(function(item) {
describeParams[item] = extraSearchCriteria[item];
});
}
// console.log(JSON.stringify(describeParams));
redshift.describeClusterSnapshots(describeParams, function(err, data) {
// got a set of snapshots, or not, so call the callback with the snap list
if (err) {
callback(err);
} else {
callback(null, data.Snapshots);
}
});
}
exports.getSnapshotsInInterval = getSnapshotsInInterval;
// function to create a manual snapshot
function createSnapshot(config, callback) {
var now = moment();
var newSnapshotId = getSnapshotId(config);
console.log("Creating new Snapshot " + newSnapshotId + " for " + config.targetResource);
var params = {
ClusterIdentifier : config.targetResource,
SnapshotIdentifier : newSnapshotId,
Tags : getTags(config, now)
};
redshift.createClusterSnapshot(params, function(err, data) {
if (err) {
callback(err);
} else {
console.log("Created new Manual Snapshot " + data.Snapshot.SnapshotIdentifier);
callback(null, config);
}
});
}
exports.createSnapshot = createSnapshot;
// function to convert an automatic snapshot into a manual snapshot
function convertAutosnapToManual(config, snapshot, callback) {
console.log("Converting Automatic Snapshot " + snapshot.SnapshotIdentifier + " to manual for long term retention");
redshift.copyClusterSnapshot({
SourceSnapshotIdentifier : snapshot.SnapshotIdentifier,
TargetSnapshotIdentifier : getSnapshotId(config),
}, function(err, data) {
if (err) {
callback(err);
} else {
// tag the converted snapshot
var params = {
ResourceName : data.SnapshotIdentifier,
Tags : getTags(config)
};
redshift.createTags(params, function(err, data) {
if (err) {
callback(err);
} else {
console.log("Converted latest Snapshot " + snapshot.SnapshotIdentifier + " to new Manual Snapshot "
+ data.SnapshotIdentifier);
callback(null, config);
}
});
}
});
}
exports.convertAutosnapToManual = convertAutosnapToManual;
// function to cleanup snapshots older than the specified retention period
function cleanupSnapshots(config, callback) {
if (!config.snapshotRetentionDays && !config.snapshotRetention) {
// customer may not have configured a snapshots retention - ok
console.log("No Snapshot retention limits configured - all Snapshots will be retained");
callback(null);
} else {
var snapEndTime;
var intervalExpression;
if (config.snapshotRetention) {
snapEndTime = moment().subtract(config.snapshotRetention.duration, config.snapshotRetention.units);
intervalExpression = config.snapshotRetention.duration + " " + config.snapshotRetention.units + " ("
+ snapEndTime.format() + ")";
} else {
snapEndTime = moment().subtract(config.snapshotRetentionDays, 'days');
intervalExpression = config.snapshotRetentionDays + " days (" + snapEndTime.format() + ")";
}
console.log("Cleaning up '" + config.namespace + "' Snapshots older than ");
exports.getSnapshotsInInterval(config.targetResource, config.namespace, undefined, snapEndTime, {
SnapshotType : "manual"
}, function(err, snapshots) {
if (err) {
callback(err);
} else {
if (snapshots && snapshots.length > 0) {
console.log("Cleaning up " + snapshots.length + " previous Snapshots");
async.map(snapshots, function(item, mapCallback) {
console.log("Deleting Snapshot " + item.SnapshotIdentifier);
// delete this manual snapshot
redshift.deleteClusterSnapshot({
SnapshotIdentifier : item.SnapshotIdentifier,
SnapshotClusterIdentifier : config.targetResource
}, function(err, data) {
if (err) {
mapCallback(err);
} else {
callback(null);
}
});
}, function(err, results) {
if (err) {
callback(err);
} else {
callback(null);
}
});
} else {
// there are no snapshots older than the retention period - so all ok
console.log("No old snapshots found to clear");
callback(null);
}
}
});
}
}
exports.cleanupSnapshots = cleanupSnapshots;
/*
* function which determines if we need to create or convert snapshots based on
* the snapshots that currently exist
*/
function createOrConvertSnapshots(config, snapshotList, callback) {
if (snapshotList.length === 0) {
// create a new snapshot
createSnapshot(config, callback);
} else {
// convert the latest auto snapshot created within the specified
// period to a manual snapshot
var manualExists = false;
snapshotList.map(function(item) {
if (item.SnapshotType === "manual") {
manualExists = true;
}
});
// only do an auto conversion if there are no manual snapshots in
// the period
if (!manualExists) {
convertAutosnapToManual(config, snapshotList[snapshotList.length - 1], callback);
} else {
callback(null, config);
}
}
}
exports.createOrConvertSnapshots = createOrConvertSnapshots;
function validateConfig(config, callback) {
var error;
if (!config.namespace || config.namespace === "") {
error = "You must provide a configuration namespace";
}
if (config.snapshotInterval && constants.validTimeUnits.indexOf(config.snapshotInterval.units) == -1) {
error = "Snapshot Interval Units must be one of: " + JSON.stringify(constants.validTimeUnits);
}
if (config.snapshotRetention && constants.validTimeUnits.indexOf(config.snapshotRetention.units) == -1) {
error = "Snapshot Retention Units must be one of: " + JSON.stringify(constants.validTimeUnits);
}
callback(error);
}
exports.validateConfig = validateConfig;
// processor entry point
function run(config, callback) {
var checksPassed = true;
var error;
if (!config.targetResource) {
checksPassed = false;
error = "Unable to resolve target resource from provided config.targetResource";
}
if (!config.namespace) {
checksPassed = false;
error = "Configuration namespace must be provided";
}
console.log("Configuration Provided: " + JSON.stringify(config));
if (!checksPassed) {
console.log(JSON.stringify(config));
callback(error);
} else {
validateConfig(config, function(err) {
if (err) {
callback(err);
} else {
async.waterfall([
// check for whether we have a snapshot taken within the required period
getSnapshots.bind(undefined, config),
// process the list of automatic snapshots, and determine if we need to
// create additional snaps
createOrConvertSnapshots.bind(undefined),
// now cleanup the existing snapshots
cleanupSnapshots.bind(undefined) ], function(err) {
callback(err);
});
}
});
}
}
exports.run = run;