Always show IP Address in list

This commit is contained in:
Raven Scott 2024-11-29 19:10:48 -05:00
parent d54328c001
commit 4f31d0657c

View File

@ -58,23 +58,36 @@ swarm.on('connection', (peer) => {
switch (parsedData.command) { switch (parsedData.command) {
case 'listContainers': case 'listContainers':
console.log('[INFO] Handling \'listContainers\' command'); console.log('[INFO] Handling \'listContainers\' command');
const containers = await docker.listContainers({ all: true }); try {
const containers = await docker.listContainers({ all: true });
const detailedContainers = await Promise.all( const detailedContainers = await Promise.all(
containers.map(async (container) => { containers.map(async (container) => {
const details = await docker.getContainer(container.Id).inspect(); try {
console.log(details.NetworkSettings.Networks); // Log the network details const details = await docker.getContainer(container.Id).inspect();
// Safely access the IP address // Safely access the IP address
const ipAddress = details.NetworkSettings.Networks let ipAddress = 'No IP Assigned';
? Object.values(details.NetworkSettings.Networks)[0]?.IPAddress || 'No IP Assigned' if (details.NetworkSettings && details.NetworkSettings.Networks) {
: 'No Networks'; const networks = Object.values(details.NetworkSettings.Networks);
if (networks.length > 0 && networks[0].IPAddress) {
ipAddress = networks[0].IPAddress;
}
}
return { ...container, ipAddress }; // Add IP address to container data return { ...container, ipAddress }; // Add IP address to container data
}) } catch (error) {
); console.error(`[ERROR] Failed to inspect container ${container.Id}: ${error.message}`);
return { ...container, ipAddress: 'Error Retrieving IP' }; // Return partial data with error
}
})
);
response = { type: 'containers', data: detailedContainers }; response = { type: 'containers', data: detailedContainers };
} catch (error) {
console.error(`[ERROR] Failed to list containers: ${error.message}`);
response = { error: 'Failed to list containers' };
}
break; break;
case 'inspectContainer': case 'inspectContainer':
@ -280,43 +293,63 @@ docker.getEvents({}, (err, stream) => {
}); });
// Collect and stream container stats // Collect and stream container stats
docker.listContainers({ all: true }, (err, containers) => { docker.listContainers({ all: true }, async (err, containers) => {
if (err) { if (err) {
console.error(`[ERROR] Failed to list containers for stats: ${err.message}`); console.error(`[ERROR] Failed to list containers for stats: ${err.message}`);
return; return;
} }
// Iterate over all containers
containers.forEach((containerInfo) => { containers.forEach((containerInfo) => {
const container = docker.getContainer(containerInfo.Id); const container = docker.getContainer(containerInfo.Id);
container.stats({ stream: true }, (err, stream) => {
if (err) { // Use the same logic as listContainers to pre-inspect and extract the IP address
return; container.inspect((inspectErr, details) => {
let ipAddress = 'No IP Assigned'; // Default fallback
if (!inspectErr && details.NetworkSettings && details.NetworkSettings.Networks) {
const networks = Object.values(details.NetworkSettings.Networks);
if (networks.length > 0 && networks[0].IPAddress) {
ipAddress = networks[0].IPAddress; // Use the first network's IP
}
} }
stream.on('data', (data) => { // Start streaming container stats
try { container.stats({ stream: true }, (statsErr, stream) => {
const stats = JSON.parse(data.toString()); if (statsErr) {
const cpuUsage = calculateCPUPercent(stats); console.error(`[ERROR] Failed to get stats for container ${containerInfo.Id}: ${statsErr.message}`);
const memoryUsage = stats.memory_stats.usage; return;
const networks = stats.networks;
const ipAddress = networks ? Object.values(networks)[0].IPAddress : '-';
const statsData = {
id: containerInfo.Id,
cpu: cpuUsage,
memory: memoryUsage,
ip: ipAddress,
};
// Broadcast stats to all connected peers
for (const peer of connectedPeers) {
peer.write(JSON.stringify({ type: 'stats', data: statsData }));
}
} catch (err) {
} }
});
stream.on('error', (err) => { stream.on('data', (data) => {
try {
const stats = JSON.parse(data.toString());
const cpuUsage = calculateCPUPercent(stats);
const memoryUsage = stats.memory_stats.usage || 0; // Default to 0 if undefined
const statsData = {
id: containerInfo.Id,
cpu: cpuUsage,
memory: memoryUsage,
ip: ipAddress, // Use the pre-inspected IP address
};
// Broadcast stats to all connected peers
for (const peer of connectedPeers) {
peer.write(JSON.stringify({ type: 'stats', data: statsData }));
}
} catch (parseErr) {
console.error(`[ERROR] Failed to parse stats for container ${containerInfo.Id}: ${parseErr.message}`);
}
});
stream.on('error', (streamErr) => {
console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${streamErr.message}`);
});
stream.on('close', () => {
console.log(`[INFO] Stats stream closed for container ${containerInfo.Id}`);
});
}); });
}); });
}); });
@ -427,18 +460,19 @@ function handleKillTerminal(containerId, peer) {
function streamContainerStats(containerInfo) { function streamContainerStats(containerInfo) {
const container = docker.getContainer(containerInfo.Id); const container = docker.getContainer(containerInfo.Id);
// First, retrieve the container's IP address using inspect // Use the same logic as listContainers to get the IP address
container.inspect((inspectErr, data) => { container.inspect((inspectErr, details) => {
let ipAddress = 'No IP Assigned'; // Default IP address fallback
if (inspectErr) { if (inspectErr) {
console.error(`[ERROR] Failed to inspect container ${containerInfo.Id}: ${inspectErr.message}`); console.error(`[ERROR] Failed to inspect container ${containerInfo.Id}: ${inspectErr.message}`);
return; } else if (details.NetworkSettings && details.NetworkSettings.Networks) {
const networks = Object.values(details.NetworkSettings.Networks);
if (networks.length > 0 && networks[0].IPAddress) {
ipAddress = networks[0].IPAddress; // Retrieve the first network's IP address
}
} }
// Retrieve the IP address from the NetworkSettings
const ipAddress = data.NetworkSettings.Networks
? Object.values(data.NetworkSettings.Networks)[0].IPAddress
: '-';
// Start streaming container stats // Start streaming container stats
container.stats({ stream: true }, (statsErr, stream) => { container.stats({ stream: true }, (statsErr, stream) => {
if (statsErr) { if (statsErr) {
@ -450,13 +484,14 @@ function streamContainerStats(containerInfo) {
try { try {
const stats = JSON.parse(data.toString()); const stats = JSON.parse(data.toString());
const cpuUsage = calculateCPUPercent(stats); const cpuUsage = calculateCPUPercent(stats);
const memoryUsage = stats.memory_stats.usage; const memoryUsage = stats.memory_stats.usage || 0; // Default to 0 if undefined
// Use the extracted IP address in the stats data
const statsData = { const statsData = {
id: containerInfo.Id, id: containerInfo.Id,
cpu: cpuUsage, cpu: cpuUsage,
memory: memoryUsage, memory: memoryUsage,
ip: ipAddress, // Use the inspected IP address ip: ipAddress, // Use the IP address retrieved during inspection
}; };
// Broadcast stats to all connected peers // Broadcast stats to all connected peers
@ -471,6 +506,10 @@ function streamContainerStats(containerInfo) {
stream.on('error', (streamErr) => { stream.on('error', (streamErr) => {
console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${streamErr.message}`); console.error(`[ERROR] Stats stream error for container ${containerInfo.Id}: ${streamErr.message}`);
}); });
stream.on('close', () => {
console.log(`[INFO] Stats stream closed for container ${containerInfo.Id}`);
});
}); });
}); });
} }