-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecosystem.js
215 lines (175 loc) · 6.78 KB
/
ecosystem.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
function copyKeys(srcPath, destPath) {
const fileName = path.basename(srcPath);
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath, { recursive: true });
}
fs.copyFileSync(srcPath, path.join(destPath, fileName));
}
function findFilesWithExtension(dir, extension) {
const files = [];
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
if (fs.statSync(fullPath).isDirectory()) {
files.push(...findFilesWithExtension(fullPath, extension));
} else if (item.endsWith(extension)) {
files.push(fullPath);
}
}
return files;
}
function replaceTokenInTemplate(templateFile, token) {
const templateContent = fs.readFileSync(templateFile, 'utf8');
return templateContent.replace(/\${GITHUB_AUTH_TOKEN}/g, token);
}
let args = process.argv.slice(2);
let action = args[0]; // up or down
args = args.slice(1); // get the rest of the arguments aside from action argument
let useOpenIdUrl = false;
let daemonMode = false;
let forceUpdateConfigs = false;
let useComposeTemplate = false;
let useLocalAusweisApp = false;
const walletClientOrigin = "http://localhost:3000";
const walletClientUrl = `${walletClientOrigin}/cb`;
function help() {
console.log("Usage: node ecosystem.js <up | down> <OPTIONS>");
console.log("OPTIONS:");
console.log(" -m Redirection will be directed to the openid:// URL. Note: It will be applied only in the first execution of the script and every time '-c' is given");
console.log(" -d Start the ecosystem in daemonized mode");
console.log(" -c Force update of the configurations to the defaults for the development environment");
console.log(" -t Force the usage of the docker-compose.template.yml");
console.log(" --local-ausweis Use local ausweis docker container");
console.log("");
console.log("Example:");
console.log("node ecosystem.js up -m -c");
console.log("");
console.log("");
}
for (const arg of args) {
if (arg === '-t') {
useComposeTemplate = true;
console.log("Refreshed docker-compose.yml using the template docker-compose.template.yml");
}
if (arg === '-m') {
useOpenIdUrl = true;
console.log("Wallet URL is now openid:// in all configurations");
}
if (arg === '-d') {
daemonMode = true;
}
if (arg === '-c') {
forceUpdateConfigs = true;
console.log("Forcing update in configs");
}
if (arg === '--local-ausweis') {
useLocalAusweisApp = true;
console.log("Using local ausweis client docker container");
}
if (arg === '--help') {
help();
process.exit();
}
}
const secret = "dsfkwfkwfwdfdsfSaSe2e34r4frwr42rAFdsf2lfmfsmklfwmer";
const githubTokenFile = '.github-token';
let githubToken;
try {
githubToken = fs.readFileSync(githubTokenFile, 'utf8').trim();
fs.chmodSync(githubTokenFile, 0o600);
} catch (error) {
console.error(`Error: ${error.message}`);
console.error(`Write GitHub token to '${githubTokenFile}' before running this script.`);
process.exit(1);
}
if (fs.existsSync(githubTokenFile)) {
const npmrcTemplateFiles = findFilesWithExtension('.', '.npmrc.template');
for (const npmrcTemplateFile of npmrcTemplateFiles) {
const npmrcFile = npmrcTemplateFile.replace('.template', '');
if (!fs.existsSync(npmrcFile) || forceUpdateConfigs) {
fs.writeFileSync(npmrcFile, '', { mode: 0o600 });
fs.writeFileSync(npmrcFile, replaceTokenInTemplate(npmrcTemplateFile, githubToken));
}
}
} else {
console.error(`Error: No such file: ${githubTokenFile}`);
console.error(`Write GitHub token to '${githubTokenFile}' before running this script.`);
process.exit(1);
}
// MariaDB configuration
const dbHost = 'wallet-db';
const dbPort = 3307;
const dbUser = 'root';
const dbPassword = 'root';
if (!fs.existsSync(`${process.cwd()}/docker-compose.yml`) || useComposeTemplate) {
fs.copyFileSync('docker-compose.template.yml', 'docker-compose.yml');
}
if (useOpenIdUrl) {
walletClientUrl = "openid://cb";
}
let dockerComposeCommand = 'docker-compose';
try {
execSync('docker compose version').toString();
dockerComposeCommand = 'docker compose'
} catch (error) {
// Fall back to default value
}
if (action === "down") {
console.log("Performing 'docker compose down'");
// Implement the logic to stop Docker services here
execSync(`${dockerComposeCommand} ${useLocalAusweisApp ? '--profile ausweis' : ""} down`, { stdio: 'inherit' });
process.exit();
}
if (action === "init") {
console.log("Performing init");
init()
process.exit();
}
if (action !== 'up') {
console.log("Error: First argument must be 'up' or 'down' or 'init'");
help();
process.exit();
}
{ // wallet backend server configuration
const configPath = 'funke-wallet-backend-server/config/config.development.ts';
const templatePath = 'funke-wallet-backend-server/config/config.template.ts';
if (fs.existsSync(configPath) && forceUpdateConfigs === false) {
console.log("funke-wallet-backend-server/config/config.development.ts was not changed");
}
else {
fs.copyFileSync(templatePath, configPath);
const servicePort = 8002;
const serviceUrl = `http://funke-wallet-backend-server:${servicePort}`;
const dbName = 'wallet';
let configContent = fs.readFileSync(configPath, 'utf-8');
configContent = configContent.replace(/SERVICE_URL/g, serviceUrl);
configContent = configContent.replace(/SERVICE_SECRET/g, secret);
configContent = configContent.replace(/SERVICE_PORT/g, servicePort);
configContent = configContent.replace(/DB_HOST/g, dbHost);
configContent = configContent.replace(/DB_PORT/g, dbPort);
configContent = configContent.replace(/DB_USER/g, dbUser);
configContent = configContent.replace(/DB_PASSWORD/g, dbPassword);
configContent = configContent.replace(/DB_NAME/g, dbName);
configContent = configContent.replace(/WALLET_CLIENT_URL/g, walletClientUrl);
configContent = configContent.replace(/WEBAUTHN_RP_ID/g, "localhost");
configContent = configContent.replace(/WEBAUTHN_ORIGIN/g, walletClientOrigin);
// Replacing the whole string so that the value of enabled is of type boolean and not string
if (args.includes('--no-notifications')) {
configContent = configContent.replace(/enabled: "NOTIFICATIONS_ENABLED"/g, 'enabled: false');
} else {
configContent = configContent.replace(/enabled: "NOTIFICATIONS_ENABLED"/g, 'enabled: true');
}
fs.writeFileSync(configPath, configContent);
}
}
if (daemonMode === false) {
console.log("Performing 'docker compose up'");
execSync(`${dockerComposeCommand} ${useLocalAusweisApp ? '--profile ausweis' : ""} up --build`, { stdio: 'inherit' });
} else {
console.log("Performing 'docker compose up -d'");
execSync(`${dockerComposeCommand} ${useLocalAusweisApp ? '--profile ausweis' : ""} up --build -d`, { stdio: 'inherit' });
}