From e61c6099b3e51c81c2f42db85f9710f45f7875b7 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 12:18:40 -0400 Subject: [PATCH] combine delete with start/stop/reboot --- lib/cli.js | 2 +- lib/do_delete_instance.js | 104 ----------------------------------- lib/do_startstop_instance.js | 28 +++++++--- 3 files changed, 21 insertions(+), 113 deletions(-) delete mode 100644 lib/do_delete_instance.js diff --git a/lib/cli.js b/lib/cli.js index f9e016d..927b34a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -129,7 +129,7 @@ CLI.prototype.do_instance_audit = require('./do_instance_audit'); 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_delete_instance'); +CLI.prototype.do_delete_instance = require('./do_startstop_instance')('delete'); CLI.prototype.do_ssh = require('./do_ssh'); // Packages diff --git a/lib/do_delete_instance.js b/lib/do_delete_instance.js deleted file mode 100644 index e6d6ebd..0000000 --- a/lib/do_delete_instance.js +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2015 Joyent Inc. - * - * `triton delete ...` - */ - -var common = require('./common'); - -function do_delete_instance(subcmd, opts, args, callback) { - var self = this; - - var now = Date.now(); - - if (opts.help) { - this.do_help('help', {}, [subcmd], callback); - return; - } else if (args.length !== 1) { - callback(new Error('invalid args: ' + args)); - return; - } - - var arg = args[0]; - var uuid, alias; - - if (common.isUUID(arg)) { - uuid = arg; - go1(); - } else { - self.triton.getMachineByAlias(arg, function (err, machine) { - if (err) { - callback(err); - return; - } - alias = arg; - uuid = machine.id; - go1(); - }); - } - - function go1() { - // called when "uuid" is set - self.triton.cloudapi.deleteMachine(uuid, function (err, body, res) { - if (err) { - callback(err); - return; - } - - if (!opts.wait) { - if (alias) - console.log('Deleted (async) instance %s (%s)', alias, uuid); - else - console.log('Deleted (async) instance %s', uuid); - callback(); - return; - } - - self.triton.cloudapi.waitForMachineStates({ - id: uuid, - states: ['deleted'] - }, function (err, machine, res) { - if (res && res.statusCode === 410) { - // gone... success! - var dur = common.humanDurationFromMs(Date.now() - now); - if (alias) - console.log('Deleted instance %s (%s, %s)', alias, uuid, dur); - else - console.log('Deleted instance %s (%s)', uuid, dur); - callback(); - return; - } else if (err) { - callback(err); - return; - } - callback(new Error('unknown state')); - }); - }); - } -} - -do_delete_instance.aliases = ['delete']; - -do_delete_instance.help = [ - 'delete a single instance.', - '', - 'Usage:', - ' {{name}} delete ', - '', - '{{options}}' -].join('\n'); -do_delete_instance.options = [ - { - names: ['help', 'h'], - type: 'bool', - help: 'Show this help.' - }, - { - names: ['wait', 'w'], - type: 'bool', - help: 'Wait for machine to be deleted.' - } -]; - - -module.exports = do_delete_instance; diff --git a/lib/do_startstop_instance.js b/lib/do_startstop_instance.js index 823db96..0ef3da4 100644 --- a/lib/do_startstop_instance.js +++ b/lib/do_startstop_instance.js @@ -11,7 +11,7 @@ var assert = require('assert-plus'); var common = require('./common'); function do_startstop_instance(action) { - assert.ok(['start', 'stop', 'reboot'].indexOf(action) >= 0, + assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0, 'invalid action'); function _do_startstop_instance(subcmd, opts, args, callback) { @@ -20,7 +20,7 @@ function do_startstop_instance(action) { _do_startstop_instance.aliases = [action]; _do_startstop_instance.help = [ - f('%s a single instance.', action), + f('%s a single instance.', common.capitalize(action)), f(''), f('Usage:'), f(' {{name}} %s ', action), @@ -62,6 +62,10 @@ function _do_instance(action, subcmd, opts, args, callback) { command = 'rebootMachine'; state = 'running'; break; + case 'delete': + command = 'deleteMachine'; + state = 'deleted'; + break; } if (opts.help) { @@ -100,9 +104,11 @@ function _do_instance(action, subcmd, opts, args, callback) { if (!opts.wait) { if (alias) - console.log('%s (async) instance %s (%s)', common.capitalize(action), alias, uuid); + console.log('%s (async) instance %s (%s)', + common.capitalize(action), alias, uuid); else - console.log('%s (async) instance %s', common.capitalize(action), uuid); + console.log('%s (async) instance %s', + common.capitalize(action), uuid); callback(); return; } @@ -110,16 +116,22 @@ function _do_instance(action, subcmd, opts, args, callback) { self.triton.cloudapi.waitForMachineStates({ id: uuid, states: [state] - }, function (err, machine) { - if (err) { + }, function (err, machine, res) { + if (action === 'delete' && res && res.statusCode === 410) { + // this is success, fall through to bottom + } else if (err) { callback(err); return; } + var dur = common.humanDurationFromMs(Date.now() - now); if (alias) - console.log('%s instance %s (%s, %s)', common.capitalize(action), alias, uuid, dur); + console.log('%s instance %s (%s, %s)', + common.capitalize(action), alias, uuid, dur); else - console.log('%s instance %s (%s)', common.capitalize(action), uuid, dur); + console.log('%s instance %s (%s)', + common.capitalize(action), uuid, dur); + callback(); }); });