diff --git a/customCodeDatabase.json b/customCodeDatabase.json index 87c4e66219f71866dd58f7563626bafb2071f338..50184e5266ff1da22ccc565097f1b79caf52a3fe 100644 --- a/customCodeDatabase.json +++ b/customCodeDatabase.json @@ -1,12 +1,12 @@ [ { "type": "Sender", - "id": "7470e3ef-d810-46e9-8d17-5f5153eccd81", + "id": "95d31679-bbf6-43b5-82d9-665ae85d6eb2", "code": "// publisher.js\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n const queue = 'hello';\n\n channel.assertQueue(queue, {\n durable: false\n });\n\n const sendMessage = () => {\n const msg = 'Hello World! ' + new Date().toISOString();\n channel.sendToQueue(queue, Buffer.from(msg));\n console.log(\" [x] Sent '%s'\", msg);\n };\n\n setInterval(sendMessage, 1000);\n });\n});\n" }, { "type": "Receiver", - "id": "4e6acb5f-49f6-40ea-a7d0-2e84bb601897", + "id": "bb3a3016-f851-4487-8255-db4a664417f6", "code": "// consumer.js\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n const queue = 'hello';\n\n channel.assertQueue(queue, {\n durable: false\n });\n\n console.log(\" [*] Waiting for messages in %s. To exit press CTRL+C\", queue);\n\n channel.consume(queue, (msg) => {\n console.log(\" [x] Received '%s'\", msg.content.toString());\n }, {\n noAck: true\n });\n });\n});\n" } ] \ No newline at end of file diff --git a/index.js b/index.js index 0e6b1cc8efbd1c9ab5be6080d08230a7bcaf5149..21234896070fefd2b382486e3aa1caee63c13241 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,8 @@ const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); -const { - emptyCustomCodeDatabase, - clearDocker, -} = require("./src/functions/helperFunctions"); +const { emptyCustomCodeDatabase } = require("./src/functions/helperFunctions"); +const { startRabbitmq } = require("./src/docker/dockerManager"); const customCodeRouter = require("./src/routes/customCodeRouter"); const deployRouter = require("./src/routes/deployRouter"); @@ -22,6 +20,7 @@ server.get("/", (req, res) => { }); emptyCustomCodeDatabase(); +startRabbitmq(); // clearDocker(); server.listen(PORT, () => { diff --git a/src/docker/dockerManager.js b/src/docker/dockerManager.js index 0157363e0c2d339a1d12b385077c298ca702e3a7..d1f78b45e932c3d7aa833573f2ed60382b05d6f1 100644 --- a/src/docker/dockerManager.js +++ b/src/docker/dockerManager.js @@ -181,6 +181,40 @@ function getContainerImageInfosByName(name) { return imageInfosArray; } +function pauseDockerContainer(containerId) { + console.log(`::Start pausing Docker container ${containerId}`); + + let pauseProcess = spawnSync(`docker pause ${containerId}`, [], { + shell: true, + encoding: "utf-8", + stdio: ["inherit", "inherit", "pipe"], + }); + + if (pauseProcess.stderr && pauseProcess.stderr.length > 0) { + console.log(`::Failed to pause Docker container ${containerId}`); + throw new Error(pauseProcess.stderr); + } + + console.log(`::Docker container ${containerId} has been paused`); +} + +function unpauseDockerContainer(containerId) { + console.log(`::Start unpausing Docker container ${containerId}`); + + let unpauseProcess = spawnSync(`docker unpause ${containerId}`, [], { + shell: true, + encoding: "utf-8", + stdio: ["inherit", "inherit", "pipe"], + }); + + if (unpauseProcess.stderr && unpauseProcess.stderr.length > 0) { + console.log(`::Failed to unpause Docker container ${containerId}`); + throw new Error(unpauseProcess.stderr); + } + + console.log(`::Docker container ${containerId} has been unpaused`); +} + function buildAndRunDocker( path, dockerImageName, @@ -204,4 +238,6 @@ module.exports = { killDockerContainer, removeDockerContainer, removeDockerImage, + pauseDockerContainer, + unpauseDockerContainer, }; diff --git a/src/functions/helperFunctions.js b/src/functions/helperFunctions.js index 2b810d30e839fbf0355b0cd5462f339012bb592a..244c378b8cca7cd603933ec7ce9defd62d73cf0e 100644 --- a/src/functions/helperFunctions.js +++ b/src/functions/helperFunctions.js @@ -1,9 +1,10 @@ const fs = require("fs").promises; const path = require("path"); const { - startRabbitmq, buildDockerImage, runDockerContainer, + pauseDockerContainer, + unpauseDockerContainer, } = require("../docker/dockerManager"); const fillDockerJS = async (code) => { @@ -92,8 +93,6 @@ let HOSTPORT = 9090; let CONTAINERPORT = 9090; const deployArchitecture = async () => { - await startRabbitmq(); - await sleep(5000); const customCodes = await getCustomCodes(); for (const customCode of customCodes) { await fillDockerJS(customCode.code); @@ -113,6 +112,20 @@ const deployArchitecture = async () => { } }; +const stopArchitecture = async () => { + const customCodes = await getCustomCodes(); + for (const customCode of customCodes) { + await pauseDockerContainer(customCode.id); + } +}; + +const restartArchitecture = async () => { + const customCodes = await getCustomCodes(); + for (const customCode of customCodes) { + await unpauseDockerContainer(customCode.id); + } +}; + const clearDocker = () => { // Docker Container // Docker Files @@ -127,4 +140,6 @@ module.exports = { getCustomCodes, deployArchitecture, clearDocker, + stopArchitecture, + restartArchitecture, }; diff --git a/src/routes/deployRouter.js b/src/routes/deployRouter.js index fe1406aa49a0ce8b2e8640a16d4aeae4ad797281..7526173ea0c17f68ce9b8903168b466863e104cd 100644 --- a/src/routes/deployRouter.js +++ b/src/routes/deployRouter.js @@ -1,9 +1,13 @@ const express = require("express"); -const { deployArchitecture } = require("../functions/helperFunctions"); +const { + deployArchitecture, + stopArchitecture, + restartArchitecture, +} = require("../functions/helperFunctions"); const deployRouter = express.Router(); -deployRouter.post("/", async (req, res) => { +deployRouter.post("/run", async (req, res) => { try { deployArchitecture(); res.status(200).json({ message: "Deployment erfolgreich!" }); @@ -13,4 +17,24 @@ deployRouter.post("/", async (req, res) => { } }); +deployRouter.post("/stop", async (req, res) => { + try { + stopArchitecture(); + res.status(200).json({ message: "Stop erfolgreich!" }); + } catch (e) { + console.error(e); + res.status(500).json({ message: "Stop fehlgeschlagen!" }); + } +}); + +deployRouter.post("/restart", async (req, res) => { + try { + restartArchitecture(); + res.status(200).json({ message: "Stop erfolgreich!" }); + } catch (e) { + console.error(e); + res.status(500).json({ message: "Stop fehlgeschlagen!" }); + } +}); + module.exports = deployRouter;