Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
936a719682 | |||
56c4b43f3c | |||
5ed11a280f | |||
346809d5be | |||
90add506e3 | |||
adea8d1d69 | |||
1b2eb1d763 | |||
cbc974b3d1 |
@ -38,23 +38,23 @@ snyk:
|
|||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- priv
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
@ -65,7 +65,7 @@ testBuild:
|
|||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
@ -100,7 +100,7 @@ trigger:
|
|||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-dbase:npmci
|
image: hosttoday/ht-docker-dbase:npmci
|
||||||
services:
|
services:
|
||||||
- docker:18-dind
|
- docker:stable-dind
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
@ -38,6 +38,6 @@ const myQenv = new Qenv('path/to/dir/where/qenv.yml/is/', 'path/to/dir/where/env
|
|||||||
For further information read the linked docs at the top of this readme.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
[](https://maintainedby.lossless.com)
|
||||||
|
4958
package-lock.json
generated
4958
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/qenv",
|
"name": "@pushrocks/qenv",
|
||||||
"version": "4.0.3",
|
"version": "4.0.7",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "easy promised environments",
|
"description": "easy promised environments",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -25,17 +25,17 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.24",
|
||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.33",
|
||||||
"@pushrocks/tapbundle": "^3.0.11",
|
"@pushrocks/tapbundle": "^3.2.1",
|
||||||
"@types/node": "^12.6.9",
|
"@types/node": "^14.0.12",
|
||||||
"tslint": "^5.18.0",
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartfile": "^7.0.4",
|
"@pushrocks/smartfile": "^7.0.12",
|
||||||
"@pushrocks/smartlog": "^2.0.19"
|
"@pushrocks/smartlog": "^2.0.26"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/*",
|
"ts/*",
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import * as plugins from './qenv.plugins';
|
import * as plugins from './qenv.plugins';
|
||||||
plugins.smartlog.defaultLogger.enableConsole();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class Qenv
|
* class Qenv
|
||||||
@ -10,7 +9,7 @@ export class Qenv {
|
|||||||
public availableEnvVars: string[] = [];
|
public availableEnvVars: string[] = [];
|
||||||
public missingEnvVars: string[] = [];
|
public missingEnvVars: string[] = [];
|
||||||
public keyValueObject: { [key: string]: any } = {};
|
public keyValueObject: { [key: string]: any } = {};
|
||||||
public logger: plugins.smartlog.Smartlog;
|
public logger = new plugins.smartlog.ConsoleLog();
|
||||||
|
|
||||||
// filePaths
|
// filePaths
|
||||||
public qenvFilePathAbsolute: string;
|
public qenvFilePathAbsolute: string;
|
||||||
@ -19,10 +18,8 @@ export class Qenv {
|
|||||||
constructor(
|
constructor(
|
||||||
qenvFileBasePathArg = process.cwd(),
|
qenvFileBasePathArg = process.cwd(),
|
||||||
envFileBasePathArg,
|
envFileBasePathArg,
|
||||||
failOnMissing = true,
|
failOnMissing = true
|
||||||
loggerArg: plugins.smartlog.Smartlog = plugins.smartlog.defaultLogger
|
|
||||||
) {
|
) {
|
||||||
this.logger = loggerArg;
|
|
||||||
|
|
||||||
// lets make sure paths are absolute
|
// lets make sure paths are absolute
|
||||||
this.qenvFilePathAbsolute = plugins.path.join(
|
this.qenvFilePathAbsolute = plugins.path.join(
|
||||||
@ -46,10 +43,10 @@ export class Qenv {
|
|||||||
console.error('However some Env variables could not be resolved:');
|
console.error('However some Env variables could not be resolved:');
|
||||||
console.log(this.missingEnvVars);
|
console.log(this.missingEnvVars);
|
||||||
if (failOnMissing) {
|
if (failOnMissing) {
|
||||||
console.error('Exiting!');
|
this.logger.log('error', 'Exiting!');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else {
|
} else {
|
||||||
console.log('qenv is not set to fail on missing environment variables');
|
this.logger.log('warn', 'qenv is not set to fail on missing environment variables');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,40 +61,39 @@ export class Qenv {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* tries to get any env var even if it is not required
|
* tries to get any env var even if it is not required
|
||||||
* @param requiredEnvVar
|
* @param wantedEnvVar
|
||||||
*/
|
*/
|
||||||
public getEnvVarOnDemand(requiredEnvVar: string): string {
|
public getEnvVarOnDemand(wantedEnvVar: string): string {
|
||||||
// lets determine the actual env yml
|
let envVarFromEnvironmentVariable: string;
|
||||||
let envYml;
|
let envVarFromEnvJsonFile: string;
|
||||||
try {
|
let envVarFromDockerSecret: string;
|
||||||
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
|
|
||||||
} catch (err) {
|
|
||||||
envYml = {};
|
|
||||||
}
|
|
||||||
let envVar: string;
|
|
||||||
let envFileVar: string;
|
|
||||||
let dockerSecret: string;
|
|
||||||
let dockerSecretJson: string;
|
let dockerSecretJson: string;
|
||||||
|
|
||||||
// env var check
|
// env var check
|
||||||
if (process.env[requiredEnvVar]) {
|
if (process.env[wantedEnvVar]) {
|
||||||
this.availableEnvVars.push(requiredEnvVar);
|
this.availableEnvVars.push(wantedEnvVar);
|
||||||
envVar = process.env[requiredEnvVar];
|
envVarFromEnvironmentVariable = process.env[wantedEnvVar];
|
||||||
}
|
}
|
||||||
|
|
||||||
// env file check
|
// env file check
|
||||||
if (envYml.hasOwnProperty(requiredEnvVar)) {
|
// lets determine the actual env yml
|
||||||
envFileVar = envYml[requiredEnvVar];
|
let envJsonFileAsObject;
|
||||||
this.availableEnvVars.push(requiredEnvVar);
|
try {
|
||||||
|
envJsonFileAsObject = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
|
||||||
|
} catch (err) {
|
||||||
|
envJsonFileAsObject = {};
|
||||||
|
}
|
||||||
|
if (envJsonFileAsObject.hasOwnProperty(wantedEnvVar)) {
|
||||||
|
envVarFromEnvJsonFile = envJsonFileAsObject[wantedEnvVar];
|
||||||
}
|
}
|
||||||
|
|
||||||
// docker secret check
|
// 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.fileExistsSync(`/run/secrets/${requiredEnvVar}`)
|
plugins.smartfile.fs.fileExistsSync(`/run/secrets/${wantedEnvVar}`)
|
||||||
) {
|
) {
|
||||||
dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`);
|
envVarFromDockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${wantedEnvVar}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// docker secret.json
|
// docker secret.json
|
||||||
@ -107,42 +103,53 @@ export class Qenv {
|
|||||||
) {
|
) {
|
||||||
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
|
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
|
||||||
for (const secret of availableSecrets) {
|
for (const secret of availableSecrets) {
|
||||||
if (secret.includes('secret.json') && !dockerSecret) {
|
if (secret.includes('secret.json') && !envVarFromDockerSecret) {
|
||||||
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
|
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
|
||||||
dockerSecret = secretObject[requiredEnvVar];
|
envVarFromDockerSecret = secretObject[wantedEnvVar];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// warn if there is more than one candidate
|
// warn if there is more than one candidate
|
||||||
let candidatesCounter = 0;
|
const availableCcandidates: any[] = [];
|
||||||
[envVar, envFileVar, dockerSecret, dockerSecretJson].forEach(candidate => {
|
[
|
||||||
|
envVarFromEnvironmentVariable,
|
||||||
|
envVarFromEnvJsonFile,
|
||||||
|
envVarFromDockerSecret,
|
||||||
|
dockerSecretJson
|
||||||
|
].forEach(candidate => {
|
||||||
if (candidate) {
|
if (candidate) {
|
||||||
candidatesCounter++;
|
availableCcandidates.push(candidate);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (candidatesCounter > 1) {
|
if (availableCcandidates.length > 1) {
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
'warn',
|
'warn',
|
||||||
`found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
|
`found multiple candidates for ${wantedEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
|
||||||
);
|
);
|
||||||
|
console.log(availableCcandidates);
|
||||||
}
|
}
|
||||||
|
|
||||||
let chosenVar: string = null;
|
switch (true) {
|
||||||
if (envVar) {
|
case !!envVarFromEnvironmentVariable:
|
||||||
this.logger.log('ok', `found ${requiredEnvVar} as environment variable`);
|
this.logger.log('ok', `found ${wantedEnvVar} as environment variable`);
|
||||||
chosenVar = envVar;
|
return envVarFromEnvironmentVariable;
|
||||||
} else if (envFileVar) {
|
case !!envVarFromEnvJsonFile:
|
||||||
this.logger.log('ok', `found ${requiredEnvVar} as env.json variable`);
|
this.logger.log('ok', `found ${wantedEnvVar} as env.json variable`);
|
||||||
chosenVar = envFileVar;
|
return envVarFromEnvJsonFile;
|
||||||
} else if (dockerSecret) {
|
case !!envVarFromDockerSecret:
|
||||||
this.logger.log('ok', `found ${requiredEnvVar} as docker secret`);
|
this.logger.log('ok', `found ${wantedEnvVar} as docker secret`);
|
||||||
chosenVar = dockerSecret;
|
return envVarFromDockerSecret;
|
||||||
} else if (dockerSecretJson) {
|
case !!dockerSecretJson:
|
||||||
this.logger.log('ok', `found ${requiredEnvVar} as docker secret.json`);
|
this.logger.log('ok', `found ${wantedEnvVar} as docker secret.json`);
|
||||||
chosenVar = dockerSecretJson;
|
return dockerSecretJson;
|
||||||
|
default:
|
||||||
|
this.logger.log(
|
||||||
|
'warn',
|
||||||
|
`could not find the wanted environment variable ${wantedEnvVar} anywhere`
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return chosenVar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -154,7 +161,10 @@ export class Qenv {
|
|||||||
qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
|
qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
|
||||||
}
|
}
|
||||||
if (!qenvFile || !qenvFile.required || !Array.isArray(qenvFile.required)) {
|
if (!qenvFile || !qenvFile.required || !Array.isArray(qenvFile.required)) {
|
||||||
this.logger.log('warn', `env File does not contain a 'required' Array!`);
|
this.logger.log(
|
||||||
|
'warn',
|
||||||
|
`qenv (promised environment): ./qenv.yml File does not contain a 'required' Array! This might be ok though.`
|
||||||
|
);
|
||||||
} else {
|
} 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]);
|
||||||
|
Reference in New Issue
Block a user