mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor(multi-server): delete server only if the server doesn't have associated services
This commit is contained in:
@@ -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 = () => {
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-1 md:grid-cols-1">
|
||||
{sshKeys?.length === 0 ? (
|
||||
{sshKeys?.length === 0 && data?.length === 0 ? (
|
||||
<div className="flex flex-col items-center gap-3 min-h-[25vh] justify-center">
|
||||
<KeyIcon className="size-8" />
|
||||
<span className="text-base text-muted-foreground">
|
||||
@@ -96,6 +97,7 @@ export const ShowServers = () => {
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{data?.map((server) => {
|
||||
const canDelete = server.totalSum === 0;
|
||||
return (
|
||||
<TableRow key={server.serverId}>
|
||||
<TableCell className="w-[100px]">{server.name}</TableCell>
|
||||
@@ -137,8 +139,26 @@ export const ShowServers = () => {
|
||||
<UpdateServer serverId={server.serverId} />
|
||||
<ShowServerActions serverId={server.serverId} />
|
||||
<DialogAction
|
||||
title={"Delete Server"}
|
||||
description="This will delete the server and all associated data"
|
||||
disabled={!canDelete}
|
||||
title={
|
||||
canDelete
|
||||
? "Delete Server"
|
||||
: "Server has active services"
|
||||
}
|
||||
description={
|
||||
canDelete ? (
|
||||
"This will delete the server and all associated data"
|
||||
) : (
|
||||
<div className="flex flex-col gap-2">
|
||||
You can not delete this server because it
|
||||
has active services.
|
||||
<AlertBlock type="warning">
|
||||
You have active services associated with
|
||||
this server, please delete them first.
|
||||
</AlertBlock>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
onClick={async () => {
|
||||
await mutateAsync({
|
||||
serverId: server.serverId,
|
||||
|
||||
@@ -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 (
|
||||
<AlertDialog>
|
||||
@@ -37,7 +39,9 @@ export const DialogAction = ({
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={onClick}>Confirm</AlertDialogAction>
|
||||
<AlertDialogAction disabled={disabled} onClick={onClick}>
|
||||
Confirm
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
|
||||
@@ -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<number>`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);
|
||||
|
||||
@@ -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<Server>,
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user