Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Add ENV variables support for few configuration option - Closes #2225 #2285

Merged
merged 4 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,32 @@ node app.js [options]

Each of that option can be appended on command line. There are also few `ENV` variables that can be utilized for this purpose.

| Option | ENV Variable | Config Option | Description |
| ------------------ | ---------------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| --network<br> -n | LISK_NETWORK | | Which configurations set to use, associated to lisk networks. Any of this option can be used `devnet`, `alphanet`, `betanet`, `testnet` and `mainnet`. Default value is `devnet`. |
| --config<br> -c | LISK_CONFIG_FILE | | Path the custom configuration file, which will override values of `config/default/config.json` |
| --port<br> -p | LISK_WS_PORT | wsPort | TCP port for P2P layer |
| --http-port<br> -h | LISK_HTTP_PORT | httpPort | TCP port for HTTP API |
| --address<br> -a | | address | Listening host name or ip |
| --database<br> -d | | db.database | PostgreSQL database name to connect |
| --peers<br> -p | LISK_PEERS | peers.list | Comma separated list of peers to connect in the format `192.168.99.100:5000,172.169.99.77:5000` |
| --log<br> -l | LISK_CONSOLE_LOG_LEVEL | consoleLogLevel | Log level for lisk for console output |
| --snapshot<br> -s | | | Number of round for which take the snapshot. If none specified it will use the highest round available. |
| Option | ENV Variable | Config Option | Description |
| ------------------------------------ | ---------------------- | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <pre nowrap>--network<br>-n</pre> | LISK_NETWORK | | Which configurations set to use, associated to lisk networks. Any of this option can be used `devnet`, `alphanet`, `betanet`, `testnet` and `mainnet`. Default value is `devnet`. |
| <pre nowrap>--config<br> -c</pre> | LISK_CONFIG_FILE | | Path the custom configuration file, which will override values of `config/default/config.json` |
| <pre nowrap>--port<br> -p</pre> | LISK_WS_PORT | wsPort | TCP port for P2P layer |
| <pre nowrap>--http-port<br> -h</pre> | LISK_HTTP_PORT | httpPort | TCP port for HTTP API |
| <pre nowrap>--address<br> -a</pre> | LISK_ADDRESS | address | Listening host name or ip |
| <pre nowrap>--log<br> -l</pre> | LISK_FILE_LOG_LEVEL | fileLogLevel | Log level for file output |
| | LISK_CONSOLE_LOG_LEVEL | consoleLogLevel | Log level for console output |
| | LISK_CACHE_ENABLED | cacheEnabled | Enable or disable cache. Must be set to true/false |
| <pre nowrap>--database<br> -d</pre> | LISK_DB_NAME | db.database | PostgreSQL database name to connect |
| | LISK_DB_HOST | db.host | PostgreSQL database host name |
| | LISK_DB_PORT | db.port | PostgreSQL database port |
| | LISK_DB_USER | db.user | PostgreSQL database username to connect |
| | LISK_DB_PASSWORD | db.password | PostgreSQL database password to connect |
| <pre nowrap>--peers<br> -p</pre> | LISK_PEERS | peers.list | Comma separated list of peers to connect in the format `192.168.99.100:5000,172.169.99.77:5000` |
| | LISK_API_PUBLIC | api.access.public | Enable or disable public access of http API. Must be set to true/false |
| | LISK_API_WHITELIST | api.access.whiteList | Comma separated list of IPs to enable API access. Format `192.168.99.100,172.169.99.77` |
| | LISK_FORGING_DELEGATES | forging.delegates | Comma separated list of delegates to load in the format _publicKey&#x7c;encryptedPassphrase,publicKey2&#x7c;encryptedPassphrase2_ |
| | LISK_FORGING_WHITELIST | forging.access.whiteList | Comma separated list of IPs to enable access to forging endpoints. Format `192.168.99.100,172.169.99.77` |
| <pre nowrap>--snapshot<br> -s</pre> | | | Number of round for which take the snapshot. If none specified it will use the highest round available. |

#### Note

* All `ENV` variables restricted with operating system constraint of `ENV` variable maximum length.
* Comma separated lists will replace the original config values. e.g. If you specify `LISK_PEERS`, original `peers.list` specific to network will be replaced completely.

