forked from project-sunbird/ml-projects-service
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvVariables.js
199 lines (182 loc) · 6.58 KB
/
envVariables.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* name : envVariables.js.
* author : Aman Karki.
* created-date : 19-June-2020.
* Description : Required Environment variables .
*/
const Log = require("log");
let log = new Log("debug");
let table = require("cli-table");
const certificateService = require(GENERICS_FILES_PATH + "/services/certificate");
let tableData = new table();
let enviromentVariables = {
"APPLICATION_PORT" : {
"message" : "Please specify the value for e.g. 4201",
"optional" : false
},
"APPLICATION_ENV" : {
"message" : "Please specify the value for e.g. local/development/qa/production",
"optional" : false
},
"MONGODB_URL" : {
"message" : "Required mongodb url",
"optional" : false
},
"INTERNAL_ACCESS_TOKEN" : {
"message" : "Required internal access token",
"optional" : false
},
"KAFKA_COMMUNICATIONS_ON_OFF" : {
"message" : "Enable/Disable kafka communications",
"optional" : false
},
"KAFKA_URL" : {
"message" : "Required",
"optional" : false
},
"USER_SERVICE_URL" : {
"message" : "Required user service base url",
"optional" : false
},
"SERVICE_NAME" : {
"message" : "current service name",
"optional" : true,
"default" : "ml-projects-service"
},
"CERTIFICATE_SERVICE_URL" : {
"message" : "certificate service base url",
"optional" : true,
"default" : "http://registry-service:8081",
"requiredIf" : {
"key": "PROJECT_CERTIFICATE_ON_OFF",
"operator" : "EQUALS",
"value" : "ON"
}
},
"PROJECT_CERTIFICATE_ON_OFF" : {
"message" : "Enable/Disable project certification",
"optional" : false,
"default" : "ON"
},
"USER_DELETE_ON_OFF": {
message: "Enable/Disable user delete flow",
optional: false,
default : "ON"
},
"USER_DELETE_TOPIC": {
message: "Required user delete kafka consumer topic name",
optional: true,
requiredIf : {
key: "USER_DELETE_ON_OFF",
operator: "EQUALS",
value: "ON"
}
},
"ID": {
message: "Required Service ID",
optional: false,
},
"TELEMETRY_ON_OFF":{
message: "Required telemetry on/off status",
optional: false,
default: "ON"
},
"TELEMETRY_TOPIC": {
message: "Required telemetry topic",
optional: true,
requiredIf : {
key: "TELEMETRY_ON_OFF",
operator: "EQUALS",
value: "ON"
}
},
}
let success = true;
module.exports = function() {
Object.keys(enviromentVariables).forEach(eachEnvironmentVariable=>{
let tableObj = {
[eachEnvironmentVariable] : "PASSED"
};
let keyCheckPass = true;
let validRequiredIfOperators = ["EQUALS","NOT_EQUALS"]
if(enviromentVariables[eachEnvironmentVariable].optional === true
&& enviromentVariables[eachEnvironmentVariable].requiredIf
&& enviromentVariables[eachEnvironmentVariable].requiredIf.key
&& enviromentVariables[eachEnvironmentVariable].requiredIf.key != ""
&& enviromentVariables[eachEnvironmentVariable].requiredIf.operator
&& validRequiredIfOperators.includes(enviromentVariables[eachEnvironmentVariable].requiredIf.operator)
&& enviromentVariables[eachEnvironmentVariable].requiredIf.value
&& enviromentVariables[eachEnvironmentVariable].requiredIf.value != "") {
switch (enviromentVariables[eachEnvironmentVariable].requiredIf.operator) {
case "EQUALS":
if(process.env[enviromentVariables[eachEnvironmentVariable].requiredIf.key] === enviromentVariables[eachEnvironmentVariable].requiredIf.value) {
enviromentVariables[eachEnvironmentVariable].optional = false;
}
break;
case "NOT_EQUALS":
if(process.env[enviromentVariables[eachEnvironmentVariable].requiredIf.key] != enviromentVariables[eachEnvironmentVariable].requiredIf.value) {
enviromentVariables[eachEnvironmentVariable].optional = false;
}
break;
default:
break;
}
}
if(enviromentVariables[eachEnvironmentVariable].optional === false) {
if(!(process.env[eachEnvironmentVariable])
|| process.env[eachEnvironmentVariable] == "") {
success = false;
keyCheckPass = false;
} else if (enviromentVariables[eachEnvironmentVariable].possibleValues
&& Array.isArray(enviromentVariables[eachEnvironmentVariable].possibleValues)
&& enviromentVariables[eachEnvironmentVariable].possibleValues.length > 0) {
if(!enviromentVariables[eachEnvironmentVariable].possibleValues.includes(process.env[eachEnvironmentVariable])) {
success = false;
keyCheckPass = false;
enviromentVariables[eachEnvironmentVariable].message += ` Valid values - ${enviromentVariables[eachEnvironmentVariable].possibleValues.join(", ")}`
}
}
}
if((!(process.env[eachEnvironmentVariable])
|| process.env[eachEnvironmentVariable] == "")
&& enviromentVariables[eachEnvironmentVariable].default
&& enviromentVariables[eachEnvironmentVariable].default != "") {
process.env[eachEnvironmentVariable] = enviromentVariables[eachEnvironmentVariable].default;
success = true;
keyCheckPass = true;
}
if(!keyCheckPass) {
if(enviromentVariables[eachEnvironmentVariable].message !== "") {
tableObj[eachEnvironmentVariable] =
enviromentVariables[eachEnvironmentVariable].message;
} else {
tableObj[eachEnvironmentVariable] = `FAILED - ${eachEnvironmentVariable} is required`;
}
}
tableData.push(tableObj);
})
log.info(tableData.toString());
getKid();
return {
success : success
}
}
async function getKid(){
if ( process.env.PROJECT_CERTIFICATE_ON_OFF === "ON" ) {
// get certificate issuer kid from sunbird-RC
let kidData = await certificateService.getCertificateIssuerKid();
if( !kidData.success ) {
console.log("failed to get kid value from registry service : ",kidData)
if( process.env.CERTIFICATE_ISSUER_KID && process.env.CERTIFICATE_ISSUER_KID != "" ) {
global.CERTIFICATE_ISSUER_KID = process.env.CERTIFICATE_ISSUER_KID;
}
// console.log("Server stoped . Failed to set certificate issuer Kid value")
// process.exit();
} else {
console.log("Kid data fetched successfully : ",kidData.data)
global.CERTIFICATE_ISSUER_KID = kidData.data
}
console.log(JSON.stringify(kidData))
// global.CERTIFICATE_ISSUER_KID = kidData.data
}
};