combine delete with start/stop/reboot

This commit is contained in:
Dave Eddy 2015-08-26 12:18:40 -04:00
parent e2edbb3215
commit e61c6099b3
3 changed files with 21 additions and 113 deletions

View File

@ -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

View File

@ -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 <alias|id>',
'',
'{{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;

View File

@ -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 <alias|id>', 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();
});
});