2014-02-07 23:21:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Joyent Inc. All rights reserved.
|
|
|
|
*
|
2014-02-08 02:49:07 +02:00
|
|
|
* The 'joyentcloud' CLI class.
|
2014-02-07 23:21:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var p = console.log;
|
|
|
|
var e = console.error;
|
|
|
|
var util = require('util'),
|
|
|
|
format = util.format;
|
|
|
|
var child_process = require('child_process'),
|
|
|
|
spawn = child_process.spawn,
|
|
|
|
exec = child_process.exec;
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var async = require('async');
|
|
|
|
var bunyan = require('bunyan');
|
|
|
|
var cmdln = require('cmdln'),
|
|
|
|
Cmdln = cmdln.Cmdln;
|
|
|
|
|
|
|
|
var common = require('./common');
|
2014-02-08 10:15:26 +02:00
|
|
|
var errors = require('./errors');
|
2014-02-08 02:49:07 +02:00
|
|
|
var JoyentCloud = require('./joyentcloud');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- globals
|
|
|
|
|
|
|
|
var pkg = require('../package.json');
|
2014-02-08 02:49:07 +02:00
|
|
|
var name = 'joyentcloud';
|
2014-02-07 23:21:24 +02:00
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: name,
|
|
|
|
serializers: bunyan.stdSerializers,
|
|
|
|
stream: process.stderr,
|
|
|
|
level: 'warn'
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- CLI class
|
|
|
|
|
|
|
|
function CLI() {
|
|
|
|
Cmdln.call(this, {
|
|
|
|
name: pkg.name,
|
|
|
|
desc: pkg.description,
|
|
|
|
options: [
|
|
|
|
{names: ['help', 'h'], type: 'bool', help: 'Print help and exit.'},
|
|
|
|
{name: 'version', type: 'bool', help: 'Print version and exit.'},
|
|
|
|
{names: ['verbose', 'v'], type: 'bool',
|
2014-02-08 04:23:18 +02:00
|
|
|
help: 'Verbose/debug output.'},
|
|
|
|
{names: ['profile', 'p'], type: 'string', env: 'SMRT_PROFILE',
|
|
|
|
helpArg: 'NAME', help: 'SMRT Profile to use.'}
|
2014-02-07 23:21:24 +02:00
|
|
|
],
|
|
|
|
helpOpts: {
|
|
|
|
includeEnv: true,
|
|
|
|
minHelpCol: 23 /* line up with option help */
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
util.inherits(CLI, Cmdln);
|
|
|
|
|
|
|
|
CLI.prototype.init = function (opts, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (opts.version) {
|
|
|
|
p(this.name, pkg.version);
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.opts = opts;
|
|
|
|
if (opts.verbose) {
|
2014-02-08 02:49:07 +02:00
|
|
|
process.env.DEBUG = 1; //TODO This is a lame req of cmdln.main().
|
2014-02-07 23:21:24 +02:00
|
|
|
log.level('trace');
|
|
|
|
log.src = true;
|
|
|
|
}
|
|
|
|
|
2014-02-08 04:23:18 +02:00
|
|
|
this.__defineGetter__('jc', function () {
|
|
|
|
if (self._jc === undefined) {
|
|
|
|
self._jc = new JoyentCloud({log: log, profile: opts.profile});
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
2014-02-08 04:23:18 +02:00
|
|
|
return self._jc;
|
2014-02-07 23:21:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Cmdln class handles `opts.help`.
|
|
|
|
Cmdln.prototype.init.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CLI.prototype.do_profile = function (subcmd, opts, args, callback) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
} else if (args.length > 1) {
|
|
|
|
return callback(new Error('too many args: ' + args));
|
|
|
|
}
|
|
|
|
|
2014-02-08 04:23:18 +02:00
|
|
|
var profs = common.deepObjCopy(this.jc.profiles);
|
|
|
|
var currProfileName = this.jc.profile.name;
|
2014-02-07 23:21:24 +02:00
|
|
|
for (var i = 0; i < profs.length; i++) {
|
|
|
|
profs[i].curr = (profs[i].name === currProfileName ? '*' : ' ');
|
2014-02-08 04:23:18 +02:00
|
|
|
profs[i].dcs = (profs[i].dcs ? profs[i].dcs : ['all'])
|
2014-02-07 23:21:24 +02:00
|
|
|
.join(',');
|
|
|
|
}
|
|
|
|
if (opts.json) {
|
|
|
|
p(JSON.stringify(profs, null, 4));
|
|
|
|
} else {
|
|
|
|
common.tabulate(profs, {
|
|
|
|
columns: 'curr,name,dcs,account,keyId',
|
|
|
|
sort: 'name,account',
|
|
|
|
validFields: 'curr,name,dcs,account,keyId'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
CLI.prototype.do_profile.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON output.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
CLI.prototype.do_profile.help = (
|
|
|
|
'Create, update or inpect joyent CLI profiles.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
|
|
|
+ ' {{name}} profile\n'
|
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
CLI.prototype.do_dcs = function (subcmd, opts, args, callback) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
} else if (args.length > 1) {
|
|
|
|
return callback(new Error('too many args: ' + args));
|
|
|
|
}
|
|
|
|
|
2014-02-08 04:23:18 +02:00
|
|
|
var dcs = this.jc.config.dcs;
|
2014-02-07 23:21:24 +02:00
|
|
|
var dcsArray = Object.keys(dcs).map(
|
|
|
|
function (n) { return {name: n, url: dcs[n]}; });
|
|
|
|
if (opts.json) {
|
|
|
|
p(JSON.stringify(dcsArray, null, 4));
|
|
|
|
} else {
|
|
|
|
common.tabulate(dcsArray, {
|
|
|
|
columns: 'name,url',
|
|
|
|
sort: 'name',
|
|
|
|
validFields: 'name,url'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
CLI.prototype.do_dcs.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON output.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
CLI.prototype.do_dcs.help = (
|
|
|
|
'List, add or remove datacenters.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
|
|
|
+ ' {{name}} dcs\n'
|
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
CLI.prototype.do_machines = function (subcmd, opts, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
} else if (args.length > 1) {
|
|
|
|
return callback(new Error('too many args: ' + args));
|
|
|
|
}
|
|
|
|
|
2014-02-08 10:15:26 +02:00
|
|
|
var machines = [];
|
|
|
|
var errs = [];
|
|
|
|
var res = this.jc.listMachines();
|
|
|
|
res.on('data', function (dc, dcMachines) {
|
|
|
|
for (var i = 0; i < dcMachines.length; i++) {
|
|
|
|
dcMachines[i].dc = dc;
|
|
|
|
machines.push(dcMachines[i]);
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
2014-02-08 10:15:26 +02:00
|
|
|
});
|
|
|
|
res.on('dcError', function (dc, dcErr) {
|
|
|
|
dcErr.dc = dc;
|
|
|
|
errs.push(dcErr);
|
|
|
|
});
|
|
|
|
res.on('end', function () {
|
2014-02-07 23:21:24 +02:00
|
|
|
if (opts.json) {
|
|
|
|
p(JSON.stringify(machines, null, 4));
|
|
|
|
} else {
|
2014-02-08 02:49:07 +02:00
|
|
|
/* BEGIN JSSTYLED */
|
2014-02-07 23:21:24 +02:00
|
|
|
// TODO: get short output down to something like
|
|
|
|
// 'us-west-1 e91897cf testforyunong2 linux running 2013-11-08'
|
|
|
|
// 'us-west-1 e91897cf testforyunong2 ubuntu/13.3.0 running 2013-11-08'
|
2014-02-08 02:49:07 +02:00
|
|
|
/* END JSSTYLED */
|
2014-02-07 23:21:24 +02:00
|
|
|
common.tabulate(machines, {
|
|
|
|
columns: 'dc,id,name,state,created',
|
|
|
|
sort: 'created',
|
2014-02-08 02:49:07 +02:00
|
|
|
validFields: 'dc,id,name,type,state,image,package,memory,'
|
|
|
|
+ 'disk,created,updated,compute_node,primaryIp'
|
2014-02-07 23:21:24 +02:00
|
|
|
});
|
|
|
|
}
|
2014-02-08 10:15:26 +02:00
|
|
|
var err;
|
|
|
|
if (errs.length === 1) {
|
|
|
|
err = errs[0];
|
|
|
|
} else if (errs.length > 1) {
|
|
|
|
err = new errors.MultiError(errs);
|
|
|
|
}
|
|
|
|
callback(err);
|
2014-02-07 23:21:24 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
CLI.prototype.do_machines.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON output.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
CLI.prototype.do_machines.help = (
|
|
|
|
'List machines.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
|
|
|
+ ' {{name}} machines [<filters>...]\n'
|
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---- exports
|
|
|
|
|
|
|
|
module.exports = CLI;
|