2015-08-26 04:46:14 +03:00
|
|
|
/*
|
2015-09-04 21:12:20 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2015 Joyent, Inc.
|
2015-08-26 04:46:14 +03:00
|
|
|
*
|
2016-01-04 23:08:16 +02:00
|
|
|
* `triton instance start ...`
|
|
|
|
* `triton instance stop ...`
|
|
|
|
* `triton instance reboot ...`
|
|
|
|
* `triton instance delete ...`
|
2015-08-26 04:46:14 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
2015-10-06 23:54:55 +03:00
|
|
|
var vasync = require('vasync');
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
|
|
|
|
var f = require('util').format;
|
|
|
|
|
|
|
|
|
2015-10-06 23:54:55 +03:00
|
|
|
function perror(err) {
|
|
|
|
console.error('error: %s', err.message);
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
|
|
|
|
function gen_do_ACTION(opts) {
|
2015-10-14 23:15:45 +03:00
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.action, 'opts.action');
|
|
|
|
assert.optionalArrayOfString(opts.aliases, 'opts.aliases');
|
|
|
|
var action = opts.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');
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
function do_ACTION(subcmd, _opts, args, callback) {
|
|
|
|
return _doTheAction.call(this, action, subcmd, _opts, args, callback);
|
2015-08-26 04:46:14 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
if (opts.aliases) {
|
|
|
|
do_ACTION.aliases = opts.aliases;
|
|
|
|
}
|
2015-10-14 23:15:45 +03:00
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
do_ACTION.help = [
|
2015-10-30 00:24:37 +02:00
|
|
|
f('%s one or more instances.', common.capitalize(action)),
|
2015-08-26 04:46:14 +03:00
|
|
|
f(''),
|
|
|
|
f('Usage:'),
|
2015-10-30 00:24:37 +02:00
|
|
|
f(' {{name}} %s <alias|id> ...', action),
|
2015-08-26 04:46:14 +03:00
|
|
|
f(''),
|
|
|
|
f('{{options}}')
|
2015-08-26 09:00:26 +03:00
|
|
|
].join('\n');
|
2016-01-04 23:08:16 +02:00
|
|
|
do_ACTION.options = [
|
2015-08-26 09:00:26 +03:00
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['wait', 'w'],
|
|
|
|
type: 'bool',
|
2016-01-04 23:08:16 +02:00
|
|
|
help: 'Block until instance state indicates the action is complete.'
|
2015-09-01 19:00:45 +03:00
|
|
|
}
|
2015-08-26 04:46:14 +03:00
|
|
|
];
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
return do_ACTION;
|
2015-08-26 04:46:14 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
function _doTheAction(action, subcmd, opts, args, callback) {
|
2015-08-26 04:46:14 +03:00
|
|
|
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;
|
2015-10-06 23:54:55 +03:00
|
|
|
} else if (args.length < 1) {
|
2015-08-26 04:46:14 +03:00
|
|
|
callback(new Error('invalid args: ' + args));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-06 23:54:55 +03:00
|
|
|
vasync.forEachParallel({
|
|
|
|
func: function (arg, cb) {
|
|
|
|
var alias, uuid;
|
|
|
|
if (common.isUUID(arg)) {
|
|
|
|
uuid = arg;
|
|
|
|
done();
|
|
|
|
} else {
|
2016-01-04 23:08:16 +02:00
|
|
|
self.top.tritonapi.getInstance(arg, function (err, inst) {
|
2015-10-06 23:54:55 +03:00
|
|
|
if (err) {
|
|
|
|
perror(err);
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
alias = arg;
|
|
|
|
uuid = inst.id;
|
|
|
|
done();
|
|
|
|
});
|
2015-08-26 04:46:14 +03:00
|
|
|
}
|
|
|
|
|
2015-10-06 23:54:55 +03:00
|
|
|
// called when "uuid" is set
|
|
|
|
function done() {
|
2016-01-04 23:08:16 +02:00
|
|
|
self.top.tritonapi.cloudapi[command](uuid,
|
2015-10-06 23:54:55 +03:00
|
|
|
function (err, body, res) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
perror(err);
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.wait) {
|
|
|
|
if (alias)
|
|
|
|
console.log('%s (async) instance %s (%s)',
|
|
|
|
common.capitalize(action), alias, uuid);
|
|
|
|
else
|
|
|
|
console.log('%s (async) instance %s',
|
|
|
|
common.capitalize(action), uuid);
|
|
|
|
cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
self.top.tritonapi.cloudapi.waitForMachineStates({
|
2015-10-06 23:54:55 +03:00
|
|
|
id: uuid,
|
|
|
|
states: [state]
|
|
|
|
}, function (err2, inst2, res2) {
|
|
|
|
if (action === 'delete' &&
|
|
|
|
res2 && res2.statusCode === 410) {
|
|
|
|
// This is success, fall through to bottom.
|
|
|
|
/* jsl:pass */
|
|
|
|
} else if (err2) {
|
|
|
|
perror(err2);
|
|
|
|
cb(err2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dur = common.humanDurationFromMs(Date.now() - now);
|
|
|
|
if (alias)
|
|
|
|
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);
|
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
2015-08-26 04:46:14 +03:00
|
|
|
}
|
2015-10-06 23:54:55 +03:00
|
|
|
},
|
|
|
|
inputs: args
|
|
|
|
}, function (err, results) {
|
|
|
|
var e = err ? (new Error('command failure')) : null;
|
|
|
|
callback(e);
|
|
|
|
});
|
2015-08-26 04:46:14 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 23:08:16 +02:00
|
|
|
module.exports = gen_do_ACTION;
|