-
Notifications
You must be signed in to change notification settings - Fork 41
/
setup.js
318 lines (296 loc) · 9.55 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
const jsonfile = require('jsonfile');
const promptly = require('promptly');
const http = require('https');
const { LocalStorage } = require('node-localstorage');
const localStorage = new LocalStorage('./config');
const Zen = require('./zencfg');
const init = require('./init.json');
const file = './config/config.json';
const newcfg = {};
const convertConfig = () => {
// get existing values if this is setup migration
// trim in case someone enters a value by editing file
const stakeaddrLS = localStorage.getItem('stakeaddr');
const emailLS = localStorage.getItem('email');
const fqdnLS = localStorage.getItem('fqdn');
const regionLS = localStorage.getItem('region');
const ipvLS = localStorage.getItem('ipv');
const catLS = localStorage.getItem('category');
const cfgOld = {};
cfgOld.nodeid = localStorage.getItem('nodeid');
cfgOld.stakeaddr = stakeaddrLS ? stakeaddrLS.trim() : '';
cfgOld.email = emailLS ? emailLS.trim() : '';
cfgOld.fqdn = fqdnLS ? fqdnLS.trim() : '';
cfgOld.region = regionLS ? regionLS.trim() : '';
cfgOld.ipv = ipvLS ? ipvLS.trim() : '4';
cfgOld.category = catLS ? catLS.trim() : '';
if (cfgOld.stakeaddr) cfgOld.replace = true;
return { cfgOld };
};
const getconfig = (cb) => {
jsonfile.readFile(file, (err, config) => {
if (err) {
// check for previous version individual files
return cb(convertConfig());
}
return cb(config);
});
};
const regions = [];
const getSetupInfo = (serverurl, cb) => {
console.log('Retrieving server list from region....');
http.get(serverurl, (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode === 301 || statusCode === 302) {
console.log('Retrieving server list from region.... redirecting');
return getSetupInfo(res.headers.location, cb);
}
if (statusCode !== 200) {
error = new Error(`Request Failed.\nStatus Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(`Invalid content-type.\nExpected application/json but received ${contentType}`);
}
if (error) {
console.log('Unable to connect to server for setup data.');
// consume response data to free up memory
res.resume();
return cb(error.message);
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
const data = JSON.parse(rawData);
let regPrompt = '';
data.regions.forEach((r) => {
regPrompt += `${r[1]}(${r[0]}) `;
regions.push(r[0]);
});
const { servers } = data;
const serverInfo = {
regionServer: data.region,
servers,
regList: data.regions,
regPrompt,
regions,
};
newcfg.servers = servers;
return cb(null, serverInfo);
} catch (e) {
console.error(e.message);
return cb(e.message);
}
});
return null;
}).on('error', (e) => {
console.error(`Error: ${e.message}`);
console.error('Can not complete setup.');
process.exit();
});
};
const addrValidator = (value) => {
if (value.length !== 35) {
throw new Error('That does not appear to be a transparent address.');
}
return value.trim();
};
const ipValidator = (value) => {
if (value === '4') {
return value;
}
if (value === '6') {
return value;
}
throw new Error('The ip address version must be either 4 or 6.');
};
const regValidator = (value) => {
const found = regions.find((reg) => reg === value);
if (found) {
return value;
}
throw new Error('Enter one of the region codes shown in (xxx)');
};
const typeValidator = (value) => {
if (value === 'secure' || value === 'super') {
return value;
}
throw new Error('Enter secure or super');
};
const catValidator = (value) => {
if (!value) {
throw new Error('Enter \'none\' to skip');
}
return value;
};
const setHomeServer = (reg, servers) => {
let found = false;
const idx = newcfg.nodetype === 'testnet' ? 2 : 1;
for (let i = 0; i < servers.length; i += 1) {
const srv = servers[i].split('.');
if (srv[idx] === reg) {
// localStorage.setItem('home', servers[i]);
newcfg.home = servers[i];
found = true;
break;
}
}
if (!found) {
console.error('ERROR SETTING THE HOME SERVER. Please try running setup again or report the issue if it persists.');
return 'error';
}
return 'ok';
};
const saveConfig = (cfg, cfgAll) => {
const config = { ...cfgAll };
if (cfgAll.cfgOld) {
// collected from separate files. remove old
localStorage.clear();
delete config.cfgOld;
}
config.active = newcfg.nodetype;
config[newcfg.nodetype] = newcfg;
jsonfile.writeFile(file, config, { spaces: 1 }, (err) => {
if (err) {
console.error(err);
return ('failed');
}
return 'ok';
});
};
const promptUser = (cfg, cfgAll, serverInfo) => {
const region = cfg.region || serverInfo.regionServer;
const msg1 = cfg.stakeaddr ? ` (Existing: ${cfg.stakeaddr}):` : ':';
const msg2 = cfg.email ? ` (Existing: ${cfg.email}):` : ':';
const msg3 = cfg.fqdn ? ` (Existing: ${cfg.fqdn}):` : ':';
const msg4 = cfg.ipv ? ` (Existing: ${cfg.ipv}):` : ':';
const msg5 = region ? ` (Default: ${region}):` : ':';
console.log(`Configure for ${newcfg.nodetype} node`);
// Prompt user for values
promptly
.prompt(`Staking transparent address ${msg1}`, { default: cfg.stakeaddr, validator: addrValidator })
.then((stake) => {
newcfg.stakeaddr = stake;
return promptly.prompt(`Alert email address ${msg2}`, { default: cfg.email });
})
.then((em) => {
newcfg.email = em.toLowerCase().trim();
return promptly.prompt(
`Full hostname (FQDN) used in cert. example: z1.mydomain.com ${msg3}`,
{ default: cfg.fqdn },
);
})
.then((hostname) => {
newcfg.fqdn = hostname.trim();
return promptly.prompt(
`IP address version used for connection - 4 or 6 ${msg4}`,
{ default: cfg.ipv, validator: ipValidator },
);
})
.then((ipType) => {
newcfg.ipv = ipType;
return promptly.choose(
`Region - ${serverInfo.regPrompt} ${msg5}`,
regions,
{ default: region, validator: regValidator },
);
})
.then((reg) => {
newcfg.region = reg;
return setHomeServer(reg, serverInfo.servers);
})
.then((regok) => {
if (regok === 'error') process.exit();
console.log(' ');
console.log('A category may be used to uniquely identify a set of nodes.');
const options = { validator: catValidator, retry: false };
let msg6 = ':';
if (!cfg.category || (cfg.category && cfg.category === 'none')) {
msg6 = ' (Default: none):';
options.default = 'none';
} else {
console.log('Enter \'none\' if you do not want to use a category');
msg6 = ` ( Existing: ${cfg.category}):`;
options.default = cfg.category;
}
return promptly.prompt(`Optional node category - alphanumeric. ${msg6}`, options);
})
.then((cat) => {
newcfg.category = cat;
return saveConfig(cfg, cfgAll);
})
.then((msg) => {
if (msg === 'failed') {
console.log('-----Unable to save the configuration. Setup did not complete.-----');
} else {
console.log(`***Configuration for ${newcfg.nodetype} node saved. Setup complete!***`);
}
})
.catch((error) => {
console.error('ERROR: ', error.message);
});
};
// start setup
console.log('Nodetracker setup for secure and super nodes.');
console.log('Enter the value for each prompt and press the \'Enter\' key.');
console.log('Press the \'Enter\' key for defaults or existing selections');
console.log('-----------------------------------------------------------');
const zencfg = Zen.getZenConfig();
console.log(zencfg);
if (zencfg.testnet) {
console.log('Zen is running on testnet');
console.log('To run on mainnet please reconfigure zen.conf and remove or comment \'#testnet=1\'');
console.log('Continuing testnet setup');
getSetupInfo(init.servers.testnet, (err, serverInfo) => {
if (err) {
console.error('Can not complete setup.', err);
process.exit();
}
newcfg.nodetype = 'testnet';
getconfig((cfgAll) => {
let cfg = {};
if (cfgAll.cfgOld) {
newcfg.nodeid = cfgAll.cfgOld.nodeid;
cfg = cfgAll.cfgOld;
} else if (cfgAll.testnet) {
cfg = cfgAll.testnet;
if (cfg.nodeid) newcfg.nodeid = cfg.nodeid;
}
promptUser(cfg, cfgAll, serverInfo);
});
});
} else {
getconfig((cfgAll) => {
const msg1 = cfgAll.active && cfgAll.active !== 'testnet' ? ` (Existing: ${cfgAll.active}):` : ':';
promptly
.choose(
`Enter the node type - secure or super ${msg1}`,
['secure', 'super'],
{ default: cfgAll.active, validator: typeValidator },
)
.then((ntype) => {
newcfg.nodetype = ntype;
let cfg = {};
if (cfgAll.cfgOld) {
newcfg.nodeid = cfgAll.cfgOld.nodeid;
cfg = cfgAll.cfgOld;
} else if (cfgAll.active === ntype) {
cfg = cfgAll[ntype];
if (cfg.nodeid) newcfg.nodeid = cfg.nodeid;
}
getSetupInfo(init.servers[ntype], (err, serverInfo) => {
if (err) {
console.error('Can not complete setup.', err);
process.exit();
}
promptUser(cfg, cfgAll, serverInfo);
});
})
.catch((error) => {
console.error('ERROR: ', error.message);
});
});
}
// });