This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/lib/do_create_instance.js

228 lines
7.0 KiB
JavaScript
Raw Normal View History

2015-08-26 06:53:48 +03:00
/*
* Copyright (c) 2015 Joyent Inc. All rights reserved.
*
* `triton create ...`
*/
var bigspinner = require('bigspinner');
var format = require('util').format;
var tabula = require('tabula');
var vasync = require('vasync');
var common = require('./common');
var errors = require('./errors');
function do_create_instance(subcmd, opts, args, callback) {
var self = this;
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length < 1 || args.length > 2) {
return callback(new errors.UsageError(format(
'incorrect number of args (%d): %s', args.length, args.join(' '))));
}
var log = this.triton.log;
var cloudapi = this.triton.cloudapi;
var cOpts = {};
vasync.pipeline({arg: {}, funcs: [
function getImg(ctx, next) {
// XXX don't get the image object if it is a UUID, waste of time
self.triton.getImage(args[0], function (err, img) {
if (err) {
return next(err);
}
ctx.img = img;
log.trace({img: img}, 'create-instance img');
next();
});
},
function getPkg(ctx, next) {
if (args.length < 2) {
return next();
}
// XXX don't get the package object if it is a UUID, waste of time
self.triton.getPackage(args[1], function (err, pkg) {
if (err) {
return next(err);
}
log.trace({pkg: pkg}, 'create-instance pkg');
ctx.pkg = pkg;
next();
});
},
function getNets(ctx, next) {
if (!opts.networks) {
return next();
}
self.triton.getNetworks(opts.networks, function (err, nets) {
if (err) {
return next(err);
}
ctx.nets = nets;
next();
});
},
function createInst(ctx, next) {
var createOpts = {
name: opts.name,
image: ctx.img.id,
'package': ctx.pkg && ctx.pkg.id,
networks: ctx.nets && ctx.nets.map(
function (net) { return net.id; })
};
2015-08-26 19:36:22 +03:00
log.trace({dryRun: opts.dry_run, createOpts: createOpts},
'create-instance createOpts');
2015-08-26 06:53:48 +03:00
ctx.start = Date.now();
2015-08-26 19:36:22 +03:00
if (opts.dry_run) {
var inst = ctx.inst = {
id: 'beefbeef-4c0e-11e5-86cd-a7fd38d2a50b',
name: 'm00'
};
console.log('Creating instance %s (%s, %s@%s%s)',
inst.name, inst.id, ctx.img.name, ctx.img.version,
inst.package ? format(', %s', inst.package) : '');
return next();
}
2015-08-26 06:53:48 +03:00
cloudapi.createMachine(createOpts, function (err, inst) {
if (err) {
return next(err);
}
ctx.inst = inst;
if (opts.json) {
console.log(JSON.stringify(inst));
} else {
2015-08-26 19:36:22 +03:00
console.log('Creating instance %s (%s, %s@%s%s)',
2015-08-26 06:53:48 +03:00
inst.name, inst.id, ctx.img.name, ctx.img.version,
2015-08-26 19:36:22 +03:00
inst.package ? format(', %s', inst.package) : '');
2015-08-26 06:53:48 +03:00
}
next();
});
},
function maybeWait(ctx, next) {
if (!opts.wait) {
return next();
}
var spinner;
if (!opts.quiet && process.stderr.isTTY) {
var BORDER = 10;
2015-08-26 06:53:48 +03:00
spinner = bigspinner.createSpinner({
2015-08-27 01:01:54 +03:00
delay: 50,
positions: 40,
2015-08-26 06:53:48 +03:00
stream: process.stderr,
height: Math.max(2, process.stdout.rows - 2 - BORDER),
width: Math.max(2, process.stdout.columns - 1 - BORDER),
2015-08-26 06:53:48 +03:00
hideCursor: true,
2015-08-27 01:01:54 +03:00
fontChar: '\u2588' // '\x1b[7m \x1b[m'
2015-08-26 06:53:48 +03:00
});
}
2015-08-26 19:36:22 +03:00
// Dry-run: fake wait for a few seconds.
var waiter = (opts.dry_run ?
function dryWait(waitOpts, waitCb) {
setTimeout(function () {
ctx.inst.state = 'running';
waitCb(null, ctx.inst);
}, 5000);
}
: cloudapi.waitForMachineStates.bind(cloudapi));
2015-08-26 19:36:22 +03:00
waiter({
2015-08-26 06:53:48 +03:00
id: ctx.inst.id,
states: ['running', 'failed']
}, function (err, inst) {
if (spinner) {
spinner.destroy();
}
if (err) {
return next(err);
}
if (opts.json) {
console.log(JSON.stringify(inst));
} else if (inst.state === 'running') {
var dur = Date.now() - ctx.start;
console.log('Created instance %s (%s) in %s',
inst.name, inst.id, common.humanDurationFromMs(dur));
}
if (inst.state !== 'running') {
next(new Error(format('failed to create instance %s (%s)',
inst.name, inst.id)));
} else {
next();
}
});
}
]}, function (err) {
callback(err);
});
};
do_create_instance.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
{
group: 'Create options'
},
{
names: ['name', 'n'],
type: 'string',
2015-08-26 06:59:35 +03:00
help: 'Instance name. If not given, a random one will be created.'
2015-08-26 06:53:48 +03:00
},
// XXX arrayOfCommaSepString dashdash type
//{
// names: ['networks', 'nets'],
// type: 'arrayOfCommaSepString',
// help: 'One or more (comma-separated) networks IDs.'
//},
// XXX enable-firewall
// XXX locality: near, far
// XXX metadata, metadata-file
// XXX script (user-script)
// XXX tag
{
group: 'Other options'
},
2015-08-26 19:36:22 +03:00
{
names: ['dry-run'],
type: 'bool',
help: 'Go through the motions without actually creating an instance.'
},
2015-08-26 06:53:48 +03:00
{
names: ['wait', 'w'],
type: 'bool',
help: 'Wait for the creation to complete.'
},
{
names: ['quiet', 'q'],
type: 'bool',
help: 'No progress spinner while waiting.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON stream output.'
}
];
do_create_instance.help = (
/* BEGIN JSSTYLED */
'Create a new instance.\n' +
'\n' +
'Usage:\n' +
' {{name}} create-instance [<options>] IMAGE [PACKAGE]\n' +
'\n' +
'{{options}}'
/* END JSSTYLED */
);
do_create_instance.aliases = ['create'];
module.exports = do_create_instance;