Compare commits

..

12 Commits

Author SHA1 Message Date
31bf1b27a4 3.0.7 2019-01-13 22:33:19 +01:00
a77def5844 fix(core): update 2019-01-13 22:33:18 +01:00
2357699f3e 3.0.6 2019-01-13 22:04:20 +01:00
01f5784488 fix(core): update 2019-01-13 22:04:20 +01:00
80f35c39aa 3.0.5 2019-01-13 21:35:57 +01:00
87f55773bd fix(core): update 2019-01-13 21:35:57 +01:00
1a25341232 3.0.4 2019-01-13 18:25:09 +01:00
ccd41d86bf fix(core): update 2019-01-13 18:25:09 +01:00
0a3a7e480e 3.0.3 2019-01-13 00:00:32 +01:00
33a91c6ae7 fix(core): update 2019-01-13 00:00:32 +01:00
0cfbdf2c9e 3.0.2 2019-01-06 03:36:40 +01:00
f7362e5444 fix(core): update 2019-01-06 03:36:40 +01:00
4 changed files with 96 additions and 47 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/qenv", "name": "@pushrocks/qenv",
"version": "3.0.1", "version": "3.0.7",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/qenv", "name": "@pushrocks/qenv",
"version": "3.0.1", "version": "3.0.7",
"private": false, "private": false,
"description": "easy promised environments", "description": "easy promised environments",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -17,10 +17,12 @@ tap.test('should create a new class', async () => {
tap.test('key1 should be not be overwritten since it is already present', async () => { tap.test('key1 should be not be overwritten since it is already present', async () => {
expect(process.env.key1).to.equal('original'); expect(process.env.key1).to.equal('original');
expect(qenvTestObject.getEnvVarOnDemand('key1')).to.equal('original');
}); });
tap.test('key2 should be read from Yml', async () => { tap.test('key2 should be read from Yml', async () => {
expect(process.env.key2).to.equal('fromYml'); expect(process.env.key2).to.equal('fromYml');
expect(qenvTestObject.getEnvVarOnDemand('key2')).to.equal('fromYml');
}); });
tap.test('keyValueObjectArray should hold all retrieved values', async () => { tap.test('keyValueObjectArray should hold all retrieved values', async () => {

View File

@ -1,10 +1,15 @@
import * as plugins from './qenv.plugins'; import * as plugins from './qenv.plugins';
plugins.smartlog.defaultLogger.enableConsole();
export interface IKeyValueObject { export interface IKeyValueObject {
key: string; key: string;
value: string; value: string;
} }
/**
* class Qenv
* allows to make assertions about the environments while being more flexibel in how to meet them
*/
export class Qenv { export class Qenv {
public requiredEnvVars: string[] = []; public requiredEnvVars: string[] = [];
public availableEnvVars: string[] = []; public availableEnvVars: string[] = [];
@ -25,8 +30,14 @@ export class Qenv {
this.logger = loggerArg; this.logger = loggerArg;
// lets make sure paths are absolute // lets make sure paths are absolute
this.qenvFilePathAbsolute = plugins.path.join(plugins.path.resolve(qenvFileBasePathArg), 'qenv.yml'); this.qenvFilePathAbsolute = plugins.path.join(
this.envFilePathAbsolute = plugins.path.join(plugins.path.resolve(envFileBasePathArg), 'env.yml'); plugins.path.resolve(qenvFileBasePathArg),
'qenv.yml'
);
this.envFilePathAbsolute = plugins.path.join(
plugins.path.resolve(envFileBasePathArg),
'env.yml'
);
this.getRequiredEnvVars(); this.getRequiredEnvVars();
this.getAvailableEnvVars(); this.getAvailableEnvVars();
@ -46,22 +57,30 @@ export class Qenv {
} }
} }
public getEnvVar(envVarName): string { /**
* only gets an environment variable if it is required within a read qenv.yml file
* @param envVarName
*/
public getEnvVarRequired(envVarName): string {
return process.env[envVarName]; return process.env[envVarName];
} }
/**
* tries to get any env var even if it is not required
* @param requiredEnvVar
*/
public getEnvVarOnDemand(requiredEnvVar: string): string { public getEnvVarOnDemand(requiredEnvVar: string): string {
// lets determine the actual env yml // lets determine the actual env yml
let envYml; let envYml;
try { try {
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute); envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
} catch (err) { } catch (err) {
console.log("env file couldn't be found at " + this.envFilePathAbsolute);
envYml = {}; envYml = {};
} }
let envVar: string; let envVar: string;
let envFileVar: string; let envFileVar: string;
let dockerSecret: string; let dockerSecret: string;
let dockerSecretJson: string;
// env var check // env var check
if (process.env[requiredEnvVar]) { if (process.env[requiredEnvVar]) {
@ -74,28 +93,53 @@ export class Qenv {
envFileVar = envYml[requiredEnvVar]; envFileVar = envYml[requiredEnvVar];
this.availableEnvVars.push(requiredEnvVar); this.availableEnvVars.push(requiredEnvVar);
} }
// docker secret check
if ( if (
plugins.smartfile.fs.isDirectory('/run') && plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets') && plugins.smartfile.fs.isDirectory('/run/secrets') &&
plugins.smartfile.fs.fileExists(`/run/secrets/${requiredEnvVar}`) plugins.smartfile.fs.fileExistsSync(`/run/secrets/${requiredEnvVar}`)
) { ) {
dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`); dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`);
} }
if ((envVar && envFileVar) || (envVar && dockerSecret) || (envFileVar && dockerSecret)) { // docker secret.json
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets') &&
plugins.smartfile.fs.fileExistsSync(`/run/secrets/secret.json`)
) {
const secretObject = plugins.smartfile.fs.toObjectSync('/run/secrets/secret.json');
dockerSecret = secretObject[requiredEnvVar];
}
// warn if there is more than one candidate
let candidatesCounter = 0;
[envVar, envFileVar, dockerSecret, dockerSecretJson].forEach(candidate => {
if (candidate) {
candidatesCounter++;
}
});
if (candidatesCounter > 1) {
this.logger.log( this.logger.log(
'warn', 'warn',
`found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret` `found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
); );
} }
let chosenVar: string = null; let chosenVar: string = null;
if (envVar) { if (envVar) {
this.logger.log('ok', `found ${requiredEnvVar} as environment variable`);
chosenVar = envVar; chosenVar = envVar;
} else if (envFileVar) { } else if (envFileVar) {
this.logger.log('ok', `found ${requiredEnvVar} as env.yml variable`);
chosenVar = envFileVar; chosenVar = envFileVar;
} else if (dockerSecret) { } else if (dockerSecret) {
this.logger.log('ok', `found ${requiredEnvVar} as docker secret`);
chosenVar = dockerSecret; chosenVar = dockerSecret;
} else if (dockerSecretJson) {
this.logger.log('ok', `found ${requiredEnvVar} as docker secret.json`);
chosenVar = dockerSecretJson;
} }
return chosenVar; return chosenVar;
} }
@ -104,20 +148,23 @@ export class Qenv {
* gets the required env values * gets the required env values
*/ */
private getRequiredEnvVars = () => { private getRequiredEnvVars = () => {
const qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute); let qenvFile: any = {};
if (!qenvFile.required) { if (plugins.smartfile.fs.fileExistsSync(this.qenvFilePathAbsolute)) {
this.logger.log('warn', `env File does not contain a 'required' Array!`); qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
} }
if (!qenvFile || !qenvFile.required || !Array.isArray(qenvFile.required)) {
this.logger.log('warn', `env File does not contain a 'required' Array!`);
} else {
for (const keyArg of Object.keys(qenvFile.required)) { for (const keyArg of Object.keys(qenvFile.required)) {
this.requiredEnvVars.push(qenvFile.required[keyArg]); this.requiredEnvVars.push(qenvFile.required[keyArg]);
} }
} }
};
/** /**
* gets the available env vars * gets the available env vars
*/ */
private getAvailableEnvVars = () => { private getAvailableEnvVars = () => {
for (const requiredEnvVar of this.requiredEnvVars) { for (const requiredEnvVar of this.requiredEnvVars) {
const chosenVar = this.getEnvVarOnDemand(requiredEnvVar); const chosenVar = this.getEnvVarOnDemand(requiredEnvVar);
if (chosenVar) { if (chosenVar) {
@ -129,7 +176,7 @@ export class Qenv {
}); });
} }
} }
} };
/** /**
* gets missing env vars * gets missing env vars