clistyle: rest of 'triton instance ...'

This commit is contained in:
Trent Mick 2016-01-04 13:08:16 -08:00
parent accf1f969e
commit d2c20a5161
14 changed files with 260 additions and 121 deletions

View File

@ -197,11 +197,10 @@ function CLI() {
'instance', 'instance',
'instances', 'instances',
'create', 'create',
'start-instance', 'delete',
'stop-instance', 'start',
'reboot-instance', 'stop',
'delete-instance', 'reboot',
'wait-instance',
'ssh', 'ssh',
{ group: 'Images, Packages, Networks' }, { group: 'Images, Packages, Networks' },
'image', 'image',
@ -349,12 +348,10 @@ CLI.prototype.do_image = require('./do_image');
CLI.prototype.do_instance = require('./do_instance'); CLI.prototype.do_instance = require('./do_instance');
CLI.prototype.do_instances = require('./do_instances'); CLI.prototype.do_instances = require('./do_instances');
CLI.prototype.do_create = require('./do_create'); CLI.prototype.do_create = require('./do_create');
CLI.prototype.do_stop_instance = require('./do_startstop_instance')('stop'); CLI.prototype.do_delete = require('./do_delete');
CLI.prototype.do_start_instance = require('./do_startstop_instance')('start'); CLI.prototype.do_start = require('./do_start');
CLI.prototype.do_reboot_instance = require('./do_startstop_instance')('reboot'); CLI.prototype.do_stop = require('./do_stop');
CLI.prototype.do_delete_instance = CLI.prototype.do_reboot = require('./do_reboot');
require('./do_startstop_instance')({action: 'delete', aliases: ['rm']});
CLI.prototype.do_wait_instance = require('./do_wait_instance');
CLI.prototype.do_ssh = require('./do_ssh'); CLI.prototype.do_ssh = require('./do_ssh');
// Packages // Packages

27
lib/do_delete.js Normal file
View File

@ -0,0 +1,27 @@
/*
* 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.
*
* `triton delete ...` bwcompat shortcut for `triton instance delete ...`.
*/
function do_delete(subcmd, opts, args, callback) {
var subcmdArgv = ['node', 'triton', 'instance', 'delete'].concat(args);
this.dispatch('instance', subcmdArgv, callback);
}
do_delete.help = [
'A shortcut for "triton instance delete".',
'',
'Usage:',
' {{name}} delete ...'
].join('\n');
do_delete.aliases = ['rm'];
module.exports = do_delete;

View File

@ -91,8 +91,6 @@ function do_audit(subcmd, opts, args, cb) {
} }
} }
do_audit.aliases = ['audit'];
do_audit.options = [ do_audit.options = [
{ {
names: ['help', 'h'], names: ['help', 'h'],

View File

@ -602,9 +602,6 @@ do_create.helpOpts = {
maxHelpCol: 18 maxHelpCol: 18
}; };
do_create.aliases = ['create'];
module.exports = do_create; module.exports = do_create;
do_create.metadataFromOpts = metadataFromOpts; // export for testing do_create.metadataFromOpts = metadataFromOpts; // export for testing

78
lib/do_instance/do_ssh.js Normal file
View File

@ -0,0 +1,78 @@
/*
* 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 2016 Joyent, Inc.
*
* `triton instance ssh ...`
*/
var spawn = require('child_process').spawn;
var common = require('../common');
function do_ssh(subcmd, opts, args, callback) {
var self = this;
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length === 0) {
callback(new Error('invalid args: ' + args));
return;
}
var id = args.shift();
var user = 'root';
var i = id.indexOf('@');
if (i >= 0) {
user = id.substr(0, i);
id = id.substr(i + 1);
}
this.top.tritonapi.getInstance(id, function (err, inst) {
if (err) {
callback(err);
return;
}
var ip = inst.primaryIp;
if (!ip) {
callback(new Error('primaryIp not found for instance'));
return;
}
args = ['-l', user].concat(ip).concat(args);
self.top.log.info({args: args}, 'forking ssh');
var child = spawn('ssh', args, {stdio: 'inherit'});
child.on('close', function (code) {
process.exit(code);
});
});
}
do_ssh.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
];
do_ssh.help = (
'SSH to the primary IP of an instance\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} ssh <alias|id> [arguments]\n'
+ '\n'
+ '{{options}}'
);
do_ssh.interspersedOptions = false;
module.exports = do_ssh;

View File

@ -5,18 +5,18 @@
*/ */
/* /*
* Copyright 2015 Joyent, Inc. * Copyright 2016 Joyent, Inc.
* *
* `triton wait-instance ...` * `triton instance wait ...`
*/ */
var vasync = require('vasync'); var vasync = require('vasync');
var distractions = require('./distractions'); var distractions = require('../distractions');
var errors = require('./errors'); var errors = require('../errors');
function do_wait_instance(subcmd, opts, args, cb) { function do_wait(subcmd, opts, args, cb) {
var self = this; var self = this;
if (opts.help) { if (opts.help) {
return this.do_help('help', {}, [subcmd], cb); return this.do_help('help', {}, [subcmd], cb);
@ -39,7 +39,7 @@ function do_wait_instance(subcmd, opts, args, cb) {
vasync.forEachParallel({ vasync.forEachParallel({
inputs: ids, inputs: ids,
func: function getInst(id, nextInst) { func: function getInst(id, nextInst) {
self.tritonapi.getInstance(id, function (err, inst) { self.top.tritonapi.getInstance(id, function (err, inst) {
if (err) { if (err) {
return nextInst(err); return nextInst(err);
} }
@ -87,7 +87,7 @@ function do_wait_instance(subcmd, opts, args, cb) {
vasync.forEachParallel({ vasync.forEachParallel({
inputs: idsToWaitFor, inputs: idsToWaitFor,
func: function waitForInst(id, nextInst) { func: function waitForInst(id, nextInst) {
self.tritonapi.cloudapi.waitForMachineStates({ self.top.tritonapi.cloudapi.waitForMachineStates({
id: id, id: id,
states: states states: states
}, function (err, inst, res) { }, function (err, inst, res) {
@ -110,8 +110,7 @@ function do_wait_instance(subcmd, opts, args, cb) {
}); });
} }
do_wait_instance.aliases = ['wait']; do_wait.help = [
do_wait_instance.help = [
'Wait on instances changing state.', 'Wait on instances changing state.',
'', '',
'Usage:', 'Usage:',
@ -119,10 +118,10 @@ do_wait_instance.help = [
'', '',
'{{options}}', '{{options}}',
'Where "states" is a comma-separated list of target instance states,', 'Where "states" is a comma-separated list of target instance states,',
'by default "running,failed". In other words, "triton wait foo0" will', 'by default "running,failed". In other words, "triton inst wait foo0" will',
'wait for instance "foo0" to complete provisioning.' 'wait for instance "foo0" to complete provisioning.'
].join('\n'); ].join('\n');
do_wait_instance.options = [ do_wait.options = [
{ {
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
@ -138,4 +137,4 @@ do_wait_instance.options = [
} }
]; ];
module.exports = do_wait_instance; module.exports = do_wait;

View File

@ -7,24 +7,27 @@
/* /*
* Copyright 2015 Joyent, Inc. * Copyright 2015 Joyent, Inc.
* *
* `triton stop-instance ...` * `triton instance start ...`
* `triton start-instance ...` * `triton instance stop ...`
* `triton reboot-instance ...` * `triton instance reboot ...`
* `triton instance delete ...`
*/ */
var f = require('util').format;
var assert = require('assert-plus'); var assert = require('assert-plus');
var common = require('./common');
var vasync = require('vasync'); var vasync = require('vasync');
var common = require('../common');
var f = require('util').format;
function perror(err) { function perror(err) {
console.error('error: %s', err.message); console.error('error: %s', err.message);
} }
function do_startstop_instance(opts) {
if (typeof (opts) === 'string') function gen_do_ACTION(opts) {
opts = {action: opts};
assert.object(opts, 'opts'); assert.object(opts, 'opts');
assert.string(opts.action, 'opts.action'); assert.string(opts.action, 'opts.action');
assert.optionalArrayOfString(opts.aliases, 'opts.aliases'); assert.optionalArrayOfString(opts.aliases, 'opts.aliases');
@ -33,16 +36,15 @@ function do_startstop_instance(opts) {
assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0, assert.ok(['start', 'stop', 'reboot', 'delete'].indexOf(action) >= 0,
'invalid action'); 'invalid action');
function _do_startstop_instance(subcmd, _opts, args, callback) { function do_ACTION(subcmd, _opts, args, callback) {
return _do_instance.call(this, action, subcmd, _opts, args, callback); return _doTheAction.call(this, action, subcmd, _opts, args, callback);
} }
var aliases = [action]; if (opts.aliases) {
if (opts.aliases) do_ACTION.aliases = opts.aliases;
aliases = aliases.concat(opts.aliases); }
_do_startstop_instance.aliases = aliases;
_do_startstop_instance.help = [ do_ACTION.help = [
f('%s one or more instances.', common.capitalize(action)), f('%s one or more instances.', common.capitalize(action)),
f(''), f(''),
f('Usage:'), f('Usage:'),
@ -50,7 +52,7 @@ function do_startstop_instance(opts) {
f(''), f(''),
f('{{options}}') f('{{options}}')
].join('\n'); ].join('\n');
_do_startstop_instance.options = [ do_ACTION.options = [
{ {
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
@ -59,14 +61,14 @@ function do_startstop_instance(opts) {
{ {
names: ['wait', 'w'], names: ['wait', 'w'],
type: 'bool', type: 'bool',
help: 'Block until desired state is reached.' help: 'Block until instance state indicates the action is complete.'
} }
]; ];
return _do_startstop_instance; return do_ACTION;
} }
function _do_instance(action, subcmd, opts, args, callback) { function _doTheAction(action, subcmd, opts, args, callback) {
var self = this; var self = this;
var now = Date.now(); var now = Date.now();
@ -109,7 +111,7 @@ function _do_instance(action, subcmd, opts, args, callback) {
uuid = arg; uuid = arg;
done(); done();
} else { } else {
self.tritonapi.getInstance(arg, function (err, inst) { self.top.tritonapi.getInstance(arg, function (err, inst) {
if (err) { if (err) {
perror(err); perror(err);
cb(err); cb(err);
@ -123,7 +125,7 @@ function _do_instance(action, subcmd, opts, args, callback) {
// called when "uuid" is set // called when "uuid" is set
function done() { function done() {
self.tritonapi.cloudapi[command](uuid, self.top.tritonapi.cloudapi[command](uuid,
function (err, body, res) { function (err, body, res) {
if (err) { if (err) {
@ -143,7 +145,7 @@ function _do_instance(action, subcmd, opts, args, callback) {
return; return;
} }
self.tritonapi.cloudapi.waitForMachineStates({ self.top.tritonapi.cloudapi.waitForMachineStates({
id: uuid, id: uuid,
states: [state] states: [state]
}, function (err2, inst2, res2) { }, function (err2, inst2, res2) {
@ -177,4 +179,4 @@ function _do_instance(action, subcmd, opts, args, callback) {
}); });
} }
module.exports = do_startstop_instance; module.exports = gen_do_ACTION;

View File

@ -13,6 +13,7 @@
var Cmdln = require('cmdln').Cmdln; var Cmdln = require('cmdln').Cmdln;
var util = require('util'); var util = require('util');
var gen_do_ACTION = require('./gen_do_ACTION');
// ---- CLI class // ---- CLI class
@ -34,6 +35,14 @@ function InstanceCLI(top) {
'list', 'list',
'get', 'get',
'create', 'create',
'delete',
{ group: '' },
'start',
'stop',
'reboot',
{ group: '' },
'ssh',
'wait',
'audit' 'audit'
] ]
}); });
@ -48,8 +57,16 @@ InstanceCLI.prototype.init = function init(opts, args, cb) {
InstanceCLI.prototype.do_list = require('./do_list'); InstanceCLI.prototype.do_list = require('./do_list');
InstanceCLI.prototype.do_get = require('./do_get'); InstanceCLI.prototype.do_get = require('./do_get');
InstanceCLI.prototype.do_create = require('./do_create'); InstanceCLI.prototype.do_create = require('./do_create');
InstanceCLI.prototype.do_audit = require('./do_audit'); InstanceCLI.prototype.do_delete = gen_do_ACTION(
{action: 'delete', aliases: ['rm']});
InstanceCLI.prototype.do_start = gen_do_ACTION({action: 'start'});
InstanceCLI.prototype.do_stop = gen_do_ACTION({action: 'stop'});
InstanceCLI.prototype.do_reboot = gen_do_ACTION({action: 'reboot'});
InstanceCLI.prototype.do_ssh = require('./do_ssh');
InstanceCLI.prototype.do_wait = require('./do_wait');
InstanceCLI.prototype.do_audit = require('./do_audit');
InstanceCLI.aliases = ['inst']; InstanceCLI.aliases = ['inst'];

25
lib/do_reboot.js Normal file
View File

@ -0,0 +1,25 @@
/*
* 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.
*
* `triton reboot ...` bwcompat shortcut for `triton instance reboot ...`.
*/
function do_reboot(subcmd, opts, args, callback) {
var subcmdArgv = ['node', 'triton', 'instance', 'reboot'].concat(args);
this.dispatch('instance', subcmdArgv, callback);
}
do_reboot.help = [
'A shortcut for "triton instance reboot".',
'',
'Usage:',
' {{name}} reboot ...'
].join('\n');
module.exports = do_reboot;

View File

@ -5,72 +5,21 @@
*/ */
/* /*
* Copyright 2015 Joyent, Inc. * Copyright 2016 Joyent, Inc.
* *
* `triton ssh ...` * `triton ssh ...` bwcompat shortcut for `triton instance ssh ...`.
*/ */
var common = require('./common');
var spawn = require('child_process').spawn;
function do_ssh(subcmd, opts, args, callback) { function do_ssh(subcmd, opts, args, callback) {
var self = this; var subcmdArgv = ['node', 'triton', 'instance', 'ssh'].concat(args);
this.dispatch('instance', subcmdArgv, callback);
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length === 0) {
callback(new Error('invalid args: ' + args));
return;
}
var id = args.shift();
var user = 'root';
var i = id.indexOf('@');
if (i >= 0) {
user = id.substr(0, i);
id = id.substr(i + 1);
}
this.tritonapi.getInstance(id, function (err, inst) {
if (err) {
callback(err);
return;
}
var ip = inst.primaryIp;
if (!ip) {
callback(new Error('primaryIp not found for instance'));
return;
}
args = ['-l', user].concat(ip).concat(args);
self.tritonapi.log.info({args: args}, 'forking ssh');
var child = spawn('ssh', args, {stdio: 'inherit'});
child.on('close', function (code) {
process.exit(code);
});
});
} }
do_ssh.options = [ do_ssh.help = [
{ 'A shortcut for "triton instance ssh".',
names: ['help', 'h'], '',
type: 'bool', 'Usage:',
help: 'Show this help.' ' {{name}} ssh ...'
} ].join('\n');
];
do_ssh.help = (
'SSH to the primary IP of an instance\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} ssh <alias|id> [arguments]\n'
+ '\n'
+ '{{options}}'
);
do_ssh.interspersedOptions = false;
module.exports = do_ssh; module.exports = do_ssh;

25
lib/do_start.js Normal file
View File

@ -0,0 +1,25 @@
/*
* 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.
*
* `triton start ...` bwcompat shortcut for `triton instance start ...`.
*/
function do_start(subcmd, opts, args, callback) {
var subcmdArgv = ['node', 'triton', 'instance', 'start'].concat(args);
this.dispatch('instance', subcmdArgv, callback);
}
do_start.help = [
'A shortcut for "triton instance start".',
'',
'Usage:',
' {{name}} start ...'
].join('\n');
module.exports = do_start;

25
lib/do_stop.js Normal file
View File

@ -0,0 +1,25 @@
/*
* 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.
*
* `triton stop ...` bwcompat shortcut for `triton instance stop ...`.
*/
function do_stop(subcmd, opts, args, callback) {
var subcmdArgv = ['node', 'triton', 'instance', 'stop'].concat(args);
this.dispatch('instance', subcmdArgv, callback);
}
do_stop.help = [
'A shortcut for "triton instance stop".',
'',
'Usage:',
' {{name}} stop ...'
].join('\n');
module.exports = do_stop;

View File

@ -259,8 +259,8 @@ test('triton manage workflow', opts, function (tt) {
}); });
// wait for the machine to start // wait for the machine to start
tt.test(' triton wait', function (t) { tt.test(' triton inst wait', function (t) {
h.safeTriton(t, ['wait', instance.id], h.safeTriton(t, ['inst', 'wait', instance.id],
function (stdout) { function (stdout) {
// parse JSON response // parse JSON response

View File

@ -35,12 +35,12 @@ var subs = [
['instance list', 'instance ls', 'instances', 'insts', 'ls'], ['instance list', 'instance ls', 'instances', 'insts', 'ls'],
['instance get'], ['instance get'],
['instance create', 'create'], ['instance create', 'create'],
['instance start', 'start'],
['instance stop', 'stop'],
['instance reboot', 'reboot'],
['instance delete', 'instance rm', 'delete', 'rm'],
['instance wait'],
['instance audit'], ['instance audit'],
['start-instance', 'start'],
['stop-instance', 'stop'],
['reboot-instance', 'reboot'],
['delete-instance', 'delete'],
['wait-instance', 'wait'],
['ssh'], ['ssh'],
['networks'], ['networks'],
['network'], ['network'],