start/stop/reboot/delete take multiple arguments, fixes #38

This commit is contained in:
Dave Eddy 2015-10-06 16:54:55 -04:00
parent 3cbf85a121
commit d79083b9a1

View File

@ -16,6 +16,12 @@ var f = require('util').format;
var assert = require('assert-plus'); var assert = require('assert-plus');
var common = require('./common'); var common = require('./common');
var vasync = require('vasync');
function perror(err) {
console.error('error: %s', err.message);
}
function do_startstop_instance(action) { function do_startstop_instance(action) {
assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0, assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0,
'invalid action'); 'invalid action');
@ -29,7 +35,7 @@ function do_startstop_instance(action) {
f('%s a single instance.', common.capitalize(action)), f('%s a single instance.', common.capitalize(action)),
f(''), f(''),
f('Usage:'), f('Usage:'),
f(' {{name}} %s <alias|id>', action), f(' {{name}} %s <alias|id> ...', action),
f(''), f(''),
f('{{options}}') f('{{options}}')
].join('\n'); ].join('\n');
@ -80,72 +86,84 @@ function _do_instance(action, subcmd, opts, args, callback) {
if (opts.help) { if (opts.help) {
this.do_help('help', {}, [subcmd], callback); this.do_help('help', {}, [subcmd], callback);
return; return;
} else if (args.length !== 1) { } else if (args.length < 1) {
callback(new Error('invalid args: ' + args)); callback(new Error('invalid args: ' + args));
return; return;
} }
var arg = args[0]; vasync.forEachParallel({
var uuid, alias; func: function (arg, cb) {
var alias, uuid;
if (common.isUUID(arg)) { if (common.isUUID(arg)) {
uuid = arg; uuid = arg;
go1(); done();
} else { } else {
self.tritonapi.getInstance(arg, function (err, inst) { self.tritonapi.getInstance(arg, function (err, inst) {
if (err) { if (err) {
callback(err); perror(err);
return; cb(err);
} return;
alias = arg; }
uuid = inst.id; alias = arg;
go1(); uuid = inst.id;
}); done();
} });
function go1() {
// called when "uuid" is set
self.tritonapi.cloudapi[command](uuid, function (err, body, res) {
if (err) {
callback(err);
return;
} }
if (!opts.wait) { // called when "uuid" is set
if (alias) function done() {
console.log('%s (async) instance %s (%s)', self.tritonapi.cloudapi[command](uuid,
common.capitalize(action), alias, uuid); function (err, body, res) {
else
console.log('%s (async) instance %s', if (err) {
common.capitalize(action), uuid); perror(err);
callback(); cb(err);
return; return;
}
if (!opts.wait) {
if (alias)
console.log('%s (async) instance %s (%s)',
common.capitalize(action), alias, uuid);
else
console.log('%s (async) instance %s',
common.capitalize(action), uuid);
cb();
return;
}
self.tritonapi.cloudapi.waitForMachineStates({
id: uuid,
states: [state]
}, function (err2, inst2, res2) {
if (action === 'delete' &&
res2 && res2.statusCode === 410) {
// This is success, fall through to bottom.
/* jsl:pass */
} else if (err2) {
perror(err2);
cb(err2);
return;
}
var dur = common.humanDurationFromMs(Date.now() - now);
if (alias)
console.log('%s instance %s (%s, %s)',
common.capitalize(action), alias, uuid, dur);
else
console.log('%s instance %s (%s)',
common.capitalize(action), uuid, dur);
cb();
});
});
} }
},
self.tritonapi.cloudapi.waitForMachineStates({ inputs: args
id: uuid, }, function (err, results) {
states: [state] var e = err ? (new Error('command failure')) : null;
}, function (err2, inst2, res2) { callback(e);
if (action === 'delete' && res2 && res2.statusCode === 410) { });
// This is success, fall through to bottom.
/* jsl:pass */
} else if (err2) {
callback(err2);
return;
}
var dur = common.humanDurationFromMs(Date.now() - now);
if (alias)
console.log('%s instance %s (%s, %s)',
common.capitalize(action), alias, uuid, dur);
else
console.log('%s instance %s (%s)',
common.capitalize(action), uuid, dur);
callback();
});
});
}
} }
module.exports = do_startstop_instance; module.exports = do_startstop_instance;