2015-08-26 08:25:26 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent Inc.
|
|
|
|
*
|
|
|
|
* `triton delete ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var common = require('./common');
|
|
|
|
|
|
|
|
function do_delete_instance(subcmd, opts, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-08-26 08:57:27 +03:00
|
|
|
var now = Date.now();
|
|
|
|
|
2015-08-26 08:25:26 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (common.isUUID(arg)) {
|
|
|
|
uuid = arg;
|
|
|
|
go1();
|
|
|
|
} else {
|
|
|
|
self.triton.getMachineByAlias(arg, function (err, machine) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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) {
|
2015-08-26 08:57:27 +03:00
|
|
|
console.log('Deleted (async) instance %s (id %s, %s)',
|
|
|
|
arg, uuid, common.humanDurationFromMs(Date.now() - now));
|
2015-08-26 08:25:26 +03:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.triton.cloudapi.waitForMachineStates({
|
|
|
|
id: uuid,
|
|
|
|
states: ['deleted']
|
|
|
|
}, function (err, machine, res) {
|
|
|
|
if (res && res.statusCode === 410) {
|
|
|
|
// gone... success!
|
2015-08-26 08:57:27 +03:00
|
|
|
console.log('Deleted instance %s (id %s, %s)',
|
|
|
|
arg, uuid, common.humanDurationFromMs(Date.now() - now));
|
2015-08-26 08:25:26 +03:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
} else if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 08:57:27 +03:00
|
|
|
callback(new Error('unknown state'));
|
2015-08-26 08:25:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_delete_instance.aliases = ['delete'];
|
|
|
|
|
|
|
|
do_delete_instance.help = [
|
|
|
|
'delete a single instance.',
|
|
|
|
'',
|
|
|
|
'Usage:',
|
|
|
|
' {{name}} delete <alias|id>',
|
|
|
|
'',
|
|
|
|
'{{options}}'
|
2015-08-26 09:00:26 +03:00
|
|
|
].join('\n');
|
2015-08-26 08:25:26 +03:00
|
|
|
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;
|