bitstream/docker/containerService.js

102 lines
3.1 KiB
JavaScript

// containerService.js
const docker = require('./dockerClient');
const calculateCPUPercent = require('../utils/calculateCPU');
const chalk = require('chalk').default;
async function listContainers() {
const containers = await docker.listContainers({ all: true });
return Promise.all(containers.map(async (containerInfo) => {
const container = docker.getContainer(containerInfo.Id);
const inspect = await container.inspect();
return {
id: containerInfo.Id,
name: containerInfo.Names[0].replace('/', ''),
image: containerInfo.Image,
status: containerInfo.State,
state: containerInfo.Status,
ports: containerInfo.Ports,
created: containerInfo.Created,
labels: containerInfo.Labels || {},
inspect: {
config: inspect.Config,
hostConfig: inspect.HostConfig,
networkSettings: inspect.NetworkSettings
}
};
}));
}
async function getContainerStats(id) {
const container = docker.getContainer(id);
const stats = await container.stats({ stream: false });
const cpuPercent = calculateCPUPercent(stats);
const memoryUsage = stats.memory_stats.usage || 0;
const memoryLimit = stats.memory_stats.limit || 0;
const memoryPercent = memoryLimit > 0 ? (memoryUsage / memoryLimit) * 100 : 0;
const networks = stats.networks || {};
const networkIO = Object.keys(networks).reduce((acc, key) => ({
rx_bytes: acc.rx_bytes + (networks[key].rx_bytes || 0),
tx_bytes: acc.tx_bytes + (networks[key].tx_bytes || 0)
}), { rx_bytes: 0, tx_bytes: 0 });
return {
cpu_percent: cpuPercent,
memory: { usage: memoryUsage, limit: memoryLimit, percent: memoryPercent },
network: networkIO,
block_io: stats.blkio_stats
};
}
async function startContainer(id) {
const container = docker.getContainer(id);
await container.start();
console.log(chalk.yellow('Container started:'), chalk.cyan(id));
}
async function stopContainer(id) {
const container = docker.getContainer(id);
await container.stop();
console.log(chalk.yellow('Container stopped:'), chalk.cyan(id));
}
async function restartContainer(id) {
const container = docker.getContainer(id);
await container.restart();
console.log(chalk.yellow('Container restarted:'), chalk.cyan(id));
}
async function createContainer(options) {
const container = await docker.createContainer(options);
await container.start();
console.log(chalk.green('Container created and started:'), chalk.cyan(container.id));
return container.id;
}
async function removeContainer(id) {
const container = docker.getContainer(id);
try { await container.stop(); } catch (e) {}
await container.remove();
console.log(chalk.red('Container deleted:'), chalk.cyan(id));
}
async function getContainerLogs(id) {
const container = docker.getContainer(id);
const logs = await container.logs({
stdout: true,
stderr: true,
tail: 100,
timestamps: true
});
return logs.toString();
}
module.exports = {
listContainers,
getContainerStats,
startContainer,
stopContainer,
restartContainer,
createContainer,
removeContainer,
getContainerLogs
};