Skip to content
Snippets Groups Projects
Commit 01b0b340 authored by Robin Leber's avatar Robin Leber
Browse files

clean up docker

parent 219500b3
No related branches found
No related tags found
No related merge requests found
const express = require("express"); const express = require("express");
const bodyParser = require("body-parser"); const bodyParser = require("body-parser");
const cors = require("cors"); const cors = require("cors");
const http = require("http");
const { emptyCustomCodeDatabase } = require("./src/functions/helperFunctions"); const { emptyCustomCodeDatabase } = require("./src/functions/helperFunctions");
const { startRabbitmq } = require("./src/docker/dockerManager"); const { startRabbitmq, dockerCleanUp } = require("./src/docker/dockerManager");
const customCodeRouter = require("./src/routes/customCodeRouter"); const customCodeRouter = require("./src/routes/customCodeRouter");
const deployRouter = require("./src/routes/deployRouter"); const deployRouter = require("./src/routes/deployRouter");
const server = express(); const app = express();
const PORT = 3001; const PORT = 3001;
server.use(cors()); app.use(cors());
server.use(bodyParser.json()); app.use(bodyParser.json());
server.use("/customCode", customCodeRouter); app.use("/customCode", customCodeRouter);
server.use("/deploy", deployRouter); app.use("/deploy", deployRouter);
server.get("/", (req, res) => { app.get("/", (req, res) => {
res.send("Hello World"); res.send("Hello World");
}); });
emptyCustomCodeDatabase(); emptyCustomCodeDatabase();
// clearDocker(); // clearDocker();
const server = http.createServer(app);
server.listen(PORT, () => { server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`); console.log(`Server listening on port ${PORT}`);
startRabbitmq(); startRabbitmq();
}); });
process.on("SIGINT", () => {
console.log("SIGINT empfangen. Server wird beendet...");
dockerCleanUp();
server.close(() => {
console.log("HTTP-Server geschlossen");
process.exit(0);
});
});
...@@ -215,6 +215,120 @@ function unpauseDockerContainer(containerId) { ...@@ -215,6 +215,120 @@ function unpauseDockerContainer(containerId) {
console.log(`::Docker container ${containerId} has been unpaused`); console.log(`::Docker container ${containerId} has been unpaused`);
} }
function dockerCleanUp() {
console.log("::Start removing all Docker containers");
// Alle Docker-Container abrufen
let listContainersProcess = spawnSync("docker ps -aq", [], {
shell: true,
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
});
if (listContainersProcess.stderr && listContainersProcess.stderr.length > 0) {
console.log("::Failed to retrieve Docker containers");
throw new Error(listContainersProcess.stderr);
}
const containerIds = listContainersProcess.stdout
.trim()
.split("\n")
.filter(Boolean);
if (containerIds.length > 0) {
console.log(`::Removing ${containerIds.length} Docker containers`);
// Alle Docker-Container stoppen
let stopContainersProcess = spawnSync("docker stop $(docker ps -aq)", [], {
shell: true,
encoding: "utf-8",
stdio: ["inherit", "inherit", "pipe"],
});
if (
stopContainersProcess.stderr &&
stopContainersProcess.stderr.length > 0
) {
console.log("::Failed to stop Docker containers");
throw new Error(stopContainersProcess.stderr);
}
// Alle Docker-Container entfernen
let removeContainersProcess = spawnSync("docker rm $(docker ps -aq)", [], {
shell: true,
encoding: "utf-8",
stdio: ["inherit", "inherit", "pipe"],
});
if (
removeContainersProcess.stderr &&
removeContainersProcess.stderr.length > 0
) {
console.log("::Failed to remove Docker containers");
throw new Error(removeContainersProcess.stderr);
}
console.log("::All Docker containers have been removed");
} else {
console.log("::No Docker containers found");
}
console.log("::Start removing all Docker images");
// Alle Docker-Images abrufen
let listImagesProcess = spawnSync("docker images -aq", [], {
shell: true,
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
});
if (listImagesProcess.stderr && listImagesProcess.stderr.length > 0) {
console.log("::Failed to retrieve Docker images");
throw new Error(listImagesProcess.stderr);
}
const imageIds = listImagesProcess.stdout.trim().split("\n").filter(Boolean);
if (imageIds.length > 0) {
console.log(`::Removing ${imageIds.length} Docker images`);
// Alle Docker-Images entfernen
let removeImagesProcess = spawnSync(
"docker rmi -f $(docker images -aq)",
[],
{
shell: true,
encoding: "utf-8",
stdio: ["inherit", "inherit", "pipe"],
}
);
if (removeImagesProcess.stderr && removeImagesProcess.stderr.length > 0) {
console.log("::Failed to remove Docker images");
throw new Error(removeImagesProcess.stderr);
}
console.log("::All Docker images have been removed");
} else {
console.log("::No Docker images found");
}
console.log("::Start pruning unused Docker networks");
// Unbenutzte Docker-Netzwerke bereinigen
let pruneNetworksProcess = spawnSync("docker network prune -f", [], {
shell: true,
encoding: "utf-8",
stdio: ["inherit", "inherit", "pipe"],
});
if (pruneNetworksProcess.stderr && pruneNetworksProcess.stderr.length > 0) {
console.log("::Failed to prune Docker networks");
throw new Error(pruneNetworksProcess.stderr);
}
console.log("::All unused Docker networks have been pruned");
}
function buildAndRunDocker( function buildAndRunDocker(
path, path,
dockerImageName, dockerImageName,
...@@ -240,4 +354,5 @@ module.exports = { ...@@ -240,4 +354,5 @@ module.exports = {
removeDockerImage, removeDockerImage,
pauseDockerContainer, pauseDockerContainer,
unpauseDockerContainer, unpauseDockerContainer,
dockerCleanUp,
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment