feat: add logs for each application

This commit is contained in:
Mauricio Siu
2024-09-08 23:11:39 -06:00
parent 6007427a6c
commit ea5349c844
14 changed files with 193 additions and 53 deletions

View File

@@ -30,12 +30,14 @@ export const DockerLogs = dynamic(
interface Props { interface Props {
appName: string; appName: string;
serverId?: string;
} }
export const ShowDockerLogs = ({ appName }: Props) => { export const ShowDockerLogs = ({ appName, serverId }: Props) => {
const { data } = api.docker.getContainersByAppNameMatch.useQuery( const { data } = api.docker.getContainersByAppNameMatch.useQuery(
{ {
appName, appName,
serverId,
}, },
{ {
enabled: !!appName, enabled: !!appName,
@@ -79,6 +81,7 @@ export const ShowDockerLogs = ({ appName }: Props) => {
</SelectContent> </SelectContent>
</Select> </Select>
<DockerLogs <DockerLogs
serverId={serverId || ""}
id="terminal" id="terminal"
containerId={containerId || "select-a-container"} containerId={containerId || "select-a-container"}
/> />

View File

@@ -30,14 +30,20 @@ export const DockerLogs = dynamic(
interface Props { interface Props {
appName: string; appName: string;
serverId?: string;
appType: "stack" | "docker-compose"; appType: "stack" | "docker-compose";
} }
export const ShowDockerLogsCompose = ({ appName, appType }: Props) => { export const ShowDockerLogsCompose = ({
appName,
appType,
serverId,
}: Props) => {
const { data } = api.docker.getContainersByAppNameMatch.useQuery( const { data } = api.docker.getContainersByAppNameMatch.useQuery(
{ {
appName, appName,
appType, appType,
serverId,
}, },
{ {
enabled: !!appName, enabled: !!appName,
@@ -81,6 +87,7 @@ export const ShowDockerLogsCompose = ({ appName, appType }: Props) => {
</SelectContent> </SelectContent>
</Select> </Select>
<DockerLogs <DockerLogs
serverId={serverId || ""}
id="terminal" id="terminal"
containerId={containerId || "select-a-container"} containerId={containerId || "select-a-container"}
/> />

View File

@@ -23,17 +23,20 @@ import { DockerMonitoring } from "../../monitoring/docker/show";
interface Props { interface Props {
appName: string; appName: string;
serverId?: string;
appType: "stack" | "docker-compose"; appType: "stack" | "docker-compose";
} }
export const ShowMonitoringCompose = ({ export const ShowMonitoringCompose = ({
appName, appName,
appType = "stack", appType = "stack",
serverId,
}: Props) => { }: Props) => {
const { data } = api.docker.getContainersByAppNameMatch.useQuery( const { data } = api.docker.getContainersByAppNameMatch.useQuery(
{ {
appName: appName, appName: appName,
appType, appType,
serverId,
}, },
{ {
enabled: !!appName, enabled: !!appName,
@@ -108,6 +111,7 @@ export const ShowMonitoringCompose = ({
</Button> </Button>
</div> </div>
<DockerMonitoring <DockerMonitoring
serverId={serverId || ""}
appName={containerAppName || ""} appName={containerAppName || ""}
appType={appType} appType={appType}
/> />

View File

@@ -8,9 +8,14 @@ import "@xterm/xterm/css/xterm.css";
interface Props { interface Props {
id: string; id: string;
containerId: string; containerId: string;
serverId?: string;
} }
export const DockerLogsId: React.FC<Props> = ({ id, containerId }) => { export const DockerLogsId: React.FC<Props> = ({
id,
containerId,
serverId,
}) => {
const [term, setTerm] = React.useState<Terminal>(); const [term, setTerm] = React.useState<Terminal>();
const [lines, setLines] = React.useState<number>(40); const [lines, setLines] = React.useState<number>(40);
@@ -38,7 +43,7 @@ export const DockerLogsId: React.FC<Props> = ({ id, containerId }) => {
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?containerId=${containerId}&tail=${lines}`; const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?containerId=${containerId}&tail=${lines}&serverId=${serverId}`;
const ws = new WebSocket(wsUrl); const ws = new WebSocket(wsUrl);
const fitAddon = new FitAddon(); const fitAddon = new FitAddon();

View File

@@ -159,7 +159,10 @@ const Service = (
</TabsContent> </TabsContent>
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogs appName={data?.appName || ""} /> <ShowDockerLogs
appName={data?.appName || ""}
serverId={data?.serverId || ""}
/>
</div> </div>
</TabsContent> </TabsContent>
<TabsContent value="deployments" className="w-full"> <TabsContent value="deployments" className="w-full">

View File

@@ -151,6 +151,7 @@ const Service = (
<TabsContent value="monitoring"> <TabsContent value="monitoring">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowMonitoringCompose <ShowMonitoringCompose
serverId={data?.serverId || ""}
appName={data?.appName || ""} appName={data?.appName || ""}
appType={data?.composeType || "docker-compose"} appType={data?.composeType || "docker-compose"}
/> />
@@ -160,6 +161,7 @@ const Service = (
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogsCompose <ShowDockerLogsCompose
serverId={data?.serverId || ""}
appName={data?.appName || ""} appName={data?.appName || ""}
appType={data?.composeType || "docker-compose"} appType={data?.composeType || "docker-compose"}
/> />

View File

@@ -143,7 +143,10 @@ const Mariadb = (
</TabsContent> </TabsContent>
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogs appName={data?.appName || ""} /> <ShowDockerLogs
serverId={data?.serverId || ""}
appName={data?.appName || ""}
/>
</div> </div>
</TabsContent> </TabsContent>
<TabsContent value="backups"> <TabsContent value="backups">

View File

@@ -145,7 +145,10 @@ const Mongo = (
</TabsContent> </TabsContent>
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogs appName={data?.appName || ""} /> <ShowDockerLogs
serverId={data?.serverId || ""}
appName={data?.appName || ""}
/>
</div> </div>
</TabsContent> </TabsContent>
<TabsContent value="backups"> <TabsContent value="backups">

View File

@@ -144,7 +144,10 @@ const MySql = (
</TabsContent> </TabsContent>
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogs appName={data?.appName || ""} /> <ShowDockerLogs
serverId={data?.serverId || ""}
appName={data?.appName || ""}
/>
</div> </div>
</TabsContent> </TabsContent>
<TabsContent value="backups"> <TabsContent value="backups">

View File

@@ -145,7 +145,10 @@ const Postgresql = (
</TabsContent> </TabsContent>
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogs appName={data?.appName || ""} /> <ShowDockerLogs
serverId={data?.serverId || ""}
appName={data?.appName || ""}
/>
</div> </div>
</TabsContent> </TabsContent>
<TabsContent value="backups"> <TabsContent value="backups">

View File

@@ -143,7 +143,10 @@ const Redis = (
</TabsContent> </TabsContent>
<TabsContent value="logs"> <TabsContent value="logs">
<div className="flex flex-col gap-4 pt-2.5"> <div className="flex flex-col gap-4 pt-2.5">
<ShowDockerLogs appName={data?.appName || ""} /> <ShowDockerLogs
serverId={data?.serverId || ""}
appName={data?.appName || ""}
/>
</div> </div>
</TabsContent> </TabsContent>
<TabsContent value="advanced"> <TabsContent value="advanced">

View File

@@ -40,10 +40,15 @@ export const dockerRouter = createTRPCRouter({
.union([z.literal("stack"), z.literal("docker-compose")]) .union([z.literal("stack"), z.literal("docker-compose")])
.optional(), .optional(),
appName: z.string().min(1), appName: z.string().min(1),
serverId: z.string().optional(),
}), }),
) )
.query(async ({ input }) => { .query(async ({ input }) => {
return await getContainersByAppNameMatch(input.appName, input.appType); return await getContainersByAppNameMatch(
input.appName,
input.appType,
input.serverId,
);
}), }),
getContainersByAppLabel: protectedProcedure getContainersByAppLabel: protectedProcedure

View File

@@ -1,4 +1,9 @@
import { readSSHKey } from "@/server/utils/filesystem/ssh";
import { execAsync } from "@/server/utils/process/execAsync"; import { execAsync } from "@/server/utils/process/execAsync";
import { tail } from "lodash";
import { stderr, stdout } from "node:process";
import { Client } from "ssh2";
import { findServerById } from "./server";
export const getContainers = async () => { export const getContainers = async () => {
try { try {
@@ -69,25 +74,65 @@ export const getConfig = async (containerId: string) => {
export const getContainersByAppNameMatch = async ( export const getContainersByAppNameMatch = async (
appName: string, appName: string,
appType?: "stack" | "docker-compose", appType?: "stack" | "docker-compose",
serverId?: string,
) => { ) => {
try { try {
let result: string[] = [];
const cmd = const cmd =
"docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | State: {{.State}}'"; "docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | State: {{.State}}'";
const { stdout, stderr } = await execAsync( const command =
appType === "docker-compose" appType === "docker-compose"
? `${cmd} --filter='label=com.docker.compose.project=${appName}'` ? `${cmd} --filter='label=com.docker.compose.project=${appName}'`
: `${cmd} | grep ${appName}`, : `${cmd} | grep ${appName}`;
); if (serverId) {
const server = await findServerById(serverId);
if (stderr) { if (!server.sshKeyId) return;
return []; const keys = await readSSHKey(server.sshKeyId);
const client = new Client();
result = await new Promise<string[]>((resolve, reject) => {
let output = "";
client
.on("ready", () => {
client.exec(command, (err, stream) => {
if (err) {
console.error("Execution error:", err);
reject(err);
return;
}
stream
.on("close", () => {
client.end();
resolve(output.trim().split("\n"));
})
.on("data", (data: string) => {
output += data.toString();
})
.stderr.on("data", (data) => {});
});
})
.connect({
host: server.ipAddress,
port: server.port,
username: server.username,
privateKey: keys.privateKey,
timeout: 99999,
});
});
} else {
const { stdout, stderr } = await execAsync(command);
if (stderr) {
return [];
}
if (!stdout) return [];
result = stdout.trim().split("\n");
} }
if (!stdout) return []; const containers = result.map((line) => {
const lines = stdout.trim().split("\n");
const containers = lines.map((line) => {
const parts = line.split(" | "); const parts = line.split(" | ");
const containerId = parts[0] const containerId = parts[0]
? parts[0].replace("CONTAINER ID : ", "").trim() ? parts[0].replace("CONTAINER ID : ", "").trim()

View File

@@ -3,6 +3,9 @@ import { spawn } from "node-pty";
import { WebSocketServer } from "ws"; import { WebSocketServer } from "ws";
import { validateWebSocketRequest } from "../auth/auth"; import { validateWebSocketRequest } from "../auth/auth";
import { getShell } from "./utils"; import { getShell } from "./utils";
import { Client } from "ssh2";
import { findServerById } from "../api/services/server";
import { readSSHKey } from "../utils/filesystem/ssh";
export const setupDockerContainerLogsWebSocketServer = ( export const setupDockerContainerLogsWebSocketServer = (
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>, server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
@@ -30,6 +33,7 @@ export const setupDockerContainerLogsWebSocketServer = (
const url = new URL(req.url || "", `http://${req.headers.host}`); const url = new URL(req.url || "", `http://${req.headers.host}`);
const containerId = url.searchParams.get("containerId"); const containerId = url.searchParams.get("containerId");
const tail = url.searchParams.get("tail"); const tail = url.searchParams.get("tail");
const serverId = url.searchParams.get("serverId");
const { user, session } = await validateWebSocketRequest(req); const { user, session } = await validateWebSocketRequest(req);
if (!containerId) { if (!containerId) {
@@ -42,41 +46,88 @@ export const setupDockerContainerLogsWebSocketServer = (
return; return;
} }
try { try {
const shell = getShell(); if (serverId) {
const ptyProcess = spawn( const server = await findServerById(serverId);
shell,
["-c", `docker container logs --tail ${tail} --follow ${containerId}`],
{
name: "xterm-256color",
cwd: process.env.HOME,
env: process.env,
encoding: "utf8",
cols: 80,
rows: 30,
},
);
ptyProcess.onData((data) => { if (!server.sshKeyId) return;
ws.send(data); const keys = await readSSHKey(server.sshKeyId);
}); const client = new Client();
ws.on("close", () => { new Promise<void>((resolve, reject) => {
ptyProcess.kill(); client
}); .on("ready", () => {
ws.on("message", (message) => { const command = `
try { bash -c "docker container logs --tail ${tail} --follow ${containerId}"
let command: string | Buffer[] | Buffer | ArrayBuffer; `;
if (Buffer.isBuffer(message)) { client.exec(command, (err, stream) => {
command = message.toString("utf8"); if (err) {
} else { console.error("Execution error:", err);
command = message; reject(err);
return;
}
stream
.on("close", () => {
console.log("Connection closed ✅");
client.end();
resolve();
})
.on("data", (data: string) => {
ws.send(data.toString());
// console.log(`OUTPUT: ${data.toString()}`);
})
.stderr.on("data", (data) => {
ws.send(data.toString());
// console.error(`STDERR: ${data.toString()}`);
});
});
})
.connect({
host: server.ipAddress,
port: server.port,
username: server.username,
privateKey: keys.privateKey,
timeout: 99999,
});
});
} else {
const shell = getShell();
const ptyProcess = spawn(
shell,
[
"-c",
`docker container logs --tail ${tail} --follow ${containerId}`,
],
{
name: "xterm-256color",
cwd: process.env.HOME,
env: process.env,
encoding: "utf8",
cols: 80,
rows: 30,
},
);
ptyProcess.onData((data) => {
ws.send(data);
});
ws.on("close", () => {
ptyProcess.kill();
});
ws.on("message", (message) => {
try {
let command: string | Buffer[] | Buffer | ArrayBuffer;
if (Buffer.isBuffer(message)) {
command = message.toString("utf8");
} else {
command = message;
}
ptyProcess.write(command.toString());
} catch (error) {
// @ts-ignore
const errorMessage = error?.message as unknown as string;
ws.send(errorMessage);
} }
ptyProcess.write(command.toString()); });
} catch (error) { }
// @ts-ignore
const errorMessage = error?.message as unknown as string;
ws.send(errorMessage);
}
});
} catch (error) { } catch (error) {
// @ts-ignore // @ts-ignore
const errorMessage = error?.message as unknown as string; const errorMessage = error?.message as unknown as string;