Container stats and actions

This commit is contained in:
2025-12-30 13:40:36 +01:00
parent baddd5e4a2
commit 7dd0dca73c
7 changed files with 198 additions and 24 deletions
+55 -1
View File
@@ -3,6 +3,8 @@
import axios from "axios";
import { Docker, Node } from "@/lib/types";
import { verifySession } from "./auth/session";
import { ContainerStats } from "dockerode";
import { getNodes } from "./node";
export async function getDocker(nodes: Node[]): Promise<Docker[]> {
if (await verifySession()) return [];
@@ -10,7 +12,7 @@ export async function getDocker(nodes: Node[]): Promise<Docker[]> {
return await Promise.all(
nodes.map(async (node) => {
const data = await (
await axios.get(`http://${node.ip}:3000/api/get-docker`, {
await axios.get(`http://${node.ip}:3000/api/container/`, {
headers: { Authorization: `ApiKey ${node.key}` },
})
).data;
@@ -19,3 +21,55 @@ export async function getDocker(nodes: Node[]): Promise<Docker[]> {
})
);
}
export async function getContainerStats(
containerId: string
): Promise<ContainerStats | undefined> {
const nodes = await getNodes();
return (
await Promise.all(
nodes.map(
async (n) =>
await (
await axios.get(`http://${n.ip}:3000/api/container/stats`, {
headers: { Authorization: `ApiKey ${n.key}` },
params: { containerId },
})
).data
)
)
).find((s) => s);
}
export async function startContainer(containerId: string) {
const nodes = await getNodes();
nodes.forEach(
async (n) =>
await (
await axios.post(
`http://${n.ip}:3000/api/container/start`,
{ containerId },
{
headers: { Authorization: `ApiKey ${n.key}` },
params: { containerId },
}
)
).data
);
}
export async function stopContainer(containerId: string) {
const nodes = await getNodes();
nodes.forEach(
async (n) =>
await (
await axios.delete(`http://${n.ip}:3000/api/container/start`, {
headers: { Authorization: `ApiKey ${n.key}` },
params: { containerId },
})
).data
);
}
+16 -2
View File
@@ -1,11 +1,25 @@
"use server";
import Docker from "dockerode";
import Docker, { ContainerStats } from "dockerode";
const docker = new Docker();
export async function getDocker() {
const version = await docker.version();
const containers = await docker.listContainers();
const containers = await docker.listContainers({ all: true });
return { version, containers };
}
export async function getContainerStats(
containerId: string
): Promise<ContainerStats | undefined> {
return await (await docker.getContainer(containerId)).stats();
}
export async function startContainer(containerId: string) {
await docker.getContainer(containerId).start();
}
export async function stopContainer(containerId: string) {
await docker.getContainer(containerId).stop();
}