2016-01-04 19:05:53 +02: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-01-13 22:18:05 +02:00
|
|
|
* Copyright 2016 Joyent, Inc.
|
2016-01-04 19:05:53 +02:00
|
|
|
*
|
|
|
|
* `triton instance list ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var format = require('util').format;
|
|
|
|
var tabula = require('tabula');
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
|
2016-01-13 22:18:05 +02:00
|
|
|
/*
|
|
|
|
* Filters to be passed as query string args to /my/machines.
|
|
|
|
* See <https://apidocs.joyent.com/cloudapi/#ListMachines>.
|
|
|
|
*/
|
2016-01-04 19:05:53 +02:00
|
|
|
var validFilters = [
|
2016-01-13 22:18:05 +02:00
|
|
|
'type',
|
|
|
|
'brand', // Added in CloudAPI 8.0.0
|
2016-01-04 19:05:53 +02:00
|
|
|
'name',
|
|
|
|
'image',
|
|
|
|
'state',
|
|
|
|
'memory',
|
2016-01-13 22:18:05 +02:00
|
|
|
'docker' // Added in CloudAPI 8.0.0
|
2016-01-04 19:05:53 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// columns default without -o
|
2016-01-13 22:18:05 +02:00
|
|
|
var columnsDefault = 'shortid,name,img,state,flags,age';
|
2016-01-04 19:05:53 +02:00
|
|
|
|
|
|
|
// columns default with -l
|
2016-01-13 22:18:05 +02:00
|
|
|
var columnsDefaultLong
|
|
|
|
= 'id,name,img,brand,package,state,flags,primaryIp,created';
|
2016-01-04 19:05:53 +02:00
|
|
|
|
|
|
|
// sort default with -s
|
|
|
|
var sortDefault = 'created';
|
|
|
|
|
|
|
|
function do_list(subcmd, opts, args, callback) {
|
|
|
|
var self = this;
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-09 00:44:46 +03:00
|
|
|
var log = self.top.log;
|
2016-01-04 19:05:53 +02:00
|
|
|
|
|
|
|
var columns = columnsDefault;
|
|
|
|
if (opts.o) {
|
|
|
|
columns = opts.o;
|
|
|
|
} else if (opts.long) {
|
|
|
|
columns = columnsDefaultLong;
|
|
|
|
}
|
|
|
|
columns = columns.split(',');
|
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
|
|
|
|
|
|
|
var listOpts;
|
|
|
|
try {
|
|
|
|
listOpts = common.kvToObj(args, validFilters);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-14 19:21:17 +02:00
|
|
|
if (opts.credentials) {
|
|
|
|
listOpts.credentials = true;
|
|
|
|
}
|
2016-01-04 19:05:53 +02:00
|
|
|
|
|
|
|
|
2016-06-09 00:44:46 +03:00
|
|
|
var imgs = [];
|
2016-01-04 19:05:53 +02:00
|
|
|
var insts;
|
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
common.cliSetupTritonApi({cli: this.top}, function onSetup(setupErr) {
|
|
|
|
if (setupErr) {
|
|
|
|
callback(setupErr);
|
|
|
|
}
|
|
|
|
vasync.parallel({funcs: [
|
|
|
|
function getTheImages(next) {
|
|
|
|
self.top.tritonapi.listImages({
|
|
|
|
state: 'all',
|
|
|
|
useCache: true
|
|
|
|
}, function (err, _imgs) {
|
|
|
|
if (err) {
|
|
|
|
if (err.statusCode === 403) {
|
|
|
|
/*
|
|
|
|
* This could be a authorization error due
|
|
|
|
* to RBAC on a subuser. We don't want to
|
|
|
|
* fail `triton inst ls` if the subuser
|
|
|
|
* can ListMachines, but not ListImages.
|
|
|
|
*/
|
|
|
|
log.debug(
|
|
|
|
err,
|
|
|
|
'authz error listing images for insts info');
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
next(err);
|
|
|
|
}
|
2016-06-09 00:44:46 +03:00
|
|
|
} else {
|
2016-12-13 20:04:41 +02:00
|
|
|
imgs = _imgs;
|
|
|
|
next();
|
2016-06-09 00:44:46 +03:00
|
|
|
}
|
2016-12-13 20:04:41 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function getTheMachines(next) {
|
|
|
|
self.top.tritonapi.cloudapi.listMachines(
|
|
|
|
listOpts,
|
2016-01-04 19:05:53 +02:00
|
|
|
function (err, _insts) {
|
2016-12-13 20:04:41 +02:00
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
} else {
|
|
|
|
insts = _insts;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]}, function (err, results) {
|
|
|
|
/*
|
|
|
|
* Error handling: vasync.parallel's `err` is always a
|
|
|
|
* MultiError. We want to prefer the `getTheMachines` err,
|
|
|
|
* e.g. if both get a self-signed cert error.
|
|
|
|
*/
|
|
|
|
if (err) {
|
|
|
|
err = results.operations[1].err || err;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// map "uuid" => "image_name"
|
|
|
|
var imgmap = {};
|
|
|
|
imgs.forEach(function (img) {
|
|
|
|
imgmap[img.id] = format('%s@%s', img.name, img.version);
|
2016-01-04 19:05:53 +02:00
|
|
|
});
|
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
// Add extra fields for nice output.
|
|
|
|
var now = new Date();
|
|
|
|
insts.forEach(function (inst) {
|
|
|
|
var created = new Date(inst.created);
|
|
|
|
inst.age = common.longAgo(created, now);
|
|
|
|
inst.img = imgmap[inst.image] ||
|
|
|
|
common.uuidToShortId(inst.image);
|
|
|
|
inst.shortid = inst.id.split('-', 1)[0];
|
|
|
|
var flags = [];
|
|
|
|
if (inst.docker) flags.push('D');
|
|
|
|
if (inst.firewall_enabled) flags.push('F');
|
|
|
|
if (inst.brand === 'kvm') flags.push('K');
|
|
|
|
inst.flags = flags.length ? flags.join('') : undefined;
|
|
|
|
});
|
2016-01-04 19:05:53 +02:00
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
if (opts.json) {
|
|
|
|
common.jsonStream(insts);
|
|
|
|
} else {
|
|
|
|
tabula(insts, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort,
|
|
|
|
dottedLookup: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
2016-01-04 19:05:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_list.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
2016-01-14 19:21:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['credentials'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Include generated credentials, in the "metadata.credentials" ' +
|
|
|
|
'keys, if any. Typically used with "-j", though one can show ' +
|
|
|
|
'values with "-o metadata.credentials".'
|
2016-01-04 19:05:53 +02:00
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
|
|
|
|
2016-06-09 00:13:16 +03:00
|
|
|
do_list.synopses = ['{{name}} {{cmd}} [OPTIONS] [FILTERS...]'];
|
|
|
|
|
2016-01-13 22:18:05 +02:00
|
|
|
do_list.help = [
|
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'List instances.',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
2016-01-13 22:18:05 +02:00
|
|
|
'',
|
|
|
|
'{{options}}',
|
|
|
|
'Filters:',
|
|
|
|
' FIELD=VALUE Equality filter. Supported fields: type, brand, name,',
|
2016-02-03 21:57:00 +02:00
|
|
|
' image, state, and memory',
|
2016-01-13 22:18:05 +02:00
|
|
|
' FIELD=true|false Boolean filter. Supported fields: docker (added in',
|
|
|
|
' CloudAPI 8.0.0)',
|
|
|
|
'',
|
|
|
|
'Fields (most are self explanatory, "*" indicates a field added client-side',
|
|
|
|
'for convenience):',
|
|
|
|
' shortid* A short ID prefix.',
|
|
|
|
' flags* Single letter flags summarizing some fields:',
|
|
|
|
' "D" docker instance',
|
|
|
|
' "F" firewall is enabled',
|
|
|
|
' "K" the brand is "kvm"',
|
|
|
|
' age* Approximate time since created, e.g. 1y, 2w.',
|
|
|
|
' img* The image "name@version", if available, else its',
|
|
|
|
' "shortid".'
|
|
|
|
/* END JSSTYLED */
|
|
|
|
].join('\n');
|
2016-01-04 19:05:53 +02:00
|
|
|
|
|
|
|
do_list.aliases = ['ls'];
|
|
|
|
|
|
|
|
module.exports = do_list;
|