Applications
This commit is contained in:
@@ -6,6 +6,9 @@ import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import { exec } from "child_process";
|
||||
import util from "util";
|
||||
import { getNodes } from "./node";
|
||||
import axios from "axios";
|
||||
import { Application } from "@/lib/types";
|
||||
|
||||
const execAsync = util.promisify(exec);
|
||||
|
||||
@@ -70,3 +73,20 @@ export async function deployNewApp(name: string, repo: string, nodeId: string) {
|
||||
image: imageTag,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getApplications(): Promise<Application[]> {
|
||||
const nodes = await getNodes();
|
||||
|
||||
return (
|
||||
await Promise.all(
|
||||
nodes.map(
|
||||
async (n) =>
|
||||
await (
|
||||
await axios.get(`http://${n.ip}:3000/api/applications`, {
|
||||
headers: { Authorization: `ApiKey ${n.key}` },
|
||||
})
|
||||
).data.applications
|
||||
)
|
||||
)
|
||||
).flat();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"use server";
|
||||
|
||||
import { Application } from "@/lib/types";
|
||||
import Docker from "dockerode";
|
||||
|
||||
const docker = new Docker();
|
||||
|
||||
export async function getApplications(): Promise<Application[]> {
|
||||
const images = await docker.listImages();
|
||||
const containers = await docker.listContainers({ all: true });
|
||||
|
||||
const apps: Application[] = [];
|
||||
|
||||
for (const image of images) {
|
||||
const imageName = image.RepoTags?.[0];
|
||||
if (!imageName) continue;
|
||||
|
||||
const relatedContainers = containers.filter((c) => c.Image === imageName);
|
||||
|
||||
let totalCpu = 0;
|
||||
let totalMem = 0;
|
||||
let running = 0;
|
||||
const ports = new Set<string>();
|
||||
|
||||
for (const containerInfo of relatedContainers) {
|
||||
const container = docker.getContainer(containerInfo.Id);
|
||||
|
||||
if (containerInfo.State === "running") running++;
|
||||
|
||||
containerInfo.Ports?.forEach((p) => {
|
||||
if (p.PublicPort && p.PrivatePort) {
|
||||
ports.add(`${p.PublicPort}:${p.PrivatePort}`);
|
||||
}
|
||||
});
|
||||
|
||||
if (containerInfo.State === "running") {
|
||||
const stats = await container.stats({ stream: false });
|
||||
}
|
||||
}
|
||||
|
||||
apps.push({
|
||||
id: image.Id.slice(7, 15),
|
||||
name: imageName.split("/").pop()?.split(":")[0] ?? imageName,
|
||||
image: imageName,
|
||||
containers: relatedContainers.length,
|
||||
replicas: `${running}/${relatedContainers.length}`,
|
||||
cpu: Math.round(totalCpu),
|
||||
memory: Math.round(totalMem),
|
||||
network: "—",
|
||||
status: running > 0 ? ("running" as const) : ("stopped" as const),
|
||||
ports: [...ports],
|
||||
});
|
||||
}
|
||||
|
||||
return apps;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ export async function getDocker() {
|
||||
export async function getContainerStats(
|
||||
containerId: string
|
||||
): Promise<ContainerStats | undefined> {
|
||||
return await (await docker.getContainer(containerId)).stats();
|
||||
return await docker.getContainer(containerId).stats();
|
||||
}
|
||||
|
||||
export async function startContainer(containerId: string) {
|
||||
|
||||
Reference in New Issue
Block a user