2015-08-26 22:16:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent Inc.
|
|
|
|
*
|
|
|
|
* `triton wait-instance ...`
|
|
|
|
*/
|
|
|
|
|
2015-09-01 01:16:44 +03:00
|
|
|
var format = require('util').format;
|
|
|
|
|
2015-08-26 22:16:01 +03:00
|
|
|
var common = require('./common');
|
2015-09-01 01:16:44 +03:00
|
|
|
var errors = require('./errors');
|
|
|
|
|
|
|
|
|
2015-08-26 22:16:01 +03:00
|
|
|
|
|
|
|
function do_wait_instance(subcmd, opts, args, cb) {
|
|
|
|
var self = this;
|
|
|
|
if (opts.help) {
|
2015-09-01 01:16:44 +03:00
|
|
|
return this.do_help('help', {}, [subcmd], cb);
|
|
|
|
} else if (args.length < 1) {
|
|
|
|
return cb(new errors.UsageError(format(
|
|
|
|
'incorrect number of args (%d): %s', args.length, args.join(' '))));
|
2015-08-26 22:16:01 +03:00
|
|
|
}
|
2015-09-01 01:16:44 +03:00
|
|
|
var ids = args;
|
|
|
|
var states = [];
|
|
|
|
opts.states.forEach(function (s) {
|
|
|
|
states = states.concat(s.trim().split(/\s*,\s*/g));
|
|
|
|
});
|
2015-08-26 22:16:01 +03:00
|
|
|
|
2015-08-26 23:15:31 +03:00
|
|
|
function log() {
|
|
|
|
if (!opts.quiet)
|
|
|
|
console.log.apply(console, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
var done = 0;
|
2015-08-26 22:16:01 +03:00
|
|
|
|
|
|
|
var machines = {};
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
ids.forEach(function (id) {
|
|
|
|
i++;
|
|
|
|
if (common.isUUID(id)) {
|
|
|
|
machines[id] = {};
|
|
|
|
go1();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:21:27 +03:00
|
|
|
self.triton.getInstance(id, function (err, machine) {
|
2015-08-26 22:16:01 +03:00
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (states.indexOf(machine.state) >= 0) {
|
|
|
|
// machine in acceptable state already... skip it
|
2015-08-26 23:15:31 +03:00
|
|
|
log('%d/%d: %s already in acceptable state: %s',
|
|
|
|
++done, ids.length, id, machine.state);
|
2015-08-26 22:16:01 +03:00
|
|
|
} 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) {
|
2015-09-01 02:56:26 +03:00
|
|
|
var waitOpts = {
|
2015-08-26 22:16:01 +03:00
|
|
|
id: id,
|
|
|
|
states: states
|
|
|
|
};
|
2015-09-01 02:56:26 +03:00
|
|
|
self.triton.cloudapi.waitForMachineStates(waitOpts,
|
|
|
|
function (err, body, res) {
|
2015-08-26 22:16:01 +03:00
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 23:15:31 +03:00
|
|
|
log('%d/%d: %s moved to state %s',
|
|
|
|
++done, ids.length, body.name, body.state);
|
2015-08-26 22:16:01 +03:00
|
|
|
if (--i === 0) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_wait_instance.aliases = ['wait'];
|
|
|
|
do_wait_instance.help = [
|
2015-08-31 21:13:39 +03:00
|
|
|
'Wait on instances moving to given states.',
|
2015-08-26 22:16:01 +03:00
|
|
|
'',
|
|
|
|
'Usage:',
|
2015-09-01 01:16:44 +03:00
|
|
|
' {{name}} wait [-s STATES] INSTANCE [INSTANCE ...]',
|
2015-08-26 22:16:01 +03:00
|
|
|
'',
|
2015-08-31 21:13:39 +03:00
|
|
|
'{{options}}',
|
|
|
|
'Where "states" is a comma-separated list of target instance states,',
|
|
|
|
'by default "running,failed". In other words, "triton wait foo0" will',
|
|
|
|
'wait for instance "foo0" to complete provisioning.'
|
2015-08-26 22:16:01 +03:00
|
|
|
].join('\n');
|
|
|
|
do_wait_instance.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
2015-08-26 23:15:31 +03:00
|
|
|
{
|
|
|
|
names: ['quiet', 'q'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Disable all output.'
|
|
|
|
},
|
2015-09-01 01:16:44 +03:00
|
|
|
{
|
|
|
|
names: ['states', 's'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
default: ['running', 'failed'],
|
|
|
|
helpArg: 'STATES',
|
|
|
|
help: 'Instance states on which to wait. Default is "running,failed".'
|
|
|
|
+ 'Values can be comma-separated or multiple uses of the option.'
|
2015-09-01 02:56:26 +03:00
|
|
|
}
|
2015-08-26 22:16:01 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
module.exports = do_wait_instance;
|