From 6bd918eb81559f79907e2817b676d5e4bcc7b234 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 00:16:41 -0400 Subject: [PATCH] do_info --- lib/cli.js | 2 + lib/cloudapi2.js | 5 +++ lib/do_info.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 103 insertions(+) create mode 100644 lib/do_info.js diff --git a/lib/cli.js b/lib/cli.js index 1df36b1..bb2e2ba 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -60,6 +60,7 @@ function CLI() { 'help', { group: 'Operator Commands' }, 'account', + 'info', { group: 'Instances (aka VMs/Machines/Containers)' }, 'create-instance', 'instances', @@ -111,6 +112,7 @@ CLI.prototype.init = function (opts, args, callback) { // Operator CLI.prototype.do_account = require('./do_account'); +CLI.prototype.do_info = require('./do_info'); // Images CLI.prototype.do_images = require('./do_images'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 561a8ca..72e4673 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -449,6 +449,7 @@ CloudAPI.prototype.waitForMachineStates = function waitForMachineStates(opts, ca CloudAPI.prototype.createListMachinesStream = function createListMachinesStream(options) { var self = this; + options = options || {}; // if the user specifies an offset we don't paginate var once = options.limit !== undefined; @@ -481,6 +482,10 @@ function createListMachinesStream(options) { * @param {Function} callback - called like `function (err, machines)` */ CloudAPI.prototype.listMachines = function listMachines(options, callback) { + if (typeof (options) === 'function') { + callback = options; + options = {}; + } var machines = []; var s = this.createListMachinesStream(options); s.on('error', function (e) { diff --git a/lib/do_info.js b/lib/do_info.js new file mode 100644 index 0000000..d555edd --- /dev/null +++ b/lib/do_info.js @@ -0,0 +1,95 @@ +/* + * Copyright 2015 Joyent Inc. + * + * `triton info ...` + */ + +var common = require('./common'); +var f = require('util').format; + +var prettybytes = require('pretty-bytes'); + +function do_info(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 out = {}; + var i = 0; + + this.triton.cloudapi.getAccount(cb.bind('account')); i++; + this.triton.cloudapi.listMachines(cb.bind('machines')); i++; + + function cb(err, data) { + if (err) { + callback(err); + return; + } + out[this.toString()] = data; + if (--i === 0) + done(); + } + + function done() { + if (opts.json) { + console.log(JSON.stringify(out)); + } else { + // pretty print + console.log('%s - %s %s <%s>', + out.account.login, + out.account.firstName, + out.account.lastName, + out.account.email); + console.log(self.triton.cloudapi.url); + console.log(); + console.log('%d instance(s)', out.machines.length); + var states = {}; + var disk = 0; + var memory = 0; + out.machines.forEach(function (machine) { + var state = machine.state; + states[state] = states[state] || 0; + states[state]++; + memory += machine.memory; + disk += machine.disk; + }); + Object.keys(states).forEach(function (state) { + console.log('- %d %s', states[state], state); + }); + console.log('- %s RAM Total', prettybytes(memory * 1000 * 1000)); + console.log('- %s Disk Total', prettybytes(disk * 1000 * 1000)); + } + callback(); + } +} + +do_info.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, + { + names: ['json', 'j'], + type: 'bool', + help: 'JSON output.' + } +]; +do_info.help = ( + 'Show account information\n' + + '\n' + + 'Usage:\n' + + ' {{name}} account\n' + + '\n' + + '{{options}}' +); + +do_info.aliases = ['whoami']; + +module.exports = do_info; diff --git a/package.json b/package.json index aaba95e..a25e8fc 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "mkdirp": "0.5.1", "node-uuid": "1.4.3", "once": "1.3.2", + "pretty-bytes": "2.0.1", "restify-clients": "1.0.0", "smartdc-auth": "git+ssh://git@github.com:joyent/node-smartdc-auth.git#9f21966", "tabula": "1.4.2",