-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsetup.js
More file actions
276 lines (240 loc) · 6.98 KB
/
setup.js
File metadata and controls
276 lines (240 loc) · 6.98 KB
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
/*
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('./lib/constants');
var common = require('./lib/common');
var async = require('async');
var uuid = require('node-uuid');
var dynamoDB;
var kmsCrypto = require('./lib/kmsCrypto');
var setRegion;
dynamoConfig = {
TableName : configTable,
Item : {
currentBatch : {
S : uuid.v4()
},
version : {
S : pjson.version
},
loadRDS : {
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) {
common.validateArrayContains([ "ap-northeast-1", "ap-southeast-1", "ap-southeast-2", "eu-central-1", "eu-west-1", "sa-east-1", "us-east-1", "us-west-1", "us-west-2" ],
answer.toLowerCase(), rl);
setRegion = answer.toLowerCase();
// configure dynamo db and kms for the correct region
dynamoDB = new aws.DynamoDB({
apiVersion : '2012-08-10',
region : setRegion
});
kmsCrypto.setRegion(setRegion);
callback(null);
}
});
};
q_s3Prefix = function(callback) {
rl.question('Enter the S3 Bucket > ', function(answer) {
common.validateNotNull(answer, 'You Must Provide an S3 Bucket Name', 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_rdsHost = function(callback) {
rl.question('Enter the RDS Host > ', function(answer) {
common.validateNotNull(answer, 'You Must Provide an RDS Host', rl);
dynamoConfig.Item.loadRDS.L[0].M.rdsHost = {
S : answer
};
callback(null);
});
};
q_rdsPort = function(callback) {
rl.question('Enter the RDS Port > ', function(answer) {
dynamoConfig.Item.loadRDS.L[0].M.rdsPort = {
N : '' + common.getIntValue(answer, rl)
};
callback(null);
});
};
q_rdsDB = function(callback) {
rl.question('Enter the Database Name > ', function(answer) {
if (common.blank(answer) !== null) {
dynamoConfig.Item.loadRDS.L[0].M.rdsDB = {
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.loadRDS.L[0].M.connectUser = {
S : answer
};
callback(null);
});
};
q_userPwd = function(callback) {
rl.question('Enter the Database Password > ', 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.loadRDS.L[0].M.connectPassword = {
S : kmsCrypto.toLambdaStringFormat(ciphertext)
};
callback(null);
}
});
});
};
q_schema = function(callback) {
rl.question('Enter the Schema (optional) > ', function(answer) {
if (answer && answer !== null && answer !== "") {
dynamoConfig.Item.loadRDS.L[0].M.targetSchema = {
S : answer
};
callback(null);
} else {
callback(null);
}
});
};
q_tablePrefix = function(callback) {
rl.question('Enter table prefix (optional but recommended if keys start with numericals) > ', function(answer) {
if (answer && answer !== null && answer !== "") {
dynamoConfig.Item.loadRDS.L[0].M.tablePrefix = {
S : answer
};
callback(null);
} else {
callback(null);
}
});
};
q_folderDepthLevelForTableName = function(callback) {
rl.question('Enter the folder depth from bucket root to use as table name. Use negative index to select from the input file > ', function(answer) {
dynamoConfig.Item.folderDepthLevelForTableName = {
N : '' + common.getIntValue(answer, rl)
};
callback(null);
});
};
q_truncateTable = function(callback) {
rl.question('Should the Table be Truncated before Load? (Y/N) > ', function(answer) {
dynamoConfig.Item.loadRDS.L[0].M.truncateTarget = {
BOOL : common.getBooleanValue(answer)
};
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);
//}
};
last = function(callback) {
rl.close();
setup(null, callback);
};
q_useSingleTable = function(callback) {
rl.question('Should the load utilize a single table for loads? (Y/N) > ', function(answer) {
dynamoConfig.Item.loadRDS.L[0].M.useSingleTable = {
BOOL : common.getBooleanValue(answer)
};
callback(null);
});
};
setup = function(overrideConfig, callback) {
// set which configuration to use
var useConfig = undefined;
if (overrideConfig) {
useConfig = overrideConfig;
} else {
useConfig = dynamoConfig;
}
var configWriter = common.writeConfig(setRegion, dynamoDB, useConfig, callback);
common.createTables(dynamoDB, configWriter);
};
// export the setup module so that customers can programmatically add new
// configurations
exports.setup = setup;
qs.push(q_region);
qs.push(q_s3Prefix);
qs.push(q_filenameFilter);
qs.push(q_rdsHost);
qs.push(q_rdsPort);
qs.push(q_rdsDB);
qs.push(q_schema);
qs.push(q_tablePrefix);
qs.push(q_useSingleTable);
qs.push(q_folderDepthLevelForTableName);
qs.push(q_truncateTable);
qs.push(q_userName);
qs.push(q_userPwd);
qs.push(q_csvDelimiter);
// 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);