/
utils.js
252 lines (208 loc) · 5.86 KB
/
utils.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
import { Client } from 'ssh2';
import debug from 'debug';
import expandTilde from 'expand-tilde';
import fs from 'fs';
import path from 'path';
import { promisify } from 'bluebird';
import readline from 'readline';
import stream from 'stream';
const log = debug('mup:utils');
export function addStdioHandlers(list) {
list._taskQueue = list._taskQueue.map(task => {
task.options = task.options || {};
task.options.onStdout = () => data => {
process.stdout.write(data);
};
task.options.onStderr = () => data => {
process.stderr.write(data);
};
return task;
});
}
export function runTaskList(list, sessions, opts) {
if (opts && opts.verbose) {
addStdioHandlers(list);
delete opts.verbose;
}
return new Promise((resolve, reject) => {
list.run(sessions, opts, summaryMap => {
for (const host in summaryMap) {
if (summaryMap.hasOwnProperty(host)) {
const summary = summaryMap[host];
if (summary.error) {
const error = summary.error;
error.nodemiralHistory = summary.history;
reject(error);
return;
}
}
}
resolve();
});
});
}
// Implements a simple readable stream to pass
// the logs from nodemiral to readline which
// then splits it into individual lines.
class Callback2Stream extends stream.Readable {
constructor(options) {
// Calls the stream.Readable(options) constructor
super(options);
this.data = [];
}
addData(data) {
if (this.reading) {
this.reading = this.push(data);
} else {
this.data.push(data);
}
}
_read() {
this.reading = true;
this.data.forEach(() => {
const shouldContinue = this.reading && this.push(this.data.shift());
if (!shouldContinue) {
this.reading = false;
}
});
}
}
export function getDockerLogs(name, sessions, args) {
const command = `sudo docker ${args.join(' ')} ${name} 2>&1`;
log(`getDockerLogs command: ${command}`);
const promises = sessions.map(session => {
const input = new Callback2Stream();
const host = `[${session._host}]`;
const lineSeperator = readline.createInterface({
input,
terminal: true
});
lineSeperator.on('line', data => {
console.log(host + data);
});
const options = {
onStdout: data => {
input.addData(data);
},
onStderr: data => {
// the logs all come in on stdout so stderr isn't added to lineSeperator
process.stdout.write(host + data);
}
};
return promisify(session.execute.bind(session))(command, options);
});
return Promise.all(promises);
}
export function createSSHOptions(server) {
const sshAgent = process.env.SSH_AUTH_SOCK;
const ssh = {
host: server.host,
port: (server.opts && server.opts.port) || 22,
username: server.username
};
if (server.pem) {
ssh.privateKey = fs.readFileSync(resolvePath(server.pem), 'utf8');
} else if (server.password) {
ssh.password = server.password;
} else if (sshAgent && fs.existsSync(sshAgent)) {
ssh.agent = sshAgent;
}
return ssh;
}
// Maybe we should create a new npm package
// for this one. Something like 'sshelljs'.
export function runSSHCommand(info, command) {
return new Promise((resolve, reject) => {
const conn = new Client();
// TODO better if we can extract SSH agent info from original session
const sshAgent = process.env.SSH_AUTH_SOCK;
const ssh = {
host: info.host,
port: (info.opts && info.opts.port) || 22,
username: info.username
};
if (info.pem) {
ssh.privateKey = fs.readFileSync(resolvePath(info.pem), 'utf8');
} else if (info.password) {
ssh.password = info.password;
} else if (sshAgent && fs.existsSync(sshAgent)) {
ssh.agent = sshAgent;
}
conn.connect(ssh);
conn.once('error', err => {
if (err) {
reject(err);
}
});
// TODO handle error events
conn.once('ready', () => {
conn.exec(command, (err, outputStream) => {
if (err) {
conn.end();
reject(err);
return;
}
let output = '';
outputStream.on('data', data => {
output += data;
});
outputStream.once('close', code => {
conn.end();
resolve({ code, output, host: info.host });
});
});
});
});
}
export function countOccurences(needle, haystack) {
const regex = new RegExp(needle, 'g');
const match = haystack.match(regex) || [];
return match.length;
}
export function resolvePath(...paths) {
const expandedPaths = paths.map(_path => expandTilde(_path));
return path.resolve(...expandedPaths);
}
/**
* Checks if the module not found is a certain module
*
* @param {Error} e - Error that was thwon
* @param {String} modulePath - path to the module to compare the error to
* @returns {Boolean} true if the modulePath and path in the error is the same
*/
export function moduleNotFoundIsPath(e, modulePath) {
const pathPosition = e.message.length - modulePath.length - 1;
return e.message.indexOf(modulePath) === pathPosition;
}
export function argvContains(argvArray, option) {
if (argvArray.indexOf(option) > -1) {
return true;
}
return argvArray.find(value => value.indexOf(`${option}=`) > -1);
}
export function createOption(key) {
if (key.length > 1) {
return `--${key}`;
}
return `-${key}`;
}
export function filterArgv(argvArray, argv, unwanted) {
const result = argv._.slice();
Object.keys(argv).forEach(key => {
const option = createOption(key);
if (
unwanted.indexOf(key) === -1 &&
argv[key] !== false &&
argv[key] !== undefined
) {
if (!argvContains(argvArray, option)) {
return;
}
result.push(option);
if (typeof argv[key] !== 'boolean') {
result.push(argv[key]);
}
}
});
return result;
}