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.
|
2014-02-07 23:21:24 +02:00
|
|
|
*/
|
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
/*
|
|
|
|
* This module provides functions to read and write (a) a TritonApi config
|
|
|
|
* and (b) TritonApi profiles.
|
|
|
|
*
|
|
|
|
* The config is a JSON object loaded from "etc/defaults.json" (shipped with
|
|
|
|
* node-triton) plus possibly overrides from "$configDir/config.json" --
|
|
|
|
* which is "~/.triton/config.json" for the `triton` CLI. The config has
|
|
|
|
* a strict set of allowed keys.
|
|
|
|
*
|
|
|
|
* A profile is a small object that includes the necessary info for talking
|
|
|
|
* to a CloudAPI. E.g.:
|
|
|
|
* {
|
|
|
|
* "name": "east1",
|
|
|
|
* "account": "billy.bob",
|
|
|
|
* "keyId": "de:e7:73:9a:aa:91:bb:3e:72:8d:cc:62:ca:58:a2:ec",
|
|
|
|
* "url": "https://us-east-1.api.joyent.com"
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Profiles are stored as separate JSON files in
|
|
|
|
* "$configDir/profiles.d/$name.json". Typically `triton profiles ...` is
|
|
|
|
* used to manage them. In addition there is the special "env" profile that
|
|
|
|
* is constructed from the "SDC_*" environment variables.
|
|
|
|
*/
|
|
|
|
|
2014-02-07 23:21:24 +02:00
|
|
|
var assert = require('assert-plus');
|
2015-08-27 03:21:27 +03:00
|
|
|
var format = require('util').format;
|
2014-02-07 23:21:24 +02:00
|
|
|
var fs = require('fs');
|
2015-09-08 19:55:48 +03:00
|
|
|
var mkdirp = require('mkdirp');
|
2014-02-07 23:21:24 +02:00
|
|
|
var path = require('path');
|
2015-09-08 19:55:48 +03:00
|
|
|
var vasync = require('vasync');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
var common = require('./common');
|
2015-07-26 08:45:20 +03:00
|
|
|
var errors = require('./errors');
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
var DEFAULTS_PATH = path.resolve(__dirname, '..', 'etc', 'defaults.json');
|
2015-09-08 19:55:48 +03:00
|
|
|
var OVERRIDE_NAMES = []; // config object keys to do a one-level deep override
|
|
|
|
|
|
|
|
// TODO: use this const to create the "Configuration" docs table.
|
|
|
|
var CONFIG_VAR_NAMES = [
|
|
|
|
'profile',
|
|
|
|
'cacheDir'
|
|
|
|
];
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-09-10 07:53:38 +03:00
|
|
|
// TODO: use this to create a profile doc table?
|
|
|
|
var PROFILE_FIELDS = {
|
|
|
|
name: true,
|
|
|
|
url: true,
|
|
|
|
account: true,
|
|
|
|
keyId: true,
|
2015-11-04 01:40:59 +02:00
|
|
|
insecure: true,
|
|
|
|
user: true
|
2015-09-10 07:53:38 +03:00
|
|
|
};
|
2015-08-27 03:21:27 +03:00
|
|
|
|
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
// --- internal support stuff
|
|
|
|
|
|
|
|
function configPathFromDir(configDir) {
|
|
|
|
return path.resolve(configDir, 'config.json');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- Config
|
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
/**
|
2015-09-08 19:55:48 +03:00
|
|
|
* Load the TritonApi config. This is a merge of the built-in "defaults" (at
|
|
|
|
* etc/defaults.json) and the "user" config (at "$configDir/config.json",
|
|
|
|
* typically "~/.triton/config.json", if it exists).
|
|
|
|
*
|
|
|
|
* This includes some internal data on keys with a leading underscore:
|
|
|
|
* _defaults the defaults.json object
|
|
|
|
* _user the "user" config.json object
|
|
|
|
* _configDir the user config dir
|
2015-07-26 08:45:20 +03:00
|
|
|
*
|
2015-09-08 19:55:48 +03:00
|
|
|
* @returns {Object} The loaded config.
|
2015-07-26 08:45:20 +03:00
|
|
|
*/
|
2015-09-08 19:55:48 +03:00
|
|
|
function loadConfig(opts) {
|
2015-08-27 03:21:27 +03:00
|
|
|
assert.object(opts, 'opts');
|
2015-09-08 19:55:48 +03:00
|
|
|
assert.string(opts.configDir, 'opts.configDir');
|
|
|
|
|
2015-12-08 21:59:45 +02:00
|
|
|
var configDir = common.tildeSync(opts.configDir);
|
|
|
|
var configPath = configPathFromDir(configDir);
|
2015-08-27 03:21:27 +03:00
|
|
|
|
2015-07-26 08:45:20 +03:00
|
|
|
var c = fs.readFileSync(DEFAULTS_PATH, 'utf8');
|
|
|
|
var _defaults = JSON.parse(c);
|
|
|
|
var config = JSON.parse(c);
|
2015-09-08 19:55:48 +03:00
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
c = fs.readFileSync(configPath, 'utf8');
|
2015-07-26 08:45:20 +03:00
|
|
|
var _user = JSON.parse(c);
|
|
|
|
var userConfig = JSON.parse(c);
|
2015-09-01 22:03:52 +03:00
|
|
|
if (typeof (userConfig) !== 'object' || Array.isArray(userConfig)) {
|
2015-07-26 08:45:20 +03:00
|
|
|
throw new errors.ConfigError(
|
2015-09-08 19:55:48 +03:00
|
|
|
format('"%s" is not an object', configPath));
|
2015-07-26 08:45:20 +03:00
|
|
|
}
|
|
|
|
// These special keys are merged into the key of the same name in the
|
|
|
|
// base "defaults.json".
|
|
|
|
Object.keys(userConfig).forEach(function (key) {
|
2015-09-08 19:55:48 +03:00
|
|
|
if (~OVERRIDE_NAMES.indexOf(key) && config[key] !== undefined) {
|
2015-07-26 08:45:20 +03:00
|
|
|
Object.keys(userConfig[key]).forEach(function (subKey) {
|
|
|
|
if (userConfig[key][subKey] === null) {
|
|
|
|
delete config[key][subKey];
|
|
|
|
} else {
|
|
|
|
config[key][subKey] = userConfig[key][subKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
config[key] = userConfig[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
config._user = _user;
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
2015-07-26 08:45:20 +03:00
|
|
|
config._defaults = _defaults;
|
2015-12-08 21:59:45 +02:00
|
|
|
config._configDir = configDir;
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setConfigVar(opts, cb) {
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.string(opts.configDir, 'opts.configDir');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
assert.string(opts.value, 'opts.value');
|
|
|
|
assert.ok(opts.name.indexOf('.') === -1,
|
|
|
|
'dotted config name not yet supported');
|
|
|
|
assert.ok(CONFIG_VAR_NAMES.indexOf(opts.name) !== -1,
|
|
|
|
'unknown config var name: ' + opts.name);
|
|
|
|
|
|
|
|
var configPath = configPathFromDir(opts.configDir);
|
|
|
|
|
|
|
|
var config;
|
|
|
|
vasync.pipeline({funcs: [
|
|
|
|
function loadExisting(_, next) {
|
|
|
|
fs.exists(configPath, function (exists) {
|
|
|
|
if (!exists) {
|
|
|
|
config = {};
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
fs.readFile(configPath, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
config = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
return next(e);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function mkConfigDir(_, next) {
|
|
|
|
fs.exists(opts.configDir, function (exists) {
|
|
|
|
if (!exists) {
|
|
|
|
mkdirp(opts.configDir, next);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
function updateAndSave(_, next) {
|
|
|
|
config[opts.name] = opts.value;
|
|
|
|
fs.writeFile(configPath, JSON.stringify(config, null, 4), next);
|
2015-08-27 03:21:27 +03:00
|
|
|
}
|
2015-09-08 19:55:48 +03:00
|
|
|
]}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- Profiles
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
function validateProfile(profile, profilePath) {
|
|
|
|
assert.object(profile, 'profile');
|
|
|
|
assert.optionalString(profilePath, 'profilePath');
|
|
|
|
|
|
|
|
try {
|
|
|
|
assert.string(profile.name, 'profile.name');
|
|
|
|
assert.string(profile.url, 'profile.url');
|
|
|
|
assert.string(profile.account, 'profile.account');
|
|
|
|
assert.string(profile.keyId, 'profile.keyId');
|
|
|
|
assert.optionalBool(profile.insecure, 'profile.insecure');
|
2015-11-04 01:40:59 +02:00
|
|
|
assert.optionalString(profile.user, 'profile.user');
|
2015-09-28 22:20:21 +03:00
|
|
|
} catch (err) {
|
|
|
|
throw new errors.ConfigError(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
var bogusFields = [];
|
|
|
|
Object.keys(profile).forEach(function (field) {
|
|
|
|
if (!PROFILE_FIELDS[field]) {
|
|
|
|
bogusFields.push(field);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (bogusFields.length) {
|
|
|
|
throw new errors.ConfigError(format(
|
|
|
|
'extraneous fields in "%s" profile: %s%s', profile.name,
|
|
|
|
(profilePath ? profilePath + ': ' : ''), bogusFields.join(', ')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
/**
|
2015-09-21 22:34:37 +03:00
|
|
|
* Load the special 'env' profile, which handles details of getting
|
|
|
|
* values from envvars. Typically we'd piggyback on dashdash's env support
|
|
|
|
* <https://github.com/trentm/node-dashdash#environment-variable-integration>.
|
|
|
|
* However, per the "Environment variable integration" comment in cli.js, we
|
|
|
|
* do that manually.
|
2015-09-08 19:55:48 +03:00
|
|
|
*
|
|
|
|
* @returns {Object} The 'env' profile.
|
|
|
|
*/
|
2015-09-21 22:34:37 +03:00
|
|
|
function _loadEnvProfile() {
|
2015-09-08 19:55:48 +03:00
|
|
|
var envProfile = {
|
2015-09-21 22:34:37 +03:00
|
|
|
name: 'env'
|
2015-09-08 19:55:48 +03:00
|
|
|
};
|
2015-09-21 22:34:37 +03:00
|
|
|
|
|
|
|
envProfile.account = process.env.TRITON_ACCOUNT || process.env.SDC_ACCOUNT;
|
2015-11-05 21:33:59 +02:00
|
|
|
var user = process.env.TRITON_USER || process.env.SDC_USER;
|
|
|
|
if (user) {
|
|
|
|
envProfile.user = user;
|
|
|
|
}
|
2015-09-21 22:34:37 +03:00
|
|
|
envProfile.url = process.env.TRITON_URL || process.env.SDC_URL;
|
2015-09-25 20:22:58 +03:00
|
|
|
envProfile.keyId = process.env.TRITON_KEY_ID || process.env.SDC_KEY_ID;
|
2015-09-21 22:34:37 +03:00
|
|
|
if (process.env.TRITON_TLS_INSECURE) {
|
|
|
|
envProfile.insecure = common.boolFromString(
|
|
|
|
process.env.TRITON_TLS_INSECURE);
|
|
|
|
} else if (process.env.SDC_TLS_INSECURE) {
|
2015-09-08 19:55:48 +03:00
|
|
|
envProfile.insecure = common.boolFromString(
|
2015-09-21 22:34:37 +03:00
|
|
|
process.env.SDC_TLS_INSECURE);
|
|
|
|
} else if (process.env.SDC_TESTING) { // deprecated
|
|
|
|
envProfile.insecure = common.boolFromString(process.env.SDC_TESTING);
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
validateProfile(envProfile);
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
return envProfile;
|
2014-02-07 23:21:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
function _profileFromPath(profilePath, name) {
|
|
|
|
if (! fs.existsSync(profilePath)) {
|
|
|
|
throw new errors.ConfigError('no such profile: ' + name);
|
|
|
|
}
|
|
|
|
var profile;
|
|
|
|
try {
|
|
|
|
profile = JSON.parse(fs.readFileSync(profilePath, 'utf8'));
|
|
|
|
} catch (e) {
|
|
|
|
throw new errors.ConfigError(e, format(
|
|
|
|
'error in "%s" profile: %s: %s', name,
|
|
|
|
profilePath, e.message));
|
|
|
|
}
|
2015-09-10 07:53:38 +03:00
|
|
|
if (profile.name) {
|
|
|
|
throw new errors.ConfigError(format(
|
|
|
|
'error in "%s" profile: %s: file must not include "name" field',
|
|
|
|
name, profilePath));
|
|
|
|
}
|
2015-09-08 19:55:48 +03:00
|
|
|
profile.name = name;
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
validateProfile(profile, profilePath);
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
function loadProfile(opts) {
|
|
|
|
assert.string(opts.name, 'opts.name');
|
2015-12-08 21:59:45 +02:00
|
|
|
assert.optionalString(opts.configDir, 'opts.configDir');
|
2015-09-08 19:55:48 +03:00
|
|
|
|
2015-09-21 22:34:37 +03:00
|
|
|
if (opts.name === 'env') {
|
|
|
|
return _loadEnvProfile();
|
2015-12-08 21:59:45 +02:00
|
|
|
} else if (!opts.configDir) {
|
|
|
|
throw new errors.TritonError(
|
|
|
|
'cannot load profiles (other than "env") without `opts.configDir`');
|
2015-09-21 22:34:37 +03:00
|
|
|
} else {
|
2015-12-08 21:59:45 +02:00
|
|
|
var profilePath = path.resolve(
|
|
|
|
common.tildeSync(opts.configDir), 'profiles.d',
|
2015-09-21 22:34:37 +03:00
|
|
|
opts.name + '.json');
|
|
|
|
return _profileFromPath(profilePath, opts.name);
|
|
|
|
}
|
2015-09-08 19:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadAllProfiles(opts) {
|
|
|
|
assert.string(opts.configDir, 'opts.configDir');
|
|
|
|
assert.object(opts.log, 'opts.log');
|
|
|
|
|
2015-09-21 22:34:37 +03:00
|
|
|
var profiles = [
|
|
|
|
_loadEnvProfile()
|
|
|
|
];
|
2015-09-21 21:01:00 +03:00
|
|
|
|
2015-12-08 21:59:45 +02:00
|
|
|
var d = path.join(common.tildeSync(opts.configDir), 'profiles.d');
|
2015-09-21 21:01:00 +03:00
|
|
|
var files = fs.readdirSync(d);
|
|
|
|
files.forEach(function (file) {
|
|
|
|
file = path.join(d, file);
|
|
|
|
var ext = path.extname(file);
|
|
|
|
if (ext !== '.json')
|
|
|
|
return;
|
|
|
|
|
2015-09-08 19:55:48 +03:00
|
|
|
var name = path.basename(file).slice(0, - path.extname(file).length);
|
|
|
|
if (name.toLowerCase() === 'env') {
|
|
|
|
// Skip the special 'env'.
|
2015-09-21 22:34:37 +03:00
|
|
|
opts.log.warn({profilePath: file},
|
|
|
|
'invalid "env" profile; skipping');
|
2015-09-21 21:01:00 +03:00
|
|
|
return;
|
2015-09-08 19:55:48 +03:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
profiles.push(_profileFromPath(file, name));
|
|
|
|
} catch (e) {
|
|
|
|
opts.log.warn({err: e, profilePath: file},
|
|
|
|
'error loading profile; skipping');
|
|
|
|
}
|
2015-09-21 21:01:00 +03:00
|
|
|
});
|
2015-09-08 19:55:48 +03:00
|
|
|
|
|
|
|
return profiles;
|
|
|
|
}
|
2014-02-07 23:21:24 +02:00
|
|
|
|
2015-09-25 22:19:29 +03:00
|
|
|
function deleteProfile(opts) {
|
|
|
|
assert.string(opts.configDir, 'opts.configDir');
|
|
|
|
assert.string(opts.name, 'opts.name');
|
|
|
|
|
|
|
|
if (opts.name === 'env') {
|
|
|
|
throw new Error('cannot delete "env" profile');
|
|
|
|
}
|
|
|
|
|
|
|
|
var profilePath = path.resolve(opts.configDir, 'profiles.d',
|
|
|
|
opts.name + '.json');
|
|
|
|
fs.unlinkSync(profilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveProfileSync(opts) {
|
|
|
|
assert.string(opts.configDir, 'opts.configDir');
|
|
|
|
assert.object(opts.profile, 'opts.profile');
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
var name = opts.profile.name;
|
|
|
|
if (name === 'env') {
|
2015-09-25 22:19:29 +03:00
|
|
|
throw new Error('cannot save "env" profile');
|
|
|
|
}
|
|
|
|
|
2015-09-28 22:20:21 +03:00
|
|
|
validateProfile(opts.profile);
|
2015-09-25 22:19:29 +03:00
|
|
|
|
|
|
|
var toSave = common.objCopy(opts.profile);
|
|
|
|
delete toSave.name;
|
|
|
|
|
|
|
|
var profilePath = path.resolve(opts.configDir, 'profiles.d',
|
2015-09-28 22:20:21 +03:00
|
|
|
name + '.json');
|
2015-09-25 22:19:29 +03:00
|
|
|
fs.writeFileSync(profilePath, JSON.stringify(toSave, null, 4), 'utf8');
|
2015-09-28 22:20:21 +03:00
|
|
|
console.log('Saved profile "%s"', name);
|
2015-09-25 22:19:29 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 23:21:24 +02:00
|
|
|
|
|
|
|
//---- exports
|
|
|
|
|
|
|
|
module.exports = {
|
2015-09-08 19:55:48 +03:00
|
|
|
loadConfig: loadConfig,
|
|
|
|
setConfigVar: setConfigVar,
|
2015-09-28 22:20:21 +03:00
|
|
|
|
|
|
|
validateProfile: validateProfile,
|
2015-09-08 19:55:48 +03:00
|
|
|
loadProfile: loadProfile,
|
2015-09-25 22:19:29 +03:00
|
|
|
loadAllProfiles: loadAllProfiles,
|
|
|
|
deleteProfile: deleteProfile,
|
|
|
|
saveProfileSync: saveProfileSync
|
2014-02-07 23:21:24 +02:00
|
|
|
};
|
|
|
|
// vim: set softtabstop=4 shiftwidth=4:
|