-
Notifications
You must be signed in to change notification settings - Fork 117
/
shell.js
139 lines (101 loc) · 3.45 KB
/
shell.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
var util = require('util')
, extend = require('util-extend')
, exec = require('child_process').exec
, byline = require('byline')
, fs = require('fs')
, writeTempFile = require('../utils').writeTempFile
, Fiber = require('fibers')
, Future = require('fibers/future')
, Transport = require('./index')
, errors = require('../errors');
function Shell(context) {
Shell.super_.call(this, context);
}
util.inherits(Shell, Transport);
Shell.prototype._exec = function(command, options) {
options = options || {};
var self = this;
options = extend(extend({}, self._options), options); // clone and extend
var result = {
code: 0,
stdout: null,
stderr: null
};
self._logger.command(command);
var fiber = Fiber.current;
var proc = exec(command, extend({ maxBuffer: 1000 * 1024 }, options.exec));
proc.stdout.on('data', function(data) {
result.stdout = (result.stdout || '') + data;
});
proc.stderr.on('data', function(data) {
result.stderr = (result.stderr || '') + data;
});
byline(proc.stdout).on('data', function(data) {
if(!options.silent) {
self._logger.stdout(String(data).trim());
}
});
byline(proc.stderr).on('data', function(data) {
self._logger[options.failsafe ? 'stdwarn' : 'stderr'](String(data));
});
proc.on('close', function(code) {
result.code = code;
if(result.code === 0) {
self._logger.success('ok');
} else if(options.failsafe) {
self._logger.warn('failed safely (' + result.code + ')');
} else {
self._logger.error('failed (' + result.code + ')');
var error = new errors.CommandExitedAbnormallyError(
'Command exited abnormally on ' + self._context.remote.host);
return fiber.throwInto(error);
}
fiber.run(result);
});
return Fiber.yield();
};
Shell.prototype.transfer = function(files, remoteDir, options) {
options = extend(extend({}, this._options), options); // clone and extend
if(!remoteDir) {
throw new errors.InvalidArgumentError('Missing remote path for transfer()');
}
if(Array.isArray(files)) {
files = files.join('\n');
} else if(files instanceof Object) {
if(!files.hasOwnProperty('stdout')) {
throw new errors.InvalidArgumentError('Invalid object passed to transfer()');
}
files = files.stdout;
}
files = (files || '').trim().replace(/[\r|\0]/mg, '\n');
if(!files) {
throw new errors.InvalidArgumentError('Empty file list passed to transfer()');
}
var tmpFile = writeTempFile(files);
var rsyncFlags = '-az' + (this._context.options.debug ? 'vv' : '');
var results = [];
var self = this;
var task = function(remote) {
var sshFlags = (remote.privateKey ? ' -i ' + remote.privateKey : '');
var remoteUrl = util.format('%s%s:%s',
(remote.username ? remote.username + '@' : ''),
remote.host, remoteDir);
var command = util.format('rsync --files-from %s %s --rsh="ssh -p%s%s" ./ %s',
tmpFile, rsyncFlags,
remote.port || 22, sshFlags, remoteUrl);
var future = new Future();
new Fiber(function() {
results.push(self.exec(command, options));
return future.return();
}).run();
return future;
};
var tasks = [];
self._context.hosts.forEach(function(remote) {
tasks.push(task(remote));
});
Future.wait(tasks);
fs.unlinkSync(tmpFile);
return results;
};
module.exports = Shell;