forked from snxraven/peardock
update
This commit is contained in:
parent
71992f004c
commit
ffb51d0d72
@ -228,23 +228,23 @@ swarm.on('connection', (peer) => {
|
|||||||
case 'deployContainer':
|
case 'deployContainer':
|
||||||
console.log('[INFO] Handling "deployContainer" command');
|
console.log('[INFO] Handling "deployContainer" command');
|
||||||
const { containerName, image: imageToDeploy, ports = [], volumes = [], env = [] } = parsedData.args;
|
const { containerName, image: imageToDeploy, ports = [], volumes = [], env = [] } = parsedData.args;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Validate and sanitize container name
|
// Validate and sanitize container name
|
||||||
if (!containerName || typeof containerName !== 'string') {
|
if (!containerName || typeof containerName !== 'string') {
|
||||||
throw new Error('Invalid or missing container name.');
|
throw new Error('Invalid or missing container name.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the name is alphanumeric with optional dashes/underscores
|
// Ensure the name is alphanumeric with optional dashes/underscores
|
||||||
if (!/^[a-zA-Z0-9-_]+$/.test(containerName)) {
|
if (!/^[a-zA-Z0-9-_]+$/.test(containerName)) {
|
||||||
throw new Error('Container name must be alphanumeric and may include dashes or underscores.');
|
throw new Error('Container name must be alphanumeric and may include dashes or underscores.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate and sanitize image
|
// Validate and sanitize image
|
||||||
if (!imageToDeploy || typeof imageToDeploy !== 'string') {
|
if (!imageToDeploy || typeof imageToDeploy !== 'string') {
|
||||||
throw new Error('Invalid or missing Docker image.');
|
throw new Error('Invalid or missing Docker image.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate and sanitize ports
|
// Validate and sanitize ports
|
||||||
const validPorts = ports.filter((port) => {
|
const validPorts = ports.filter((port) => {
|
||||||
if (typeof port === 'string' && /^\d+\/(tcp|udp)$/.test(port)) {
|
if (typeof port === 'string' && /^\d+\/(tcp|udp)$/.test(port)) {
|
||||||
@ -254,7 +254,7 @@ swarm.on('connection', (peer) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validate and sanitize volumes
|
// Validate and sanitize volumes
|
||||||
const validVolumes = volumes.filter((volume) => {
|
const validVolumes = volumes.filter((volume) => {
|
||||||
if (typeof volume === 'string' && volume.includes(':')) {
|
if (typeof volume === 'string' && volume.includes(':')) {
|
||||||
@ -264,7 +264,7 @@ swarm.on('connection', (peer) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validate and sanitize environment variables
|
// Validate and sanitize environment variables
|
||||||
const validEnv = env
|
const validEnv = env
|
||||||
.map(({ name, value }) => {
|
.map(({ name, value }) => {
|
||||||
@ -276,17 +276,17 @@ swarm.on('connection', (peer) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
console.log(`[INFO] Pulling Docker image "${imageToDeploy}"`);
|
console.log(`[INFO] Pulling Docker image "${imageToDeploy}"`);
|
||||||
|
|
||||||
// Pull the Docker image
|
// Pull the Docker image
|
||||||
const pullStream = await docker.pull(imageToDeploy);
|
const pullStream = await docker.pull(imageToDeploy);
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
docker.modem.followProgress(pullStream, (err) => (err ? reject(err) : resolve()));
|
docker.modem.followProgress(pullStream, (err) => (err ? reject(err) : resolve()));
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`[INFO] Image "${imageToDeploy}" pulled successfully`);
|
console.log(`[INFO] Image "${imageToDeploy}" pulled successfully`);
|
||||||
|
|
||||||
// Configure container creation settings
|
// Configure container creation settings
|
||||||
const hostConfig = {
|
const hostConfig = {
|
||||||
PortBindings: {},
|
PortBindings: {},
|
||||||
@ -297,7 +297,7 @@ swarm.on('connection', (peer) => {
|
|||||||
const [containerPort, protocol] = port.split('/');
|
const [containerPort, protocol] = port.split('/');
|
||||||
hostConfig.PortBindings[`${containerPort}/${protocol}`] = [{ HostPort: containerPort }];
|
hostConfig.PortBindings[`${containerPort}/${protocol}`] = [{ HostPort: containerPort }];
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create and start the container with a custom name
|
// Create and start the container with a custom name
|
||||||
console.log('[INFO] Creating the container...');
|
console.log('[INFO] Creating the container...');
|
||||||
const container = await docker.createContainer({
|
const container = await docker.createContainer({
|
||||||
@ -306,12 +306,12 @@ swarm.on('connection', (peer) => {
|
|||||||
Env: validEnv,
|
Env: validEnv,
|
||||||
HostConfig: hostConfig,
|
HostConfig: hostConfig,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('[INFO] Starting the container...');
|
console.log('[INFO] Starting the container...');
|
||||||
await container.start();
|
await container.start();
|
||||||
|
|
||||||
console.log(`[INFO] Container "${containerName}" deployed successfully from image "${imageToDeploy}"`);
|
console.log(`[INFO] Container "${containerName}" deployed successfully from image "${imageToDeploy}"`);
|
||||||
|
|
||||||
// Respond with success message
|
// Respond with success message
|
||||||
peer.write(
|
peer.write(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@ -319,11 +319,11 @@ swarm.on('connection', (peer) => {
|
|||||||
message: `Container "${containerName}" deployed successfully from image "${imageToDeploy}"`,
|
message: `Container "${containerName}" deployed successfully from image "${imageToDeploy}"`,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update all peers with the latest container list
|
// Update all peers with the latest container list
|
||||||
const containers = await docker.listContainers({ all: true });
|
const containers = await docker.listContainers({ all: true });
|
||||||
const update = { type: 'containers', data: containers };
|
const update = { type: 'containers', data: containers };
|
||||||
|
|
||||||
for (const connectedPeer of connectedPeers) {
|
for (const connectedPeer of connectedPeers) {
|
||||||
connectedPeer.write(JSON.stringify(update));
|
connectedPeer.write(JSON.stringify(update));
|
||||||
}
|
}
|
||||||
@ -336,7 +336,7 @@ swarm.on('connection', (peer) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
case 'startTerminal':
|
case 'startTerminal':
|
||||||
|
Loading…
Reference in New Issue
Block a user