added basic support for Docker Swarm services

This commit is contained in:
Ivan Bravo Bravo 2022-10-26 00:12:46 -05:00
förälder 6d46647fc9
incheckning 99b2ba8944
2 ändrade filer med 58 tillägg och 14 borttagningar

Visa fil

@ -31,18 +31,41 @@ export default async function handler(req, res) {
const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
const containerExists = containerNames.includes(containerName);
if (!containerExists) {
res.status(200).send({
error: "not found",
if (containerExists) {
const container = docker.getContainer(containerName);
const stats = await container.stats({ stream: false });
res.status(200).json({
stats,
});
return;
}
const container = docker.getContainer(containerName);
const stats = await container.stats({ stream: false });
// Try with a service deployed in Docker Swarm
const tasks = await docker.listTasks({
filters: {
service: [containerName],
// A service can have several offline containers, so we only look for an active one.
'desired-state': ['running']
}
}).catch(() => []);
res.status(200).json({
stats,
// For now we are only interested in the first one (in case replicas > 1).
// TODO: Show the result for all replicas/containers?
const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID
if (taskContainerId) {
const container = docker.getContainer(taskContainerId);
const stats = await container.stats({ stream: false });
res.status(200).json({
stats,
});
return;
}
res.status(200).send({
error: "not found",
});
} catch {
res.status(500).send({

Visa fil

@ -29,17 +29,38 @@ export default async function handler(req, res) {
const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
const containerExists = containerNames.includes(containerName);
if (!containerExists) {
return res.status(200).send({
error: "not found",
if (containerExists) {
const container = docker.getContainer(containerName);
const info = await container.inspect();
return res.status(200).json({
status: info.State.Status,
});
}
const container = docker.getContainer(containerName);
const info = await container.inspect();
const tasks = await docker.listTasks({
filters: {
service: [containerName],
// A service can have several offline containers, we only look for an active one.
'desired-state': ['running']
}
}).catch(() => []);
return res.status(200).json({
status: info.State.Status,
// For now we are only interested in the first one (in case replicas > 1).
// TODO: Show the result for all replicas/containers?
const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID
if (taskContainerId) {
const container = docker.getContainer(taskContainerId);
const info = await container.inspect();
return res.status(200).json({
status: info.State.Status,
});
}
return res.status(200).send({
error: "not found",
});
} catch {
return res.status(500).send({