-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
executable file
·361 lines (327 loc) · 9.16 KB
/
index.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env node
"use strict";
const
commandLineArgs = require("command-line-args"),
getUsage = require("command-line-usage"),
Logger = require("js-logger"),
SnapToS3 = require("./lib/snap-to-s3.js"),
SnapCostAnalysis = require("./lib/snap-cost-analysis");
class OptionsError extends Error {
constructor(message) {
super(message);
}
}
const
commonOptions = [
{
name: "help",
type: Boolean,
description: "Show this page\n"
},
{
name: "tag",
type: String,
defaultValue: "snap-to-s3",
typeLabel: "{underline name}",
description: "Name of tag you have used to mark snapshots for migration, and to mark created EBS temporary volumes (default: $default)"
},
{
name: "bucket",
type: String,
required: true,
typeLabel: "{underline name}",
description: "S3 bucket to upload to (required)"
},
{
name: "mount-point",
type: String,
required: true,
defaultValue: "/mnt",
typeLabel: "{underline path}",
description: "Temporary volumes will be mounted here, created if it doesn't already exist (default: $default)"
},
{
name: "keep-temp-volumes",
type: Boolean,
defaultValue: false,
description: "Don't delete temporary volumes after we're done with them"
},
{
name: "volume-type",
type: String,
defaultValue: "auto",
typeLabel: "{underline type}",
description: "Volume type to use for temporary EBS volumes (e.g. standard or gp2, by default will use standard for <=1TiB volumes and gp2 for larger)"
}
],
migrateOptions = [
{
name: "migrate",
type: Boolean,
defaultValue: false,
description: "Migrate EBS snapshots to S3"
},
{
name: "all",
type: Boolean,
defaultValue: false,
description: "Migrate all snapshots whose tag is set to \"migrate\""
},
{
name: "one",
type: Boolean,
defaultValue: false,
description: "... or migrate any one snapshot whose tag is set to \"migrate\""
},
{
name: "snapshots",
type: String,
multiple: true,
typeLabel: "{underline SnapshotId} ...",
description: "... or provide an explicit list of snapshots to migrate (tags are ignored)"
},
{
name: "upload-streams",
type: Number,
defaultValue: 1,
typeLabel: "{underline num}",
description: "Number of simultaneous streams to send to S3 (increases memory usage, default: $default)"
},
{
name: "compression",
type: String,
defaultValue: "lz4",
typeLabel: "{underline name}",
description: "Compression type to use (lz4 or zstd, default: $default)"
},
{
name: "compression-level",
type: Number,
defaultValue: 1,
typeLabel: "{underline level}",
description: "Compression level (1-9 for lz4, 1-19 for zstd, default: $default)"
},
{
name: "dd",
type: Boolean,
defaultValue: false,
description: "Use dd to create a raw image of the entire volume, instead of tarring up the files of each partition"
},
{
name: "sse",
type: String,
typeLabel: "{underline mode}",
description: "Enables server-side encryption, valid modes are AES256 and aws:kms"
},
{
name: "sse-kms-key-id",
type: String,
requireNotEmpty: true,
typeLabel: "{underline id}",
description: "KMS key ID to use for aws:kms encryption, if not using the S3 master KMS key"
},
{
name: "gpg-recipient",
type: String,
requireNotEmpty: true,
lazyMultiple: true,
typeLabel: "{underline KeyName/KeyID}",
description: "Encrypt the image for the given GPG recipient (add multiple times for multiple recipients)"
}
],
validateOptions = [
{
name: "validate",
type: Boolean,
defaultValue: false,
description: "Validate uploaded snapshots from S3 against the original EBS snapshots (can be combined with --migrate)"
},
{
name: "gpg-session-key",
type: String,
requireNotEmpty: true,
typeLabel: "{underline key}",
description: "For snapshot validation, see readme for details"
}
],
// There are just provided for display to make it obvious that --all --one and --snapshots apply to --validate too
validateOptionsForDisplayOnly = [
{
name: "all",
type: Boolean,
defaultValue: false,
description: "Validate all snapshots whose tag is set to \"migrated\""
},
{
name: "one",
type: Boolean,
defaultValue: false,
description: "... or validate any one snapshot whose tag is set to \"migrated\""
},
{
name: "snapshots",
type: String,
multiple: true,
typeLabel: "{underline SnapshotId} ...",
description: "... or provide an explicit list of snapshots to validate (tags are ignored)"
}
],
analyzeOptions = [
{
name: "analyze",
typeLabel: "{underline filename}",
description: "Analyze an AWS Cost and Usage report to find opportunities for savings"
}
],
usageSections = [
{
header: "snap-to-s3",
content: "Creates EBS volumes from snapshots, tars up their files, compresses them with LZ4/ZSTD, and uploads them to S3."
},
{
header: "Migrate snapshots to S3",
optionList: migrateOptions
},
{
header: "Validate uploaded snapshots",
optionList: validateOptions.concat(validateOptionsForDisplayOnly)
},
{
header: "Analyze AWS Cost and Usage reports",
optionList: analyzeOptions
},
{
header: "Common options",
optionList: commonOptions
},
{
header: ""
}
],
allOptions = commonOptions.concat(migrateOptions, validateOptions, analyzeOptions);
let
options;
Logger.useDefaults();
for (let option of allOptions) {
if (option.description) {
option.description = option.description.replace("$default", option.defaultValue);
}
}
try {
// Parse command-line options
options = commandLineArgs(allOptions)
} catch (e) {
Logger.error(e.message);
options = null;
}
if (options === null || options.help || process.argv.length <= 2) {
console.log(getUsage(usageSections));
} else {
Promise.resolve().then(function() {
if (options.analyze !== undefined) {
let
analysis = new SnapCostAnalysis(options);
return analysis.analyzeReport(options.analyze);
}
for (let option of allOptions) {
if (option.required && options[option.name] === undefined) {
throw new OptionsError("Option --" + option.name + " is required!");
}
if ((option.required || option.requireNotEmpty) && options[option.name] === null) {
throw new OptionsError("Option --" + option.name + " requires an argument!");
}
}
let
subjectCount = 0;
if (options.snapshots && options.snapshots.length !== 0) {
subjectCount++;
}
if (options.all) {
subjectCount++;
}
if (options.one) {
subjectCount++;
}
if (subjectCount !== 1) {
throw new OptionsError("You must supply exactly one of --snapshots, --all or --one options");
}
if (!options.migrate && !options.validate) {
throw new OptionsError("You must supply at least one of --migrate or --validate");
}
if (options.sse === "aes256") {
// Be nice and capitalise things for the user
options.sse = "AES256";
} else if (options.sse === null) {
// If the user specified --sse, but didn't specify an algorithm, default to AES256 like AWS CLI would
options.sse = "AES256";
}
let
snap = new SnapToS3(options);
if (options.migrate) {
let
promise;
if (options.all) {
promise = snap.migrateAllTaggedSnapshots();
} else if (options.one) {
promise = snap.migrateOneTaggedSnapshot();
} else {
promise = snap.migrateSnapshots(options.snapshots);
}
return promise.then(
migrated => {
if (migrated.length === 0) {
Logger.error("No snapshots to migrate (snapshots must have tag \"" + options.tag + "\" set to \"migrate\" to be eligible)");
}
}
);
} else if (options.validate) {
let
promise;
if (options.all) {
promise = snap.validateAllTaggedSnapshots();
} else if (options.one) {
promise = snap.validateOneTaggedSnapshot();
} else {
promise = snap.validateSnapshots(options.snapshots);
}
return promise.then(
successes => {
if (successes.length === 0) {
Logger.error("No snapshots to validate (snapshots must have tag \"" + options.tag + "\" set to \"migrated\" to be eligible)");
} else {
Logger.info("");
Logger.info("These snapshots validated successfully:\n" + successes.join("\n"));
}
},
error => {
if (error instanceof SnapToS3.SnapshotValidationError) {
Logger.info("");
if (error.successes.length > 0) {
Logger.info("These snapshots validated successfully:\n" + error.successes.join("\n") + "\n");
}
Logger.error("These snapshots failed to validate:\n" + Object.keys(error.failures).map(snapshotID => snapshotID + ": " + error.failures[snapshotID]).join("\n\n"));
process.exitCode = 1;
} else {
throw error;
}
}
);
}
})
.catch(err => {
process.exitCode = 1;
if (err instanceof OptionsError) {
Logger.error(err.message);
} else if (err instanceof SnapToS3.SnapshotMigrationError) {
Logger.get(err.snapshotID).error(err);
Logger.error("");
Logger.error("Terminating due to fatal errors.");
} else if (err instanceof SnapToS3.SnapshotsMissingError) {
Logger.error(err);
} else {
Logger.error(err);
Logger.error("");
Logger.error("Terminating due to fatal errors.");
}
});
}