-
Notifications
You must be signed in to change notification settings - Fork 2
/
juke.js
executable file
·357 lines (328 loc) · 11.7 KB
/
juke.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
'use strict';
// grouped full modules a-z
const DigitalOceanApi = require('doapi');
const NodeSSH = require('node-ssh');
const inquirer = require('inquirer');
// grouped module methods a-z
const Readable = require('stream').Readable;
// grouped global constants, a-z
// path of configuration files that need to be edited on remote server
const configurations = {
shadowsocks: {
'config.json': '/etc/shadowsocks-libev/config.json',
},
};
const prompt = inquirer.createPromptModule();
const promptQuestions = [{
type: 'input',
name: 'do_api_token',
message: 'Digital Ocean token:',
}, {
type: 'input',
name: 'ssh_key_location',
message: 'Name of SSH key in ~/.ssh/ to connect to Streisand droplet:',
}, {
type: 'input',
name: 'domain',
message: 'domain tied to Streisand:',
}];
const ssh = new NodeSSH();
const time = new Date();
let client;
let sshKey;
// gather required info from user
function getSessionInfo() {
const sessionInfo = {
do_api_token: null,
ssh_key_location: null,
};
return prompt(promptQuestions).then((answers) => {
sessionInfo.do_api_token = answers.do_api_token;
sessionInfo.ssh_key_location = answers.ssh_key_location;
sessionInfo.domain = answers.domain;
sshKey = {
location: `${process.env.HOME}/.ssh/${sessionInfo.ssh_key_location}`,
doID: [],
};
client = new DigitalOceanApi({
token: sessionInfo.do_api_token,
});
return sessionInfo;
}).then(() => {
let keys;
return client.sshKeyGetAll().then((results) => {
keys = results.filter(result => result.name === sessionInfo.ssh_key_location);
if (keys[0].name === sessionInfo.ssh_key_location) {
sshKey.doID.push(keys[0].id);
console.log('Remote key found');
return sessionInfo;
}
console.error('SSH Key not found on DO, aborting . . .');
process.exit(1);
});
}).catch(error => console.log(error));
}
// find the latest instance of streisand from Digital Ocean and store it.
function getCurrentServer(sessionInfo) {
console.log('finding current instance of Streisand. . .');
return client.dropletGetAll().then(droplets => droplets.filter((el) => {
if (el.name.search('streisand-') !== -1) {
return el.name;
}
return console.log('Could not find a droplet name starting with "streisand-" ');
})).then((droplet) => {
sessionInfo.old_server = droplet[0];
console.log(`The current server is: ${sessionInfo.old_server.name
} ${sessionInfo.old_server.networks.v4[0].ip_address}`);
return sessionInfo;
}).catch(error => console.error(error));
}
// using the return from getCurrentServer, start a new droplet using the snapshot of the old server.
function startNewServer(sessionInfo) {
let image;
if (!sessionInfo.old_server.snapshot_ids[0]) {
image = sessionInfo.old_server.image.id;
} else image = sessionInfo.old_server.snapshot_ids[0];
let newServerDetails = {
name: `streisand-${time.getMonth() + 1}-${time.getDate()
}-${time.getHours()}.${time.getMinutes()}`,
region: sessionInfo.old_server.region.slug,
size: '512mb',
image,
ssh_keys: sshKey.doID,
};
console.log('starting new droplet. . .');
return client.dropletNew(newServerDetails).then((newDroplet) => {
newServerDetails = newDroplet;
let numAttempts = 0;
function getIP() {
return client.dropletGet(newServerDetails.id).then((startingDroplet) => {
numAttempts += 1;
console.log('Find IP attempt # ', numAttempts);
if (startingDroplet.networks.v4[0].ip_address !== undefined) {
newServerDetails = startingDroplet;
console.log('newServer updated with IPv4 address: ',
startingDroplet.networks.v4[0].ip_address);
return newServerDetails;
} else if (numAttempts > 15) {
return console.log(`failed to reach server after ${numAttempts} attempts`);
}
return getIP();
}).then((server) => {
sessionInfo.new_server = server;
console.log(`${sessionInfo.new_server.name} ${
sessionInfo.new_server.networks.v4[0].ip_address}`);
return sessionInfo;
});
}
return getIP();
}).catch(error => console.log(error));
}
// check if the new_server is online and reachable by ssh
function checkConnection(sessionInfo) {
const execUptime = sessionInfo => ssh.connect({
host: sessionInfo.new_server.networks.v4[0].ip_address,
username: 'root',
privateKey: sshKey.location,
}).then(() => ssh.execCommand('uptime'));
function tryConnect() {
let attempt = 0;
function loop() {
console.log(`Attempting to connect to server ${sessionInfo.new_server.name}. . . `);
console.log(`Attempt: ${attempt + 1}`);
return execUptime(sessionInfo).then((results) => {
if (results.hasOwnProperty('stdout') || results.hasOwnProperty('stderr')) {
if (results.hasOwnProperty('stdout')) {
console.log(`STDOUT: ${results.stdout}`);
return sessionInfo;
} else if (results.hasOwnProperty('stderr')) {
console.log(`command failed - STDERR: ${results.stderr}`);
attempt += 1;
if (attempt !== 10) {
console.log('trying again . . . ');
return loop();
}
}
} else {
console.log(`unknown failure: ${results}`);
process.exit(1);
}
// whoah, I have the loop logic in here twice now. Not sure which one actually works. Rework.
}).catch((error) => {
console.log('failed to connect . . . ');
attempt += 1;
console.error(error);
if (attempt !== 25) {
console.log('trying again . . . ');
return loop();
}
console.log(`failed to connect after ${attempt} attempts`);
return process.exit(1);
});
}
return loop();
}
return tryConnect();
}
// download, update, and upload Shadowsocks config file
function getShadowsocks(sessionInfo) {
console.log('updating Shadowsocks. . . ');
function readConfig(service, config) {
sessionInfo[service] = {};
sessionInfo[service][config] = config;
const streamOpts = {
flags: 'r',
encoding: 'utf-8',
handle: null,
mode: 0o666,
autoClose: true,
};
const streamData = [];
return ssh.connect({
host: sessionInfo.old_server.networks.v4[0].ip_address,
username: 'root',
privateKey: sshKey.location,
}).then(session => session.requestSFTP().then((sftp) => {
console.log(`successfully connected, downloading ${config}`);
const readStream = sftp.createReadStream(configurations[service][config], streamOpts);
const stream = new Promise((resolve, reject) => {
readStream.on('data', (data) => {
streamData.push(data);
});
readStream.on('end', () => {
console.log(`${config} was successfully downloaded . . . `);
sessionInfo[service][config] = JSON.parse(streamData);
sessionInfo[service][config].server = sessionInfo.new_server.networks.v4[0].ip_address;
resolve();
});
readStream.on('error', () => reject(process.exit(1)));
});
return stream;
}).catch((STFPerror) => {
console.error(STFPerror);
process.exit(1);
}))
.catch((error) => {
console.error(error);
process.exit(1);
});
}
function writeConfig(service, config) {
const streamOpts = {
flags: 'w',
encoding: 'utf-8',
handle: null,
mode: 0o666,
autoClose: true,
};
return ssh.connect({
host: sessionInfo.new_server.networks.v4[0].ip_address,
username: 'root',
privateKey: sshKey.location,
}).then(session => session.requestSFTP().then((sftp) => {
const configReadable = new Readable();
const writeStream = sftp.createWriteStream(configurations[service][config], streamOpts);
const stream = new Promise((resolve) => {
writeStream.on('close', () => {
console.log(`${config} transferred succesfully`);
resolve();
});
});
configReadable.push(`${JSON.stringify(sessionInfo[service][config], null, 2)}\n`);
configReadable.push(null);
configReadable.pipe(writeStream);
return stream;
}).catch(error => console.error(error)));
}
return readConfig('shadowsocks', 'config.json').then(() => writeConfig('shadowsocks', 'config.json'))
.then(() => serviceRestart('shadowsocks-libev', sessionInfo))
.then((results) => {
console.log(results);
return results;
})
.catch((error) => {
console.error(error);
});
}
// restart a specified service on the new_server
function serviceRestart(service, sessionInfo) {
console.log(`restarting ${service}`);
return ssh.connect({
host: sessionInfo.new_server.networks.v4[0].ip_address,
username: 'root',
privateKey: sshKey.location,
})
.then(() => ssh.execCommand(`service ${service} restart && service ${service} status`))
.then((results) => {
if (results.hasOwnProperty('stdout') || results.hasOwnProperty('stderr')) {
if (results.hasOwnProperty('stdout')) {
console.log(`STDOUT: ${results.stdout}`);
return sessionInfo;
}
return console.error(`STDERR: ${results.stderr}`);
}
return console.error(`unknown error: ${results}`);
}).catch(error => console.error(error));
}
// update the specified domain A record on Digital Ocean
function updateDomainRecords(sessionInfo) {
console.log('updating domain record . . .');
const domain = sessionInfo.domain;
return client.domainRecordGetAll(domain).then((domainRecords) => {
const newRecord = {
type: 'A',
data: sessionInfo.new_server.networks.v4[0].ip_address,
name: '@',
};
let targetRecord = domainRecords.filter(() => domainRecords);
if (targetRecord != null) {
targetRecord = targetRecord.filter(el => el.type === 'A');
if (targetRecord[0].data === sessionInfo.old_server.networks.v4[0].ip_address) {
return console.log('the targetRecord and current server record match, updating record');
}
console.error('DNS Records do not match, skipping DNS record update.');
return client.domainRecordEdit(domain, targetRecord[0].id, newRecord)
.then((updatedRecord) => {
sessionInfo.domain = updatedRecord;
return console.error('The new record is : \n', sessionInfo.domain);
});
} else if (!targetRecord[0]) {
console.error(`The record details could not be found, skipping DNS record update ${targetRecord}`);
return sessionInfo;
}
return console.error(`unknown error ${targetRecord}`);
}).then(() => sessionInfo).catch((error) => {
console.error(`There was an error when trying to look up the domain, skipping DNS record update:\n ${error}`);
return sessionInfo;
});
}
function destroyServer(sessionInfo) {
return client.dropletDestroy(sessionInfo.old_server.id).then((results) => {
console.log(results);
return sessionInfo;
}).catch(error => console.error(error));
}
// run the program
getSessionInfo()
.then(results => getCurrentServer(results))
.then(results => startNewServer(results))
.then(results => checkConnection(results))
.then(results => getShadowsocks(results))
.then(results => updateDomainRecords(results))
.then((results) => {
const latestResults = results;
return prompt([{
type: 'confirm',
name: 'destroy',
message: 'Would you like to destroy the old Droplet?',
}]).then((answers) => {
if (answers.destroy) {
return destroyServer(latestResults);
}
return latestResults;
}).catch(error => console.log(error));
})
.then((results) => {
console.log(`redeployment complete: \n${results.new_server.networks.v4[0].ip_address}`);
return process.exit();
});