2015-12-31 01:25:12 +02:00
|
|
|
/*
|
2017-02-21 01:46:08 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2015-12-31 01:25:12 +02:00
|
|
|
* `triton profile create ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var format = require('util').format;
|
|
|
|
var fs = require('fs');
|
|
|
|
var sshpk = require('sshpk');
|
|
|
|
var vasync = require('vasync');
|
2016-12-13 20:04:41 +02:00
|
|
|
var auth = require('smartdc-auth');
|
2017-02-21 01:46:08 +02:00
|
|
|
var wordwrap = require('wordwrap');
|
2015-12-31 01:25:12 +02:00
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var errors = require('../errors');
|
|
|
|
var mod_config = require('../config');
|
2016-03-19 01:51:22 +02:00
|
|
|
var profilecommon = require('./profilecommon');
|
2015-12-31 01:25:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
function _createProfile(opts, cb) {
|
|
|
|
assert.object(opts.cli, 'opts.cli');
|
|
|
|
assert.optionalString(opts.file, 'opts.file');
|
|
|
|
assert.optionalString(opts.copy, 'opts.copy');
|
2016-03-19 01:51:22 +02:00
|
|
|
assert.optionalBool(opts.noDocker, 'opts.noDocker');
|
2017-02-24 19:29:05 +02:00
|
|
|
assert.optionalBool(opts.yes, 'opts.yes');
|
2015-12-31 01:25:12 +02:00
|
|
|
assert.func(cb, 'cb');
|
|
|
|
var cli = opts.cli;
|
|
|
|
var log = cli.log;
|
|
|
|
|
|
|
|
var data;
|
2017-02-21 01:46:08 +02:00
|
|
|
var wrap80 = wordwrap(Math.min(process.stdout.columns, 80));
|
2015-12-31 01:25:12 +02:00
|
|
|
|
|
|
|
vasync.pipeline({arg: {}, funcs: [
|
|
|
|
function getExistingProfiles(ctx, next) {
|
|
|
|
try {
|
|
|
|
ctx.profiles = mod_config.loadAllProfiles({
|
|
|
|
configDir: cli.configDir,
|
|
|
|
log: cli.log
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
function getCopy(ctx, next) {
|
|
|
|
if (!opts.copy) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
for (var i = 0; i < ctx.profiles.length; i++) {
|
|
|
|
if (ctx.profiles[i].name === opts.copy) {
|
|
|
|
ctx.copy = ctx.profiles[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ctx.copy) {
|
|
|
|
next(new errors.UsageError(format(
|
|
|
|
'no such profile from which to copy: "%s"', opts.copy)));
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
},
|
2017-02-21 01:46:08 +02:00
|
|
|
function determineInputType(ctx, next) {
|
|
|
|
/*
|
|
|
|
* Are we gathering profile data from stdin, a file, or
|
|
|
|
* interactively?
|
|
|
|
*/
|
|
|
|
if (opts.file === '-') {
|
|
|
|
ctx.inputType = 'stdin';
|
|
|
|
} else if (opts.file) {
|
|
|
|
ctx.inputType = 'file';
|
|
|
|
} else if (!process.stdin.isTTY) {
|
|
|
|
return next(new errors.UsageError('cannot interactively ' +
|
|
|
|
'create profile: stdin is not a TTY'));
|
|
|
|
} else if (!process.stdout.isTTY) {
|
|
|
|
return next(new errors.UsageError('cannot interactively ' +
|
|
|
|
'create profile: stdout is not a TTY'));
|
|
|
|
} else {
|
|
|
|
ctx.inputType = 'interactive';
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
function gatherDataStdin(ctx, next) {
|
|
|
|
if (ctx.inputType !== 'stdin') {
|
|
|
|
next();
|
|
|
|
return;
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
var stdin = '';
|
|
|
|
process.stdin.resume();
|
|
|
|
process.stdin.on('data', function (chunk) {
|
|
|
|
stdin += chunk;
|
|
|
|
});
|
|
|
|
process.stdin.on('end', function () {
|
|
|
|
try {
|
|
|
|
data = JSON.parse(stdin);
|
|
|
|
} catch (err) {
|
|
|
|
log.trace({stdin: stdin}, 'invalid profile JSON on stdin');
|
|
|
|
return next(new errors.TritonError(
|
|
|
|
format('invalid profile JSON on stdin: %s', err)));
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function gatherDataFile(ctx, next) {
|
2017-02-21 01:46:08 +02:00
|
|
|
if (ctx.inputType !== 'file') {
|
|
|
|
next();
|
|
|
|
return;
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
ctx.filePath = opts.file;
|
|
|
|
var input = fs.readFileSync(opts.file);
|
|
|
|
try {
|
|
|
|
data = JSON.parse(input);
|
|
|
|
} catch (err) {
|
|
|
|
return next(new errors.TritonError(format(
|
|
|
|
'invalid profile JSON in "%s": %s', opts.file, err)));
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
2017-02-21 01:46:08 +02:00
|
|
|
|
|
|
|
function interactiveGatherKeyChoices(ctx, next) {
|
|
|
|
if (ctx.inputType !== 'interactive') {
|
|
|
|
next();
|
|
|
|
return;
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
|
2017-02-21 01:46:08 +02:00
|
|
|
/*
|
|
|
|
* The description of the `keyId` field includes discovered SSH keys
|
|
|
|
* for this user.
|
|
|
|
*/
|
2016-12-13 20:04:41 +02:00
|
|
|
var kr = new auth.KeyRing();
|
2017-02-21 01:46:08 +02:00
|
|
|
ctx.keyChoices = [];
|
|
|
|
|
|
|
|
kr.list(function (err, pairs) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object.keys(pairs).forEach(function (keyId) {
|
|
|
|
var valid = pairs[keyId].filter(function (kp) {
|
|
|
|
return (kp.canSign());
|
|
|
|
});
|
|
|
|
if (valid.length < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx.keyChoices.push({
|
|
|
|
keyId: keyId,
|
|
|
|
keyPairs: pairs[keyId],
|
|
|
|
pubKey: valid[0].getPublicKey()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function gatherDataInteractive(ctx, next) {
|
|
|
|
if (ctx.inputType !== 'interactive') {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
2016-12-13 20:04:41 +02:00
|
|
|
|
2015-12-31 01:25:12 +02:00
|
|
|
var defaults = {};
|
|
|
|
if (ctx.copy) {
|
|
|
|
defaults = ctx.copy;
|
|
|
|
delete defaults.name; // we don't copy a profile name
|
|
|
|
} else {
|
|
|
|
defaults.url = 'https://us-sw-1.api.joyent.com';
|
|
|
|
}
|
|
|
|
|
2017-02-21 01:46:08 +02:00
|
|
|
/*
|
|
|
|
* The description of the `keyId` field includes discovered SSH keys
|
|
|
|
* for this user.
|
|
|
|
*/
|
|
|
|
var keyIdDesc = 'The fingerprint of the SSH key you want to use ' +
|
|
|
|
'to authenticate with CloudAPI.\n' +
|
|
|
|
'Specify the fingerprint or the index of one of the found ' +
|
|
|
|
'keys in the list\n' +
|
|
|
|
'below. If the key you want to use is not listed, make sure ' +
|
|
|
|
'it is either saved\n' +
|
|
|
|
'in your SSH keys directory (~/.ssh) or loaded into your ' +
|
|
|
|
'SSH agent.\n';
|
|
|
|
if (ctx.keyChoices.length === 0) {
|
|
|
|
keyIdDesc += '\n(No SSH keys were found.)\n';
|
|
|
|
} else {
|
|
|
|
var n = 1;
|
|
|
|
ctx.keyChoices.forEach(function (keyChoice) {
|
|
|
|
keyIdDesc += format('\n%d. Fingerprint "%s" (%s-bit %s)\n',
|
|
|
|
n, keyChoice.keyId, keyChoice.pubKey.size,
|
|
|
|
keyChoice.pubKey.type.toUpperCase());
|
|
|
|
keyChoice.keyPairs.forEach(function (kp) {
|
|
|
|
var lockedStr = (kp.isLocked() ? ' (locked)' : '');
|
|
|
|
var comment = kp.comment || kp.getPublicKey().comment;
|
|
|
|
var detailsStr;
|
|
|
|
switch (kp.plugin) {
|
|
|
|
case 'agent':
|
|
|
|
detailsStr = comment;
|
|
|
|
break;
|
|
|
|
case 'homedir':
|
|
|
|
detailsStr = format('$HOME/.ssh/%s (comment "%s")',
|
|
|
|
kp.source, comment);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
detailsStr = format('%s %s',
|
|
|
|
comment, (kp.source || ''));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
keyIdDesc += format(' - in %s%s: %s\n',
|
|
|
|
kp.plugin, lockedStr, detailsStr);
|
|
|
|
});
|
|
|
|
n++;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-31 01:25:12 +02:00
|
|
|
var fields = [ {
|
2017-02-21 01:46:08 +02:00
|
|
|
desc: 'A profile name. A short string to identify this ' +
|
|
|
|
'profile to the `triton` command.',
|
2015-12-31 01:25:12 +02:00
|
|
|
key: 'name',
|
|
|
|
default: defaults.name,
|
|
|
|
validate: function validateName(value, valCb) {
|
|
|
|
var regex = /^[a-z][a-z0-9_.-]*$/;
|
|
|
|
if (!regex.test(value)) {
|
|
|
|
return valCb(new Error('Must start with a lowercase ' +
|
|
|
|
'letter followed by lowercase letters, numbers ' +
|
|
|
|
'and "_", "." and "-".'));
|
|
|
|
}
|
|
|
|
for (var i = 0; i < ctx.profiles.length; i++) {
|
|
|
|
if (ctx.profiles[i].name === value) {
|
|
|
|
return valCb(new Error(format(
|
|
|
|
'Profile "%s" already exists.', value)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valCb();
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
desc: 'The CloudAPI endpoint URL.',
|
|
|
|
default: defaults.url,
|
|
|
|
key: 'url'
|
|
|
|
}, {
|
|
|
|
desc: 'Your account login name.',
|
|
|
|
key: 'account',
|
|
|
|
default: defaults.account,
|
|
|
|
validate: function validateAccount(value, valCb) {
|
|
|
|
var regex = /^[^\\]{3,}$/;
|
|
|
|
if (value.length < 3) {
|
|
|
|
return valCb(new Error(
|
|
|
|
'Must be at least 3 characters'));
|
|
|
|
}
|
|
|
|
if (!regex.test(value)) {
|
|
|
|
return valCb(new Error('Must not container a "\\"'));
|
|
|
|
}
|
|
|
|
valCb();
|
|
|
|
}
|
|
|
|
}, {
|
2017-02-21 01:46:08 +02:00
|
|
|
desc: keyIdDesc,
|
2015-12-31 01:25:12 +02:00
|
|
|
key: 'keyId',
|
2017-02-21 18:56:04 +02:00
|
|
|
default: defaults.keyId,
|
2015-12-31 01:25:12 +02:00
|
|
|
validate: function validateKeyId(value, valCb) {
|
|
|
|
// First try as a fingerprint.
|
|
|
|
try {
|
|
|
|
sshpk.parseFingerprint(value);
|
|
|
|
return valCb();
|
|
|
|
} catch (fpErr) {
|
|
|
|
}
|
|
|
|
|
2017-02-21 01:46:08 +02:00
|
|
|
// Try as a list index.
|
|
|
|
var idx = Number(value) - 1;
|
|
|
|
if (ctx.keyChoices[idx] !== undefined) {
|
|
|
|
var keyId = ctx.keyChoices[idx].keyId;
|
|
|
|
console.log('Using key %s: %s', value, keyId);
|
|
|
|
return valCb(null, keyId);
|
2016-12-13 20:04:41 +02:00
|
|
|
}
|
2016-03-19 01:51:22 +02:00
|
|
|
|
2016-12-13 20:04:41 +02:00
|
|
|
valCb(new Error(format(
|
2017-02-21 01:46:08 +02:00
|
|
|
'"%s" is neither a valid fingerprint, nor an index ' +
|
2016-12-13 20:04:41 +02:00
|
|
|
'from the list of available keys', value)));
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
} ];
|
|
|
|
|
|
|
|
data = {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There are some value profile fields that we don't yet prompt
|
|
|
|
* for -- because they are experimental, optional, or I'm just
|
|
|
|
* unsure about adding them yet. :) We should still *copy* those
|
|
|
|
* over for a `triton profile create --copy ...`.
|
|
|
|
*
|
|
|
|
* Eventually the need for this block should go away.
|
|
|
|
*/
|
|
|
|
if (ctx.copy) {
|
|
|
|
var promptKeys = fields.map(
|
|
|
|
function (field) { return field.key; });
|
|
|
|
Object.keys(ctx.copy).forEach(function (key) {
|
|
|
|
if (promptKeys.indexOf(key) === -1) {
|
|
|
|
data[key] = ctx.copy[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
vasync.forEachPipeline({
|
|
|
|
inputs: fields,
|
|
|
|
func: function getField(field, nextField) {
|
2016-12-13 20:04:41 +02:00
|
|
|
if (field.key !== 'name')
|
2017-02-21 01:46:08 +02:00
|
|
|
console.log('\n');
|
|
|
|
|
|
|
|
common.promptField(field, function (err, value) {
|
|
|
|
data[field.key] = value;
|
|
|
|
nextField(err);
|
|
|
|
});
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
}, function (err) {
|
|
|
|
console.log();
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function guardAlreadyExists(ctx, next) {
|
|
|
|
for (var i = 0; i < ctx.profiles.length; i++) {
|
|
|
|
if (data.name === ctx.profiles[i].name) {
|
|
|
|
return next(new errors.TritonError(format(
|
|
|
|
'profile "%s" already exists', data.name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
function validateIt(ctx, next) {
|
|
|
|
// We ignore 'curr'. For now at least.
|
|
|
|
delete data.curr;
|
|
|
|
|
|
|
|
try {
|
|
|
|
mod_config.validateProfile(data, ctx.filePath);
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
function saveIt(_, next) {
|
|
|
|
try {
|
|
|
|
mod_config.saveProfileSync({
|
|
|
|
configDir: cli.configDir,
|
|
|
|
profile: data
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
2016-03-19 01:51:22 +02:00
|
|
|
|
|
|
|
function dockerSetup(ctx, next) {
|
|
|
|
if (opts.noDocker || process.platform === 'win32') {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:13:27 +02:00
|
|
|
console.log(common.ansiStylizeTty('\n\n# Docker setup\n', 'bold'));
|
2017-02-21 01:46:08 +02:00
|
|
|
console.log(wrap80('This section will setup authentication to ' +
|
|
|
|
'Triton DataCenter\'s Docker endpoint using your account ' +
|
|
|
|
'and key information specified above. This is only required ' +
|
2017-02-21 05:39:16 +02:00
|
|
|
'if you intend to use `docker` with this profile.\n'));
|
2017-02-21 01:46:08 +02:00
|
|
|
|
2016-03-19 01:51:22 +02:00
|
|
|
profilecommon.profileDockerSetup({
|
|
|
|
cli: cli,
|
|
|
|
name: data.name,
|
|
|
|
keyPaths: ctx.keyPaths,
|
2017-02-24 19:29:05 +02:00
|
|
|
implicit: true,
|
|
|
|
yes: opts.yes
|
2016-03-19 01:51:22 +02:00
|
|
|
}, next);
|
|
|
|
},
|
|
|
|
|
2015-12-31 01:25:12 +02:00
|
|
|
function setCurrIfTheOnlyProfile(ctx, next) {
|
|
|
|
if (ctx.profiles.length !== 0) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:42:23 +02:00
|
|
|
mod_config.setConfigVars({
|
2015-12-31 01:25:12 +02:00
|
|
|
configDir: cli.configDir,
|
2016-03-10 21:42:23 +02:00
|
|
|
vars: {
|
|
|
|
profile: data.name
|
|
|
|
}
|
2015-12-31 01:25:12 +02:00
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-21 21:24:24 +02:00
|
|
|
console.log('\nSet "%s" as current profile (because it is ' +
|
2016-03-19 01:51:22 +02:00
|
|
|
'your only profile).', data.name);
|
2015-12-31 01:25:12 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function do_create(subcmd, opts, args, cb) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
} else if (args.length !== 0) {
|
|
|
|
return cb(new errors.UsageError('too many arguments'));
|
|
|
|
} else if (opts.copy && opts.file) {
|
|
|
|
return cb(new errors.UsageError(
|
|
|
|
'cannot specify --file and --copy at the same time'));
|
|
|
|
}
|
|
|
|
|
|
|
|
_createProfile({
|
|
|
|
cli: this.top,
|
|
|
|
file: opts.file,
|
2016-03-19 01:51:22 +02:00
|
|
|
copy: opts.copy,
|
2017-02-24 19:29:05 +02:00
|
|
|
noDocker: opts.no_docker,
|
|
|
|
yes: opts.yes
|
2015-12-31 01:25:12 +02:00
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_create.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['file', 'f'],
|
|
|
|
type: 'string',
|
|
|
|
helpArg: 'FILE',
|
|
|
|
help: 'A JSON file (of the same form as "triton profile get -j") ' +
|
|
|
|
'with the profile, or "-" to read JSON from stdin.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['copy'],
|
|
|
|
type: 'string',
|
2016-10-01 01:43:51 +03:00
|
|
|
helpArg: 'PROFILE',
|
|
|
|
help: 'A profile from which to copy values.',
|
|
|
|
completionType: 'tritonprofile'
|
2016-03-19 01:51:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['no-docker'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'As of Triton CLI 4.9, creating a profile will attempt (on '
|
|
|
|
+ 'non-Windows) to also setup for running Docker. This is '
|
|
|
|
+ 'experimental and might fail. Use this option to disable '
|
|
|
|
+ 'the attempt.'
|
2017-02-24 19:29:05 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['yes', 'y'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Answer yes to any confirmations.'
|
2015-12-31 01:25:12 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2016-06-09 00:13:16 +03:00
|
|
|
do_create.synopses = ['{{name}} {{cmd}} [OPTIONS]'];
|
2015-12-31 01:25:12 +02:00
|
|
|
do_create.help = [
|
|
|
|
'Create a Triton CLI profile.',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
2015-12-31 01:25:12 +02:00
|
|
|
'',
|
|
|
|
'{{options}}',
|
|
|
|
'',
|
|
|
|
'Examples:',
|
|
|
|
' triton profile create # interactively create a profile',
|
|
|
|
' triton profile create --copy env # ... copying from "env" profile',
|
|
|
|
'',
|
|
|
|
' # Or non-interactively create from stdin or a file:',
|
|
|
|
' cat a-profile.json | triton profile create -f -',
|
|
|
|
' triton profile create -f another-profile.json'
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = do_create;
|