-
Notifications
You must be signed in to change notification settings - Fork 163
/
setup.js
458 lines (410 loc) · 13.7 KB
/
setup.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Copyright 2014-2015 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, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/**
* Ask questions of the end user via STDIN and then setup the DynamoDB table
* entry for the configuration when done
*/
var pjson = require('./package.json');
var readline = require('readline');
var aws = require('aws-sdk');
require('./constants');
var common = require('./common');
var async = require('async');
var uuid = require('uuid');
var dynamoDB;
var s3;
var lambda;
var kmsCrypto = require('./kmsCrypto');
var setRegion;
dynamoConfig = {
TableName: configTable,
Item: {
currentBatch: {
S: uuid.v4()
},
version: {
S: pjson.version
},
loadClusters: {
L: [{
M: {}
}]
}
}
};
/* configuration of question prompts and config assignment */
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var qs = [];
q_region = function (callback) {
rl.question('Enter the Region for the Configuration > ', function (answer) {
if (common.blank(answer) !== null) {
setRegion = answer.toLowerCase();
// configure dynamo db, kms, s3 and lambda for the correct region
dynamoDB = new aws.DynamoDB({
apiVersion: '2012-08-10',
region: setRegion
});
kmsCrypto.setRegion(setRegion);
s3 = new aws.S3({
apiVersion: '2006-03-01'
});
lambda = new aws.Lambda({
apiVersion: '2015-03-31',
region: setRegion
});
callback(null);
}
});
};
q_s3Prefix = function (callback) {
rl.question('Enter the S3 Bucket & Prefix to watch for files > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide an S3 Bucket Name, and optionally a Prefix', rl);
// setup prefix to be * if one was not provided
var stripped = answer.replace(new RegExp('s3://', 'g'), '');
var elements = stripped.split("/");
var setPrefix = undefined;
if (elements.length === 1) {
// bucket only so use "bucket" alone
setPrefix = elements[0];
} else {
// right trim "/"
setPrefix = stripped.replace(/\/$/, '');
}
dynamoConfig.Item.s3Prefix = {
S: setPrefix
};
callback(null);
});
};
q_filenameFilter = function (callback) {
rl.question('Enter a Filename Filter Regex > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.filenameFilterRegex = {
S: answer
};
}
callback(null);
});
};
q_clusterEndpoint = function (callback) {
rl.question('Enter the Cluster Endpoint > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Cluster Endpoint', rl);
dynamoConfig.Item.loadClusters.L[0].M.clusterEndpoint = {
S: answer
};
callback(null);
});
};
q_clusterPort = function (callback) {
rl.question('Enter the Cluster Port > ', function (answer) {
dynamoConfig.Item.loadClusters.L[0].M.clusterPort = {
N: '' + common.getIntValue(answer, rl)
};
callback(null);
});
};
q_clusterUseSSL = function (callback) {
rl.question('Does your cluster use SSL (Y/N) > ', function (answer) {
dynamoConfig.Item.loadClusters.L[0].M.useSSL = {
BOOL: common.getBooleanValue(answer)
};
callback(null);
});
};
q_clusterDB = function (callback) {
rl.question('Enter the Database Name > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.loadClusters.L[0].M.clusterDB = {
S: answer
};
}
callback(null);
});
};
q_userName = function (callback) {
rl.question('Enter the Database Username > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Username', rl);
dynamoConfig.Item.loadClusters.L[0].M.connectUser = {
S: answer
};
callback(null);
});
};
q_userPwd = function (callback) {
rl.question('Enter the Database Password (will be displayed, but encrypted before storing) > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Password', rl);
kmsCrypto.encrypt(answer, function (err, ciphertext) {
if (err) {
console.log(JSON.stringify(err));
process.exit(ERROR);
} else {
dynamoConfig.Item.loadClusters.L[0].M.connectPassword = {
S: kmsCrypto.toLambdaStringFormat(ciphertext)
};
callback(null);
}
});
});
};
q_table = function (callback) {
rl.question('Enter the Table to be Loaded > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Table Name', rl);
dynamoConfig.Item.loadClusters.L[0].M.targetTable = {
S: answer
};
callback(null);
});
};
q_columnList = function (callback) {
rl.question('Enter the comma-delimited column list (optional) > ', function (answer) {
if (answer && answer !== null && answer !== "") {
dynamoConfig.Item.loadClusters.L[0].M.columnList = {
S: answer
};
callback(null);
} else {
callback(null);
}
});
};
q_truncateTable = function (callback) {
rl.question('Should the Table be Truncated before Load? (Y/N) > ', function (answer) {
dynamoConfig.Item.loadClusters.L[0].M.truncateTarget = {
BOOL: common.getBooleanValue(answer)
};
callback(null);
});
};
q_df = function (callback) {
rl.question('Enter the Data Format (CSV, JSON, AVRO, PARQUET, and ORC) > ', function (answer) {
common.validateArrayContains(['CSV', 'JSON', 'AVRO', 'PARQUET', 'ORC'], answer.toUpperCase(), rl);
dynamoConfig.Item.dataFormat = {
S: answer.toUpperCase()
};
callback(null);
});
};
q_csvDelimiter = function (callback) {
if (dynamoConfig.Item.dataFormat.S === 'CSV') {
rl.question('Enter the CSV Delimiter > ', function (answer) {
common.validateNotNull(answer, 'You Must the Delimiter for CSV Input', rl);
dynamoConfig.Item.csvDelimiter = {
S: answer
};
callback(null);
});
} else {
callback(null);
}
};
q_ignoreCsvHeader = function (callback) {
rl.question('Ignore Header (first line) of the CSV file? (Y/N) > ', function (answer) {
dynamoConfig.Item.ignoreCsvHeader = {
BOOL: common.getBooleanValue(answer)
};
callback(null);
});
};
q_jsonPaths = function (callback) {
if (dynamoConfig.Item.dataFormat.S === 'JSON' || dynamoConfig.Item.dataFormat.S === 'AVRO') {
rl.question('Enter the JSON Paths File Location on S3 (or NULL for Auto) > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.jsonPath = {
S: answer
};
}
callback(null);
});
} else {
callback(null);
}
};
q_manifestBucket = function (callback) {
rl.question('Enter the S3 Bucket for Redshift COPY Manifests > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Bucket Name for Manifest File Storage', rl);
dynamoConfig.Item.manifestBucket = {
S: answer
};
callback(null);
});
};
q_manifestPrefix = function (callback) {
rl.question('Enter the Prefix for Redshift COPY Manifests > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Prefix for Manifests', rl);
dynamoConfig.Item.manifestKey = {
S: answer
};
callback(null);
});
};
q_failedManifestPrefix = function (callback) {
rl.question('Enter the Prefix to use for Failed Load Manifest Storage (must differ from the initial manifest path) > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Prefix for Manifests', rl);
dynamoConfig.Item.failedManifestKey = {
S: answer
};
callback(null);
});
};
q_accessKey = function (callback) {
rl.question('Enter the Access Key used by Redshift to get data from S3. If NULL then Lambda execution role credentials will be used > ', function (answer) {
if (!answer) {
callback(null);
} else {
dynamoConfig.Item.accessKeyForS3 = {
S: answer
};
callback(null);
}
});
};
q_secretKey = function (callback) {
rl.question('Enter the Secret Key used by Redshift to get data from S3. If NULL then Lambda execution role credentials will be used > ', function (answer) {
if (!answer) {
callback(null);
} else {
kmsCrypto.encrypt(answer, function (err, ciphertext) {
if (err) {
console.log(JSON.stringify(err));
process.exit(ERROR);
} else {
dynamoConfig.Item.secretKeyForS3 = {
S: kmsCrypto.toLambdaStringFormat(ciphertext)
};
callback(null);
}
});
}
});
};
q_symmetricKey = function (callback) {
rl.question('If Encrypted Files are used, Enter the Symmetric Master Key Value > ', function (answer) {
if (answer && answer !== null && answer !== "") {
kmsCrypto.encrypt(answer, function (err, ciphertext) {
if (err) {
console.log(JSON.stringify(err));
process.exit(ERROR);
} else {
dynamoConfig.Item.masterSymmetricKey = {
S: kmsCrypto.toLambdaStringFormat(ciphertext)
};
callback(null);
}
});
} else {
callback(null);
}
});
};
q_failureTopic = function (callback) {
rl.question('Enter the SNS Topic ARN for Failed Loads > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.failureTopicARN = {
S: answer
};
}
callback(null);
});
};
q_successTopic = function (callback) {
rl.question('Enter the SNS Topic ARN for Successful Loads > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.successTopicARN = {
S: answer
};
}
callback(null);
});
};
q_batchSize = function (callback) {
rl.question('How many files should be buffered before loading? > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.batchSize = {
N: '' + common.getIntValue(answer, rl)
};
}
callback(null);
});
};
q_batchTimeoutSecs = function (callback) {
rl.question('How old should we allow a Batch to be before loading (seconds)? > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.batchTimeoutSecs = {
N: '' + common.getIntValue(answer, rl)
};
}
callback(null);
});
};
q_batchBytes = function (callback) {
rl.question('Batches can be buffered up to a specified size. How large should a batch be before processing (bytes)? > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.batchSizeBytes = {
N: '' + common.getIntValue(answer, rl)
};
}
callback(null);
});
};
q_copyOptions = function (callback) {
rl.question('Additional Copy Options to be added > ', function (answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.copyOptions = {
S: answer
};
}
callback(null);
});
};
last = function (callback) {
rl.close();
exports.setup(dynamoConfig, callback);
};
// export the setup module so that customers can programmatically add new
// configurations
setup = function (useConfig, callback) {
console.log(JSON.stringify(useConfig));
common.setup(useConfig, dynamoDB, s3, lambda, callback);
};
exports.setup = setup;
qs.push(q_region);
qs.push(q_s3Prefix);
qs.push(q_filenameFilter);
qs.push(q_clusterEndpoint);
qs.push(q_clusterPort);
qs.push(q_clusterUseSSL);
qs.push(q_clusterDB);
qs.push(q_table);
qs.push(q_columnList);
qs.push(q_truncateTable);
qs.push(q_userName);
qs.push(q_userPwd);
qs.push(q_df);
qs.push(q_csvDelimiter);
qs.push(q_ignoreCsvHeader);
qs.push(q_jsonPaths);
qs.push(q_manifestBucket);
qs.push(q_manifestPrefix);
qs.push(q_failedManifestPrefix);
qs.push(q_accessKey);
qs.push(q_secretKey);
qs.push(q_successTopic);
qs.push(q_failureTopic);
qs.push(q_batchSize);
qs.push(q_batchBytes);
qs.push(q_batchTimeoutSecs);
qs.push(q_copyOptions);
qs.push(q_symmetricKey);
// always have to have the 'last' function added to halt the readline channel
// and run the setup
qs.push(last);
// call the first function in the function list, to invoke the callback
// reference chain
async.waterfall(qs);