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 { ShowDockerContainersModal } from "./show-docker-containers-modal";
|
||||||
import { ShowTraefikFileSystemModal } from "./show-traefik-file-system-modal";
|
import { ShowTraefikFileSystemModal } from "./show-traefik-file-system-modal";
|
||||||
import { UpdateServer } from "./update-server";
|
import { UpdateServer } from "./update-server";
|
||||||
|
import { AlertBlock } from "@/components/shared/alert-block";
|
||||||
|
|
||||||
export const ShowServers = () => {
|
export const ShowServers = () => {
|
||||||
const { data, refetch } = api.server.all.useQuery();
|
const { data, refetch } = api.server.all.useQuery();
|
||||||
@@ -54,7 +55,7 @@ export const ShowServers = () => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="grid gap-4 sm:grid-cols-1 md:grid-cols-1">
|
<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">
|
<div className="flex flex-col items-center gap-3 min-h-[25vh] justify-center">
|
||||||
<KeyIcon className="size-8" />
|
<KeyIcon className="size-8" />
|
||||||
<span className="text-base text-muted-foreground">
|
<span className="text-base text-muted-foreground">
|
||||||
@@ -96,6 +97,7 @@ export const ShowServers = () => {
|
|||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{data?.map((server) => {
|
{data?.map((server) => {
|
||||||
|
const canDelete = server.totalSum === 0;
|
||||||
return (
|
return (
|
||||||
<TableRow key={server.serverId}>
|
<TableRow key={server.serverId}>
|
||||||
<TableCell className="w-[100px]">{server.name}</TableCell>
|
<TableCell className="w-[100px]">{server.name}</TableCell>
|
||||||
@@ -137,8 +139,26 @@ export const ShowServers = () => {
|
|||||||
<UpdateServer serverId={server.serverId} />
|
<UpdateServer serverId={server.serverId} />
|
||||||
<ShowServerActions serverId={server.serverId} />
|
<ShowServerActions serverId={server.serverId} />
|
||||||
<DialogAction
|
<DialogAction
|
||||||
title={"Delete Server"}
|
disabled={!canDelete}
|
||||||
description="This will delete the server and all associated data"
|
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 () => {
|
onClick={async () => {
|
||||||
await mutateAsync({
|
await mutateAsync({
|
||||||
serverId: server.serverId,
|
serverId: server.serverId,
|
||||||
|
|||||||
@@ -11,10 +11,11 @@ import {
|
|||||||
} from "@/components/ui/alert-dialog";
|
} from "@/components/ui/alert-dialog";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
title?: string;
|
title?: string | React.ReactNode;
|
||||||
description?: string;
|
description?: string | React.ReactNode;
|
||||||
onClick: () => void;
|
onClick: () => void;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
|
disabled?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DialogAction = ({
|
export const DialogAction = ({
|
||||||
@@ -22,6 +23,7 @@ export const DialogAction = ({
|
|||||||
children,
|
children,
|
||||||
description,
|
description,
|
||||||
title,
|
title,
|
||||||
|
disabled,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
return (
|
return (
|
||||||
<AlertDialog>
|
<AlertDialog>
|
||||||
@@ -37,7 +39,9 @@ export const DialogAction = ({
|
|||||||
</AlertDialogHeader>
|
</AlertDialogHeader>
|
||||||
<AlertDialogFooter>
|
<AlertDialogFooter>
|
||||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
<AlertDialogAction onClick={onClick}>Confirm</AlertDialogAction>
|
<AlertDialogAction disabled={disabled} onClick={onClick}>
|
||||||
|
Confirm
|
||||||
|
</AlertDialogAction>
|
||||||
</AlertDialogFooter>
|
</AlertDialogFooter>
|
||||||
</AlertDialogContent>
|
</AlertDialogContent>
|
||||||
</AlertDialog>
|
</AlertDialog>
|
||||||
|
|||||||
@@ -5,16 +5,24 @@ import {
|
|||||||
apiFindOneServer,
|
apiFindOneServer,
|
||||||
apiRemoveServer,
|
apiRemoveServer,
|
||||||
apiUpdateServer,
|
apiUpdateServer,
|
||||||
|
applications,
|
||||||
|
compose,
|
||||||
|
mariadb,
|
||||||
|
mongo,
|
||||||
|
mysql,
|
||||||
|
postgres,
|
||||||
|
redis,
|
||||||
server,
|
server,
|
||||||
} from "@/server/db/schema";
|
} from "@/server/db/schema";
|
||||||
import { serverSetup } from "@/server/setup/server-setup";
|
import { serverSetup } from "@/server/setup/server-setup";
|
||||||
import { TRPCError } from "@trpc/server";
|
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 { removeDeploymentsByServerId } from "../services/deployment";
|
||||||
import {
|
import {
|
||||||
createServer,
|
createServer,
|
||||||
deleteServer,
|
deleteServer,
|
||||||
findServerById,
|
findServerById,
|
||||||
|
haveActiveServices,
|
||||||
updateServerById,
|
updateServerById,
|
||||||
} from "../services/server";
|
} from "../services/server";
|
||||||
|
|
||||||
@@ -41,14 +49,32 @@ export const serverRouter = createTRPCRouter({
|
|||||||
return await findServerById(input.serverId);
|
return await findServerById(input.serverId);
|
||||||
}),
|
}),
|
||||||
all: protectedProcedure.query(async ({ ctx }) => {
|
all: protectedProcedure.query(async ({ ctx }) => {
|
||||||
return await db.query.server.findMany({
|
const result = await db
|
||||||
orderBy: desc(server.createdAt),
|
.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 }) => {
|
withSSHKey: protectedProcedure.query(async ({ input, ctx }) => {
|
||||||
return await db.query.server.findMany({
|
return await db.query.server.findMany({
|
||||||
orderBy: desc(server.createdAt),
|
orderBy: desc(server.createdAt),
|
||||||
where: isNotNull(server.sshKeyId),
|
where: and(
|
||||||
|
isNotNull(server.sshKeyId),
|
||||||
|
eq(server.adminId, ctx.user.adminId),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
setup: protectedProcedure
|
setup: protectedProcedure
|
||||||
@@ -69,6 +95,14 @@ export const serverRouter = createTRPCRouter({
|
|||||||
.input(apiRemoveServer)
|
.input(apiRemoveServer)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
try {
|
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);
|
const currentServer = await findServerById(input.serverId);
|
||||||
await removeDeploymentsByServerId(currentServer);
|
await removeDeploymentsByServerId(currentServer);
|
||||||
await deleteServer(input.serverId);
|
await deleteServer(input.serverId);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { db } from "@/server/db";
|
import { db } from "@/server/db";
|
||||||
import { type apiCreateServer, server } from "@/server/db/schema";
|
import { type apiCreateServer, server } from "@/server/db/schema";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { eq } from "drizzle-orm";
|
import { desc, eq } from "drizzle-orm";
|
||||||
|
|
||||||
export type Server = typeof server.$inferSelect;
|
export type Server = typeof server.$inferSelect;
|
||||||
|
|
||||||
@@ -45,6 +45,15 @@ export const findServerById = async (serverId: string) => {
|
|||||||
return currentServer;
|
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) => {
|
export const deleteServer = async (serverId: string) => {
|
||||||
const currentServer = await db
|
const currentServer = await db
|
||||||
.delete(server)
|
.delete(server)
|
||||||
@@ -55,6 +64,40 @@ export const deleteServer = async (serverId: string) => {
|
|||||||
return currentServer;
|
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 (
|
export const updateServerById = async (
|
||||||
serverId: string,
|
serverId: string,
|
||||||
serverData: Partial<Server>,
|
serverData: Partial<Server>,
|
||||||
|
|||||||
@@ -479,6 +479,9 @@ export const apiFindMonitoringStats = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdateApplication = createSchema.partial().extend({
|
export const apiUpdateApplication = createSchema
|
||||||
applicationId: z.string().min(1),
|
.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"),
|
type: z.enum(["fetch", "cache"]).optional().default("cache"),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const apiUpdateCompose = createSchema.partial().extend({
|
export const apiUpdateCompose = createSchema
|
||||||
composeId: z.string(),
|
.partial()
|
||||||
composeFile: z.string().optional(),
|
.extend({
|
||||||
command: z.string().optional(),
|
composeId: z.string(),
|
||||||
});
|
composeFile: z.string().optional(),
|
||||||
|
command: z.string().optional(),
|
||||||
|
})
|
||||||
|
.omit({ serverId: true });
|
||||||
|
|
||||||
export const apiRandomizeCompose = createSchema
|
export const apiRandomizeCompose = createSchema
|
||||||
.pick({
|
.pick({
|
||||||
|
|||||||
@@ -141,6 +141,9 @@ export const apiResetMariadb = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdateMariaDB = createSchema.partial().extend({
|
export const apiUpdateMariaDB = createSchema
|
||||||
mariadbId: z.string().min(1),
|
.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 { relations } from "drizzle-orm";
|
||||||
import { integer, pgTable, text } from "drizzle-orm/pg-core";
|
import { integer, pgTable, text } from "drizzle-orm/pg-core";
|
||||||
import { createInsertSchema } from "drizzle-zod";
|
import { createInsertSchema } from "drizzle-zod";
|
||||||
@@ -126,9 +125,12 @@ export const apiDeployMongo = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdateMongo = createSchema.partial().extend({
|
export const apiUpdateMongo = createSchema
|
||||||
mongoId: z.string().min(1),
|
.partial()
|
||||||
});
|
.extend({
|
||||||
|
mongoId: z.string().min(1),
|
||||||
|
})
|
||||||
|
.omit({ serverId: true });
|
||||||
|
|
||||||
export const apiResetMongo = createSchema
|
export const apiResetMongo = createSchema
|
||||||
.pick({
|
.pick({
|
||||||
|
|||||||
@@ -138,6 +138,9 @@ export const apiDeployMySql = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdateMySql = createSchema.partial().extend({
|
export const apiUpdateMySql = createSchema
|
||||||
mysqlId: z.string().min(1),
|
.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 { relations } from "drizzle-orm";
|
||||||
import { integer, pgTable, text } from "drizzle-orm/pg-core";
|
import { integer, pgTable, text } from "drizzle-orm/pg-core";
|
||||||
import { createInsertSchema } from "drizzle-zod";
|
import { createInsertSchema } from "drizzle-zod";
|
||||||
@@ -135,6 +134,9 @@ export const apiResetPostgres = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdatePostgres = createSchema.partial().extend({
|
export const apiUpdatePostgres = createSchema
|
||||||
postgresId: z.string().min(1),
|
.partial()
|
||||||
});
|
.extend({
|
||||||
|
postgresId: z.string().min(1),
|
||||||
|
})
|
||||||
|
.omit({ serverId: true });
|
||||||
|
|||||||
@@ -127,6 +127,9 @@ export const apiResetRedis = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdateRedis = createSchema.partial().extend({
|
export const apiUpdateRedis = createSchema
|
||||||
redisId: z.string().min(1),
|
.partial()
|
||||||
});
|
.extend({
|
||||||
|
redisId: z.string().min(1),
|
||||||
|
})
|
||||||
|
.omit({ serverId: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user