From 1a877340d393a09ccba390b1dabcb935d3649ca3 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 22 Sep 2024 11:56:31 -0600 Subject: [PATCH] refactor(multi-server): delete server only if the server doesn't have associated services --- .../settings/servers/show-servers.tsx | 26 +++++++++-- .../components/shared/dialog-action.tsx | 10 +++-- apps/dokploy/server/api/routers/server.ts | 44 +++++++++++++++--- apps/dokploy/server/api/services/server.ts | 45 ++++++++++++++++++- apps/dokploy/server/db/schema/application.ts | 9 ++-- apps/dokploy/server/db/schema/compose.ts | 13 +++--- apps/dokploy/server/db/schema/mariadb.ts | 9 ++-- apps/dokploy/server/db/schema/mongo.ts | 10 +++-- apps/dokploy/server/db/schema/mysql.ts | 9 ++-- apps/dokploy/server/db/schema/postgres.ts | 10 +++-- apps/dokploy/server/db/schema/redis.ts | 9 ++-- 11 files changed, 157 insertions(+), 37 deletions(-) diff --git a/apps/dokploy/components/dashboard/settings/servers/show-servers.tsx b/apps/dokploy/components/dashboard/settings/servers/show-servers.tsx index f1ac64c8..5b78065f 100644 --- a/apps/dokploy/components/dashboard/settings/servers/show-servers.tsx +++ b/apps/dokploy/components/dashboard/settings/servers/show-servers.tsx @@ -30,6 +30,7 @@ import { SetupServer } from "./setup-server"; import { ShowDockerContainersModal } from "./show-docker-containers-modal"; import { ShowTraefikFileSystemModal } from "./show-traefik-file-system-modal"; import { UpdateServer } from "./update-server"; +import { AlertBlock } from "@/components/shared/alert-block"; export const ShowServers = () => { const { data, refetch } = api.server.all.useQuery(); @@ -54,7 +55,7 @@ export const ShowServers = () => {
- {sshKeys?.length === 0 ? ( + {sshKeys?.length === 0 && data?.length === 0 ? (
@@ -96,6 +97,7 @@ export const ShowServers = () => { {data?.map((server) => { + const canDelete = server.totalSum === 0; return ( {server.name} @@ -137,8 +139,26 @@ export const ShowServers = () => { + You can not delete this server because it + has active services. + + You have active services associated with + this server, please delete them first. + +
+ ) + } onClick={async () => { await mutateAsync({ serverId: server.serverId, diff --git a/apps/dokploy/components/shared/dialog-action.tsx b/apps/dokploy/components/shared/dialog-action.tsx index 8b6f2847..d3c59693 100644 --- a/apps/dokploy/components/shared/dialog-action.tsx +++ b/apps/dokploy/components/shared/dialog-action.tsx @@ -11,10 +11,11 @@ import { } from "@/components/ui/alert-dialog"; interface Props { - title?: string; - description?: string; + title?: string | React.ReactNode; + description?: string | React.ReactNode; onClick: () => void; children?: React.ReactNode; + disabled?: boolean; } export const DialogAction = ({ @@ -22,6 +23,7 @@ export const DialogAction = ({ children, description, title, + disabled, }: Props) => { return ( @@ -37,7 +39,9 @@ export const DialogAction = ({ Cancel - Confirm + + Confirm + diff --git a/apps/dokploy/server/api/routers/server.ts b/apps/dokploy/server/api/routers/server.ts index 1bdb373e..9ec0bfa4 100644 --- a/apps/dokploy/server/api/routers/server.ts +++ b/apps/dokploy/server/api/routers/server.ts @@ -5,16 +5,24 @@ import { apiFindOneServer, apiRemoveServer, apiUpdateServer, + applications, + compose, + mariadb, + mongo, + mysql, + postgres, + redis, server, } from "@/server/db/schema"; import { serverSetup } from "@/server/setup/server-setup"; import { TRPCError } from "@trpc/server"; -import { desc, isNotNull } from "drizzle-orm"; +import { and, desc, eq, getTableColumns, isNotNull, sql } from "drizzle-orm"; import { removeDeploymentsByServerId } from "../services/deployment"; import { createServer, deleteServer, findServerById, + haveActiveServices, updateServerById, } from "../services/server"; @@ -41,14 +49,32 @@ export const serverRouter = createTRPCRouter({ return await findServerById(input.serverId); }), all: protectedProcedure.query(async ({ ctx }) => { - return await db.query.server.findMany({ - orderBy: desc(server.createdAt), - }); + const result = await db + .select({ + ...getTableColumns(server), + totalSum: sql`cast(count(${applications.applicationId}) + count(${compose.composeId}) + count(${redis.redisId}) + count(${mariadb.mariadbId}) + count(${mongo.mongoId}) + count(${mysql.mysqlId}) + count(${postgres.postgresId}) as integer)`, + }) + .from(server) + .leftJoin(applications, eq(applications.serverId, server.serverId)) + .leftJoin(compose, eq(compose.serverId, server.serverId)) + .leftJoin(redis, eq(redis.serverId, server.serverId)) + .leftJoin(mariadb, eq(mariadb.serverId, server.serverId)) + .leftJoin(mongo, eq(mongo.serverId, server.serverId)) + .leftJoin(mysql, eq(mysql.serverId, server.serverId)) + .leftJoin(postgres, eq(postgres.serverId, server.serverId)) + .where(eq(server.adminId, ctx.user.adminId)) + .orderBy(desc(server.createdAt)) + .groupBy(server.serverId); + + return result; }), withSSHKey: protectedProcedure.query(async ({ input, ctx }) => { return await db.query.server.findMany({ orderBy: desc(server.createdAt), - where: isNotNull(server.sshKeyId), + where: and( + isNotNull(server.sshKeyId), + eq(server.adminId, ctx.user.adminId), + ), }); }), setup: protectedProcedure @@ -69,6 +95,14 @@ export const serverRouter = createTRPCRouter({ .input(apiRemoveServer) .mutation(async ({ input, ctx }) => { try { + const activeServers = await haveActiveServices(input.serverId); + + if (activeServers) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Server has active services, please delete them first", + }); + } const currentServer = await findServerById(input.serverId); await removeDeploymentsByServerId(currentServer); await deleteServer(input.serverId); diff --git a/apps/dokploy/server/api/services/server.ts b/apps/dokploy/server/api/services/server.ts index 58df6643..4cd79cdc 100644 --- a/apps/dokploy/server/api/services/server.ts +++ b/apps/dokploy/server/api/services/server.ts @@ -1,7 +1,7 @@ import { db } from "@/server/db"; import { type apiCreateServer, server } from "@/server/db/schema"; import { TRPCError } from "@trpc/server"; -import { eq } from "drizzle-orm"; +import { desc, eq } from "drizzle-orm"; export type Server = typeof server.$inferSelect; @@ -45,6 +45,15 @@ export const findServerById = async (serverId: string) => { return currentServer; }; +export const findServersByAdminId = async (adminId: string) => { + const servers = await db.query.server.findMany({ + where: eq(server.adminId, adminId), + orderBy: desc(server.createdAt), + }); + + return servers; +}; + export const deleteServer = async (serverId: string) => { const currentServer = await db .delete(server) @@ -55,6 +64,40 @@ export const deleteServer = async (serverId: string) => { return currentServer; }; +export const haveActiveServices = async (serverId: string) => { + const currentServer = await db.query.server.findFirst({ + where: eq(server.serverId, serverId), + with: { + applications: true, + compose: true, + redis: true, + mariadb: true, + mongo: true, + mysql: true, + postgres: true, + }, + }); + + if (!currentServer) { + return false; + } + + const total = + currentServer?.applications?.length + + currentServer?.compose?.length + + currentServer?.redis?.length + + currentServer?.mariadb?.length + + currentServer?.mongo?.length + + currentServer?.mysql?.length + + currentServer?.postgres?.length; + + if (total === 0) { + return false; + } + + return true; +}; + export const updateServerById = async ( serverId: string, serverData: Partial, diff --git a/apps/dokploy/server/db/schema/application.ts b/apps/dokploy/server/db/schema/application.ts index 4a479d95..09175edc 100644 --- a/apps/dokploy/server/db/schema/application.ts +++ b/apps/dokploy/server/db/schema/application.ts @@ -479,6 +479,9 @@ export const apiFindMonitoringStats = createSchema }) .required(); -export const apiUpdateApplication = createSchema.partial().extend({ - applicationId: z.string().min(1), -}); +export const apiUpdateApplication = createSchema + .partial() + .extend({ + applicationId: z.string().min(1), + }) + .omit({ serverId: true }); diff --git a/apps/dokploy/server/db/schema/compose.ts b/apps/dokploy/server/db/schema/compose.ts index ec376606..b1517161 100644 --- a/apps/dokploy/server/db/schema/compose.ts +++ b/apps/dokploy/server/db/schema/compose.ts @@ -157,11 +157,14 @@ export const apiFetchServices = z.object({ type: z.enum(["fetch", "cache"]).optional().default("cache"), }); -export const apiUpdateCompose = createSchema.partial().extend({ - composeId: z.string(), - composeFile: z.string().optional(), - command: z.string().optional(), -}); +export const apiUpdateCompose = createSchema + .partial() + .extend({ + composeId: z.string(), + composeFile: z.string().optional(), + command: z.string().optional(), + }) + .omit({ serverId: true }); export const apiRandomizeCompose = createSchema .pick({ diff --git a/apps/dokploy/server/db/schema/mariadb.ts b/apps/dokploy/server/db/schema/mariadb.ts index 3bc37185..b5e13284 100644 --- a/apps/dokploy/server/db/schema/mariadb.ts +++ b/apps/dokploy/server/db/schema/mariadb.ts @@ -141,6 +141,9 @@ export const apiResetMariadb = createSchema }) .required(); -export const apiUpdateMariaDB = createSchema.partial().extend({ - mariadbId: z.string().min(1), -}); +export const apiUpdateMariaDB = createSchema + .partial() + .extend({ + mariadbId: z.string().min(1), + }) + .omit({ serverId: true }); diff --git a/apps/dokploy/server/db/schema/mongo.ts b/apps/dokploy/server/db/schema/mongo.ts index 8c8af166..757ba9c9 100644 --- a/apps/dokploy/server/db/schema/mongo.ts +++ b/apps/dokploy/server/db/schema/mongo.ts @@ -1,4 +1,3 @@ -import { generatePassword } from "@/templates/utils"; import { relations } from "drizzle-orm"; import { integer, pgTable, text } from "drizzle-orm/pg-core"; import { createInsertSchema } from "drizzle-zod"; @@ -126,9 +125,12 @@ export const apiDeployMongo = createSchema }) .required(); -export const apiUpdateMongo = createSchema.partial().extend({ - mongoId: z.string().min(1), -}); +export const apiUpdateMongo = createSchema + .partial() + .extend({ + mongoId: z.string().min(1), + }) + .omit({ serverId: true }); export const apiResetMongo = createSchema .pick({ diff --git a/apps/dokploy/server/db/schema/mysql.ts b/apps/dokploy/server/db/schema/mysql.ts index d4c11955..b5352069 100644 --- a/apps/dokploy/server/db/schema/mysql.ts +++ b/apps/dokploy/server/db/schema/mysql.ts @@ -138,6 +138,9 @@ export const apiDeployMySql = createSchema }) .required(); -export const apiUpdateMySql = createSchema.partial().extend({ - mysqlId: z.string().min(1), -}); +export const apiUpdateMySql = createSchema + .partial() + .extend({ + mysqlId: z.string().min(1), + }) + .omit({ serverId: true }); diff --git a/apps/dokploy/server/db/schema/postgres.ts b/apps/dokploy/server/db/schema/postgres.ts index 7d784404..c751a681 100644 --- a/apps/dokploy/server/db/schema/postgres.ts +++ b/apps/dokploy/server/db/schema/postgres.ts @@ -1,4 +1,3 @@ -import { generatePassword } from "@/templates/utils"; import { relations } from "drizzle-orm"; import { integer, pgTable, text } from "drizzle-orm/pg-core"; import { createInsertSchema } from "drizzle-zod"; @@ -135,6 +134,9 @@ export const apiResetPostgres = createSchema }) .required(); -export const apiUpdatePostgres = createSchema.partial().extend({ - postgresId: z.string().min(1), -}); +export const apiUpdatePostgres = createSchema + .partial() + .extend({ + postgresId: z.string().min(1), + }) + .omit({ serverId: true }); diff --git a/apps/dokploy/server/db/schema/redis.ts b/apps/dokploy/server/db/schema/redis.ts index 1fc64697..b27bbc49 100644 --- a/apps/dokploy/server/db/schema/redis.ts +++ b/apps/dokploy/server/db/schema/redis.ts @@ -127,6 +127,9 @@ export const apiResetRedis = createSchema }) .required(); -export const apiUpdateRedis = createSchema.partial().extend({ - redisId: z.string().min(1), -}); +export const apiUpdateRedis = createSchema + .partial() + .extend({ + redisId: z.string().min(1), + }) + .omit({ serverId: true });