added yaml file variable for swarm mode checks

This commit is contained in:
Vinay Dawani 2022-12-11 03:03:20 -05:00
parent f51e755216
commit fb883c7b27
4 changed files with 54 additions and 46 deletions

View File

@ -14,7 +14,8 @@ export default async function handler(req, res) {
} }
try { try {
const docker = new Docker(getDockerArguments(containerServer)); const dockerArgs = getDockerArguments(containerServer)
const docker = new Docker(dockerArgs.conn);
const containers = await docker.listContainers({ const containers = await docker.listContainers({
all: true, all: true,
}); });
@ -41,18 +42,20 @@ export default async function handler(req, res) {
return; return;
} }
// Try with a service deployed in Docker Swarm // Try with a service deployed in Docker Swarm, if enabled
if (dockerArgs.swarm) {
const tasks = await docker.listTasks({ const tasks = await docker.listTasks({
filters: { filters: {
service: [containerName], service: [containerName],
// A service can have several offline containers, so we only look for an active one. // A service can have several offline containers, so we only look for an active one.
'desired-state': ['running'] "desired-state": ["running"],
} },
}).catch(() => []); })
.catch(() => []);
// For now we are only interested in the first one (in case replicas > 1). // For now we are only interested in the first one (in case replicas > 1).
// TODO: Show the result for all replicas/containers? // TODO: Show the result for all replicas/containers?
const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID;
if (taskContainerId) { if (taskContainerId) {
const container = docker.getContainer(taskContainerId); const container = docker.getContainer(taskContainerId);
@ -63,6 +66,7 @@ export default async function handler(req, res) {
}); });
return; return;
} }
}
res.status(200).send({ res.status(200).send({
error: "not found", error: "not found",

View File

@ -13,7 +13,8 @@ export default async function handler(req, res) {
} }
try { try {
const docker = new Docker(getDockerArguments(containerServer)); const dockerArgs = getDockerArguments(containerServer);
const docker = new Docker(dockerArgs.conn);
const containers = await docker.listContainers({ const containers = await docker.listContainers({
all: true, all: true,
}); });
@ -35,21 +36,23 @@ export default async function handler(req, res) {
return res.status(200).json({ return res.status(200).json({
status: info.State.Status, status: info.State.Status,
health: info.State.Health?.Status health: info.State.Health?.Status,
}); });
} }
if (dockerArgs.swarm) {
const tasks = await docker.listTasks({ const tasks = await docker.listTasks({
filters: { filters: {
service: [containerName], service: [containerName],
// A service can have several offline containers, we only look for an active one. // A service can have several offline containers, we only look for an active one.
'desired-state': ['running'] "desired-state": ["running"],
} },
}).catch(() => []); })
.catch(() => []);
// For now we are only interested in the first one (in case replicas > 1). // For now we are only interested in the first one (in case replicas > 1).
// TODO: Show the result for all replicas/containers? // TODO: Show the result for all replicas/containers?
const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID;
if (taskContainerId) { if (taskContainerId) {
const container = docker.getContainer(taskContainerId); const container = docker.getContainer(taskContainerId);
@ -57,9 +60,10 @@ export default async function handler(req, res) {
return res.status(200).json({ return res.status(200).json({
status: info.State.Status, status: info.State.Status,
health: info.State.Health?.Status health: info.State.Health?.Status,
}); });
} }
}
return res.status(200).send({ return res.status(200).send({
error: "not found", error: "not found",

View File

@ -22,11 +22,11 @@ export default function getDockerArguments(server) {
if (servers[server]) { if (servers[server]) {
if (servers[server].socket) { if (servers[server].socket) {
return { socketPath: servers[server].socket }; return { conn: { socketPath: servers[server].socket }, swarm: servers[server].swarm };
} }
if (servers[server].host) { if (servers[server].host) {
return { host: servers[server].host, port: servers[server].port || null }; return { conn: { host: servers[server].host, port: servers[server].port || null }, swarm: servers[server].swarm };
} }
return servers[server]; return servers[server];

View File

@ -44,7 +44,7 @@ export async function servicesFromDocker() {
const serviceServers = await Promise.all( const serviceServers = await Promise.all(
Object.keys(servers).map(async (serverName) => { Object.keys(servers).map(async (serverName) => {
const docker = new Docker(getDockerArguments(serverName)); const docker = new Docker(getDockerArguments(serverName).conn);
const containers = await docker.listContainers({ const containers = await docker.listContainers({
all: true, all: true,
}); });