Patched a few things with deployment
This commit is contained in:
Generated
+28
@@ -32,6 +32,7 @@
|
||||
"lucide-react": "^0.562.0",
|
||||
"next": "16.1.1",
|
||||
"os-name": "^6.1.0",
|
||||
"p-limit": "^7.2.0",
|
||||
"react": "19.2.3",
|
||||
"react-dom": "19.2.3",
|
||||
"recharts": "^2.15.4",
|
||||
@@ -4517,6 +4518,21 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/p-limit": {
|
||||
"version": "7.2.0",
|
||||
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-7.2.0.tgz",
|
||||
"integrity": "sha512-ATHLtwoTNDloHRFFxFJdHnG6n2WUeFjaR8XQMFdKIv0xkXjrER8/iG9iu265jOM95zXHAfv9oTkqhrfbIzosrQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"yocto-queue": "^1.2.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/path-key": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
|
||||
@@ -5348,6 +5364,18 @@
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/yocto-queue": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.2.tgz",
|
||||
"integrity": "sha512-4LCcse/U2MHZ63HAJVE+v71o7yOdIe4cZ70Wpf8D/IyjDKYQLV5GD46B+hSTjJsvV5PztjvHoU580EftxjDZFQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=12.20"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
"lucide-react": "^0.562.0",
|
||||
"next": "16.1.1",
|
||||
"os-name": "^6.1.0",
|
||||
"p-limit": "^7.2.0",
|
||||
"react": "19.2.3",
|
||||
"react-dom": "19.2.3",
|
||||
"recharts": "^2.15.4",
|
||||
|
||||
@@ -17,6 +17,7 @@ import { useEffect, useState } from "react";
|
||||
import { getApplications, removeApp } from "@/lib/dockship/application";
|
||||
import RemoveDialog from "@/components/dialogs/Remove";
|
||||
import { Application } from "@/lib/types";
|
||||
import DeployAppDialog from "@/components/dialogs/DeployApp";
|
||||
|
||||
export default function ApplicationsPage() {
|
||||
const [applications, setApplications] = useState<Application[]>([]);
|
||||
@@ -75,9 +76,11 @@ export default function ApplicationsPage() {
|
||||
</CardContent>
|
||||
|
||||
<CardFooter className="justify-end">
|
||||
<Button variant="ghost" size="icon-sm" aria-label="Deploy">
|
||||
<Rocket className="w-3 h-3" />
|
||||
</Button>
|
||||
<DeployAppDialog defaultApp={app.id}>
|
||||
<Button variant="ghost" size="icon-sm" aria-label="Deploy">
|
||||
<Rocket className="w-3 h-3" />
|
||||
</Button>
|
||||
</DeployAppDialog>
|
||||
|
||||
<RemoveDialog remove={() => remove(app.id)} />
|
||||
|
||||
|
||||
@@ -82,7 +82,8 @@ export default function DeploymentsPage() {
|
||||
{/* Desktop */}
|
||||
<div className="hidden md:block space-y-3">
|
||||
{deployments.map((dep) => {
|
||||
const appName = appNameByContainer.get(dep.container) ?? "Unknown app";
|
||||
const appName =
|
||||
appNameByContainer.get(dep.container) ?? "Unknown app";
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -128,7 +129,7 @@ export default function DeploymentsPage() {
|
||||
appNameByContainer.get(dep.container) ?? "Unknown app";
|
||||
|
||||
return (
|
||||
<div key={`${dep.image}-mobile`} className="p-4 space-y-3">
|
||||
<div key={`${dep.container}-mobile`} className="p-4 space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<Rocket className="w-4 h-4 text-muted-foreground" />
|
||||
|
||||
@@ -19,12 +19,18 @@ import { Application, Node, NodeLiveData } from "@/lib/types";
|
||||
import { getApplications } from "@/core/application";
|
||||
import { deployApp } from "@/lib/dockship/deploy";
|
||||
|
||||
export default function DeployAppDialog({ children }: { children: ReactNode }) {
|
||||
export default function DeployAppDialog({
|
||||
defaultApp,
|
||||
children,
|
||||
}: {
|
||||
defaultApp?: string;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const [nodeList, setNodeList] = useState<(NodeLiveData & Node)[]>([]);
|
||||
const [apps, setApps] = useState<Application[]>([]);
|
||||
|
||||
const [name, setName] = useState("");
|
||||
const [app, setApp] = useState("");
|
||||
const [app, setApp] = useState(defaultApp || "");
|
||||
const [node, setNode] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
+63
-32
@@ -5,6 +5,7 @@ import { Application, Deployment } from "@/lib/types";
|
||||
import { CPUusage } from "@/lib/server/calc";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import pLimit from "p-limit";
|
||||
|
||||
const docker = new Docker();
|
||||
|
||||
@@ -18,46 +19,76 @@ function sanitizeString(str: string): string {
|
||||
|
||||
export async function getDeployments(
|
||||
apps: Application[]
|
||||
): Promise<(null | Deployment)[]> {
|
||||
return await Promise.all(
|
||||
apps
|
||||
.map((app) => app.deployments)
|
||||
.flat()
|
||||
.map(async (deployId) => {
|
||||
const container = docker.getContainer(deployId);
|
||||
const containerInfo = await container.inspect();
|
||||
): Promise<Deployment[]> {
|
||||
// Limit concurrent Docker calls (adjust if needed)
|
||||
const limit = pLimit(5);
|
||||
|
||||
let cpu = 0;
|
||||
let memory = 0;
|
||||
let ports: Set<[number, number]> = new Set();
|
||||
const deploymentIds = apps.flatMap((app) => app.deployments);
|
||||
|
||||
if (containerInfo.State.Running) {
|
||||
const stats = await container.stats({ stream: false });
|
||||
memory += (stats.memory_stats.usage / stats.memory_stats.limit) * 100;
|
||||
cpu += CPUusage(stats);
|
||||
}
|
||||
const results = await Promise.all(
|
||||
deploymentIds.map((deployId) =>
|
||||
limit(async (): Promise<Deployment | null> => {
|
||||
try {
|
||||
const container = docker.getContainer(deployId);
|
||||
const containerInfo = await container.inspect();
|
||||
|
||||
let cpu = 0;
|
||||
let memory = 0;
|
||||
|
||||
// Use Map to deduplicate ports reliably
|
||||
const ports = new Map<number, number>();
|
||||
|
||||
if (containerInfo.State?.Running) {
|
||||
const stats = await container.stats({ stream: false });
|
||||
|
||||
const usage = stats.memory_stats?.usage ?? 0;
|
||||
const limitMem = stats.memory_stats?.limit ?? 0;
|
||||
|
||||
if (limitMem > 0) {
|
||||
memory = (usage / limitMem) * 100;
|
||||
}
|
||||
|
||||
cpu = CPUusage(stats);
|
||||
}
|
||||
|
||||
const portMappings = containerInfo.NetworkSettings?.Ports ?? {};
|
||||
|
||||
for (const [key, mappings] of Object.entries(portMappings)) {
|
||||
if (!mappings) continue;
|
||||
|
||||
const privatePort = Number.parseInt(key.split("/")[0], 10);
|
||||
|
||||
for (const [key, mappings] of Object.entries(
|
||||
containerInfo.NetworkSettings.Ports
|
||||
)) {
|
||||
const privatePort = parseInt(key.split("/")[0], 10);
|
||||
if (mappings) {
|
||||
for (const mapping of mappings) {
|
||||
const publicPort = parseInt(mapping.HostPort, 10);
|
||||
ports.add([publicPort, privatePort]);
|
||||
const publicPort = Number.parseInt(mapping.HostPort, 10);
|
||||
|
||||
if (!Number.isNaN(publicPort) && !Number.isNaN(privatePort)) {
|
||||
ports.set(publicPort, privatePort);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
image: containerInfo.Image,
|
||||
container: containerInfo.Id,
|
||||
cpu,
|
||||
memory,
|
||||
status: containerInfo.State.Running ? "running" : "stopped",
|
||||
ports: [...ports],
|
||||
};
|
||||
return {
|
||||
image: containerInfo.Image,
|
||||
container: containerInfo.Id,
|
||||
cpu,
|
||||
memory,
|
||||
status: containerInfo.State?.Running ? "running" : "stopped",
|
||||
ports: [...ports.entries()].map(
|
||||
([publicPort, privatePort]) =>
|
||||
[publicPort, privatePort] as [number, number]
|
||||
),
|
||||
};
|
||||
} catch {
|
||||
// Container may have stopped / been removed
|
||||
return null;
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
// Remove failed containers
|
||||
return results.filter(
|
||||
(deployment: any): deployment is Deployment => deployment !== null
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user