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,72 +86,84 @@ 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;
if (common.isUUID(arg)) {
uuid = arg;
go1();
} else {
self.tritonapi.getInstance(arg, function (err, inst) {
if (err) {
callback(err);
return;
}
alias = arg;
uuid = inst.id;
go1();
});
}
function go1() {
// called when "uuid" is set
self.tritonapi.cloudapi[command](uuid, function (err, body, res) {
if (err) {
callback(err);
return;
vasync.forEachParallel({
func: function (arg, cb) {
var alias, uuid;
if (common.isUUID(arg)) {
uuid = arg;
done();
} else {
self.tritonapi.getInstance(arg, function (err, inst) {
if (err) {
perror(err);
cb(err);
return;
}
alias = arg;
uuid = inst.id;
done();
});
}
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);
callback();
return;
// called when "uuid" is set
function done() {
self.tritonapi.cloudapi[command](uuid,
function (err, body, res) {
if (err) {
perror(err);
cb(err);
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({
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) {
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();
});
});
}
},
inputs: args
}, function (err, results) {
var e = err ? (new Error('command failure')) : null;
callback(e);
});
}
module.exports = do_startstop_instance;