2015-08-26 22:16:01 +03:00
|
|
|
/*
|
2015-09-04 21:12:20 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-01-04 23:08:16 +02:00
|
|
|
* Copyright 2016 Joyent, Inc.
|
2015-08-26 22:16:01 +03:00
|
|
|
*
|
2016-01-04 23:08:16 +02:00
|
|
|
* `triton instance wait ...`
|
2015-08-26 22:16:01 +03:00
|
|
|
*/
|
|
|
|
|
2015-09-01 20:43:28 +03:00
|
|
|
var vasync = require('vasync');
|
2015-09-01 01:16:44 +03:00
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
var common = require('../common');
|
2016-01-04 23:08:16 +02:00
|
|
|
var distractions = require('../distractions');
|
|
|
|
var errors = require('../errors');
|
2015-09-01 01:16:44 +03:00
|
|
|
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
function do_wait(subcmd, opts, args, cb) {
|
2015-08-26 22:16:01 +03:00
|
|
|
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) {
|
2016-06-09 00:13:16 +03:00
|
|
|
return cb(new errors.UsageError('missing INST arg(s)'));
|
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) {
|
2015-09-02 20:47:06 +03:00
|
|
|
/* JSSTYLED */
|
2015-09-01 01:16:44 +03:00
|
|
|
states = states.concat(s.trim().split(/\s*,\s*/g));
|
|
|
|
});
|
2015-08-26 22:16:01 +03:00
|
|
|
|
2015-09-01 20:43:28 +03:00
|
|
|
var distraction;
|
2015-08-26 23:15:31 +03:00
|
|
|
var done = 0;
|
2015-09-01 20:43:28 +03:00
|
|
|
var instFromId = {};
|
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
vasync.pipeline({arg: {cli: this.top}, funcs: [
|
|
|
|
common.cliSetupTritonApi,
|
2015-09-01 20:43:28 +03:00
|
|
|
function getInsts(_, next) {
|
|
|
|
vasync.forEachParallel({
|
|
|
|
inputs: ids,
|
|
|
|
func: function getInst(id, nextInst) {
|
2016-01-04 23:08:16 +02:00
|
|
|
self.top.tritonapi.getInstance(id, function (err, inst) {
|
2015-09-01 20:43:28 +03:00
|
|
|
if (err) {
|
|
|
|
return nextInst(err);
|
|
|
|
}
|
|
|
|
if (states.indexOf(inst.state) !== -1) {
|
|
|
|
console.log('%d/%d: Instance %s (%s) already %s',
|
|
|
|
++done, ids.length, inst.name, id, inst.state);
|
|
|
|
} else {
|
|
|
|
instFromId[inst.id] = inst;
|
|
|
|
}
|
|
|
|
nextInst();
|
|
|
|
});
|
2015-09-01 21:51:02 +03:00
|
|
|
}
|
2015-09-01 20:43:28 +03:00
|
|
|
}, next);
|
|
|
|
},
|
|
|
|
|
|
|
|
function waitForInsts(_, next) {
|
|
|
|
var idsToWaitFor = Object.keys(instFromId);
|
|
|
|
if (idsToWaitFor.length === 0) {
|
|
|
|
return next();
|
|
|
|
}
|
2015-08-26 22:16:01 +03:00
|
|
|
|
2015-09-01 20:43:28 +03:00
|
|
|
if (idsToWaitFor.length === 1) {
|
|
|
|
var inst2 = instFromId[idsToWaitFor[0]];
|
|
|
|
console.log(
|
2015-09-01 22:03:52 +03:00
|
|
|
'Waiting for instance %s (%s) to enter state (states: %s)',
|
2015-09-01 20:43:28 +03:00
|
|
|
inst2.name, inst2.id, states.join(', '));
|
|
|
|
} else {
|
|
|
|
console.log(
|
2015-09-01 22:03:52 +03:00
|
|
|
'Waiting for %d instances to enter state (states: %s)',
|
2015-09-01 20:43:28 +03:00
|
|
|
idsToWaitFor.length, states.join(', '));
|
|
|
|
}
|
2015-08-26 22:16:01 +03:00
|
|
|
|
2015-09-23 10:19:42 +03:00
|
|
|
/*
|
|
|
|
* TODO: need BigSpinner.log first.
|
|
|
|
* TODO: Also when adding a spinner, we need an equiv option to
|
|
|
|
* `triton create -wwww` to trigger the spinner (and size). By
|
|
|
|
* default: no spinner.
|
|
|
|
*/
|
|
|
|
if (false &&
|
|
|
|
process.stderr.isTTY)
|
2015-09-01 20:43:28 +03:00
|
|
|
{
|
|
|
|
distraction = distractions.createDistraction();
|
|
|
|
}
|
|
|
|
|
|
|
|
vasync.forEachParallel({
|
|
|
|
inputs: idsToWaitFor,
|
|
|
|
func: function waitForInst(id, nextInst) {
|
2016-01-04 23:08:16 +02:00
|
|
|
self.top.tritonapi.cloudapi.waitForMachineStates({
|
2015-09-01 20:43:28 +03:00
|
|
|
id: id,
|
|
|
|
states: states
|
|
|
|
}, function (err, inst, res) {
|
|
|
|
if (err) {
|
|
|
|
return nextInst(err);
|
|
|
|
}
|
|
|
|
console.log('%d/%d: Instance %s (%s) moved to state %s',
|
|
|
|
++done, ids.length, inst.name, inst.id, inst.state);
|
|
|
|
nextInst();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, next);
|
2015-08-26 22:16:01 +03:00
|
|
|
}
|
|
|
|
|
2015-09-01 20:43:28 +03:00
|
|
|
]}, function (err) {
|
|
|
|
if (distraction) {
|
|
|
|
distraction.destroy();
|
|
|
|
}
|
|
|
|
cb(err);
|
2015-08-26 22:16:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-09 00:13:16 +03:00
|
|
|
do_wait.synopses = ['{{name}} {{cmd}} [-s STATES] INST [INST ...]'];
|
2016-01-04 23:08:16 +02:00
|
|
|
do_wait.help = [
|
2016-06-09 00:13:16 +03:00
|
|
|
/* BEGIN JSSTYLED */
|
2015-09-01 20:43:28 +03:00
|
|
|
'Wait on instances changing state.',
|
2015-08-26 22:16:01 +03:00
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
2015-08-26 22:16:01 +03:00
|
|
|
'',
|
2015-08-31 21:13:39 +03:00
|
|
|
'{{options}}',
|
2016-06-09 00:13:16 +03:00
|
|
|
'Where "INST" is an instance name, id, or short id; and "STATES" is a',
|
|
|
|
'comma-separated list of target instance states, by default "running,failed".',
|
2018-02-10 20:17:02 +02:00
|
|
|
'In other words, "spearhead inst wait foo0" will wait for instance "foo0" to',
|
2016-06-09 00:13:16 +03:00
|
|
|
'complete provisioning.'
|
|
|
|
/* END JSSTYLED */
|
2015-08-26 22:16:01 +03:00
|
|
|
].join('\n');
|
2016-01-04 23:08:16 +02:00
|
|
|
do_wait.options = [
|
2015-08-26 22:16:01 +03:00
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
2015-09-01 01:16:44 +03:00
|
|
|
{
|
|
|
|
names: ['states', 's'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
default: ['running', 'failed'],
|
|
|
|
helpArg: 'STATES',
|
2015-09-23 10:19:42 +03:00
|
|
|
help: 'Instance states on which to wait. Default is "running,failed". '
|
2015-09-01 01:16:44 +03:00
|
|
|
+ '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
|
|
|
];
|
|
|
|
|
2016-03-09 19:19:44 +02:00
|
|
|
do_wait.completionArgtypes = ['tritoninstance'];
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
module.exports = do_wait;
|