2015-08-26 06:53:48 +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 06:53:48 +03:00
|
|
|
*
|
|
|
|
* `triton create ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var format = require('util').format;
|
|
|
|
var tabula = require('tabula');
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('./common');
|
2015-09-01 20:43:28 +03:00
|
|
|
var distractions = require('./distractions');
|
2015-08-26 06:53:48 +03:00
|
|
|
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;
|
2015-09-03 20:18:35 +03:00
|
|
|
} else if (args.length !== 2) {
|
|
|
|
return callback(new errors.UsageError('incorrect number of args'));
|
2015-08-26 06:53:48 +03:00
|
|
|
}
|
|
|
|
|
2015-09-04 21:04:45 +03:00
|
|
|
var log = this.tritonapi.log;
|
|
|
|
var cloudapi = this.tritonapi.cloudapi;
|
2015-08-26 06:53:48 +03:00
|
|
|
|
|
|
|
vasync.pipeline({arg: {}, funcs: [
|
|
|
|
function getImg(ctx, next) {
|
2015-10-05 23:34:24 +03:00
|
|
|
var _opts = {
|
|
|
|
name: args[0],
|
|
|
|
useCache: true
|
|
|
|
};
|
|
|
|
self.tritonapi.getImage(_opts, function (err, img) {
|
2015-08-26 06:53:48 +03:00
|
|
|
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();
|
|
|
|
}
|
2015-09-21 23:37:26 +03:00
|
|
|
|
|
|
|
var id = args[1];
|
|
|
|
if (common.isUUID(id)) {
|
2015-09-22 00:12:33 +03:00
|
|
|
ctx.pkg = {id: id};
|
2015-09-21 23:37:26 +03:00
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tritonapi.getPackage(id, function (err, pkg) {
|
2015-08-26 06:53:48 +03:00
|
|
|
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();
|
|
|
|
}
|
2015-09-04 21:04:45 +03:00
|
|
|
self.tritonapi.getNetworks(opts.networks, function (err, nets) {
|
2015-08-26 06:53:48 +03:00
|
|
|
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
|
|
|
|
2015-10-17 22:43:24 +03:00
|
|
|
for (var i = 0; i < opts._order.length; i++) {
|
|
|
|
var opt = opts._order[i];
|
|
|
|
if (opt.key === 'firewall') {
|
|
|
|
createOpts.firewall_enabled = opt.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-09-01 19:00:45 +03:00
|
|
|
ctx.inst = {
|
2015-08-26 19:36:22 +03:00
|
|
|
id: 'beefbeef-4c0e-11e5-86cd-a7fd38d2a50b',
|
2015-10-17 22:43:24 +03:00
|
|
|
name: 'this-is-a-dry-run'
|
2015-08-26 19:36:22 +03:00
|
|
|
};
|
2015-09-01 19:00:45 +03:00
|
|
|
console.log('Creating instance %s (%s, %s@%s)',
|
2015-09-02 20:47:06 +03:00
|
|
|
ctx.inst.name, ctx.inst.id,
|
2015-09-01 19:00:45 +03:00
|
|
|
ctx.img.name, ctx.img.version);
|
2015-08-26 19:36:22 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:19:42 +03:00
|
|
|
// 1 'wait': no distraction.
|
|
|
|
// >1 'wait': distraction, pass in the N.
|
2015-09-01 20:43:28 +03:00
|
|
|
var distraction;
|
2015-09-23 10:19:42 +03:00
|
|
|
if (process.stderr.isTTY && opts.wait.length > 1) {
|
|
|
|
distraction = distractions.createDistraction(opts.wait.length);
|
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);
|
|
|
|
}
|
2015-08-26 20:13:09 +03:00
|
|
|
: 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) {
|
2015-09-01 20:43:28 +03:00
|
|
|
if (distraction) {
|
|
|
|
distraction.destroy();
|
2015-08-26 06:53:48 +03:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
2015-09-01 19:00:45 +03:00
|
|
|
}
|
2015-08-26 06:53:48 +03:00
|
|
|
|
|
|
|
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
|
|
|
},
|
2015-10-17 22:43:24 +03:00
|
|
|
{
|
|
|
|
// TODO: add boolNegationPrefix:'no-' when that cmdln pull is in
|
|
|
|
names: ['firewall'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Enable Cloud Firewall on this instance. See ' +
|
|
|
|
'<https://docs.joyent.com/public-cloud/network/firewall>'
|
|
|
|
},
|
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 script (user-script)
|
|
|
|
// XXX tag
|
2015-10-19 19:03:39 +03:00
|
|
|
// XXX locality: near, far
|
|
|
|
// XXX metadata, metadata-file
|
2015-08-26 06:53:48 +03:00
|
|
|
{
|
|
|
|
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'],
|
2015-09-23 10:19:42 +03:00
|
|
|
type: 'arrayOfBool',
|
|
|
|
help: 'Wait for the creation to complete. Use multiple times for a ' +
|
|
|
|
'spinner.'
|
2015-08-26 06:53:48 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON stream output.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
do_create_instance.help = (
|
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'Create a new instance.\n' +
|
|
|
|
'\n' +
|
|
|
|
'Usage:\n' +
|
2015-09-03 20:18:35 +03:00
|
|
|
' {{name}} create-instance [<options>] IMAGE PACKAGE\n' +
|
2015-08-26 06:53:48 +03:00
|
|
|
'\n' +
|
|
|
|
'{{options}}'
|
|
|
|
/* END JSSTYLED */
|
|
|
|
);
|
|
|
|
|
|
|
|
do_create_instance.aliases = ['create'];
|
|
|
|
|
|
|
|
module.exports = do_create_instance;
|