2015-08-26 07:16:41 +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 07:16:41 +03:00
|
|
|
*
|
|
|
|
* `triton info ...`
|
|
|
|
*/
|
|
|
|
|
2015-08-26 07:27:59 +03:00
|
|
|
var assert = require('assert-plus');
|
2015-08-26 07:16:41 +03:00
|
|
|
var common = require('./common');
|
2015-08-26 07:27:59 +03:00
|
|
|
var format = require('util').format;
|
2015-08-26 07:16:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
this.tritonapi.cloudapi.getAccount(cb.bind('account')); i++;
|
|
|
|
this.tritonapi.cloudapi.listMachines(cb.bind('machines')); i++;
|
2015-08-26 07:16:41 +03:00
|
|
|
|
|
|
|
function cb(err, data) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
out[this.toString()] = data;
|
|
|
|
if (--i === 0)
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
function done() {
|
2015-09-04 01:24:14 +03:00
|
|
|
// parse name
|
|
|
|
var name;
|
|
|
|
if (out.account.firstName && out.account.lastName)
|
|
|
|
name = format('%s %s', out.account.firstName,
|
|
|
|
out.account.lastName);
|
|
|
|
else if (out.account.firstName)
|
|
|
|
name = out.account.firstName;
|
|
|
|
|
|
|
|
// parse machine states and accounting
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
disk *= 1000 * 1000;
|
|
|
|
memory *= 1000 * 1000;
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
data.login = out.account.login;
|
|
|
|
if (name)
|
|
|
|
data.name = name;
|
|
|
|
data.email = out.account.email;
|
2015-09-04 21:04:45 +03:00
|
|
|
data.url = self.tritonapi.cloudapi.url;
|
2015-09-04 01:24:14 +03:00
|
|
|
data.totalDisk = disk;
|
|
|
|
data.totalMemory = memory;
|
|
|
|
|
2015-08-26 07:16:41 +03:00
|
|
|
if (opts.json) {
|
2015-09-04 20:44:40 +03:00
|
|
|
data.totalInstances = out.machines.length;
|
|
|
|
data.instances = states;
|
2015-09-04 01:24:14 +03:00
|
|
|
console.log(JSON.stringify(data));
|
2015-08-26 07:16:41 +03:00
|
|
|
} else {
|
2015-09-04 01:24:14 +03:00
|
|
|
data.totalDisk = common.humanSizeFromBytes(disk);
|
|
|
|
data.totalMemory = common.humanSizeFromBytes(memory);
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
console.log('%s: %s', key, data[key]);
|
2015-08-26 07:16:41 +03:00
|
|
|
});
|
2015-09-04 20:44:40 +03:00
|
|
|
console.log('instances: %d', out.machines.length);
|
2015-09-04 01:24:14 +03:00
|
|
|
Object.keys(states).forEach(function (key) {
|
2015-09-04 20:44:40 +03:00
|
|
|
console.log(' %s: %d', key, states[key]);
|
2015-08-26 07:16:41 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_info.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON output.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
do_info.help = (
|
2015-08-26 07:19:22 +03:00
|
|
|
'Print an account summary.\n'
|
2015-08-26 07:16:41 +03:00
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
2015-10-30 00:24:37 +02:00
|
|
|
+ ' {{name}} info\n'
|
2015-08-26 07:16:41 +03:00
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
|
|
|
module.exports = do_info;
|