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
|
|
|
*
|
2016-01-04 19:05:53 +02:00
|
|
|
* `triton instance create ...`
|
2015-08-26 06:53:48 +03:00
|
|
|
*/
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var assert = require('assert-plus');
|
2015-08-26 06:53:48 +03:00
|
|
|
var format = require('util').format;
|
2015-12-07 21:28:59 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var strsplit = require('strsplit');
|
2015-08-26 06:53:48 +03:00
|
|
|
var tabula = require('tabula');
|
2015-12-07 21:28:59 +02:00
|
|
|
var tilde = require('tilde-expansion');
|
2015-08-26 06:53:48 +03:00
|
|
|
var vasync = require('vasync');
|
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var distractions = require('../distractions');
|
|
|
|
var errors = require('../errors');
|
2015-08-26 06:53:48 +03:00
|
|
|
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
// ---- loading/parsing metadata (and tags) from relevant options
|
2015-12-07 21:28:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load and validate metadata from these options:
|
|
|
|
* -m,--metadata DATA
|
|
|
|
* -M,--metadata-file KEY=FILE
|
|
|
|
* --script FILE
|
|
|
|
*
|
|
|
|
* <https://github.com/joyent/sdc-vmapi/blob/master/docs/index.md#vm-metadata>
|
|
|
|
* says values may be string, num or bool.
|
|
|
|
*/
|
|
|
|
function metadataFromOpts(opts, log, cb) {
|
|
|
|
assert.arrayOfObject(opts._order, 'opts._order');
|
|
|
|
assert.object(log, 'log');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var metadata = {};
|
|
|
|
|
|
|
|
vasync.forEachPipeline({
|
|
|
|
inputs: opts._order,
|
|
|
|
func: function metadataFromOpt(o, next) {
|
|
|
|
log.trace({opt: o}, 'metadataFromOpt');
|
|
|
|
if (o.key === 'metadata') {
|
|
|
|
if (!o.value) {
|
|
|
|
next(new errors.UsageError(
|
|
|
|
'empty metadata option value'));
|
|
|
|
return;
|
|
|
|
} else if (o.value[0] === '{') {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromJsonStr(
|
|
|
|
'metadata', metadata, o.value, null, next);
|
2015-12-07 21:28:59 +02:00
|
|
|
} else if (o.value[0] === '@') {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromFile(
|
|
|
|
'metadata', metadata, o.value.slice(1), next);
|
2015-12-07 21:28:59 +02:00
|
|
|
} else {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromKvStr(
|
|
|
|
'metadata', metadata, o.value, null, next);
|
2015-12-07 21:28:59 +02:00
|
|
|
}
|
|
|
|
} else if (o.key === 'metadata_file') {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromKfStr(
|
|
|
|
'metadata', metadata, o.value, null, next);
|
2015-12-07 21:28:59 +02:00
|
|
|
} else if (o.key === 'script') {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadatumFromFile('metadata', metadata,
|
|
|
|
'user-script', o.value, o.value, next);
|
2015-12-07 21:28:59 +02:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else if (Object.keys(metadata).length) {
|
|
|
|
cb(null, metadata);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
/*
|
|
|
|
* Load and validate tags from these options:
|
|
|
|
* -t,--tag DATA
|
|
|
|
*
|
|
|
|
* <https://github.com/joyent/sdc-vmapi/blob/master/docs/index.md#vm-metadata>
|
|
|
|
* says values may be string, num or bool.
|
|
|
|
*/
|
|
|
|
function tagsFromOpts(opts, log, cb) {
|
|
|
|
assert.arrayOfObject(opts._order, 'opts._order');
|
|
|
|
assert.object(log, 'log');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
var tags = {};
|
|
|
|
|
|
|
|
vasync.forEachPipeline({
|
|
|
|
inputs: opts._order,
|
|
|
|
func: function tagsFromOpt(o, next) {
|
|
|
|
log.trace({opt: o}, 'tagsFromOpt');
|
|
|
|
if (o.key === 'tag') {
|
|
|
|
if (!o.value) {
|
|
|
|
next(new errors.UsageError(
|
|
|
|
'empty tag option value'));
|
|
|
|
return;
|
|
|
|
} else if (o.value[0] === '{') {
|
|
|
|
_addMetadataFromJsonStr('tag', tags, o.value, null, next);
|
|
|
|
} else if (o.value[0] === '@') {
|
|
|
|
_addMetadataFromFile('tag', tags, o.value.slice(1), next);
|
|
|
|
} else {
|
|
|
|
_addMetadataFromKvStr('tag', tags, o.value, null, next);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
} else if (Object.keys(tags).length) {
|
|
|
|
cb(null, tags);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var allowedTypes = ['string', 'number', 'boolean'];
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadatum(ilk, metadata, key, value, from, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
2015-12-07 21:28:59 +02:00
|
|
|
assert.object(metadata, 'metadata');
|
|
|
|
assert.string(key, 'key');
|
|
|
|
assert.optionalString(from, 'from');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
if (allowedTypes.indexOf(typeof (value)) === -1) {
|
|
|
|
cb(new errors.UsageError(format(
|
2015-12-31 02:11:54 +02:00
|
|
|
'invalid %s value type%s: must be one of %s: %s=%j',
|
|
|
|
ilk, (from ? ' (from ' + from + ')' : ''),
|
2015-12-07 21:28:59 +02:00
|
|
|
allowedTypes.join(', '), key, value)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metadata.hasOwnProperty(key)) {
|
|
|
|
var valueStr = value.toString();
|
|
|
|
console.error(
|
2015-12-31 02:11:54 +02:00
|
|
|
'warning: %s "%s=%s"%s replaces earlier value for "%s"',
|
|
|
|
ilk,
|
2015-12-07 21:28:59 +02:00
|
|
|
key,
|
|
|
|
(valueStr.length > 10
|
|
|
|
? valueStr.slice(0, 7) + '...' : valueStr),
|
|
|
|
(from ? ' (from ' + from + ')' : ''),
|
|
|
|
key);
|
|
|
|
}
|
|
|
|
metadata[key] = value;
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadataFromObj(ilk, metadata, obj, from, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
2015-12-07 21:28:59 +02:00
|
|
|
assert.object(metadata, 'metadata');
|
|
|
|
assert.object(obj, 'obj');
|
|
|
|
assert.optionalString(from, 'from');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
vasync.forEachPipeline({
|
|
|
|
inputs: Object.keys(obj),
|
|
|
|
func: function _oneField(key, next) {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadatum(ilk, metadata, key, obj[key], from, next);
|
2015-12-07 21:28:59 +02:00
|
|
|
}
|
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadataFromJsonStr(ilk, metadata, s, from, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
2015-12-07 21:28:59 +02:00
|
|
|
try {
|
|
|
|
var obj = JSON.parse(s);
|
|
|
|
} catch (parseErr) {
|
|
|
|
cb(new errors.TritonError(parseErr,
|
2015-12-31 02:11:54 +02:00
|
|
|
format('%s%s is not valid JSON', ilk,
|
2015-12-07 21:28:59 +02:00
|
|
|
(from ? ' (from ' + from + ')' : ''))));
|
|
|
|
return;
|
|
|
|
}
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromObj(ilk, metadata, obj, from, cb);
|
2015-12-07 21:28:59 +02:00
|
|
|
}
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadataFromFile(ilk, metadata, file, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
2015-12-07 21:28:59 +02:00
|
|
|
tilde(file, function (metaPath) {
|
|
|
|
fs.stat(metaPath, function (statErr, stats) {
|
|
|
|
if (statErr || !stats.isFile()) {
|
|
|
|
cb(new errors.TritonError(format(
|
|
|
|
'"%s" is not an existing file', file)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fs.readFile(metaPath, 'utf8', function (readErr, data) {
|
|
|
|
if (readErr) {
|
|
|
|
cb(readErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The file is either a JSON object (first non-space
|
|
|
|
* char is '{'), or newline-separated key=value
|
|
|
|
* pairs.
|
|
|
|
*/
|
|
|
|
var dataTrim = data.trim();
|
|
|
|
if (dataTrim.length && dataTrim[0] === '{') {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromJsonStr(ilk, metadata, dataTrim, file, cb);
|
2015-12-07 21:28:59 +02:00
|
|
|
} else {
|
|
|
|
var lines = dataTrim.split(/\r?\n/g).filter(
|
|
|
|
function (line) { return line.trim(); });
|
|
|
|
vasync.forEachPipeline({
|
|
|
|
inputs: lines,
|
|
|
|
func: function oneLine(line, next) {
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadataFromKvStr(
|
|
|
|
ilk, metadata, line, file, next);
|
2015-12-07 21:28:59 +02:00
|
|
|
}
|
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadataFromKvStr(ilk, metadata, s, from, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var parts = strsplit(s, '=', 2);
|
|
|
|
if (parts.length !== 2) {
|
2015-12-31 02:11:54 +02:00
|
|
|
cb(new errors.UsageError(format(
|
|
|
|
'invalid KEY=VALUE %s argument: %s', ilk, s)));
|
2015-12-07 21:28:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var value = parts[1];
|
|
|
|
var valueTrim = value.trim();
|
|
|
|
if (valueTrim === 'true') {
|
|
|
|
value = true;
|
|
|
|
} else if (valueTrim === 'false') {
|
|
|
|
value = false;
|
|
|
|
} else {
|
|
|
|
var num = Number(value);
|
|
|
|
if (!isNaN(num)) {
|
|
|
|
value = num;
|
|
|
|
}
|
|
|
|
}
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadatum(ilk, metadata, parts[0].trim(), value, from, cb);
|
2015-12-07 21:28:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add metadata from `KEY=FILE` argument.
|
|
|
|
* Here "Kf" stands for "key/file".
|
|
|
|
*/
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadataFromKfStr(ilk, metadata, s, from, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
var parts = strsplit(s, '=', 2);
|
|
|
|
if (parts.length !== 2) {
|
2015-12-31 02:11:54 +02:00
|
|
|
cb(new errors.UsageError(format(
|
|
|
|
'invalid KEY=FILE %s argument: %s', ilk, s)));
|
2015-12-07 21:28:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var key = parts[0].trim();
|
|
|
|
var file = parts[1];
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadatumFromFile(ilk, metadata, key, file, file, cb);
|
2015-12-07 21:28:59 +02:00
|
|
|
}
|
|
|
|
|
2015-12-31 02:11:54 +02:00
|
|
|
function _addMetadatumFromFile(ilk, metadata, key, file, from, cb) {
|
|
|
|
assert.string(ilk, 'ilk');
|
|
|
|
|
2015-12-07 21:28:59 +02:00
|
|
|
tilde(file, function (filePath) {
|
|
|
|
fs.stat(filePath, function (statErr, stats) {
|
|
|
|
if (statErr || !stats.isFile()) {
|
|
|
|
cb(new errors.TritonError(format(
|
2015-12-31 02:11:54 +02:00
|
|
|
'%s path "%s" is not an existing file', ilk, file)));
|
2015-12-07 21:28:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fs.readFile(filePath, 'utf8', function (readErr, content) {
|
|
|
|
if (readErr) {
|
|
|
|
cb(readErr);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-31 02:11:54 +02:00
|
|
|
_addMetadatum(ilk, metadata, key, content, from, cb);
|
2015-12-07 21:28:59 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- the command
|
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
function do_create(subcmd, opts, args, cb) {
|
2015-08-26 06:53:48 +03:00
|
|
|
var self = this;
|
|
|
|
if (opts.help) {
|
2015-12-07 21:28:59 +02:00
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
2015-08-26 06:53:48 +03:00
|
|
|
return;
|
2015-09-03 20:18:35 +03:00
|
|
|
} else if (args.length !== 2) {
|
2015-12-07 21:28:59 +02:00
|
|
|
return cb(new errors.UsageError('incorrect number of args'));
|
2015-08-26 06:53:48 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
var log = this.top.log;
|
|
|
|
var cloudapi = this.top.tritonapi.cloudapi;
|
2015-08-26 06:53:48 +03:00
|
|
|
|
|
|
|
vasync.pipeline({arg: {}, funcs: [
|
2015-12-07 21:28:59 +02:00
|
|
|
function loadMetadata(ctx, next) {
|
|
|
|
metadataFromOpts(opts, self.log, function (err, metadata) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (metadata) {
|
|
|
|
log.trace({metadata: metadata},
|
|
|
|
'metadata loaded from opts');
|
|
|
|
ctx.metadata = metadata;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2015-12-31 02:11:54 +02:00
|
|
|
function loadTags(ctx, next) {
|
|
|
|
tagsFromOpts(opts, self.log, function (err, tags) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tags) {
|
|
|
|
log.trace({tags: tags}, 'tags loaded from opts');
|
|
|
|
ctx.tags = tags;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2015-08-26 06:53:48 +03:00
|
|
|
function getImg(ctx, next) {
|
2015-10-05 23:34:24 +03:00
|
|
|
var _opts = {
|
|
|
|
name: args[0],
|
|
|
|
useCache: true
|
|
|
|
};
|
2016-01-04 19:05:53 +02:00
|
|
|
self.top.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;
|
|
|
|
}
|
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
self.top.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) {
|
2015-12-31 19:55:31 +02:00
|
|
|
if (!opts.network) {
|
2015-08-26 06:53:48 +03:00
|
|
|
return next();
|
|
|
|
}
|
2015-12-31 19:55:31 +02:00
|
|
|
// TODO: want an error or warning on no networks?
|
|
|
|
ctx.nets = [];
|
|
|
|
vasync.forEachPipeline({
|
|
|
|
inputs: opts.network,
|
|
|
|
func: function getOneNetwork(name, nextNet) {
|
2016-01-04 19:05:53 +02:00
|
|
|
self.top.tritonapi.getNetwork(name, function (err, net) {
|
2015-12-31 19:55:31 +02:00
|
|
|
if (err) {
|
|
|
|
nextNet(err);
|
|
|
|
} else {
|
|
|
|
ctx.nets.push(net);
|
|
|
|
nextNet();
|
|
|
|
}
|
|
|
|
});
|
2015-08-26 06:53:48 +03:00
|
|
|
}
|
2015-12-31 19:55:31 +02:00
|
|
|
}, next);
|
2015-08-26 06:53:48 +03:00
|
|
|
},
|
|
|
|
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-12-07 21:28:59 +02:00
|
|
|
if (ctx.metadata) {
|
|
|
|
Object.keys(ctx.metadata).forEach(function (key) {
|
|
|
|
createOpts['metadata.'+key] = ctx.metadata[key];
|
|
|
|
});
|
|
|
|
}
|
2015-12-31 02:11:54 +02:00
|
|
|
if (ctx.tags) {
|
|
|
|
Object.keys(ctx.tags).forEach(function (key) {
|
|
|
|
createOpts['tag.'+key] = ctx.tags[key];
|
|
|
|
});
|
|
|
|
}
|
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) {
|
2015-12-07 21:28:59 +02:00
|
|
|
next(new errors.TritonError(err,
|
|
|
|
'error creating instance'));
|
|
|
|
return;
|
2015-08-26 06:53:48 +03:00
|
|
|
}
|
|
|
|
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) {
|
2015-12-07 21:28:59 +02:00
|
|
|
cb(err);
|
2015-08-26 06:53:48 +03:00
|
|
|
});
|
2015-09-01 19:00:45 +03:00
|
|
|
}
|
2015-08-26 06:53:48 +03:00
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
do_create.options = [
|
2015-08-26 06:53:48 +03:00
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Create options'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['name', 'n'],
|
2015-12-07 21:28:59 +02:00
|
|
|
helpArg: 'NAME',
|
2015-08-26 06:53:48 +03:00
|
|
|
type: 'string',
|
2015-12-07 21:28:59 +02:00
|
|
|
help: 'Instance name. If not given, one will be generated server-side.'
|
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-12-07 21:28:59 +02:00
|
|
|
{
|
|
|
|
names: ['metadata', 'm'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
helpArg: 'DATA',
|
2015-12-31 02:11:54 +02:00
|
|
|
help: 'Add metadata when creating the instance. Metadata are ' +
|
|
|
|
'key/value pairs available on the instance API object as the ' +
|
2015-12-07 21:28:59 +02:00
|
|
|
'"metadata" field, and inside the instance via the "mdata-*" ' +
|
|
|
|
'commands. DATA is one of: a "key=value" string (bool and ' +
|
|
|
|
'numeric "value" are converted to that type), a JSON object ' +
|
|
|
|
'(if first char is "{"), or a "@FILE" to have metadata be ' +
|
2015-12-31 02:11:54 +02:00
|
|
|
'loaded from FILE. This option can be used multiple times.'
|
2015-12-07 21:28:59 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['metadata-file', 'M'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
helpArg: 'KEY=FILE',
|
|
|
|
help: 'Set a metadata key KEY from the contents of FILE.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['script'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
helpArg: 'FILE',
|
|
|
|
help: 'Load a file to be used for the "user-script" metadata key. In ' +
|
|
|
|
'Joyent-provided images, the user-script is run at every boot ' +
|
|
|
|
'of the instance. This is a shortcut for `-M user-script=FILE`.'
|
|
|
|
},
|
2015-12-31 02:11:54 +02:00
|
|
|
{
|
|
|
|
names: ['tag', 't'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
helpArg: 'TAG',
|
|
|
|
help: 'Add a tag when creating the instance. Tags are ' +
|
|
|
|
'key/value pairs available on the instance API object as the ' +
|
|
|
|
'"tags" field. TAG is one of: a "key=value" string (bool and ' +
|
|
|
|
'numeric "value" are converted to that type), a JSON object ' +
|
|
|
|
'(if first char is "{"), or a "@FILE" to have tags be ' +
|
|
|
|
'loaded from FILE. This option can be used multiple times.'
|
|
|
|
},
|
2015-12-31 19:55:31 +02:00
|
|
|
{
|
|
|
|
names: ['network', 'N'],
|
|
|
|
type: 'arrayOfCommaSepString',
|
|
|
|
helpArg: 'NETWORK',
|
|
|
|
help: 'One or more comma-separated networks (ID, name or short id). ' +
|
|
|
|
'This option can be used multiple times.'
|
|
|
|
},
|
|
|
|
|
2015-10-19 19:03:39 +03:00
|
|
|
// XXX locality: near, far
|
2015-12-31 02:11:54 +02:00
|
|
|
|
2015-08-26 06:53:48 +03:00
|
|
|
{
|
|
|
|
group: 'Other options'
|
|
|
|
},
|
2015-08-26 19:36:22 +03:00
|
|
|
{
|
|
|
|
names: ['dry-run'],
|
|
|
|
type: 'bool',
|
2015-12-07 21:28:59 +02:00
|
|
|
help: 'Go through the motions without actually creating.'
|
2015-08-26 19:36:22 +03:00
|
|
|
},
|
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.'
|
|
|
|
}
|
|
|
|
];
|
2015-12-07 21:28:59 +02:00
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
do_create.help = (
|
2015-08-26 06:53:48 +03:00
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'Create a new instance.\n' +
|
|
|
|
'\n' +
|
|
|
|
'Usage:\n' +
|
2016-01-04 19:05:53 +02:00
|
|
|
' {{name}} create [<options>] IMAGE PACKAGE\n' +
|
2015-08-26 06:53:48 +03:00
|
|
|
'\n' +
|
|
|
|
'{{options}}'
|
|
|
|
/* END JSSTYLED */
|
|
|
|
);
|
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
do_create.helpOpts = {
|
2015-12-31 02:11:54 +02:00
|
|
|
maxHelpCol: 18
|
2015-12-07 21:28:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-04 19:05:53 +02:00
|
|
|
module.exports = do_create;
|
|
|
|
do_create.metadataFromOpts = metadataFromOpts; // export for testing
|
|
|
|
do_create.tagsFromOpts = tagsFromOpts; // export for testing
|