diff --git a/app.js b/app.js index 94b118f..fe580fe 100644 --- a/app.js +++ b/app.js @@ -398,7 +398,11 @@ function openDuplicateModal(container) { // Populate the modal fields with the current configurations document.getElementById('container-name').value = config.Name.replace(/^\//, ''); + document.getElementById('container-hostname').value = config.Config.Hostname.replace(/^\//, ''); document.getElementById('container-image').value = config.Config.Image; + document.getElementById('container-netmode').value = config.HostConfig?.NetworkMode; + document.getElementById('container-cpu').value = config.HostConfig?.CpuCount; + document.getElementById('container-memory').value = Math.round(config.HostConfig?.Memory / (1024 * 1024)); document.getElementById('container-config').value = JSON.stringify(config, null, 2); // Show the modal @@ -412,7 +416,11 @@ duplicateContainerForm.addEventListener('submit', (e) => { e.preventDefault(); const name = document.getElementById('container-name').value.trim(); + const hostname = document.getElementById('container-hostname').value.trim(); const image = document.getElementById('container-image').value.trim(); + const netmode = document.getElementById('container-netmode').value.trim(); + const cpu = document.getElementById('container-cpu').value.trim(); + const memory = document.getElementById('container-memory').value.trim(); const configJSON = document.getElementById('container-config').value.trim(); let config; @@ -424,9 +432,9 @@ duplicateContainerForm.addEventListener('submit', (e) => { } console.log(`[INFO] Sending duplicateContainer command for name: ${name}`); - + // Send the duplicate command to the server - sendCommand('duplicateContainer', { name, config }); + sendCommand('duplicateContainer', { name, image, hostname, netmode, cpu, memory, config }); // Close the modal duplicateModal.hide(); @@ -453,4 +461,4 @@ window.addEventListener('beforeunload', () => { connection.swarm.destroy(); } } -}); +}); \ No newline at end of file diff --git a/index.html b/index.html index a1dfd5e..92125e0 100644 --- a/index.html +++ b/index.html @@ -190,10 +190,26 @@ +
+ + +
+
+ + +
+
+ + +
+
+ + +
@@ -229,4 +245,4 @@ - + \ No newline at end of file diff --git a/server/server.js b/server/server.js index 44f20ed..8907019 100644 --- a/server/server.js +++ b/server/server.js @@ -44,8 +44,11 @@ swarm.on('connection', (peer) => { case 'duplicateContainer': console.log('[INFO] Handling \'duplicateContainer\' command'); - const { name, config: dupConfig } = parsedData.args; - await duplicateContainer(name, dupConfig, peer); + const { name, image, hostname, netmode, cpu, memory, config: dupConfig } = parsedData.args; + const memoryInMB = memory * 1024 * 1024; + console.log("MEMEMMEMEMEMEMEMEMMEME " + memoryInMB) + + await duplicateContainer(name, image, hostname, netmode, cpu, memoryInMB, dupConfig, peer); return; // Response is handled within the duplicateContainer function case 'startContainer': @@ -132,7 +135,7 @@ function cleanupPeer(peer) { } // Function to duplicate a container -async function duplicateContainer(name, config, peer) { +async function duplicateContainer(name, image, hostname, netmode, cpu, memory, config, peer) { try { // Remove non-essential fields from the configuration const sanitizedConfig = { ...config }; @@ -144,6 +147,13 @@ async function duplicateContainer(name, config, peer) { delete sanitizedConfig.Path; delete sanitizedConfig.Args; delete sanitizedConfig.Image; + delete sanitizedConfig.Hostname; + delete sanitizedConfig.CpuCount; + delete sanitizedConfig.Memory; + delete sanitizedConfig.CpuShares; + delete sanitizedConfig.CpusetCpus; + + // Ensure the container has a unique name const newName = name; @@ -155,12 +165,25 @@ async function duplicateContainer(name, config, peer) { return; } + + const cpusetCpus = Array.from({ length: cpu }, (_, i) => i).join(","); + const nanoCpus = cpu * 1e9; + // Create a new container with the provided configuration const newContainer = await docker.createContainer({ - ...sanitizedConfig.Config, - name: newName, - }); + ...sanitizedConfig.Config, // General configuration + name: newName, // Container name + Hostname: hostname, // Hostname for the container + Image: image, // Container image + HostConfig: { // Host-specific configurations + CpusetCpus: cpusetCpus.toString(), // Number of CPUs + NanoCpus: nanoCpus, // Restrict CPU time (e.g., 4 cores = 4e9 nanoseconds) + Memory: Number(memory), // Memory limit in bytes + MemoryReservation: Number(memory), // Memory limit in bytes + NetworkMode: netmode.toString(), // Network mode + }, + }); // Start the new container await newContainer.start(); @@ -404,4 +427,4 @@ process.on('SIGINT', () => { console.log('[INFO] Server shutting down'); swarm.destroy(); process.exit(); -}); +}); \ No newline at end of file