-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
227 lines (210 loc) · 5.82 KB
/
index.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
'use strict';
var path = require('path'),
debug = require('debug')('fleetctl'),
execFile = require('child_process').execFile;
var FleetAPI = function (host, config) {
if (!host) {
throw new Error('Expected arguments: (host[, config]).');
}
this.host = host;
this.config = config || {};
this.binPath = path.join(__dirname, 'bin', 'fleetctl.sh');
debug('host', this.host);
debug('config', this.config);
debug('binPath', this.binPath);
};
function getUnitFilename(unit) {
return unit.indexOf('.service') >= 0 ? unit : unit + '.service';
}
FleetAPI.prototype._ssh_fleetctl = function (command, args, options, cb) {
if (args && !options && !cb && typeof args === 'function') {
cb = args;
args = null;
}
else if (args && options && !cb && typeof options === 'function') {
// callback passed instead of options
cb = options;
options = null;
}
if (typeof args === 'string') {
args = [args];
}
args = args || [];
args.unshift(command);
var env = JSON.parse(JSON.stringify(process.env));
env.PATH = '/usr/local/bin:/usr/bin:/bin:' + path.resolve(path.join('.', 'bin'));
env.FLEETW_HOST = this.host;
if (this.config.key) {
env.FLEETW_KEY = this.config.key;
}
if (this.config.port) {
env.FLEETW_PORT = this.config.port;
}
if (options && typeof options === 'object') {
if (options.unit) {
env.FLEETW_UNIT = options.unit;
if (options.unitFileData) {
env.FLEETW_UNIT_DATA = options.unitFileData;
}
else {
env.FLEETW_UNIT_FILE = options.unitFile || getUnitFilename(options.unit);
}
}
}
if (this.config.debug) {
env.DEBUG = true;
}
debug('args', args);
debug('env', env);
return execFile(this.binPath, args, {env: env}, cb);
};
FleetAPI.prototype.cat = function (unit, cb) {
return this._ssh_fleetctl('cat', unit, cb);
};
FleetAPI.prototype.debugInfo = function (cb) {
return this._ssh_fleetctl('debug-info', cb);
};
FleetAPI.prototype.destroy = function () {
var cb;
if (typeof arguments[arguments.length - 1] === 'function') {
cb = Array.prototype.pop.call(arguments);
}
var units = Array.prototype.slice.call(arguments);
if (units.length === 0) {
throw new Error('Expected one or more unit names');
}
return this._ssh_fleetctl('destroy', units, cb);
};
FleetAPI.prototype.journal = function (unit, options, cb) {
if (!unit) {
throw new Error('Expected unit name and optional options');
}
var terms = [];
if (options) {
if (options.follow) {
terms.push('--follow=' + options.follow);
}
if (options.lines) {
terms.push('--lines=' + options.lines);
}
}
terms.push(unit);
return this._ssh_fleetctl('journal', terms, cb);
};
FleetAPI.prototype.listMachines = function (cb) {
return this._ssh_fleetctl('list-machines', ['--full', '--no-legend'], cb);
};
FleetAPI.prototype.listUnits = function (cb) {
return this._ssh_fleetctl('list-units', ['--full', '--no-legend'], cb);
};
FleetAPI.prototype.load = function (unit, options, cb) {
var args = [];
var ufOptions = {
unit: path.basename(unit)
};
if (options) {
if (options.blockAttempts) {
args.push('--block-attempts=' + options.blockAttempts);
}
if (options.noBlock) {
args.push('--no-block=' + !!options.noBlock);
}
if (options.sign) {
args.push('--sign=' + !!options.sign);
}
if (options.unitFile) {
ufOptions.unitFile = options.unitFile;
}
if (options.unitFileData) {
ufOptions.unitFileData = options.unitFileData;
}
}
args.push(unit);
return this._ssh_fleetctl('load', args, ufOptions, cb);
};
FleetAPI.prototype.start = function (unit, options, cb) {
var args = [];
var ufOptions = {
unit: path.basename(unit)
};
if (options) {
if (options.blockAttempts) {
args.push('--block-attempts=' + options.blockAttempts);
}
if (options.noBlock) {
args.push('--no-block=' + !!options.noBlock);
}
if (options.sign) {
args.push('--sign=' + !!options.sign);
}
if (options.unitFile) {
ufOptions.unitFile = options.unitFile;
}
if (options.unitFileData) {
ufOptions.unitFileData = options.unitFileData;
}
}
args.push(unit);
return this._ssh_fleetctl('start', args, ufOptions, cb);
};
FleetAPI.prototype.status = function (unit, cb) {
return this._ssh_fleetctl('status', unit, cb);
};
FleetAPI.prototype.stop = function (units, options, cb) {
if (typeof units === 'string') {
units = [units];
}
var args = [];
if (options) {
if (options.blockAttempts) {
args.push('--block-attempts=' + options.blockAttempts);
}
if (options.noBlock) {
args.push('--no-block=' + !!options.noBlock);
}
}
units.forEach(function (u) {
args.push(u);
});
return this._ssh_fleetctl('stop', args, cb);
};
FleetAPI.prototype.submit = function (unit, options, cb) {
// XXX in fixing an undef error here i realise all these funcs
// expect only 1 unit, contrary to the README
var args = [];
var ufOptions = {
unit: path.basename(unit)
};
if (options) {
if (options.sign) {
args.push('--sign=' + !!options.sign);
}
if (options.unitFile) {
ufOptions.unitFile = options.unitFile;
}
if (options.unitFileData) {
ufOptions.unitFileData = options.unitFileData;
}
}
args.push(unit);
return this._ssh_fleetctl('submit', args, ufOptions, cb);
};
FleetAPI.prototype.unload = function (units, options, cb) {
if (typeof units === 'string') {
units = [units];
}
var args = [];
if (options) {
if (options.blockAttempts) {
args.push('--block-attempts=' + options.blockAttempts);
}
if (options.noBlock) {
args.push('--no-block=' + !!options.noBlock);
}
}
units.forEach(function (u) {
args.push(u);
});
return this._ssh_fleetctl('unload', args, cb);
};
module.exports = FleetAPI;