cispacempt-v2 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "main": "src/index.js",
3
3
  "name": "cispacempt-v2",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "description": "Fast CI engine for CodespaceMPT with script stages and file system hooks",
6
6
  "author": "argentidev",
7
7
  "license": "MIT",
Binary file
package/src/index.js CHANGED
@@ -2,8 +2,92 @@ const path = require("path"),
2
2
  os = require("os"),
3
3
  fs = require("fs"),
4
4
  { spawn } = require("child_process"),
5
- { prepareProotEnv } = require("./lib/prootEnv"),
6
5
  yaml = require("js-yaml"),
6
+ https = require("https"),
7
+ { spawnSync } = require("child_process"),
8
+ rootfsMap = {
9
+ debian: "https://github.com/EXALAB/Anlinux-Resources/blob/master/Rootfs/Debian/arm64/debian-rootfs-arm64.tar.xz",
10
+ alpine: "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-3.19.1-x86_64.tar.gz",
11
+ ubuntu: "https://partner-images.canonical.com/core/focal/current/ubuntu-focal-core-cloudimg-amd64-root.tar.gz",
12
+ arch: "https://mirror.rackspace.com/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.gz",
13
+ void: "https://repo-default.voidlinux.org/live/current/void-x86_64-ROOTFS-20230628.tar.xz",
14
+ fedora: "https://download.fedoraproject.org/pub/fedora/linux/releases/39/Container/x86_64/images/Fedora-Container-Base-39-1.5.x86_64.tar.xz",
15
+ centos: "https://buildlogs.centos.org/centos/7/container/x86_64/images/CentOS-7-Container-7.9.2009-20210916.0.x86_64.tar.xz"
16
+ };
17
+
18
+ const downloadFile = (url, dest) => new Promise((resolve, reject) => {
19
+ console.log(`Starting download from: ${url}`);
20
+ const file = fs.createWriteStream(dest);
21
+
22
+ const request = https.get(url, (response) => {
23
+ if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
24
+ console.log(`Redirected to: ${response.headers.location}`);
25
+ // Si hay redirección, cerrar este stream y descargar la nueva url
26
+ file.close();
27
+ fs.unlinkSync(dest);
28
+ return downloadFile(response.headers.location, dest).then(resolve).catch(reject);
29
+ }
30
+
31
+ if (response.statusCode !== 200) {
32
+ file.close();
33
+ fs.unlinkSync(dest);
34
+ return reject(new Error(`Failed to download file, status code: ${response.statusCode}`));
35
+ }
36
+
37
+ response.pipe(file);
38
+
39
+ file.on("finish", () => {
40
+ console.log(`Download finished: ${dest}`);
41
+ file.close(resolve);
42
+ });
43
+ });
44
+
45
+ request.on("error", (err) => {
46
+ console.error(`Download error: ${err.message}`);
47
+ file.close();
48
+ fs.unlinkSync(dest);
49
+ reject(err);
50
+ });
51
+
52
+ file.on("error", (err) => {
53
+ console.error(`File write error: ${err.message}`);
54
+ file.close();
55
+ fs.unlinkSync(dest);
56
+ reject(err);
57
+ });
58
+ }),
59
+ prepareProotEnv = async (osName, cacheDir) => {
60
+ const prootBin = path.join(cacheDir, "proot");
61
+ const rootfsDir = path.join(cacheDir, `rootfs_${osName}`);
62
+ const tarball = path.join(cacheDir, `${osName}.tar.${osName.includes("xz") ? "xz" : "gz"}`);
63
+
64
+ // Crear el directorio cacheDir si no existe
65
+ fs.mkdirSync(cacheDir, { recursive: true });
66
+
67
+ // Descargar proot
68
+ if (!fs.existsSync(prootBin)) {
69
+ const url = "https://proot.gitlab.io/proot/bin/proot";
70
+ await downloadFile(url, prootBin);
71
+ fs.chmodSync(prootBin, 0o755);
72
+ }
73
+
74
+ // Descargar rootfs
75
+ if (!fs.existsSync(rootfsDir)) {
76
+ const url = rootfsMap[osName];
77
+ if (!url) throw new Error(`Unsupported OS "${osName}"`);
78
+ await downloadFile(url, tarball);
79
+
80
+ fs.mkdirSync(rootfsDir, { recursive: true });
81
+
82
+ // Extraer el tarball
83
+ const extCmd = tarball.endsWith(".xz") ? "tar -xJf" : "tar -xzf";
84
+ spawnSync(extCmd, [tarball, "-C", rootfsDir], { shell: true });
85
+
86
+ fs.unlinkSync(tarball); // limpiar
87
+ }
88
+
89
+ return { prootBin, rootfsDir };
90
+ },
7
91
  getCPUUsageSnapshot = function () {
8
92
  const cpus = os.cpus();
9
93
  return cpus.map(cpu => ({ ...cpu.times }));
@@ -214,8 +298,9 @@ module.exports = {
214
298
  await new Promise(async (res) => {
215
299
  const commandToRun = resolvedCommand;
216
300
  let finalCommand;
217
- if (ciScript.os && Object.keys(require("./lib/prootEnv").prepareProotEnv.rootfsMap || {}).includes(ciScript.os)) {
301
+ if (ciScript.os && Object.keys(rootfsMap || {}).includes(ciScript.os)) {
218
302
  const { prootBin, rootfsDir } = await prepareProotEnv(ciScript.os, path.join(__dirname, "ci_proot_cache"));
303
+ console.log("PROOT BIN:", prootBin, fs.existsSync(prootBin));
219
304
 
220
305
  finalCommand = `${prootBin} -R ${rootfsDir} -b ${tempDir}:/sandbox -w /sandbox /bin/sh -c "${commandToRun.replace(/"/g, '\\"')}"`;
221
306
  } else {
@@ -1,59 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const { spawnSync } = require("child_process");
4
- const https = require("https");
5
-
6
- const rootfsMap = {
7
- debian: "https://github.com/Proot-me/proot-distro/releases/download/20240515/debian-rootfs.tar.gz",
8
- alpine: "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/alpine-minirootfs-3.19.1-x86_64.tar.gz",
9
- ubuntu: "https://partner-images.canonical.com/core/focal/current/ubuntu-focal-core-cloudimg-amd64-root.tar.gz",
10
- arch: "https://mirror.rackspace.com/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.gz",
11
- void: "https://repo-default.voidlinux.org/live/current/void-x86_64-ROOTFS-20230628.tar.xz",
12
- fedora: "https://download.fedoraproject.org/pub/fedora/linux/releases/39/Container/x86_64/images/Fedora-Container-Base-39-1.5.x86_64.tar.xz",
13
- centos: "https://buildlogs.centos.org/centos/7/container/x86_64/images/CentOS-7-Container-7.9.2009-20210916.0.x86_64.tar.xz"
14
- };
15
-
16
- const downloadFile = (url, dest) => new Promise((resolve, reject) => {
17
- const file = fs.createWriteStream(dest);
18
- https.get(url, (response) => {
19
- response.pipe(file);
20
- file.on("finish", () => {
21
- file.close(resolve);
22
- });
23
- }).on("error", (err) => {
24
- fs.unlinkSync(dest);
25
- reject(err);
26
- });
27
- });
28
-
29
- async function prepareProotEnv(osName, cacheDir) {
30
- const prootBin = path.join(cacheDir, "proot");
31
- const rootfsDir = path.join(cacheDir, `rootfs_${osName}`);
32
- const tarball = path.join(cacheDir, `${osName}.tar.${osName.includes("xz") ? "xz" : "gz"}`);
33
-
34
- // Descargar proot
35
- if (!fs.existsSync(prootBin)) {
36
- const url = "https://github.com/proot-me/proot/releases/download/v5.3.0/proot-static";
37
- await downloadFile(url, prootBin);
38
- fs.chmodSync(prootBin, 0o755);
39
- }
40
-
41
- // Descargar rootfs
42
- if (!fs.existsSync(rootfsDir)) {
43
- const url = rootfsMap[osName];
44
- if (!url) throw new Error(`Unsupported OS "${osName}"`);
45
- await downloadFile(url, tarball);
46
-
47
- fs.mkdirSync(rootfsDir, { recursive: true });
48
-
49
- // Extraer el tarball
50
- const extCmd = tarball.endsWith(".xz") ? "tar -xJf" : "tar -xzf";
51
- spawnSync(extCmd, [tarball, "-C", rootfsDir], { shell: true });
52
-
53
- fs.unlinkSync(tarball); // limpiar
54
- }
55
-
56
- return { prootBin, rootfsDir };
57
- }
58
-
59
- module.exports = { prepareProotEnv };