mirror of
https://github.com/andwati/dokploy.git
synced 2026-12-01 15:28:49 +00:00
refactor: enhance getContainerLogs function to support app name or ID
- Updated the `getContainerLogs` function to accept either an application name or container ID, improving flexibility in log retrieval. - Simplified the command execution logic by consolidating the remote and local execution paths. - Added a new parameter to directly use container IDs, streamlining the process for users. These changes enhance the usability of the logging feature, allowing for more efficient access to container logs.
This commit is contained in:
@@ -1168,6 +1168,7 @@ export const composeRouter = createTRPCRouter({
|
|||||||
input.since,
|
input.since,
|
||||||
input.search,
|
input.search,
|
||||||
compose.serverId,
|
compose.serverId,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -355,60 +355,45 @@ export const getContainersByAppLabel = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getContainerLogs = async (
|
export const getContainerLogs = async (
|
||||||
appName: string,
|
appNameOrId: string,
|
||||||
tail = 100,
|
tail = 100,
|
||||||
since = "all",
|
since = "all",
|
||||||
search?: string,
|
search?: string,
|
||||||
serverId?: string | null,
|
serverId?: string | null,
|
||||||
|
useContainerIdDirectly = false,
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
// First, find the real container ID by appName filter
|
const exec = (cmd: string) =>
|
||||||
const findCommand = `docker ps -q --filter "name=^${appName}" | head -1`;
|
serverId ? execAsyncRemote(serverId, cmd) : execAsync(cmd);
|
||||||
const findResult = serverId
|
|
||||||
? await execAsyncRemote(serverId, findCommand)
|
|
||||||
: await execAsync(findCommand);
|
|
||||||
|
|
||||||
|
let target = appNameOrId;
|
||||||
|
let isService = false;
|
||||||
|
|
||||||
|
if (!useContainerIdDirectly) {
|
||||||
|
// Find the real container ID by appName filter
|
||||||
|
const findResult = await exec(
|
||||||
|
`docker ps -q --filter "name=^${appNameOrId}" | head -1`,
|
||||||
|
);
|
||||||
const containerId = findResult.stdout.trim();
|
const containerId = findResult.stdout.trim();
|
||||||
|
|
||||||
if (!containerId) {
|
if (!containerId) {
|
||||||
// Fallback: try as a swarm service
|
// Fallback: try as a swarm service
|
||||||
const svcCommand = `docker service ls -q --filter "name=${appName}" | head -1`;
|
const svcResult = await exec(
|
||||||
const svcResult = serverId
|
`docker service ls -q --filter "name=${appNameOrId}" | head -1`,
|
||||||
? await execAsyncRemote(serverId, svcCommand)
|
);
|
||||||
: await execAsync(svcCommand);
|
|
||||||
|
|
||||||
const serviceId = svcResult.stdout.trim();
|
const serviceId = svcResult.stdout.trim();
|
||||||
if (!serviceId) {
|
if (!serviceId) {
|
||||||
throw new Error(`No container or service found for: ${appName}`);
|
throw new Error(`No container or service found for: ${appNameOrId}`);
|
||||||
}
|
}
|
||||||
|
isService = true;
|
||||||
// Use docker service logs for swarm
|
} else {
|
||||||
const sinceFlag = since === "all" ? "" : `--since ${since}`;
|
target = containerId;
|
||||||
const baseCommand = `docker service logs --timestamps --raw --tail ${tail} ${sinceFlag} ${appName}`;
|
|
||||||
const escapedSearch = search?.replace(/'/g, "'\\''") ?? "";
|
|
||||||
const command = search
|
|
||||||
? `${baseCommand} 2>&1 | grep -iF '${escapedSearch}'`
|
|
||||||
: `${baseCommand} 2>&1`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = serverId
|
|
||||||
? await execAsyncRemote(serverId, command)
|
|
||||||
: await execAsync(command);
|
|
||||||
return result.stdout;
|
|
||||||
} catch (error: unknown) {
|
|
||||||
if (
|
|
||||||
error &&
|
|
||||||
typeof error === "object" &&
|
|
||||||
"stdout" in error &&
|
|
||||||
typeof (error as { stdout: string }).stdout === "string" &&
|
|
||||||
(error as { stdout: string }).stdout.length > 0
|
|
||||||
) {
|
|
||||||
return (error as { stdout: string }).stdout;
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sinceFlag = since === "all" ? "" : `--since ${since}`;
|
const sinceFlag = since === "all" ? "" : `--since ${since}`;
|
||||||
const baseCommand = `docker container logs --timestamps --tail ${tail} ${sinceFlag} ${containerId}`;
|
const baseCommand = isService
|
||||||
|
? `docker service logs --timestamps --raw --tail ${tail} ${sinceFlag} ${target}`
|
||||||
|
: `docker container logs --timestamps --tail ${tail} ${sinceFlag} ${target}`;
|
||||||
|
|
||||||
const escapedSearch = search?.replace(/'/g, "'\\''") ?? "";
|
const escapedSearch = search?.replace(/'/g, "'\\''") ?? "";
|
||||||
const command = search
|
const command = search
|
||||||
@@ -416,10 +401,7 @@ export const getContainerLogs = async (
|
|||||||
: `${baseCommand} 2>&1`;
|
: `${baseCommand} 2>&1`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = serverId
|
const result = await exec(command);
|
||||||
? await execAsyncRemote(serverId, command)
|
|
||||||
: await execAsync(command);
|
|
||||||
|
|
||||||
return result.stdout;
|
return result.stdout;
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user