cispacempt-v2 0.0.6 → 0.0.8
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 +1 -1
- package/src/index.js +46 -37
- package/src/lib/prootEnv.js +59 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@ 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"),
|
|
5
6
|
yaml = require("js-yaml"),
|
|
6
7
|
getCPUUsageSnapshot = function () {
|
|
7
8
|
const cpus = os.cpus();
|
|
@@ -110,46 +111,46 @@ module.exports = {
|
|
|
110
111
|
};
|
|
111
112
|
|
|
112
113
|
const findFileInMap = function (fileSystem, searchFilePath) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
114
|
+
const pathParts = searchFilePath.split("/");
|
|
115
|
+
let currentMap = fileSystem;
|
|
116
|
+
|
|
117
|
+
for (let i = 0; i < pathParts.length; i++) {
|
|
118
|
+
const part = pathParts[i];
|
|
119
|
+
|
|
120
|
+
if (currentMap[part]) {
|
|
121
|
+
if (typeof currentMap[part] === "object") {
|
|
122
|
+
currentMap = currentMap[part];
|
|
123
|
+
} else {
|
|
124
|
+
return currentMap[part]; // Return the found file if it exists
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
return null; // Return null if not found at all
|
|
128
|
+
}
|
|
127
129
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return null; // Return null if path doesn't exist
|
|
130
|
+
|
|
131
|
+
return null; // Return null if path doesn't exist
|
|
131
132
|
}
|
|
132
133
|
|
|
133
134
|
if (ciScript.include) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
for (const includePath of ciScript.include) {
|
|
136
|
+
const templateContent = findFileInMap(fileSystem, includePath);
|
|
137
|
+
if (!templateContent) {
|
|
138
|
+
console.warn(`Included file '${includePath}' not found`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const includedYaml = yaml.load(templateContent);
|
|
143
|
+
|
|
144
|
+
// Merge included content
|
|
145
|
+
ciScript.stages = {
|
|
146
|
+
...(includedYaml.stages || {}),
|
|
147
|
+
...(ciScript.stages || {})
|
|
148
|
+
};
|
|
149
|
+
ciScript.env = {
|
|
150
|
+
...(includedYaml.env || {}),
|
|
151
|
+
...(ciScript.env || {}),
|
|
152
|
+
};
|
|
139
153
|
}
|
|
140
|
-
|
|
141
|
-
const includedYaml = yaml.load(templateContent);
|
|
142
|
-
|
|
143
|
-
// Merge included content
|
|
144
|
-
ciScript.stages = {
|
|
145
|
-
...(includedYaml.stages || {}),
|
|
146
|
-
...(ciScript.stages || {})
|
|
147
|
-
};
|
|
148
|
-
ciScript.env = {
|
|
149
|
-
...(includedYaml.env || {}),
|
|
150
|
-
...(ciScript.env || {}),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
154
|
}
|
|
154
155
|
|
|
155
156
|
for (const stage of stages) {
|
|
@@ -184,9 +185,17 @@ module.exports = {
|
|
|
184
185
|
for (const command of commands) {
|
|
185
186
|
const resolvedCommand = substituteEnvVariables(command, ciScript.env || {});
|
|
186
187
|
|
|
187
|
-
await new Promise((res) => {
|
|
188
|
+
await new Promise(async (res) => {
|
|
188
189
|
const commandToRun = resolvedCommand;
|
|
189
|
-
|
|
190
|
+
let finalCommand;
|
|
191
|
+
if (ciScript.os && Object.keys(require("/lib/prootEnv").prepareProotEnv.rootfsMap || {}).includes(ciScript.os)) {
|
|
192
|
+
const { prootBin, rootfsDir } = await prepareProotEnv(ciScript.os, path.join(__dirname, "ci_proot_cache"));
|
|
193
|
+
|
|
194
|
+
finalCommand = `${prootBin} -R ${rootfsDir} -b ${tempDir}:/sandbox -w /sandbox /bin/sh -c "${commandToRun.replace(/"/g, '\\"')}"`;
|
|
195
|
+
} else {
|
|
196
|
+
finalCommand = commandToRun;
|
|
197
|
+
}
|
|
198
|
+
const child = spawn(finalCommand, { cwd: tempDir, shell: true });
|
|
190
199
|
let stdoutData = "";
|
|
191
200
|
let stderrData = "";
|
|
192
201
|
|
|
@@ -0,0 +1,59 @@
|
|
|
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 };
|