This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/do_wait_instance.js

111 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-08-26 22:16:01 +03:00
/*
* 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;
}
2015-08-26 23:15:31 +03:00
function log() {
if (!opts.quiet)
console.log.apply(console, arguments);
}
2015-08-26 22:16:01 +03:00
var ids = args[0].split(',');
var states = (args[1] || 'failed,running').split(',');
2015-08-26 23:15:31 +03:00
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;
}
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
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) {
var opts = {
id: id,
states: states
};
self.triton.cloudapi.waitForMachineStates(opts, function (err, body, res) {
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 = [
'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.'
},
2015-08-26 23:15:31 +03:00
{
names: ['quiet', 'q'],
type: 'bool',
help: 'Disable all output.'
},
2015-08-26 22:16:01 +03:00
];
module.exports = do_wait_instance;