2015-08-26 04:46:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent Inc.
|
|
|
|
*
|
|
|
|
* `triton stop-instance ...`
|
2015-08-26 19:18:24 +03:00
|
|
|
* `triton start-instance ...`
|
|
|
|
* `triton reboot-instance ...`
|
2015-08-26 04:46:14 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
var f = require('util').format;
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var common = require('./common');
|
|
|
|
|
|
|
|
function do_startstop_instance(action) {
|
2015-08-26 19:18:40 +03:00
|
|
|
assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0,
|
2015-08-26 04:46:14 +03:00
|
|
|
'invalid action');
|
|
|
|
|
|
|
|
function _do_startstop_instance(subcmd, opts, args, callback) {
|
|
|
|
return _do_instance.call(this, action, subcmd, opts, args, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
_do_startstop_instance.aliases = [action];
|
|
|
|
_do_startstop_instance.help = [
|
2015-08-26 19:18:40 +03:00
|
|
|
f('%s a single instance.', common.capitalize(action)),
|
2015-08-26 04:46:14 +03:00
|
|
|
f(''),
|
|
|
|
f('Usage:'),
|
|
|
|
f(' {{name}} %s <alias|id>', action),
|
|
|
|
f(''),
|
|
|
|
f('{{options}}')
|
2015-08-26 09:00:26 +03:00
|
|
|
].join('\n');
|
2015-08-26 04:46:14 +03:00
|
|
|
_do_startstop_instance.options = [
|
2015-08-26 09:00:26 +03:00
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['wait', 'w'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Block until desired state is reached.'
|
2015-09-01 19:00:45 +03:00
|
|
|
}
|
2015-08-26 04:46:14 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
return _do_startstop_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _do_instance(action, subcmd, opts, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-08-26 08:57:27 +03:00
|
|
|
var now = Date.now();
|
|
|
|
|
2015-08-26 04:46:14 +03:00
|
|
|
var command, state;
|
|
|
|
switch (action) {
|
|
|
|
case 'start':
|
|
|
|
command = 'startMachine';
|
|
|
|
state = 'running';
|
|
|
|
break;
|
|
|
|
case 'stop':
|
|
|
|
command = 'stopMachine';
|
|
|
|
state = 'stopped';
|
|
|
|
break;
|
|
|
|
case 'reboot':
|
|
|
|
command = 'rebootMachine';
|
|
|
|
state = 'running';
|
|
|
|
break;
|
2015-08-26 19:18:40 +03:00
|
|
|
case 'delete':
|
|
|
|
command = 'deleteMachine';
|
|
|
|
state = 'deleted';
|
|
|
|
break;
|
2015-09-01 19:00:45 +03:00
|
|
|
default:
|
|
|
|
callback(new Error('unknown action: ' + action));
|
|
|
|
break;
|
2015-08-26 04:46:14 +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];
|
2015-08-26 19:18:24 +03:00
|
|
|
var uuid, alias;
|
2015-08-26 04:46:14 +03:00
|
|
|
|
|
|
|
if (common.isUUID(arg)) {
|
|
|
|
uuid = arg;
|
|
|
|
go1();
|
|
|
|
} else {
|
2015-09-01 19:00:45 +03:00
|
|
|
self.triton.getInstance(arg, function (err, inst) {
|
2015-08-26 04:46:14 +03:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 19:18:24 +03:00
|
|
|
alias = arg;
|
2015-09-01 19:00:45 +03:00
|
|
|
uuid = inst.id;
|
2015-08-26 04:46:14 +03:00
|
|
|
go1();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function go1() {
|
|
|
|
// called when "uuid" is set
|
|
|
|
self.triton.cloudapi[command](uuid, function (err, body, res) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.wait) {
|
2015-08-26 19:18:24 +03:00
|
|
|
if (alias)
|
2015-08-26 19:18:40 +03:00
|
|
|
console.log('%s (async) instance %s (%s)',
|
|
|
|
common.capitalize(action), alias, uuid);
|
2015-08-26 19:18:24 +03:00
|
|
|
else
|
2015-08-26 19:18:40 +03:00
|
|
|
console.log('%s (async) instance %s',
|
|
|
|
common.capitalize(action), uuid);
|
2015-08-26 04:46:14 +03:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:53:48 +03:00
|
|
|
self.triton.cloudapi.waitForMachineStates({
|
|
|
|
id: uuid,
|
|
|
|
states: [state]
|
2015-09-01 19:00:45 +03:00
|
|
|
}, function (err2, inst2, res2) {
|
|
|
|
if (action === 'delete' && res2 && res2.statusCode === 410) {
|
|
|
|
// This is success, fall through to bottom.
|
|
|
|
/* jsl:pass */
|
|
|
|
} else if (err2) {
|
|
|
|
callback(err2);
|
2015-08-26 04:46:14 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-08-26 19:18:40 +03:00
|
|
|
|
2015-08-26 19:18:24 +03:00
|
|
|
var dur = common.humanDurationFromMs(Date.now() - now);
|
|
|
|
if (alias)
|
2015-08-26 19:18:40 +03:00
|
|
|
console.log('%s instance %s (%s, %s)',
|
|
|
|
common.capitalize(action), alias, uuid, dur);
|
2015-08-26 19:18:24 +03:00
|
|
|
else
|
2015-08-26 19:18:40 +03:00
|
|
|
console.log('%s instance %s (%s)',
|
|
|
|
common.capitalize(action), uuid, dur);
|
|
|
|
|
2015-08-26 04:46:14 +03:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = do_startstop_instance;
|