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 common = require('./common');
var vasync = require('vasync');
function perror(err) {
console.error('error: %s', err.message);
}
function do_startstop_instance(action) {
assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0,
'invalid action');
@ -29,7 +35,7 @@ function do_startstop_instance(action) {
f('%s a single instance.', common.capitalize(action)),
f(''),
f('Usage:'),
f(' {{name}} %s <alias|id>', action),
f(' {{name}} %s <alias|id> ...', action),
f(''),
f('{{options}}')
].join('\n');
@ -80,34 +86,38 @@ function _do_instance(action, subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length !== 1) {
} else if (args.length < 1) {
callback(new Error('invalid args: ' + args));
return;
}
var arg = args[0];
var uuid, alias;
vasync.forEachParallel({
func: function (arg, cb) {
var alias, uuid;
if (common.isUUID(arg)) {
uuid = arg;
go1();
done();
} else {
self.tritonapi.getInstance(arg, function (err, inst) {
if (err) {
callback(err);
perror(err);
cb(err);
return;
}
alias = arg;
uuid = inst.id;
go1();
done();
});
}
function go1() {
// 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) {
callback(err);
perror(err);
cb(err);
return;
}
@ -118,7 +128,7 @@ function _do_instance(action, subcmd, opts, args, callback) {
else
console.log('%s (async) instance %s',
common.capitalize(action), uuid);
callback();
cb();
return;
}
@ -126,11 +136,13 @@ function _do_instance(action, subcmd, opts, args, callback) {
id: uuid,
states: [state]
}, 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.
/* jsl:pass */
} else if (err2) {
callback(err2);
perror(err2);
cb(err2);
return;
}
@ -142,10 +154,16 @@ function _do_instance(action, subcmd, opts, args, callback) {
console.log('%s instance %s (%s)',
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;