2017-02-22 01:42:07 +02: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 2017 Joyent, Inc.
|
|
|
|
*
|
|
|
|
* `triton volume create ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var format = require('util').format;
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var distractions = require('../distractions');
|
|
|
|
var errors = require('../errors');
|
2017-03-21 03:55:59 +02:00
|
|
|
var mod_volumes = require('../volumes');
|
2017-02-22 01:42:07 +02:00
|
|
|
|
2017-03-21 03:55:59 +02:00
|
|
|
function do_create(subcmd, opts, args, cb) {
|
2017-02-22 01:42:07 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (opts.help) {
|
2017-03-21 03:55:59 +02:00
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length !== 0) {
|
|
|
|
cb(new errors.UsageError('incorrect number of args'));
|
2017-02-22 01:42:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:54:36 +02:00
|
|
|
vasync.pipeline({arg: {cli: this.top}, funcs: [
|
2017-03-21 03:55:59 +02:00
|
|
|
function validateVolumeSize(ctx, next) {
|
|
|
|
if (opts.size === undefined) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.size = mod_volumes.parseVolumeSize(opts.size);
|
|
|
|
} catch (parseSizeErr) {
|
|
|
|
next(parseSizeErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
2017-03-23 01:54:36 +02:00
|
|
|
common.cliSetupTritonApi,
|
2017-02-22 01:42:07 +02:00
|
|
|
function getNetworks(ctx, next) {
|
|
|
|
if (!opts.network) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.networks = [];
|
|
|
|
|
|
|
|
vasync.forEachParallel({
|
|
|
|
inputs: opts.network,
|
|
|
|
func: function getNetwork(networkName, nextNet) {
|
|
|
|
self.top.tritonapi.getNetwork(networkName,
|
|
|
|
function onGetNetwork(getNetErr, net) {
|
|
|
|
if (net) {
|
2017-05-18 06:15:28 +03:00
|
|
|
ctx.networks.push(net.id);
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nextNet(getNetErr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, next);
|
|
|
|
},
|
|
|
|
function createVolume(ctx, next) {
|
|
|
|
var createVolumeParams = {
|
|
|
|
type: 'tritonnfs',
|
|
|
|
name: opts.name,
|
|
|
|
networks: ctx.networks,
|
2017-03-21 03:55:59 +02:00
|
|
|
size: ctx.size
|
2017-02-22 01:42:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (opts.type) {
|
|
|
|
createVolumeParams.type = opts.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.top.tritonapi.cloudapi.createVolume(createVolumeParams,
|
|
|
|
function onRes(volCreateErr, volume) {
|
|
|
|
ctx.volume = volume;
|
|
|
|
next(volCreateErr);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function maybeWait(ctx, next) {
|
2017-03-23 01:54:36 +02:00
|
|
|
var distraction;
|
2017-03-23 22:12:24 +02:00
|
|
|
var waitTimeout = opts.wait_timeout === undefined ?
|
|
|
|
undefined : opts.wait_timeout * 1000;
|
2017-03-23 01:54:36 +02:00
|
|
|
|
2017-02-22 01:42:07 +02:00
|
|
|
if (!opts.wait) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:54:36 +02:00
|
|
|
if (process.stderr.isTTY && opts.wait.length > 1) {
|
|
|
|
distraction = distractions.createDistraction(opts.wait.length);
|
|
|
|
}
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
self.top.tritonapi.cloudapi.waitForVolumeStates({
|
|
|
|
id: ctx.volume.id,
|
2017-03-23 01:54:36 +02:00
|
|
|
states: ['ready', 'failed'],
|
2017-03-23 22:12:24 +02:00
|
|
|
timeout: waitTimeout
|
2017-02-22 01:42:07 +02:00
|
|
|
}, function onWaitDone(waitErr, volume) {
|
2017-03-23 01:54:36 +02:00
|
|
|
if (distraction) {
|
|
|
|
distraction.destroy();
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:42:07 +02:00
|
|
|
ctx.volume = volume;
|
|
|
|
next(waitErr);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function outputRes(ctx, next) {
|
|
|
|
assert.object(ctx.volume, 'ctx.volume');
|
|
|
|
|
|
|
|
if (opts.json) {
|
|
|
|
console.log(JSON.stringify(ctx.volume));
|
|
|
|
} else {
|
|
|
|
console.log(JSON.stringify(ctx.volume, null, 4));
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 01:54:36 +02:00
|
|
|
]}, cb);
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do_create.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Create options'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['name', 'n'],
|
|
|
|
helpArg: 'NAME',
|
|
|
|
type: 'string',
|
|
|
|
help: 'Volume name. If not given, one will be generated server-side.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['type', 't'],
|
|
|
|
helpArg: 'TYPE',
|
|
|
|
type: 'string',
|
|
|
|
help: 'Volume type. Default is "tritonnfs".'
|
|
|
|
},
|
|
|
|
{
|
2017-03-21 03:55:59 +02:00
|
|
|
names: ['size', 's'],
|
2017-02-22 01:42:07 +02:00
|
|
|
type: 'string',
|
|
|
|
helpArg: 'SIZE',
|
2017-04-04 04:11:06 +03:00
|
|
|
help: 'The size of the volume to create, in the form ' +
|
|
|
|
'`<integer><unit>`, e.g. `20G`. <integer> must be > 0. Supported ' +
|
2017-04-06 01:19:46 +03:00
|
|
|
'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.',
|
2017-03-21 03:55:59 +02:00
|
|
|
completionType: 'tritonvolumesize'
|
2017-02-22 01:42:07 +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.',
|
|
|
|
completionType: 'tritonnetwork'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Other options'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON stream output.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['wait', 'w'],
|
|
|
|
type: 'arrayOfBool',
|
|
|
|
help: 'Wait for the creation to complete. Use multiple times for a ' +
|
|
|
|
'spinner.'
|
2017-03-23 01:54:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['wait-timeout'],
|
|
|
|
type: 'positiveInteger',
|
2017-03-23 22:12:24 +02:00
|
|
|
help: 'The number of seconds to wait before timing out with an error.'
|
2017-02-22 01:42:07 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2017-03-09 03:58:04 +02:00
|
|
|
do_create.synopses = ['{{name}} {{cmd}} [OPTIONS]'];
|
2017-02-22 01:42:07 +02:00
|
|
|
|
|
|
|
do_create.help = [
|
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
'Create a volume.',
|
|
|
|
'',
|
|
|
|
'{{usage}}',
|
|
|
|
'',
|
|
|
|
'{{options}}',
|
|
|
|
'',
|
|
|
|
'Note: Currently this dumps prettified JSON by default. That might change',
|
|
|
|
'in the future. Use "-j" to explicitly get JSON output.'
|
|
|
|
/* END JSSTYLED */
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
do_create.completionArgtypes = ['tritonvolume', 'none'];
|
|
|
|
|
|
|
|
module.exports = do_create;
|