2015-08-26 01:30:25 +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 01:30:25 +03:00
|
|
|
*
|
|
|
|
* `triton packages ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var tabula = require('tabula');
|
|
|
|
|
|
|
|
var common = require('./common');
|
|
|
|
|
2015-09-04 01:09:21 +03:00
|
|
|
// valid filters to pass to cloudapi.listPackages
|
|
|
|
var validFilters = [
|
|
|
|
'name',
|
|
|
|
'memory',
|
|
|
|
'disk',
|
|
|
|
'swap',
|
|
|
|
'lwps',
|
|
|
|
'version',
|
|
|
|
'vcpus',
|
|
|
|
'group'
|
|
|
|
];
|
|
|
|
|
|
|
|
// columns default without -o
|
2015-12-16 20:56:22 +02:00
|
|
|
var columnsDefault = 'shortid,name,default,memory,swap,disk,vcpus';
|
2015-09-04 01:09:21 +03:00
|
|
|
|
|
|
|
// columns default with -l
|
2015-12-16 20:56:22 +02:00
|
|
|
var columnsDefaultLong = 'id,name,default,memory,swap,disk,vcpus';
|
2015-09-04 01:09:21 +03:00
|
|
|
|
|
|
|
// sort default with -s
|
|
|
|
var sortDefault = '_groupPlus,memory';
|
|
|
|
|
2015-09-02 20:47:06 +03:00
|
|
|
function do_packages(subcmd, opts, args, callback) {
|
2015-09-04 20:01:55 +03:00
|
|
|
var i;
|
2015-08-26 01:30:25 +03:00
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:09:21 +03:00
|
|
|
var columns = columnsDefault;
|
2015-08-26 20:02:01 +03:00
|
|
|
if (opts.o) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = opts.o;
|
2015-08-26 20:02:01 +03:00
|
|
|
} else if (opts.long) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columnsDefaultLong;
|
2015-08-26 20:02:01 +03:00
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columns.split(',');
|
2015-09-04 09:52:12 +03:00
|
|
|
var rightAligned = {memory: true, disk: true, swap: true,
|
|
|
|
vcpus: true, lwps: true};
|
2015-09-04 20:01:55 +03:00
|
|
|
for (i = 0; i < columns.length; i++) {
|
2015-09-04 09:52:12 +03:00
|
|
|
if (rightAligned[columns[i]]) {
|
|
|
|
columns[i] = {lookup: columns[i], align: 'right'};
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
2015-08-26 01:30:25 +03:00
|
|
|
|
|
|
|
var listOpts;
|
|
|
|
try {
|
|
|
|
listOpts = common.kvToObj(args, validFilters);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
this.tritonapi.cloudapi.listPackages(listOpts, function (err, pkgs) {
|
2015-08-26 02:46:14 +03:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-26 01:30:25 +03:00
|
|
|
if (opts.json) {
|
2015-08-26 23:53:23 +03:00
|
|
|
common.jsonStream(pkgs);
|
2015-08-26 01:30:25 +03:00
|
|
|
} else {
|
2015-09-04 20:01:55 +03:00
|
|
|
for (i = 0; i < pkgs.length; i++) {
|
2015-08-26 20:02:01 +03:00
|
|
|
var pkg = pkgs[i];
|
|
|
|
pkg.shortid = pkg.id.split('-', 1)[0];
|
2015-09-01 01:16:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We take a slightly "smarter" view of "group" for default
|
|
|
|
* sorting, to accomodate usage in the JPC. More recent
|
|
|
|
* common usage is for packages to have "foo-*" naming.
|
|
|
|
* JPC includes package sets of yore *and* recent that don't
|
|
|
|
* use the "group" field. We secondarily separate those
|
|
|
|
* on a possible "foo-" prefix.
|
|
|
|
*/
|
|
|
|
pkg._groupPlus = (pkg.group || (pkg.name.indexOf('-') === -1
|
2015-09-01 22:03:52 +03:00
|
|
|
? '' : pkg.name.split('-', 1)[0]));
|
2015-09-01 01:16:12 +03:00
|
|
|
|
|
|
|
if (!opts.p) {
|
2015-08-31 22:13:17 +03:00
|
|
|
pkg.memoryHuman = common.humanSizeFromBytes({
|
|
|
|
precision: 1,
|
|
|
|
narrow: true
|
|
|
|
}, pkg.memory * 1024 * 1024);
|
|
|
|
pkg.swapHuman = common.humanSizeFromBytes({
|
|
|
|
precision: 1,
|
|
|
|
narrow: true
|
|
|
|
}, pkg.swap * 1024 * 1024);
|
|
|
|
pkg.diskHuman = common.humanSizeFromBytes({
|
|
|
|
precision: 1,
|
|
|
|
narrow: true
|
|
|
|
}, pkg.disk * 1024 * 1024);
|
2015-12-16 20:56:22 +02:00
|
|
|
pkg.vcpusHuman = pkg.vcpus === 0 ? '-' : pkg.vcpus;
|
2015-08-31 22:13:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 01:16:12 +03:00
|
|
|
if (!opts.p) {
|
2015-08-31 22:13:17 +03:00
|
|
|
columns = columns.map(function (c) {
|
2015-09-04 09:52:12 +03:00
|
|
|
switch (c.lookup || c) {
|
2015-08-31 22:13:17 +03:00
|
|
|
case 'memory':
|
2015-09-04 09:52:12 +03:00
|
|
|
return {lookup: 'memoryHuman', name: 'MEMORY',
|
|
|
|
align: 'right'};
|
2015-08-31 22:13:17 +03:00
|
|
|
case 'swap':
|
2015-09-04 09:52:12 +03:00
|
|
|
return {lookup: 'swapHuman', name: 'SWAP',
|
|
|
|
align: 'right'};
|
2015-08-31 22:13:17 +03:00
|
|
|
case 'disk':
|
2015-09-04 09:52:12 +03:00
|
|
|
return {lookup: 'diskHuman', name: 'DISK',
|
|
|
|
align: 'right'};
|
2015-12-16 20:56:22 +02:00
|
|
|
case 'vcpus':
|
|
|
|
return {lookup: 'vcpusHuman', name: 'VCPUS',
|
|
|
|
align: 'right'};
|
2015-08-31 22:13:17 +03:00
|
|
|
default:
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
});
|
2015-08-26 20:02:01 +03:00
|
|
|
}
|
|
|
|
tabula(pkgs, {
|
2015-08-26 01:30:25 +03:00
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
2015-08-26 06:53:48 +03:00
|
|
|
sort: sort
|
2015-08-26 01:30:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_packages.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
2015-09-04 01:09:21 +03:00
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
})).concat([
|
2015-08-31 22:13:17 +03:00
|
|
|
{
|
2015-09-01 01:16:12 +03:00
|
|
|
names: ['p'],
|
2015-08-31 22:13:17 +03:00
|
|
|
type: 'bool',
|
2015-09-01 01:16:12 +03:00
|
|
|
help: 'Display numbers in parsable (exact) values.'
|
2015-08-26 01:30:25 +03:00
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
]);
|
|
|
|
|
2015-08-26 01:30:25 +03:00
|
|
|
do_packages.help = (
|
2015-12-16 20:56:22 +02:00
|
|
|
/* BEGIN JSSTYLED */
|
2015-08-26 01:30:25 +03:00
|
|
|
'List packgaes.\n'
|
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
|
|
|
+ ' {{name}} packages\n'
|
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
2015-12-16 20:56:22 +02:00
|
|
|
+ '\n'
|
|
|
|
+ 'Notes on some fields:\n'
|
|
|
|
+ '- The "memory" (a.k.a. RAM), "swap", and "disk" fields are shown in\n'
|
|
|
|
+ ' more human readable units in tabular output (i.e. if neither "-p" nor\n'
|
|
|
|
+ ' "-j" is specified.\n'
|
|
|
|
+ '- The "vcpus" field is only relevant for KVM instances. It is therefore\n'
|
|
|
|
+ ' typically set to zero for packages not intended for KVM usage. This\n'
|
|
|
|
+ ' zero is shown as "-" in tabular output.\n'
|
|
|
|
/* END JSSTYLED */
|
2015-08-26 01:30:25 +03:00
|
|
|
);
|
|
|
|
|
2015-08-26 02:19:19 +03:00
|
|
|
do_packages.aliases = ['pkgs'];
|
2015-08-26 01:30:25 +03:00
|
|
|
|
|
|
|
module.exports = do_packages;
|