-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.env.js
48 lines (39 loc) · 1.26 KB
/
init.env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const fs = require('fs');
const httpClient = require('./http-client');
const namespace = process.env.DATA_STACK_NAMESPACE || 'appveen';
const appNamespace = process.env.DATA_STACK_APP_NS;
const serviceId = process.env.SERVICE_ID;
const isK8sEnv = function () {
return process.env.KUBERNETES_SERVICE_HOST && process.env.KUBERNETES_SERVICE_PORT;
};
let baseUrlSM = 'http://localhost:10003';
if (isK8sEnv()) baseUrlSM = `http://sm.${namespace}`;
const e = { namespace, appNamespace, serviceId };
e.LOGGER_NAME = isK8sEnv() ? `[${process.env.HOSTNAME}] [${serviceId}]` : `[${serviceId}]`;
e.init = async () => {
try {
let logger = global.logger;
logger.info('INIT :: Fetch env variables');
const options = {
url: `${baseUrlSM}/sm/internal/ds/env`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
json: true
};
let envFromSM = await httpClient.httpRequest(options);
envFromSM = envFromSM.body;
fs.writeFileSync('envVars.json', JSON.stringify(envFromSM));
} catch (err) {
console.log(err);
process.exit(0);
}
};
e.loadEnvVars = () => {
let envFromFile = require('./envVars.json');
Object.keys(envFromFile).forEach(env => {
process.env[env] = process.env[env] ? process.env[env] : envFromFile[env];
});
};
module.exports = e;