This commit is contained in:
Raven Scott 2024-11-28 07:07:46 -05:00
parent d60f37f2ed
commit 6658e74197
3 changed files with 58 additions and 11 deletions

14
app.js
View File

@ -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();
}
}
});
});

View File

@ -190,10 +190,26 @@
<label for="container-name" class="form-label">Container Name</label>
<input type="text" class="form-control" id="container-name" required>
</div>
<div class="mb-3">
<label for="container-image" class="form-label">Hostname</label>
<input type="text" class="form-control" id="container-hostname" required>
</div>
<div class="mb-3">
<label for="container-image" class="form-label">Image</label>
<input type="text" class="form-control" id="container-image" required>
</div>
<div class="mb-3">
<label for="container-image" class="form-label">Net Mode</label>
<input type="text" class="form-control" id="container-netmode" required>
</div>
<div class="mb-3">
<label for="container-cpu" class="form-label">CPU Count</label>
<input type="number" class="form-control" id="container-cpu" required>
</div>
<div class="mb-3">
<label for="container-memory" class="form-label">Memory (MB)</label>
<input type="number" class="form-control" id="container-memory" required>
</div>
<!-- Container Configuration as JSON -->
<div class="mb-3">
<label for="container-config" class="form-label">Container Configuration (JSON)</label>
@ -229,4 +245,4 @@
<!-- Your App JS -->
<script type="module" src="app.js"></script>
</body>
</html>
</html>

View File

@ -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();
});
});