2015-08-26 01:10:13 +03:00
|
|
|
/*
|
2015-08-26 02:46:14 +03:00
|
|
|
* Copyright 2015 Joyent Inc.
|
2015-08-26 01:10:13 +03:00
|
|
|
*
|
2015-08-26 01:15:02 +03:00
|
|
|
* `triton instances ...`
|
2015-08-26 01:10:13 +03:00
|
|
|
*/
|
|
|
|
|
2015-08-26 19:59:12 +03:00
|
|
|
var f = require('util').format;
|
|
|
|
|
2015-08-26 01:10:13 +03:00
|
|
|
var tabula = require('tabula');
|
|
|
|
|
2015-08-26 02:46:14 +03:00
|
|
|
var common = require('./common');
|
2015-08-26 01:10:13 +03:00
|
|
|
|
2015-08-26 02:46:14 +03:00
|
|
|
// to be passed as query string args to /my/machines
|
|
|
|
var validFilters = [
|
|
|
|
'name',
|
|
|
|
'image',
|
|
|
|
'state',
|
|
|
|
'memory',
|
|
|
|
'tombstone',
|
|
|
|
'credentials'
|
|
|
|
];
|
|
|
|
|
2015-08-26 01:15:02 +03:00
|
|
|
function do_instances(subcmd, opts, args, callback) {
|
2015-08-26 01:10:13 +03:00
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
2015-08-26 02:46:14 +03:00
|
|
|
}
|
|
|
|
|
2015-08-31 23:11:31 +03:00
|
|
|
var columns = 'shortid,name,img,state,primaryIp,ago'.split(',');
|
2015-08-27 03:21:27 +03:00
|
|
|
if (opts.o) {
|
|
|
|
/* JSSTYLED */
|
|
|
|
columns = opts.o.trim().split(/\s*,\s*/g);
|
|
|
|
} else if (opts.long) {
|
2015-08-31 23:11:31 +03:00
|
|
|
columns = 'id,name,img,package,state,primaryIp,created'.split(',');
|
2015-08-27 03:21:27 +03:00
|
|
|
}
|
|
|
|
/* JSSTYLED */
|
|
|
|
var sort = opts.s.trim().split(/\s*,\s*/g);
|
2015-08-26 02:46:14 +03:00
|
|
|
|
|
|
|
var listOpts;
|
|
|
|
try {
|
|
|
|
listOpts = common.kvToObj(args, validFilters);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
2015-08-26 01:10:13 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 19:59:12 +03:00
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
var images;
|
2015-08-27 03:21:27 +03:00
|
|
|
this.triton.listImages({useCache: true}, function (err, _images) {
|
2015-08-26 02:46:14 +03:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
2015-08-26 01:10:13 +03:00
|
|
|
}
|
2015-08-26 19:59:12 +03:00
|
|
|
images = _images;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
i++;
|
|
|
|
var machines;
|
|
|
|
this.triton.cloudapi.listMachines(listOpts, function (err, _machines) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
machines = _machines;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (--i > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// map "uuid" => "image_name"
|
|
|
|
var imgmap = {};
|
|
|
|
images.forEach(function (image) {
|
|
|
|
imgmap[image.id] = f('%s@%s', image.name, image.version);
|
|
|
|
});
|
2015-08-26 03:00:50 +03:00
|
|
|
|
2015-08-27 03:21:27 +03:00
|
|
|
// Add extra fields for nice output.
|
|
|
|
// XXX FWIW, the "extra fields" for images and packages are not added
|
|
|
|
// for `opts.json`. Thoughts? We should be consistent there. --TM
|
2015-08-26 03:00:50 +03:00
|
|
|
var now = new Date();
|
|
|
|
machines.forEach(function (machine) {
|
|
|
|
var created = new Date(machine.created);
|
|
|
|
machine.ago = common.longAgo(created, now);
|
2015-08-26 19:59:12 +03:00
|
|
|
machine.img = imgmap[machine.image] || machine.image;
|
2015-08-27 03:21:27 +03:00
|
|
|
machine.shortid = machine.id.split('-', 1)[0];
|
2015-08-26 03:00:50 +03:00
|
|
|
});
|
|
|
|
|
2015-08-26 01:10:13 +03:00
|
|
|
if (opts.json) {
|
2015-08-26 23:53:23 +03:00
|
|
|
common.jsonStream(machines);
|
2015-08-26 01:10:13 +03:00
|
|
|
} else {
|
2015-08-26 02:46:14 +03:00
|
|
|
tabula(machines, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort,
|
2015-08-31 23:11:31 +03:00
|
|
|
dottedLookup: true
|
2015-08-26 01:10:13 +03:00
|
|
|
});
|
|
|
|
}
|
2015-08-26 02:46:14 +03:00
|
|
|
callback();
|
2015-08-26 19:59:12 +03:00
|
|
|
}
|
2015-08-26 02:46:14 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 01:15:02 +03:00
|
|
|
do_instances.options = [
|
2015-08-26 01:10:13 +03:00
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
2015-08-27 03:21:27 +03:00
|
|
|
{
|
|
|
|
group: 'Output options'
|
|
|
|
},
|
2015-08-26 02:46:14 +03:00
|
|
|
{
|
|
|
|
names: ['H'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Omit table header row.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['o'],
|
|
|
|
type: 'string',
|
|
|
|
help: 'Specify fields (columns) to output.',
|
|
|
|
helpArg: 'field1,...'
|
|
|
|
},
|
2015-08-27 03:21:27 +03:00
|
|
|
{
|
|
|
|
names: ['long', 'l'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Long/wider output. Ignored if "-o ..." is used.'
|
|
|
|
},
|
2015-08-26 02:46:14 +03:00
|
|
|
{
|
|
|
|
names: ['s'],
|
|
|
|
type: 'string',
|
2015-08-31 23:11:31 +03:00
|
|
|
default: 'created',
|
|
|
|
help: 'Sort on the given fields. Default is "created".',
|
2015-08-26 02:46:14 +03:00
|
|
|
helpArg: 'field1,...'
|
|
|
|
},
|
2015-08-26 01:10:13 +03:00
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON output.'
|
|
|
|
}
|
|
|
|
];
|
2015-08-26 01:15:02 +03:00
|
|
|
do_instances.help = (
|
|
|
|
'List instances.\n'
|
2015-08-26 01:10:13 +03:00
|
|
|
+ '\n'
|
|
|
|
+ 'Usage:\n'
|
2015-08-26 01:15:02 +03:00
|
|
|
+ ' {{name}} instances [<filters>...]\n'
|
2015-08-26 01:10:13 +03:00
|
|
|
+ '\n'
|
|
|
|
+ '{{options}}'
|
|
|
|
);
|
|
|
|
|
2015-08-26 03:00:50 +03:00
|
|
|
do_instances.aliases = ['insts'];
|
2015-08-26 01:10:13 +03:00
|
|
|
|
2015-08-26 01:15:02 +03:00
|
|
|
module.exports = do_instances;
|