Skip to content

Commit 6afb2f4

Browse files
author
Matthew Dobson
authored
Merge pull request #86 from apigee-internal/custom-config-path
Adding support for setting a custom config directory.
2 parents 9fadc50 + b0b0903 commit 6afb2f4

File tree

7 files changed

+120
-33
lines changed

7 files changed

+120
-33
lines changed

cli/cmd.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ const setup = function setup() {
2828
.option('-p, --password <password>', 'password of the organization admin')
2929
.option('-r, --url <url>', 'organization\'s custom API URL (https://api.example.com)')
3030
.option('-d, --debug', 'execute with debug output')
31+
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
3132
.action((options) => {
3233
options.error = optionError;
3334
if (!options.username) { return options.error('username is required'); }
3435
if (!options.org) { return options.error('org is required'); }
3536
if (!options.env) { return options.error('env is required'); }
37+
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
3638
promptForPassword(options,(options)=>{
3739
if (!options.password) { return options.error('password is required'); }
3840
configure.configure(options, () => {
@@ -43,8 +45,10 @@ const setup = function setup() {
4345
commander
4446
.command('init')
4547
.description('initialize default.yaml into home dir')
48+
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
4649
.action((options) => {
47-
init((err,location)=>{
50+
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
51+
init(options, (err,location)=>{
4852
console.log("config initialized to %s",location)
4953
})
5054
});
@@ -75,6 +79,7 @@ const setup = function setup() {
7579
.option('-p, --processes <processes>', 'number of processes to start, defaults to # of cores')
7680
.option('-d, --pluginDir <pluginDir>','absolute path to plugin directory')
7781
.option('-r, --port <portNumber>','override port in the config.yaml file')
82+
.option('-c, --configDir <configDir>', 'Set the directory where configs are read from.')
7883
.description('start the gateway based on configuration')
7984
.action((options)=>{
8085
options.error = optionError;
@@ -83,6 +88,7 @@ const setup = function setup() {
8388
options.org = options.org || process.env.EDGEMICRO_ORG;
8489
options.env = options.env || process.env.EDGEMICRO_ENV;
8590
options.processes = options.processes || process.env.EDGEMICRO_PROCESSES;
91+
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
8692

8793
if (options.port) {
8894
portastic.test(options.port)

cli/lib/configure.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ Configure.prototype.configure = function configure(options, cb) {
7171
fs.unlinkSync(targetPath);
7272
//console.log('deleted ' + targetPath);
7373
}
74-
74+
75+
var configFileDirectory = options.configDir || configLocations.homeDir;
7576
//console.log('init config');
7677
edgeconfig.init({
7778
source: configLocations.getDefaultPath(),
78-
targetDir: configLocations.homeDir,
79+
targetDir: configFileDirectory,
7980
targetFile: targetFile,
8081
overwrite: true
8182
}, function (err, configPath) {
@@ -147,7 +148,7 @@ function configureEdgemicroWithCreds(options, cb) {
147148
if (err) {
148149
return cb(err)
149150
}
150-
agentConfigPath = configLocations.getSourcePath(options.org, options.env);
151+
agentConfigPath = configLocations.getSourcePath(options.org, options.env, options.configDir);
151152
const agentConfig = edgeconfig.load({ source: agentConfigPath });
152153

153154
addEnvVars(agentConfig);

cli/lib/gateway.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Gateway.prototype.start = (options) => {
3232
// so ignore and proceed
3333
}
3434

35-
const source = configLocations.getSourcePath(options.org, options.env);
36-
const cache = configLocations.getCachePath(options.org, options.env);
35+
const source = configLocations.getSourcePath(options.org, options.env, options.configDir);
36+
const cache = configLocations.getCachePath(options.org, options.env, options.configDir);
3737

3838
const keys = {key: options.key, secret: options.secret};
3939
const args = {target: cache, keys: keys, pluginDir: options.pluginDir};

cli/lib/init.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
'use strict'
22
const fs = require('fs')
3+
const path = require('path');
34
const configLocations = require('../../config/locations');
45

5-
module.exports = function init(cb) {
6-
const initConfigPath = configLocations.getInitPath();
7-
const defaultConfigPath = configLocations.getDefaultPath();
8-
if (fs.existsSync(defaultConfigPath)) {
9-
fs.unlinkSync(defaultConfigPath);
6+
module.exports = function init(opts, cb) {
7+
if(typeof opts == 'function') {
8+
cb = opts;
9+
}
10+
11+
const setupConfigPath = (srcFile, destFile, destFileDir, cb) => {
12+
13+
if(fs.existsSync(destFile)) {
14+
fs.unlinkSync(destFile);
15+
}
16+
17+
fs.mkdir(destFileDir, () => {
18+
copyFile(srcFile, destFile, (err) => {
19+
err && console.log("failed to init configpath file %s", err);
20+
cb(err, destFile);
21+
});
22+
});
23+
}
24+
25+
if(!opts.configDir) {
26+
const initConfigPath = configLocations.getInitPath();
27+
const defaultConfigPath = configLocations.getDefaultPath();
28+
29+
setupConfigPath(initConfigPath, defaultConfigPath, configLocations.homeDir, cb);
30+
} else {
31+
const initConfigPath = configLocations.getInitPath();
32+
const customConfigPath = path.join(opts.configDir, configLocations.defaultFile);
33+
34+
setupConfigPath(initConfigPath, customConfigPath, opts.configDir, cb);
1035
}
11-
fs.mkdir(configLocations.homeDir,function(){
12-
copyFile(initConfigPath,defaultConfigPath,(err)=>{
13-
err && console.log("failed to init configpath file %s",err)
14-
cb(err,defaultConfigPath);
15-
})
16-
});
1736
}
1837
function copyFile(source, target, cb) {
1938
var cbCalled = false;

config/locations.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,29 @@ const defaultIPCFileName = 'edgemicro';
1212
const isWin = /^win/.test(process.platform);
1313

1414
module.exports = {
15-
getInitPath: function(){
16-
return path.join(configDir,defaultFile);
15+
getInitPath: function(opts){
16+
return path.join(configDir,defaultFile);
1717
},
1818
getDefaultPath: function(){
19-
return path.join(this.homeDir,defaultFile);
19+
return path.join(this.homeDir,defaultFile);
2020
},
2121
defaultFile: defaultFile,
22-
getSourcePath: function getSource(org,env){
23-
return path.join(this.homeDir, this.getSourceFile(org,env));
22+
getSourcePath: function getSource(org, env, customConfigDir){
23+
if(customConfigDir) {
24+
return path.join(customConfigDir, this.getSourceFile(org,env));
25+
} else {
26+
return path.join(this.homeDir, this.getSourceFile(org,env));
27+
}
2428
},
2529
getSourceFile: function getSourceFile(org,env){
2630
return org + "-" + env + "-" + sourceFile;
2731
},
28-
getCachePath: function getCachePath(org,env){
29-
return path.join(this.homeDir, org + "-" + env + "-" + cacheFile);
32+
getCachePath: function getCachePath(org, env, customConfigDir){
33+
if(customConfigDir) {
34+
return path.join(customConfigDir, org + "-" + env + "-" + cacheFile);
35+
} else {
36+
return path.join(this.homeDir, org + "-" + env + "-" + cacheFile);
37+
}
3038
},
3139
getIPCFilePath: function getIPCFilePath() {
3240
if (!isWin) {
@@ -35,6 +43,6 @@ module.exports = {
3543
return path.join('\\\\?\\pipe', process.cwd(), defaultIPCFileName);
3644
}
3745
},
46+
homeDir: homeDir,
3847
defaultDir: configDir,
39-
homeDir: homeDir
40-
};
48+
};

tests/config.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const assert = require('assert');
2+
const locations = require('../config/locations');
3+
const init = require('../cli/lib/init');
4+
const fs = require('fs');
5+
6+
//these are needed to account for the mutation of a singleton pathing module
7+
const path = require('path');
8+
const normalizedPath = path.normalize(__dirname);
9+
10+
describe('configure', () => {
11+
describe('init module', () => {
12+
it('will copy file to custom dir', (done) => {
13+
init({configDir: 'foo'}, (err, file) => {
14+
assert.equal(file, 'foo/default.yaml');
15+
16+
const fileBuf = fs.readFileSync('config/default.yaml');
17+
const testBuf = fs.readFileSync(file);
18+
assert.equal(fileBuf.toString(), testBuf.toString());
19+
done();
20+
});
21+
});
22+
it('will copy file to default dir', () => {
23+
init({}, (err, file) => {
24+
console.log(file);
25+
assert.equal(file, path.join(normalizedPath, 'default.yaml'));
26+
27+
const fileBuf = fs.readFileSync('config/default.yaml');
28+
const testBuf = fs.readFileSync(file);
29+
assert.equal(fileBuf.toString(), testBuf.toString());
30+
done();
31+
});
32+
});
33+
});
34+
describe('locations module', () => {
35+
it('will build a source path without a configDir', () => {
36+
var configPath = locations.getSourcePath('test', 'foo');
37+
//This path differs from the actual config path because we mutate the singleton in other tests.
38+
assert.equal(configPath, path.join(normalizedPath, 'test-foo-config.yaml'));
39+
});
40+
it('will build a source path with a configDir', () => {
41+
var configPath = locations.getSourcePath('test', 'foo', 'foo');
42+
assert.equal(configPath, 'foo/test-foo-config.yaml');
43+
});
44+
it('will build a cache path without a configDir', () => {
45+
var cachePath = locations.getCachePath('test', 'foo');
46+
//This path differs from the actual config path because we mutate the singleton in other tests.
47+
assert.equal(cachePath, path.join(normalizedPath, 'test-foo-cache-config.yaml'));
48+
});
49+
it('will build a cache path with a configDir', () => {
50+
var cachePath = locations.getCachePath('test', 'foo', 'foo');
51+
assert.equal(cachePath, 'foo/test-foo-cache-config.yaml');
52+
});
53+
it('will put together a properly named source file', () => {
54+
var sourceFile = locations.getSourceFile('test', 'foo');
55+
assert.equal(sourceFile, 'test-foo-config.yaml');
56+
});
57+
});
58+
});

tests/default.yaml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@ edgemicro:
1616
restart_sleep: 500
1717
restart_max: 50
1818
max_times: 300
19+
config_change_poll_interval: 600
1920
logging:
20-
level: none
21+
level: error
2122
dir: /var/tmp
2223
stats_log_interval: 60
2324
rotate_interval: 24
2425
plugins:
2526
sequence:
2627
- oauth
27-
- sample
28-
spikearrest:
29-
timeUnit: minute
30-
allow: 100000
31-
buffersize: 0
3228

3329
headers:
3430
x-forwarded-for: true
@@ -40,4 +36,3 @@ headers:
4036
oauth:
4137
allowNoAuthorization: false
4238
allowInvalidAuthorization: false
43-
sample:

0 commit comments

Comments
 (0)