Skip to content

Commit

Permalink
Merge pull request #86 from apigee-internal/custom-config-path
Browse files Browse the repository at this point in the history
Adding support for setting a custom config directory.
  • Loading branch information
Matthew Dobson committed Jan 11, 2017
2 parents 9fadc50 + b0b0903 commit 6afb2f4
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 33 deletions.
8 changes: 7 additions & 1 deletion cli/cmd.js
Expand Up @@ -28,11 +28,13 @@ const setup = function setup() {
.option('-p, --password <password>', 'password of the organization admin')
.option('-r, --url <url>', 'organization\'s custom API URL (https://api.example.com)')
.option('-d, --debug', 'execute with debug output')
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
.action((options) => {
options.error = optionError;
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
configure.configure(options, () => {
Expand All @@ -43,8 +45,10 @@ const setup = function setup() {
commander
.command('init')
.description('initialize default.yaml into home dir')
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
.action((options) => {
init((err,location)=>{
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
init(options, (err,location)=>{
console.log("config initialized to %s",location)
})
});
Expand Down Expand Up @@ -75,6 +79,7 @@ const setup = function setup() {
.option('-p, --processes <processes>', 'number of processes to start, defaults to # of cores')
.option('-d, --pluginDir <pluginDir>','absolute path to plugin directory')
.option('-r, --port <portNumber>','override port in the config.yaml file')
.option('-c, --configDir <configDir>', 'Set the directory where configs are read from.')
.description('start the gateway based on configuration')
.action((options)=>{
options.error = optionError;
Expand All @@ -83,6 +88,7 @@ const setup = function setup() {
options.org = options.org || process.env.EDGEMICRO_ORG;
options.env = options.env || process.env.EDGEMICRO_ENV;
options.processes = options.processes || process.env.EDGEMICRO_PROCESSES;
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;

if (options.port) {
portastic.test(options.port)
Expand Down
7 changes: 4 additions & 3 deletions cli/lib/configure.js
Expand Up @@ -71,11 +71,12 @@ Configure.prototype.configure = function configure(options, cb) {
fs.unlinkSync(targetPath);
//console.log('deleted ' + targetPath);
}


var configFileDirectory = options.configDir || configLocations.homeDir;
//console.log('init config');
edgeconfig.init({
source: configLocations.getDefaultPath(),
targetDir: configLocations.homeDir,
targetDir: configFileDirectory,
targetFile: targetFile,
overwrite: true
}, function (err, configPath) {
Expand Down Expand Up @@ -147,7 +148,7 @@ function configureEdgemicroWithCreds(options, cb) {
if (err) {
return cb(err)
}
agentConfigPath = configLocations.getSourcePath(options.org, options.env);
agentConfigPath = configLocations.getSourcePath(options.org, options.env, options.configDir);
const agentConfig = edgeconfig.load({ source: agentConfigPath });

addEnvVars(agentConfig);
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/gateway.js
Expand Up @@ -32,8 +32,8 @@ Gateway.prototype.start = (options) => {
// so ignore and proceed
}

const source = configLocations.getSourcePath(options.org, options.env);
const cache = configLocations.getCachePath(options.org, options.env);
const source = configLocations.getSourcePath(options.org, options.env, options.configDir);
const cache = configLocations.getCachePath(options.org, options.env, options.configDir);

const keys = {key: options.key, secret: options.secret};
const args = {target: cache, keys: keys, pluginDir: options.pluginDir};
Expand Down
41 changes: 30 additions & 11 deletions cli/lib/init.js
@@ -1,19 +1,38 @@
'use strict'
const fs = require('fs')
const path = require('path');
const configLocations = require('../../config/locations');

module.exports = function init(cb) {
const initConfigPath = configLocations.getInitPath();
const defaultConfigPath = configLocations.getDefaultPath();
if (fs.existsSync(defaultConfigPath)) {
fs.unlinkSync(defaultConfigPath);
module.exports = function init(opts, cb) {
if(typeof opts == 'function') {
cb = opts;
}

const setupConfigPath = (srcFile, destFile, destFileDir, cb) => {

if(fs.existsSync(destFile)) {
fs.unlinkSync(destFile);
}

fs.mkdir(destFileDir, () => {
copyFile(srcFile, destFile, (err) => {
err && console.log("failed to init configpath file %s", err);
cb(err, destFile);
});
});
}

if(!opts.configDir) {
const initConfigPath = configLocations.getInitPath();
const defaultConfigPath = configLocations.getDefaultPath();

setupConfigPath(initConfigPath, defaultConfigPath, configLocations.homeDir, cb);
} else {
const initConfigPath = configLocations.getInitPath();
const customConfigPath = path.join(opts.configDir, configLocations.defaultFile);

setupConfigPath(initConfigPath, customConfigPath, opts.configDir, cb);
}
fs.mkdir(configLocations.homeDir,function(){
copyFile(initConfigPath,defaultConfigPath,(err)=>{
err && console.log("failed to init configpath file %s",err)
cb(err,defaultConfigPath);
})
});
}
function copyFile(source, target, cb) {
var cbCalled = false;
Expand Down
26 changes: 17 additions & 9 deletions config/locations.js
Expand Up @@ -12,21 +12,29 @@ const defaultIPCFileName = 'edgemicro';
const isWin = /^win/.test(process.platform);

module.exports = {
getInitPath: function(){
return path.join(configDir,defaultFile);
getInitPath: function(opts){
return path.join(configDir,defaultFile);
},
getDefaultPath: function(){
return path.join(this.homeDir,defaultFile);
return path.join(this.homeDir,defaultFile);
},
defaultFile: defaultFile,
getSourcePath: function getSource(org,env){
return path.join(this.homeDir, this.getSourceFile(org,env));
getSourcePath: function getSource(org, env, customConfigDir){
if(customConfigDir) {
return path.join(customConfigDir, this.getSourceFile(org,env));
} else {
return path.join(this.homeDir, this.getSourceFile(org,env));
}
},
getSourceFile: function getSourceFile(org,env){
return org + "-" + env + "-" + sourceFile;
},
getCachePath: function getCachePath(org,env){
return path.join(this.homeDir, org + "-" + env + "-" + cacheFile);
getCachePath: function getCachePath(org, env, customConfigDir){
if(customConfigDir) {
return path.join(customConfigDir, org + "-" + env + "-" + cacheFile);
} else {
return path.join(this.homeDir, org + "-" + env + "-" + cacheFile);
}
},
getIPCFilePath: function getIPCFilePath() {
if (!isWin) {
Expand All @@ -35,6 +43,6 @@ module.exports = {
return path.join('\\\\?\\pipe', process.cwd(), defaultIPCFileName);
}
},
homeDir: homeDir,
defaultDir: configDir,
homeDir: homeDir
};
};
58 changes: 58 additions & 0 deletions tests/config.js
@@ -0,0 +1,58 @@
const assert = require('assert');
const locations = require('../config/locations');
const init = require('../cli/lib/init');
const fs = require('fs');

//these are needed to account for the mutation of a singleton pathing module
const path = require('path');
const normalizedPath = path.normalize(__dirname);

describe('configure', () => {
describe('init module', () => {
it('will copy file to custom dir', (done) => {
init({configDir: 'foo'}, (err, file) => {
assert.equal(file, 'foo/default.yaml');

const fileBuf = fs.readFileSync('config/default.yaml');
const testBuf = fs.readFileSync(file);
assert.equal(fileBuf.toString(), testBuf.toString());
done();
});
});
it('will copy file to default dir', () => {
init({}, (err, file) => {
console.log(file);
assert.equal(file, path.join(normalizedPath, 'default.yaml'));

const fileBuf = fs.readFileSync('config/default.yaml');
const testBuf = fs.readFileSync(file);
assert.equal(fileBuf.toString(), testBuf.toString());
done();
});
});
});
describe('locations module', () => {
it('will build a source path without a configDir', () => {
var configPath = locations.getSourcePath('test', 'foo');
//This path differs from the actual config path because we mutate the singleton in other tests.
assert.equal(configPath, path.join(normalizedPath, 'test-foo-config.yaml'));
});
it('will build a source path with a configDir', () => {
var configPath = locations.getSourcePath('test', 'foo', 'foo');
assert.equal(configPath, 'foo/test-foo-config.yaml');
});
it('will build a cache path without a configDir', () => {
var cachePath = locations.getCachePath('test', 'foo');
//This path differs from the actual config path because we mutate the singleton in other tests.
assert.equal(cachePath, path.join(normalizedPath, 'test-foo-cache-config.yaml'));
});
it('will build a cache path with a configDir', () => {
var cachePath = locations.getCachePath('test', 'foo', 'foo');
assert.equal(cachePath, 'foo/test-foo-cache-config.yaml');
});
it('will put together a properly named source file', () => {
var sourceFile = locations.getSourceFile('test', 'foo');
assert.equal(sourceFile, 'test-foo-config.yaml');
});
});
});
9 changes: 2 additions & 7 deletions tests/default.yaml
Expand Up @@ -16,19 +16,15 @@ edgemicro:
restart_sleep: 500
restart_max: 50
max_times: 300
config_change_poll_interval: 600
logging:
level: none
level: error
dir: /var/tmp
stats_log_interval: 60
rotate_interval: 24
plugins:
sequence:
- oauth
- sample
spikearrest:
timeUnit: minute
allow: 100000
buffersize: 0

headers:
x-forwarded-for: true
Expand All @@ -40,4 +36,3 @@ headers:
oauth:
allowNoAuthorization: false
allowInvalidAuthorization: false
sample:

0 comments on commit 6afb2f4

Please sign in to comment.