-
Notifications
You must be signed in to change notification settings - Fork 9
/
docker.js
332 lines (301 loc) · 10.3 KB
/
docker.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
const Docker = require("dockerode");
const fs = require("fs");
const path = require("path");
const tar = require("tar");
const manifestUtil = require("./manifest.js");
const debug = require("debug");
const { exec } = require("child_process");
var debugDocker = debug("docker");
exports.debbugOptions = {
docker: ""
};
const docker = new Docker();
const IMAGE_NAME = "node";
const IMAGE_TAG = "mergemarkdown";
const CONTAINER_NAME = "mergemarkdown";
const WORKING_DIR = "/home/runner/workspace/cli";
const TAR_NAME = "archive.tar.gz";
const EXCLUDE_PATHS = [
/.*\/node-modules\/.*/,
/.*\/merged\/.*/,
/.*\/target\/.*/,
];
async function runMergeMarkdownInDocker(manifestFilePath, mergeMarkdownArgs) {
debugDocker(`manifestFilePath: ${manifestFilePath}`);
const manifest = manifestUtil.getManifestObj(manifestFilePath, false);
const outputPath = path.parse(manifest.output.name).dir;
debugDocker(`outputPath: ${outputPath}`);
const manifestPath = path.parse(manifestFilePath).dir || "./";
debugDocker(`manifestPath: ${manifestPath}`);
const excludePaths = [
...(EXCLUDE_PATHS || []), // Add EXCLUDE_PATHS if it exists
...(manifest.docker?.excludePaths || []) // Add manifest.docker.excludePaths if it exists
];
try {
const dockerRunning = await isDockerRunning();
if(!dockerRunning){
console.error("Docker is not running. Start the Docker engine and retry.");
return;
}
const imageExists = await dockerImageExists(`${IMAGE_NAME}:${IMAGE_TAG}`);
if (!imageExists) {
console.log("Docker Image DNE. Creating...");
var command = " docker-compose up -d --build";
console.log(command);
await runExecCommands(command, manifestPath)
.then(output => {
console.log("Success.", output);
})
.catch(error => {
console.error("Error executing:", error);
});
}
console.log("--------Docker Container START--------");
getContainer()
.then(container => {
return container.inspect()
.then(info => {
if (info.State.Running == true) return container;
else return startContainer(container);
});
})
.then(resultContainer => {
console.log("Copying this project to the docker container.");
return createTarArchive(manifestPath, TAR_NAME, excludePaths)
.then(() => {
console.log("Tar archive created successfully");
return copyIntoContainer(resultContainer.id, TAR_NAME, WORKING_DIR);
});
})
.then(resultContainerID => {
console.log("Extracting files into the container.");
var extractCommand = `tar xzf ${WORKING_DIR}/${TAR_NAME} -C ${WORKING_DIR}`;
debugDocker(extractCommand);
return execContainer(resultContainerID, extractCommand);
})
.then(resultContainerID => {
debugDocker(`Running container is ${resultContainerID}`);
var mergeMarkdown = `merge-markdown ${mergeMarkdownArgs}`;
mergeMarkdown = mergeMarkdown.replace(/-m \S+\/\S+\/\S+\//, "-m ");
mergeMarkdown = mergeMarkdown.replace("--docker", "");
const commands = [`cd ${WORKING_DIR}`, mergeMarkdown].join(" && ");
const cmd = ["/bin/sh", "-c", commands];
debugDocker(cmd);
debugDocker("Merging in Docker");
return execContainer(resultContainerID, cmd, true);
})
.then(resultContainerID => {
debugDocker("Downloading locally");
return downloadFromContainer(resultContainerID, path.join(WORKING_DIR, outputPath));
})
.then(() => {
if (!debug.enabled("docker")) {
const fileToDel = path.join("./", TAR_NAME);
fs.unlink(fileToDel, (err) => {
if (err) {
console.error(`Could not delete ${fileToDel}`);
}
});
}
console.log("--------Docker Container END--------");
});
} catch (err) {
console.error("Error getting or creating container:", err);
throw err;
}
}
/* Starts a docker container or returns the running container */
function startContainer(container) {
return new Promise((resolve, reject) => {
debugDocker(`Starting container: ${container.id}`);
container.start((err) => {
if (err) {
if (err.statusCode === 304) {
debugDocker("304. Container already running.");
resolve(container);
} else {
console.error("Error starting container:", err);
reject(err);
}
} else {
debugDocker("Container started successfully");
resolve(container);
}
});
});
}
/* Gets the running container or starts and returns it */
function getContainer(containerName) {
var name = containerName || CONTAINER_NAME;
debugDocker(`Getting containter: ${name}`);
return new Promise((resolve, reject) => {
docker.getContainer(name).inspect((err, data) => {
if (err) {
if (err.statusCode === 404) {
debugDocker("404. Container doesn't exist. Creating...");
resolve(createContainer());
} else reject(err);
} else {
debugDocker(`Found container ${name} with ID: ${data.Id}`);
resolve(docker.getContainer(data.Id)); //returns a container
}
});
});
}
/* Function to create a container based on an existing image */
function createContainer(containerName, imageName) {
var name = containerName || CONTAINER_NAME;
var img = imageName || `${IMAGE_NAME}:${IMAGE_TAG}`;
debugDocker(`Creating container ${name} from image ${img}`);
return new Promise((resolve, reject) => {
docker.createContainer({
Image: img,
Tty: true,
name: name
}, (err, container) => {
if (err) reject(err); // Failed to create container
debugDocker(`Container created: ${container.id}`);
resolve(startContainer(container));
});
});
}
/* execute commands within the running container */
async function execContainer(containerId, command, attachStd) {
const execOptions = {
Cmd: typeof command === "string" ? command.split(" ") : Array.isArray(command) ? command : [],
AttachStdout: attachStd,
AttachStderr: attachStd,
};
const container = docker.getContainer(containerId);
return new Promise((resolve, reject) => {
container.exec(execOptions, function (err, exec) {
const dockerConsole = "";
if (err) {
console.error("Error creating exec instance:", err);
reject(err);
} else {
exec.start(function (err, stream) {
if (err) {
console.error("Error starting exec instance:", err);
reject(err);
} else {
stream.on("data", function (chunk) {
console.log(dockerConsole + chunk.toString());
});
exec.inspect(function (err, data) {
if (err) {
console.error("Error inspecting exec instance:", err);
} else {
console.log(dockerConsole + "Running: ", data.ProcessConfig.entrypoint);
}
});
stream.on("end", function () {
resolve(containerId);
});
}
});
}
});
});
}
function dockerImageExists(imageName) {
var img = imageName || `${IMAGE_NAME}:${IMAGE_TAG}`;
return new Promise((resolve, reject) => {
docker.listImages({ filters: { reference: [img] } }, (err, images) => {
if (images.length > 0) {
resolve(true);
} else {
resolve(false);
}
});
});
}
function isDockerRunning() {
return new Promise((resolve, reject) => {
docker.ping((err) => {
if (err) {
resolve(false); // Docker is not running or not accessible
} else {
resolve(true); // Docker is running
}
});
});
}
/* Run command line arguments with exec */
function runExecCommands(command, cwd) {
return new Promise((resolve, reject) => {
exec(command, { cwd }, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
reject(stderr);
return;
}
resolve(stdout);
});
});
}
/* Copy the local tarFilePath into the destDir in the container */
function copyIntoContainer(containerId, tarFilePath, destDir) {
debugDocker(`Copying ${tarFilePath} to ${destDir}`);
return new Promise((resolve, reject) => {
const container = docker.getContainer(containerId);
const tarStream = fs.createReadStream(tarFilePath);
container.putArchive(tarStream, { path: destDir }, (err) => {
if (err) {
reject(err);
return;
}
resolve(containerId);
});
});
}
/* Takes the sourceDir and compresses it into a tar at tarFilePath, excluding specified paths using regex */
function createTarArchive(sourceDir, tarFilePath, excludedPaths = []) {
debugDocker(`${sourceDir} to tarball ${tarFilePath}`);
return new Promise((resolve, reject) => {
const tarStream = tar.c({
gzip: true,
cwd: sourceDir,
filter: (filePath) => {
// Get the relative path of the file from the sourceDir
const relativePath = path.relative(sourceDir, filePath);
// Check if any of the excludedPaths matches the relativePath using regex
return !excludedPaths.some(excludedPath => {
const regex = typeof excludedPath === "string" ? new RegExp(excludedPath) : excludedPath;
// console.log("File ignored: " + regex);
return regex.test(relativePath);
});
}
}, ["."]);
const fileStream = fs.createWriteStream(tarFilePath);
tarStream.pipe(fileStream);
tarStream.on("end", () => {
resolve();
});
tarStream.on("error", (err) => {
reject(err);
});
fileStream.on("error", (err) => {
reject(err);
});
});
}
/* Downloads the srcPath in the containerId to the local destPath */
async function downloadFromContainer(containerId, srcPath, destPath) {
const stream = await docker.getContainer(containerId).getArchive({ path: srcPath });
if (!destPath) destPath = "./";
stream.pipe(tar.extract({ cwd: destPath }));
return new Promise((resolve, reject) => {
stream.on("end", () => {
console.log(`Output ${srcPath} downloaded to ${path.join(destPath, path.basename(srcPath))}`);
resolve(containerId);
});
stream.on("error", (err) => {
reject(err);
});
});
}
exports.runMergeMarkdownInDocker = runMergeMarkdownInDocker;