-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert.js
464 lines (417 loc) · 14.4 KB
/
convert.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const { exec } = require("child_process");
const path = require("path");
const https = require("https");
const fs = require("fs");
const yaml = require("yaml");
// Function to clone the GitHub repository
function cloneRepo(repoUrl, targetFolder) {
return new Promise((resolve, reject) => {
// Construct the full clone command
const command = `git clone ${repoUrl} ${targetFolder}`;
console.log("Cloning the repository...");
// Execute the command
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.log(`Error: ${stderr}`);
// Decide whether stderr should cause a reject or not
}
console.log(`Repository cloned successfully:\n${stdout}`);
resolve(stdout); // Resolve the promise once cloning is done
});
});
}
async function scanFiles(folder) {
console.log("Scanning folder: " + folder);
// list all the folders in the target folder
const folders = fs.readdirSync(folder);
const foldersSub = [];
for (const subfolder of folders) {
const subfolderPath = path.join(folder, subfolder);
const files = fs.readdirSync(subfolderPath);
for (const file of files) {
if (file === "docker-compose.yml") {
foldersSub[subfolder] = subfolderPath;
}
}
}
return foldersSub;
}
function composeConvert(input) {
// change $AppID to {ServiceName}
let output = input.replace(/\$AppID/g, "{ServiceName}");
output = output.replace(/\$PGID/g, "1000");
output = output.replace(/\$PUID/g, "1000");
output = output.replace(/\$TZ/g, "auto");
// change source: /DATA/AppData/ to source: {DefaultDataPath}/
// output = output.replace(/source: \/DATA\/AppData\//g, 'source: \\{DefaultDataPath}/');
// add store
output = `cosmos-installer:\n${output}`;
// parse yml
const doc = yaml.parse(output);
// convert services names to {ServiceName}-name
for (const [name, service] of Object.entries(doc.services)) {
let newName = "{ServiceName}-" + name;
if (Object.entries(doc.services).length === 1) {
newName = "{ServiceName}";
}
service.container_name = newName;
service.hostname = newName;
let volumeIndex = 1;
// convert volumes
service.volumes =
service.volumes &&
service.volumes.map((volume) => {
// if string, replace
if (typeof volume === "string") {
if (volume.startsWith("/DATA/AppData/")) {
const r =
newName + "-data" + (volumeIndex === 1 ? "" : `-${volumeIndex}`);
volumeIndex++;
return r + ":" + volume.split(":")[1];
}
return volume.replace("/DATA/", "{DefaultDataPath}/");
} else if (typeof volume === "object") {
// if object, replace source
if (volume.source.startsWith("/DATA/AppData/")) {
volume.source =
newName + "-data" + (volumeIndex === 1 ? "" : `-${volumeIndex}`);
volumeIndex++;
} else {
volume.source = volume.source.replace(
"/DATA/",
"{DefaultDataPath}/"
);
}
volume.type = volume.source[0] === "/" ? "bind" : "volume";
return volume;
}
});
// convert ports
service.ports =
service.ports &&
service.ports.map((port) => {
// if string, replace
if (typeof port === "object") {
return port.protocol
? `${port.published}:${port.target}/${port.protocol}`
: `${port.published}:${port.target}`;
} else {
return port;
}
});
const varsToPrefix = [
"POSTGRES_HOST",
"MONGO_HOST",
"REDIS_HOST",
"DB_HOST",
];
// Handle environment variable modifications
if (service.environment) {
if (Array.isArray(service.environment)) {
service.environment = service.environment.map((env) => {
const [key, value] = env.split("=");
if (varsToPrefix.includes(key)) {
return `${key}={ServiceName}-` + value;
}
return env;
});
} else if (typeof service.environment === "object") {
Object.keys(service.environment).forEach((key) => {
if (varsToPrefix.includes(key)) {
service.environment[key] =
`{ServiceName}-` + service.environment[key];
}
});
}
}
// Handle environment variable modifications
if (service.environment) {
if (Array.isArray(service.environment)) {
service.environment = service.environment.map((env) => {
if (env.startsWith("MONGO_HOST=")) {
const parts = env.split("=");
return `MONGO_HOST={ServiceName}-` + parts[1];
}
return env;
});
} else if (typeof service.environment === "object") {
Object.keys(service.environment).forEach((key) => {
if (key === "MONGO_HOST") {
service.environment[key] =
`{ServiceName}-` + service.environment[key];
}
});
}
}
// Handle environment variable modifications for DATABASE_URL
if (service.environment) {
if (Array.isArray(service.environment)) {
service.environment = service.environment.map((env) => {
const [key, value] = env.split("=", 2); // Split the string only at the first "="
if (key === "DATABASE_URL") {
// Regex to parse the URL
const urlRegex =
/(postgresql?:\/\/)([^@]+@)?([^:\/]+)(:\d+\/)?([^?]*)(\?.+)?/;
const match = value.match(urlRegex);
if (match) {
const [
fullMatch,
protocol,
userInfo,
host,
port,
database,
queryParams,
] = match;
// Ensure prefixed host does not double-prefix
const prefixedHost = host.startsWith("{ServiceName}-")
? host
: `{ServiceName}-` + host;
// Reconstruct the URL ensuring that undefined groups are handled as empty strings
return `${key}=${protocol}${userInfo || ""}${prefixedHost}${
port || ""
}${database}${queryParams || ""}`;
}
}
return env;
});
} else if (typeof service.environment === "object") {
Object.keys(service.environment).forEach((key) => {
if (key === "DATABASE_URL") {
const value = service.environment[key];
const urlRegex =
/(postgresql?:\/\/)([^@]+@)?([^:\/]+)(:\d+\/)?([^?]*)(\?.+)?/;
const match = value.match(urlRegex);
if (match) {
const [
fullMatch,
protocol,
userInfo,
host,
port,
database,
queryParams,
] = match;
// Ensure prefixed host does not double-prefix
const prefixedHost = host.startsWith("{ServiceName}-")
? host
: `{ServiceName}-` + host;
// Reconstruct the URL ensuring that undefined groups are handled as empty strings
service.environment[key] = `${protocol}${
userInfo || ""
}${prefixedHost}${port || ""}${database}${queryParams || ""}`;
}
}
});
}
}
// Correctly handle depends_on with {ServiceName}- prefix
if (service.depends_on) {
if (
typeof service.depends_on === "object" &&
!Array.isArray(service.depends_on)
) {
service.depends_on = Object.keys(service.depends_on).map(
(key) => `{ServiceName}-` + key
);
} else if (Array.isArray(service.depends_on)) {
service.depends_on = service.depends_on.map(
(dep) => `{ServiceName}-` + dep
);
}
}
delete service["x-casaos"];
doc.services[newName] = service;
delete doc.services[name];
}
doc["minVersion"] = "0.14.0";
delete doc["x-casaos"];
return yaml.stringify(doc);
}
function descriptionConvert(name, input) {
const doc = yaml.parse(input)["x-casaos"];
let output = {
name: name,
description: (doc.tagline || doc.overview || doc.description).en_us,
longDescription: (doc.description || doc.overview || doc.tagline).en_us,
tags: [doc.category],
repository: "https://github.com/bigbeartechworld/big-bear-casaos",
image: doc.icon,
supported_architectures: doc.architectures,
icon: doc.icon,
};
let screenshots = doc.screenshot_link
? doc.screenshot_link.map((screenshot, index) => {
return screenshot.split("/").pop();
})
: [];
return [output, screenshots, doc.icon.split("/").pop()];
}
function downloadImage(url, filePath, maxRedirects = 5) {
return new Promise((resolve, reject) => {
// Function to handle the download process
const download = (url, filePath, remainingRedirects) => {
const file = fs.createWriteStream(filePath);
console.log(`Downloading ${url} to ${filePath}`);
https
.get(url, (response) => {
// Check for redirect
if (
[301, 302, 307, 308].includes(response.statusCode) &&
response.headers.location
) {
console.log(`Redirected to ${response.headers.location}`);
if (remainingRedirects > 0) {
// Close and delete the current file stream before redirecting
file.close(() => {
fs.unlink(filePath, () => {}); // Ignore unlink error
// Follow the redirect
download(
response.headers.location,
filePath,
remainingRedirects - 1
);
});
} else {
reject(new Error("Too many redirects"));
}
} else if (response.statusCode === 200) {
console.log(`Downloaded ${url} to ${filePath}`);
response.pipe(file);
file.on("finish", () => {
console.log(`File downloaded and saved to ${filePath}`);
fs.exists(filePath, (exists) => {
console.log(`File exists: ${exists}`);
if (exists) {
resolve(filePath);
} else {
reject(new Error("File was not created"));
}
});
});
} else {
file.close(() => {
fs.unlink(filePath, () =>
reject(
new Error(`Server responded with ${response.statusCode}`)
)
); // Delete the file async and reject
});
}
})
.on("error", (err) => {
// Close and delete the file stream on request error
file.close(() => {
fs.unlink(filePath, () => reject(err)); // Delete the file async and reject
});
});
};
// Start the download process
download(url, filePath, maxRedirects);
});
}
function extractImages(folder) {
// list all the images (png, jpg, jpeg, ico) that start with screenshot
const images = fs.readdirSync(folder);
const imagesSub = [];
let icon = "";
console.log({ images });
for (const image of images) {
if (
image.endsWith(".png") ||
image.endsWith(".webp") ||
image.endsWith(".jpg") ||
image.endsWith(".jpeg") ||
image.endsWith(".ico")
) {
if (image.startsWith("screenshot")) {
imagesSub.push(image);
} else if (image.startsWith("icon: ")) {
icon = image;
}
}
}
return [imagesSub, icon];
}
// Example usage
const repoUrl = "https://github.com/bigbeartechworld/big-bear-casaos";
const targetFolder = path.join(__dirname, "casaos");
async function run() {
console.log("Starting to clone the repository...");
await cloneRepo(repoUrl, targetFolder);
console.log("Cloned the repository, analysing the files...");
const composeFiles = await scanFiles(path.join(targetFolder, "Apps"));
console.log(
"Exporting " +
Object.keys(composeFiles).length +
" docker-compose.yml files..."
);
// if output folder exist, remove it
if (fs.existsSync(path.join(__dirname, "servapps"))) {
fs.rmSync(path.join(__dirname, "servapps"), { recursive: true });
}
for (const [name, appPath] of Object.entries(composeFiles)) {
try {
const data = composeConvert(
fs.readFileSync(path.join(appPath, "docker-compose.yml"), "utf8")
);
const [description, _, _1] = descriptionConvert(
name,
fs.readFileSync(path.join(appPath, "docker-compose.yml"), "utf8")
);
const [screenshots, icon] = extractImages(appPath);
// create folders
fs.mkdirSync(path.join(__dirname, "servapps", name, "screenshots"), {
recursive: true,
});
fs.writeFileSync(
path.join(__dirname, "servapps", name, "docker-compose.yml"),
data
);
fs.writeFileSync(
path.join(__dirname, "servapps", name, "description.json"),
JSON.stringify(description, null, 2)
);
// Copy config.json file
const configSrcPath = path.join(appPath, "config.json");
const configDestPath = path.join(
__dirname,
"servapps",
name,
"config.json"
);
if (fs.existsSync(configSrcPath)) {
fs.copyFileSync(configSrcPath, configDestPath);
console.log(`Copied config.json for ${name}`);
} else {
console.error(`config.json does not exist for ${name}`);
}
const iconUrl = description.icon; // Assuming this is the extracted icon URL from the YAML
const iconPath = path.join(__dirname, "servapps", name, "icon.png"); // Define the path where you want to save the icon
try {
await downloadImage(iconUrl, iconPath);
console.log(`Downloaded icon for ${name}`);
} catch (error) {
console.error(`Failed to download icon for ${name}:`, error);
}
for (let i = 0; i < screenshots.length; i++) {
fs.copyFileSync(
path.join(appPath, screenshots[i]),
path.join(__dirname, "servapps", name, "screenshots", screenshots[i])
);
}
// fs.copyFileSync(
// path.join(appPath, icon),
// path.join(__dirname, "servapps", name, "icon.png")
// );
} catch (err) {
console.error("error with " + name + ": " + err);
}
}
}
run();