From 9b57df6962544426fa3abd6d6aec3c09e8566332 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 15:16:01 -0400 Subject: [PATCH] add wait-instance --- lib/cli.js | 2 + lib/do_wait_instance.js | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 lib/do_wait_instance.js diff --git a/lib/cli.js b/lib/cli.js index 2ef3956..ae5313d 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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 diff --git a/lib/do_wait_instance.js b/lib/do_wait_instance.js new file mode 100644 index 0000000..946661f --- /dev/null +++ b/lib/do_wait_instance.js @@ -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 [state]', + '', + '{{options}}' +].join('\n'); +do_wait_instance.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, +]; + +module.exports = do_wait_instance;