add wait-instance

This commit is contained in:
Dave Eddy 2015-08-26 15:16:01 -04:00
parent 4a82869e2e
commit 9b57df6962
2 changed files with 97 additions and 0 deletions

View File

@ -72,6 +72,7 @@ function CLI() {
'stop-instance',
'reboot-instance',
'delete-instance',
'wait-instance',
'ssh',
{ group: 'Images' },
'images',
@ -148,6 +149,7 @@ CLI.prototype.do_stop_instance = require('./do_startstop_instance')('stop');
CLI.prototype.do_start_instance = require('./do_startstop_instance')('start');
CLI.prototype.do_reboot_instance = require('./do_startstop_instance')('reboot');
CLI.prototype.do_delete_instance = require('./do_startstop_instance')('delete');
CLI.prototype.do_wait_instance = require('./do_wait_instance');
CLI.prototype.do_ssh = require('./do_ssh');
// Packages

95
lib/do_wait_instance.js Normal file
View File

@ -0,0 +1,95 @@
/*
* Copyright 2015 Joyent Inc.
*
* `triton wait-instance ...`
*/
var common = require('./common');
function do_wait_instance(subcmd, opts, args, cb) {
var self = this;
if (opts.help) {
this.do_help('help', {}, [subcmd], cb);
return;
} else if (args.length < 1 || args.length > 2) {
cb(new Error('invalid args: ' + args));
return;
}
var ids = args[0].split(',');
var states = (args[1] || 'failed,running').split(',');
var machines = {};
var i = 0;
ids.forEach(function (id) {
i++;
if (common.isUUID(id)) {
machines[id] = {};
go1();
return;
}
self.triton.getMachineByAlias(id, function (err, machine) {
if (err) {
cb(err);
return;
}
if (states.indexOf(machine.state) >= 0) {
// machine in acceptable state already... skip it
} else {
machines[machine.id] = machine;
}
go1();
});
});
function go1() {
if (--i > 0)
return;
var uuids = Object.keys(machines);
var num = uuids.length;
i = num;
if (num === 0) {
cb();
return;
}
uuids.forEach(function (id) {
var opts = {
id: id,
states: states
};
self.triton.cloudapi.waitForMachineStates(opts, function (err, body, res) {
if (err) {
cb(err);
return;
}
if (--i === 0) {
cb();
}
});
});
}
}
do_wait_instance.aliases = ['wait'];
do_wait_instance.help = [
'Wait on 1 or more instances.',
'',
'Usage:',
' {{name}} wait <alias|id> [state]',
'',
'{{options}}'
].join('\n');
do_wait_instance.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
];
module.exports = do_wait_instance;