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,34 +86,38 @@ 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);
cb(err);
return; return;
} }
alias = arg; alias = arg;
uuid = inst.id; uuid = inst.id;
go1(); done();
}); });
} }
function go1() {
// called when "uuid" is set // called when "uuid" is set
self.tritonapi.cloudapi[command](uuid, function (err, body, res) { function done() {
self.tritonapi.cloudapi[command](uuid,
function (err, body, res) {
if (err) { if (err) {
callback(err); perror(err);
cb(err);
return; return;
} }
@ -118,7 +128,7 @@ function _do_instance(action, subcmd, opts, args, callback) {
else else
console.log('%s (async) instance %s', console.log('%s (async) instance %s',
common.capitalize(action), uuid); common.capitalize(action), uuid);
callback(); cb();
return; return;
} }
@ -126,11 +136,13 @@ function _do_instance(action, subcmd, opts, args, callback) {
id: uuid, id: uuid,
states: [state] states: [state]
}, function (err2, inst2, res2) { }, function (err2, inst2, res2) {
if (action === 'delete' && res2 && res2.statusCode === 410) { if (action === 'delete' &&
res2 && res2.statusCode === 410) {
// This is success, fall through to bottom. // This is success, fall through to bottom.
/* jsl:pass */ /* jsl:pass */
} else if (err2) { } else if (err2) {
callback(err2); perror(err2);
cb(err2);
return; return;
} }
@ -142,10 +154,16 @@ function _do_instance(action, subcmd, opts, args, callback) {
console.log('%s instance %s (%s)', console.log('%s instance %s (%s)',
common.capitalize(action), uuid, dur); common.capitalize(action), uuid, dur);
callback(); cb();
}); });
}); });
} }
},
inputs: args
}, function (err, results) {
var e = err ? (new Error('command failure')) : null;
callback(e);
});
} }
module.exports = do_startstop_instance; module.exports = do_startstop_instance;