For more detail understanding of configuration read this [online documentation](https://lisk.io/documentation/lisk-core/user-guide/configuration)

Expand Down
3 changes: 0 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ if (typeof gc !== 'undefined') {
*/
var appConfig = AppConfig(require('./package.json'));

// Define lisk network env variable to be used by child processes load config files
process.env.LISK_NETWORK = appConfig.network;

// Global objects to be utilized under modules/helpers where scope is not accessible
global.constants = appConfig.constants;
global.exceptions = appConfig.exceptions;
Expand Down
81 changes: 70 additions & 11 deletions helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function Config(packageJson, parseCommandLineOptions = true) {
program.parse(process.argv);
}

const network = program.network || process.env.LISK_NETWORK || 'devnet';
const network = program.network || getenv(process.env.LISK_NETWORK, 'devnet');
// Define lisk network env variable to be used by child processes load config files
process.env.LISK_NETWORK = network;

const genesisBlock = loadJSONFile(`./config/${network}/genesis_block.json`);

Expand All @@ -73,6 +75,7 @@ function Config(packageJson, parseCommandLineOptions = true) {

const defaultConfig = loadJSONFile('config/default/config.json');
const networkConfig = loadJSONFile(`config/${network}/config.json`);

let customConfig = {};
if (program.config || process.env.LISK_CONFIG_FILE) {
customConfig = loadJSONFile(program.config || process.env.LISK_CONFIG_FILE);
Expand All @@ -88,31 +91,59 @@ function Config(packageJson, parseCommandLineOptions = true) {
};

let commandLineConfig = {
wsPort: +program.port || process.env.LISK_WS_PORT || null,
httpPort: +program.httpPort || process.env.LISK_HTTP_PORT || null,
address: program.address,
consoleLogLevel: program.log || process.env.LISK_CONSOLE_LOG_LEVEL,
db: { database: program.database },
wsPort: +program.port || parseInt(getenv(process.env.LISK_WS_PORT)) || null,
httpPort:
+program.httpPort || parseInt(getenv(process.env.LISK_HTTP_PORT)) || null,
address: program.address || getenv(process.env.LISK_ADDRESS),
fileLogLevel: program.log || getenv(process.env.LISK_FILE_LOG_LEVEL),
consoleLogLevel: getenv(process.env.LISK_CONSOLE_LOG_LEVEL),
cacheEnabled: getenv(process.env.LISK_CACHE_ENABLED, null, true),
db: {
database: program.database || getenv(process.env.LISK_DB_NAME),
host: getenv(process.env.LISK_DB_HOST),
port: parseInt(getenv(process.env.LISK_DB_PORT)) || null,
user: getenv(process.env.LISK_DB_USER),
password: getenv(process.env.LISK_DB_PASSWORD),
},
api: {
access: {
public: getenv(process.env.LISK_API_PUBLIC, null, true),
whiteList: extractWhiteListIPs(process.env.LISK_API_WHITELIST),
},
},
forging: {
delegates: extractDelegatesList(process.env.LISK_FORGING_DELEGATES),
access: {
whiteList: extractWhiteListIPs(process.env.LISK_FORGING_WHITELIST),
},
},
loading: { snapshotRound: program.snapshot },
peers: {
list: extractPeersList(
program.peers || process.env.LISK_PEERS,
program.peers || getenv(process.env.LISK_PEERS),
+program.port ||
process.env.LISK_WS_PORT ||
parseInt(getenv(process.env.LISK_WS_PORT)) ||
customConfig.wsPort ||
networkConfig.wsPort ||
defaultConfig.wsPort
),
},
coverage: process.env.NODE_ENV === 'test',
coverage: getenv(process.env.NODE_ENV) === 'test',
};
commandLineConfig = cleanDeep(commandLineConfig);

const appConfig = _.merge(
const appConfig = _.mergeWith(
{},
defaultConfig,
networkConfig,
customConfig,
commandLineConfig,
runtimeConfig
runtimeConfig,
(objValue, srcValue) => {
if (_.isArray(objValue)) {
return srcValue;
}
}
);

var validator = new z_schema();
Expand All @@ -134,6 +165,14 @@ function Config(packageJson, parseCommandLineOptions = true) {
}
}

const getenv = (variable, defaultValue = null, isBoolean = false) => {
if (isBoolean) {
return variable ? variable === 'true' : defaultValue;
}

return variable || defaultValue;
};

function loadJSONFile(filePath) {
try {
filePath = path.resolve(rootPath, filePath);
Expand All @@ -158,6 +197,26 @@ function extractPeersList(peers, defaultPort) {
return [];
}

function extractWhiteListIPs(ips) {
if (typeof ips === 'string') {
return ips.split(',');
}
return [];
}

function extractDelegatesList(delegates) {
if (typeof delegates === 'string') {
return delegates.split(',').map(delegate => {
delegate = delegate.split('|');
return {
publicKey: delegate[0],
encryptedPassphrase: delegate[1],
};
});
}
return [];
}

function cleanDeep(
object,
{
Expand Down