2015-08-26 01:10:13 +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:10:13 +03:00
|
|
|
*
|
2015-08-26 01:15:02 +03:00
|
|
|
* `triton instances ...`
|
2015-08-26 01:10:13 +03:00
|
|
|
*/
|
|
|
|
|
2015-09-21 20:33:42 +03:00
|
|
|
var format = require('util').format;
|
2015-08-26 01:10:13 +03:00
|
|
|
var tabula = require('tabula');
|
2015-09-21 20:33:42 +03:00
|
|
|
var vasync = require('vasync');
|
2015-08-26 01:10:13 +03:00
|
|
|
|
2015-08-26 02:46:14 +03:00
|
|
|
var common = require('./common');
|
2015-08-26 01:10:13 +03:00
|
|
|
|
2015-09-21 20:33:42 +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-09-04 01:09:21 +03:00
|
|
|
// columns default without -o
|
|
|
|
var columnsDefault = 'shortid,name,img,state,primaryIp,ago';
|
|
|
|
|
|
|
|
// columns default with -l
|
|
|
|
var columnsDefaultLong = 'id,name,img,package,state,primaryIp,created';
|
|
|
|
|
|
|
|
// sort default with -s
|
|
|
|
var sortDefault = 'created';
|
|
|
|
|
2015-08-26 01:15:02 +03:00
|
|
|
function do_instances(subcmd, opts, args, callback) {
|
2015-09-21 20:33:42 +03:00
|
|
|
var self = this;
|
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-09-04 01:09:21 +03:00
|
|
|
var columns = columnsDefault;
|
2015-08-27 03:21:27 +03:00
|
|
|
if (opts.o) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = opts.o;
|
2015-08-27 03:21:27 +03:00
|
|
|
} else if (opts.long) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columnsDefaultLong;
|
2015-08-27 03:21:27 +03:00
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columns.split(',');
|
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
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
|
|
|
|
2015-09-21 20:33:42 +03:00
|
|
|
var imgs;
|
|
|
|
var insts;
|
2015-08-26 19:59:12 +03:00
|
|
|
|
2015-09-21 20:33:42 +03:00
|
|
|
vasync.parallel({funcs: [
|
|
|
|
function getTheImages(next) {
|
|
|
|
self.tritonapi.listImages({useCache: true}, function (err, _imgs) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
} else {
|
|
|
|
imgs = _imgs;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getTheMachines(next) {
|
|
|
|
self.tritonapi.cloudapi.listMachines(listOpts,
|
|
|
|
function (err, _insts) {
|
|
|
|
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.
|
|
|
|
*/
|
2015-08-26 19:59:12 +03:00
|
|
|
if (err) {
|
2015-09-21 20:33:42 +03:00
|
|
|
err = results.operations[1].err || err;
|
|
|
|
return callback(err);
|
2015-08-26 19:59:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// map "uuid" => "image_name"
|
|
|
|
var imgmap = {};
|
2015-09-21 20:33:42 +03:00
|
|
|
imgs.forEach(function (img) {
|
|
|
|
imgmap[img.id] = format('%s@%s', img.name, img.version);
|
2015-08-26 19:59:12 +03:00
|
|
|
});
|
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();
|
2015-09-21 20:33:42 +03:00
|
|
|
insts.forEach(function (inst) {
|
|
|
|
var created = new Date(inst.created);
|
|
|
|
inst.ago = common.longAgo(created, now);
|
|
|
|
inst.img = imgmap[inst.image] || inst.image;
|
|
|
|
inst.shortid = inst.id.split('-', 1)[0];
|
2015-08-26 03:00:50 +03:00
|
|
|
});
|
|
|
|
|
2015-08-26 01:10:13 +03:00
|
|
|
if (opts.json) {
|
2015-09-21 20:33:42 +03:00
|
|
|
common.jsonStream(insts);
|
2015-08-26 01:10:13 +03:00
|
|
|
} else {
|
2015-09-21 20:33:42 +03:00
|
|
|
tabula(insts, {
|
2015-08-26 02:46:14 +03:00
|
|
|
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-09-21 20:33:42 +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-09-04 01:09:21 +03:00
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
2015-09-03 06:24:08 +03:00
|
|
|
|
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;
|