feat(multi-server): add docker containers view to servers

This commit is contained in:
Mauricio Siu
2024-09-21 15:16:15 -06:00
parent 698ff9e918
commit c03c154fc4
11 changed files with 344 additions and 20 deletions

View File

@@ -9,9 +9,15 @@ import {
import { createTRPCRouter, protectedProcedure } from "../trpc";
export const dockerRouter = createTRPCRouter({
getContainers: protectedProcedure.query(async () => {
return await getContainers();
}),
getContainers: protectedProcedure
.input(
z.object({
serverId: z.string().optional(),
}),
)
.query(async ({ input }) => {
return await getContainers(input.serverId);
}),
restartContainer: protectedProcedure
.input(
@@ -27,10 +33,11 @@ export const dockerRouter = createTRPCRouter({
.input(
z.object({
containerId: z.string().min(1),
serverId: z.string().optional(),
}),
)
.query(async ({ input }) => {
return await getConfig(input.containerId);
return await getConfig(input.containerId, input.serverId);
}),
getContainersByAppNameMatch: protectedProcedure

View File

@@ -1,11 +1,22 @@
import { execAsync, execAsyncRemote } from "@/server/utils/process/execAsync";
export const getContainers = async () => {
export const getContainers = async (serverId?: string | null) => {
try {
const { stdout, stderr } = await execAsync(
"docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | Image: {{.Image}} | Ports: {{.Ports}} | State: {{.State}} | Status: {{.Status}}'",
);
const command =
"docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | Image: {{.Image}} | Ports: {{.Ports}} | State: {{.State}} | Status: {{.Status}}'";
let stdout = "";
let stderr = "";
if (serverId) {
const result = await execAsyncRemote(serverId, command);
stdout = result.stdout;
stderr = result.stderr;
} else {
const result = await execAsync(command);
stdout = result.stdout;
stderr = result.stderr;
}
if (stderr) {
console.error(`Error: ${stderr}`);
return;
@@ -41,6 +52,7 @@ export const getContainers = async () => {
ports,
state,
status,
serverId,
};
})
.filter((container) => !container.name.includes("dokploy"));
@@ -49,11 +61,23 @@ export const getContainers = async () => {
} catch (error) {}
};
export const getConfig = async (containerId: string) => {
export const getConfig = async (
containerId: string,
serverId?: string | null,
) => {
try {
const { stdout, stderr } = await execAsync(
`docker inspect ${containerId} --format='{{json .}}'`,
);
const command = `docker inspect ${containerId} --format='{{json .}}'`;
let stdout = "";
let stderr = "";
if (serverId) {
const result = await execAsyncRemote(serverId, command);
stdout = result.stdout;
stderr = result.stderr;
} else {
const result = await execAsync(command);
stdout = result.stdout;
stderr = result.stderr;
}
if (stderr) {
console.error(`Error: ${stderr}`);