-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoker.js
60 lines (55 loc) · 1.62 KB
/
invoker.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
'use strict';
const _ = require('highland')
, AWS = require('aws-sdk')
, ssh2 = require('ssh2')
, fs = require('fs')
, spread = require('./util').spread;
module.exports = (actions, opts, ec2, ssh) => {
ec2 = ec2 || new AWS.EC2();
ssh = ssh || _.wrapCallback((i, cmd, done) => {
const conn = new ssh2.Client();
conn.on('ready', () => {
conn.exec(cmd, (err, chn) => {
if (err) {
done(err);
} else {
const stderr = [];
chn.on('close', (code, signal, didCoreDump) => {
if (signal) {
err = new Error(`caught ${signal} (core dumped: ${didCoreDump})`);
} else if (code !== null && code !== 0) {
err = new Error(`exit ${code}: ${Buffer.concat(stderr)}`);
}
conn.end();
done(err, i);
}).on('data', () => {}).stderr.on('data', (buf) => {
stderr.push(buf);
});
}
});
}).on('error', (err) => {
done(err);
}).connect({
username: opts.user,
privateKey: fs.readFileSync(opts.privateKey),
host: opts.publicIp ? i.PublicIpAddress : i.PrivateIpAddress
});
});
const describeInstance = _.wrapCallback((i, done) => {
ec2.describeInstances({
InstanceIds: [
i.InstanceId
]
}, (err, data) => {
if (err) {
done(err);
} else {
done(null, _.extend(data.Reservations[0].Instances[0], { group: i.group }));
}
});
});
return _.pipeline(
_.flatMap(describeInstance),
spread(actions.map((a) => a((cmd) => _.flatMap((i) => ssh(i, cmd)), ec2)))
);
};