mirror of
				https://github.com/karl0ss/homepage.git
				synced 2025-11-04 08:20:58 +00:00 
			
		
		
		
	added basic support for Docker Swarm services
This commit is contained in:
		
							parent
							
								
									6d46647fc9
								
							
						
					
					
						commit
						99b2ba8944
					
				@ -31,19 +31,42 @@ export default async function handler(req, res) {
 | 
				
			|||||||
    const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
 | 
					    const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
 | 
				
			||||||
    const containerExists = containerNames.includes(containerName);
 | 
					    const containerExists = containerNames.includes(containerName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!containerExists) {
 | 
					    if (containerExists) {
 | 
				
			||||||
      res.status(200).send({
 | 
					 | 
				
			||||||
        error: "not found",
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
      return;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      const container = docker.getContainer(containerName);
 | 
					      const container = docker.getContainer(containerName);
 | 
				
			||||||
      const stats = await container.stats({ stream: false });
 | 
					      const stats = await container.stats({ stream: false });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      res.status(200).json({
 | 
					      res.status(200).json({
 | 
				
			||||||
        stats,
 | 
					        stats,
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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(() => []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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 {
 | 
					  } catch {
 | 
				
			||||||
    res.status(500).send({
 | 
					    res.status(500).send({
 | 
				
			||||||
      error: "unknown error",
 | 
					      error: "unknown error",
 | 
				
			||||||
 | 
				
			|||||||
@ -29,18 +29,39 @@ export default async function handler(req, res) {
 | 
				
			|||||||
    const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
 | 
					    const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
 | 
				
			||||||
    const containerExists = containerNames.includes(containerName);
 | 
					    const containerExists = containerNames.includes(containerName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!containerExists) {
 | 
					    if (containerExists) {
 | 
				
			||||||
      return res.status(200).send({
 | 
					 | 
				
			||||||
        error: "not found",
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      const container = docker.getContainer(containerName);
 | 
					      const container = docker.getContainer(containerName);
 | 
				
			||||||
      const info = await container.inspect();
 | 
					      const info = await container.inspect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return res.status(200).json({
 | 
					      return res.status(200).json({
 | 
				
			||||||
        status: info.State.Status,
 | 
					        status: info.State.Status,
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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(() => []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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 {
 | 
					  } catch {
 | 
				
			||||||
    return res.status(500).send({
 | 
					    return res.status(500).send({
 | 
				
			||||||
      error: "unknown error",
 | 
					      error: "unknown error",
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user