mirror of
https://github.com/andwati/dokploy.git
synced 2026-12-02 07:48:49 +00:00
refactor(patch): unify patch retrieval and application logic
- Consolidated patch retrieval for applications and composes into a single query method, improving code clarity and reducing redundancy. - Updated the ShowPatches component to utilize the new unified query, simplifying data fetching logic. - Refactored patch application commands to streamline the process for both application and compose types, enhancing maintainability and consistency across the codebase.
This commit is contained in:
@@ -33,22 +33,8 @@ export const ShowPatches = ({ id, type }: Props) => {
|
|||||||
|
|
||||||
const utils = api.useUtils();
|
const utils = api.useUtils();
|
||||||
|
|
||||||
const queryMap = {
|
const { data: patches, isLoading: isPatchesLoading } =
|
||||||
application: () =>
|
api.patch.byEntityId.useQuery({ id, type }, { enabled: !!id });
|
||||||
api.patch.byApplicationId.useQuery(
|
|
||||||
{ applicationId: id },
|
|
||||||
{ enabled: !!id },
|
|
||||||
),
|
|
||||||
compose: () =>
|
|
||||||
api.patch.byComposeId.useQuery({ composeId: id }, { enabled: !!id }),
|
|
||||||
};
|
|
||||||
|
|
||||||
const { data: patches, isLoading: isPatchesLoading } = queryMap[type]
|
|
||||||
? queryMap[type]()
|
|
||||||
: api.patch.byApplicationId.useQuery(
|
|
||||||
{ applicationId: id },
|
|
||||||
{ enabled: !!id },
|
|
||||||
);
|
|
||||||
|
|
||||||
const mutationMap = {
|
const mutationMap = {
|
||||||
application: () => api.patch.delete.useMutation(),
|
application: () => api.patch.delete.useMutation(),
|
||||||
@@ -166,11 +152,9 @@ export const ShowPatches = ({ id, type }: Props) => {
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("Patch updated");
|
toast.success("Patch updated");
|
||||||
utils.patch.byApplicationId.invalidate({
|
utils.patch.byEntityId.invalidate({
|
||||||
applicationId: id,
|
id,
|
||||||
});
|
type,
|
||||||
utils.patch.byComposeId.invalidate({
|
|
||||||
composeId: id,
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@@ -190,11 +174,9 @@ export const ShowPatches = ({ id, type }: Props) => {
|
|||||||
mutateAsync({ patchId: patch.patchId })
|
mutateAsync({ patchId: patch.patchId })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("Patch deleted");
|
toast.success("Patch deleted");
|
||||||
utils.patch.byApplicationId.invalidate({
|
utils.patch.byEntityId.invalidate({
|
||||||
applicationId: id,
|
id,
|
||||||
});
|
type,
|
||||||
utils.patch.byComposeId.invalidate({
|
|
||||||
composeId: id,
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ import {
|
|||||||
findComposeById,
|
findComposeById,
|
||||||
findPatchByFilePath,
|
findPatchByFilePath,
|
||||||
findPatchById,
|
findPatchById,
|
||||||
findPatchesByApplicationId,
|
findPatchesByEntityId,
|
||||||
findPatchesByComposeId,
|
|
||||||
generatePatch,
|
|
||||||
readPatchRepoDirectory,
|
readPatchRepoDirectory,
|
||||||
readPatchRepoFile,
|
readPatchRepoFile,
|
||||||
updatePatch,
|
updatePatch,
|
||||||
@@ -26,8 +24,6 @@ import {
|
|||||||
apiCreatePatch,
|
apiCreatePatch,
|
||||||
apiDeletePatch,
|
apiDeletePatch,
|
||||||
apiFindPatch,
|
apiFindPatch,
|
||||||
apiFindPatchesByApplicationId,
|
|
||||||
apiFindPatchesByComposeId,
|
|
||||||
apiTogglePatchEnabled,
|
apiTogglePatchEnabled,
|
||||||
apiUpdatePatch,
|
apiUpdatePatch,
|
||||||
} from "@/server/db/schema";
|
} from "@/server/db/schema";
|
||||||
@@ -77,38 +73,37 @@ export const patchRouter = createTRPCRouter({
|
|||||||
return await findPatchById(input.patchId);
|
return await findPatchById(input.patchId);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
byApplicationId: protectedProcedure
|
byEntityId: protectedProcedure
|
||||||
.input(apiFindPatchesByApplicationId)
|
.input(
|
||||||
|
z.object({ id: z.string(), type: z.enum(["application", "compose"]) }),
|
||||||
|
)
|
||||||
.query(async ({ input, ctx }) => {
|
.query(async ({ input, ctx }) => {
|
||||||
const app = await findApplicationById(input.applicationId);
|
if (input.type === "application") {
|
||||||
if (
|
const app = await findApplicationById(input.id);
|
||||||
app.environment.project.organizationId !==
|
if (
|
||||||
ctx.session.activeOrganizationId
|
app.environment.project.organizationId !==
|
||||||
) {
|
ctx.session.activeOrganizationId
|
||||||
throw new TRPCError({
|
) {
|
||||||
code: "UNAUTHORIZED",
|
throw new TRPCError({
|
||||||
message: "You are not authorized to access this application",
|
code: "UNAUTHORIZED",
|
||||||
});
|
message: "You are not authorized to access this application",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (input.type === "compose") {
|
||||||
|
const compose = await findComposeById(input.id);
|
||||||
|
if (
|
||||||
|
compose.environment.project.organizationId !==
|
||||||
|
ctx.session.activeOrganizationId
|
||||||
|
) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You are not authorized to access this compose",
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
const result = await findPatchesByEntityId(input.id, input.type);
|
||||||
|
|
||||||
return await findPatchesByApplicationId(input.applicationId);
|
return result;
|
||||||
}),
|
|
||||||
|
|
||||||
byComposeId: protectedProcedure
|
|
||||||
.input(apiFindPatchesByComposeId)
|
|
||||||
.query(async ({ input, ctx }) => {
|
|
||||||
const compose = await findComposeById(input.composeId);
|
|
||||||
if (
|
|
||||||
compose.environment.project.organizationId !==
|
|
||||||
ctx.session.activeOrganizationId
|
|
||||||
) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "UNAUTHORIZED",
|
|
||||||
message: "You are not authorized to access this compose",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return await findPatchesByComposeId(input.composeId);
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
update: protectedProcedure
|
update: protectedProcedure
|
||||||
|
|||||||
@@ -44,10 +44,7 @@ import {
|
|||||||
issueCommentExists,
|
issueCommentExists,
|
||||||
updateIssueComment,
|
updateIssueComment,
|
||||||
} from "./github";
|
} from "./github";
|
||||||
import {
|
import { generateApplyPatchesCommand } from "./patch";
|
||||||
findPatchesByApplicationId,
|
|
||||||
generateApplyPatchesCommand,
|
|
||||||
} from "./patch";
|
|
||||||
import {
|
import {
|
||||||
findPreviewDeploymentById,
|
findPreviewDeploymentById,
|
||||||
updatePreviewDeployment,
|
updatePreviewDeployment,
|
||||||
@@ -206,20 +203,12 @@ export const deployApplication = async ({
|
|||||||
command += await buildRemoteDocker(application);
|
command += await buildRemoteDocker(application);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply patches after cloning (for non-docker sources only)
|
|
||||||
if (application.sourceType !== "docker") {
|
if (application.sourceType !== "docker") {
|
||||||
const patches = await findPatchesByApplicationId(
|
command += await generateApplyPatchesCommand({
|
||||||
application.applicationId,
|
id: application.applicationId,
|
||||||
);
|
type: "application",
|
||||||
const enabledPatches = patches.filter((p) => p.enabled);
|
serverId,
|
||||||
if (enabledPatches.length > 0) {
|
});
|
||||||
command += generateApplyPatchesCommand({
|
|
||||||
appName: application.appName,
|
|
||||||
type: "application",
|
|
||||||
serverId,
|
|
||||||
patches: enabledPatches,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
command += await getBuildCommand(application);
|
command += await getBuildCommand(application);
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ import {
|
|||||||
updateDeployment,
|
updateDeployment,
|
||||||
updateDeploymentStatus,
|
updateDeploymentStatus,
|
||||||
} from "./deployment";
|
} from "./deployment";
|
||||||
import { findPatchesByComposeId, generateApplyPatchesCommand } from "./patch";
|
import { generateApplyPatchesCommand } from "./patch";
|
||||||
import { validUniqueServerAppName } from "./project";
|
import { validUniqueServerAppName } from "./project";
|
||||||
|
|
||||||
export type Compose = typeof compose.$inferSelect;
|
export type Compose = typeof compose.$inferSelect;
|
||||||
@@ -249,24 +249,12 @@ export const deployCompose = async ({
|
|||||||
await execAsync(commandWithLog);
|
await execAsync(commandWithLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply patches after cloning (for non-raw sources only)
|
|
||||||
if (compose.sourceType !== "raw") {
|
if (compose.sourceType !== "raw") {
|
||||||
const patches = await findPatchesByComposeId(compose.composeId);
|
command += await generateApplyPatchesCommand({
|
||||||
const enabledPatches = patches.filter((p) => p.enabled);
|
id: compose.composeId,
|
||||||
if (enabledPatches.length > 0) {
|
type: "compose",
|
||||||
const patchCommand = generateApplyPatchesCommand({
|
serverId: compose.serverId,
|
||||||
appName: compose.appName,
|
});
|
||||||
type: "compose",
|
|
||||||
serverId: compose.serverId,
|
|
||||||
patches: enabledPatches,
|
|
||||||
});
|
|
||||||
const patchCommandWithLog = `(${patchCommand}) >> ${deployment.logPath} 2>&1`;
|
|
||||||
if (compose.serverId) {
|
|
||||||
await execAsyncRemote(compose.serverId, patchCommandWithLog);
|
|
||||||
} else {
|
|
||||||
await execAsync(patchCommandWithLog);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
command = "set -e;";
|
command = "set -e;";
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ import { join } from "node:path";
|
|||||||
import { paths } from "@dokploy/server/constants";
|
import { paths } from "@dokploy/server/constants";
|
||||||
import { db } from "@dokploy/server/db";
|
import { db } from "@dokploy/server/db";
|
||||||
import { type apiCreatePatch, patch } from "@dokploy/server/db/schema";
|
import { type apiCreatePatch, patch } from "@dokploy/server/db/schema";
|
||||||
import {
|
|
||||||
execAsync,
|
|
||||||
execAsyncRemote,
|
|
||||||
} from "@dokploy/server/utils/process/execAsync";
|
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { and, eq, isNotNull } from "drizzle-orm";
|
import { and, eq } from "drizzle-orm";
|
||||||
|
import { encodeBase64 } from "../utils/docker/utils";
|
||||||
|
import { findApplicationById } from "./application";
|
||||||
|
import { findComposeById } from "./compose";
|
||||||
|
|
||||||
export type Patch = typeof patch.$inferSelect;
|
export type Patch = typeof patch.$inferSelect;
|
||||||
|
|
||||||
@@ -54,33 +53,29 @@ export const findPatchById = async (patchId: string) => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const findPatchesByApplicationId = async (applicationId: string) => {
|
export const findPatchesByEntityId = async (
|
||||||
|
id: string,
|
||||||
|
type: "application" | "compose",
|
||||||
|
) => {
|
||||||
return await db.query.patch.findMany({
|
return await db.query.patch.findMany({
|
||||||
where: and(
|
where: eq(
|
||||||
eq(patch.applicationId, applicationId),
|
type === "application" ? patch.applicationId : patch.composeId,
|
||||||
isNotNull(patch.applicationId),
|
id,
|
||||||
),
|
),
|
||||||
orderBy: (patch, { asc }) => [asc(patch.filePath)],
|
orderBy: (patch, { asc }) => [asc(patch.filePath)],
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const findPatchesByComposeId = async (composeId: string) => {
|
|
||||||
return await db.query.patch.findMany({
|
|
||||||
where: and(eq(patch.composeId, composeId), isNotNull(patch.composeId)),
|
|
||||||
orderBy: (patch, { asc }) => [asc(patch.filePath)],
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const findPatchByFilePath = async (
|
export const findPatchByFilePath = async (
|
||||||
filePath: string,
|
filePath: string,
|
||||||
id: string,
|
id: string,
|
||||||
type: "application" | "compose",
|
type: "application" | "compose",
|
||||||
) => {
|
) => {
|
||||||
return await db.query.patch.findFirst({
|
return await db.query.patch.findFirst({
|
||||||
where:
|
where: and(
|
||||||
type === "application"
|
eq(patch.filePath, filePath),
|
||||||
? and(eq(patch.filePath, filePath), eq(patch.applicationId, id))
|
eq(type === "application" ? patch.applicationId : patch.composeId, id),
|
||||||
: and(eq(patch.filePath, filePath), eq(patch.composeId, id)),
|
),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -111,76 +106,45 @@ export const deletePatch = async (patchId: string) => {
|
|||||||
return result[0];
|
return result[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Patch Application Functions
|
|
||||||
|
|
||||||
interface ApplyPatchesOptions {
|
interface ApplyPatchesOptions {
|
||||||
appName: string;
|
id: string;
|
||||||
type: "application" | "compose";
|
type: "application" | "compose";
|
||||||
serverId: string | null;
|
serverId: string | null;
|
||||||
patches: Patch[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export const generateApplyPatchesCommand = async ({
|
||||||
* Generate shell commands to apply patches to cloned repository
|
id,
|
||||||
* Uses git apply to apply unified diff patches
|
|
||||||
*/
|
|
||||||
export const generateApplyPatchesCommand = ({
|
|
||||||
appName,
|
|
||||||
type,
|
type,
|
||||||
patches,
|
|
||||||
serverId,
|
serverId,
|
||||||
}: ApplyPatchesOptions): string => {
|
}: ApplyPatchesOptions) => {
|
||||||
|
const entity =
|
||||||
|
type === "application"
|
||||||
|
? await findApplicationById(id)
|
||||||
|
: await findComposeById(id);
|
||||||
|
const { COMPOSE_PATH, APPLICATIONS_PATH } = paths(!!serverId);
|
||||||
|
const basePath = type === "compose" ? COMPOSE_PATH : APPLICATIONS_PATH;
|
||||||
|
const codePath = join(basePath, entity.appName, "code");
|
||||||
|
|
||||||
|
const patches = (await findPatchesByEntityId(id, type)).filter(
|
||||||
|
(p) => p.enabled,
|
||||||
|
);
|
||||||
|
|
||||||
if (patches.length === 0) {
|
if (patches.length === 0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const { COMPOSE_PATH, APPLICATIONS_PATH } = paths(!!serverId);
|
|
||||||
const basePath = type === "compose" ? COMPOSE_PATH : APPLICATIONS_PATH;
|
|
||||||
const codePath = join(basePath, appName, "code");
|
|
||||||
|
|
||||||
let command = `echo "Applying ${patches.length} patch(es)...";`;
|
let command = `echo "Applying ${patches.length} patch(es)...";`;
|
||||||
|
|
||||||
for (const p of patches) {
|
for (const p of patches) {
|
||||||
// Create a temporary patch file and apply it
|
if (!p.enabled) {
|
||||||
const patchFileName = `/tmp/patch_${p.patchId}.patch`;
|
continue;
|
||||||
// Escape content for shell - use base64 encoding
|
}
|
||||||
const encodedContent = Buffer.from(p.content).toString("base64");
|
const filePath = join(codePath, p.filePath);
|
||||||
|
|
||||||
command += `
|
command += `
|
||||||
echo "${encodedContent}" | base64 -d > ${patchFileName};
|
rm -f ${filePath};
|
||||||
cd ${codePath} && git apply --whitespace=fix ${patchFileName} && echo "✅ Applied patch for: ${p.filePath}" || echo "⚠️ Warning: Failed to apply patch for: ${p.filePath}";
|
echo "${encodeBase64(p.content)}" | base64 -d > ${filePath};
|
||||||
rm -f ${patchFileName};
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply patches during build process
|
|
||||||
*/
|
|
||||||
export const applyPatches = async ({
|
|
||||||
appName,
|
|
||||||
type,
|
|
||||||
serverId,
|
|
||||||
patches,
|
|
||||||
}: ApplyPatchesOptions): Promise<void> => {
|
|
||||||
const enabledPatches = patches.filter((p) => p.enabled);
|
|
||||||
|
|
||||||
if (enabledPatches.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const command = generateApplyPatchesCommand({
|
|
||||||
appName,
|
|
||||||
type,
|
|
||||||
serverId,
|
|
||||||
patches: enabledPatches,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (serverId) {
|
|
||||||
await execAsyncRemote(serverId, command);
|
|
||||||
} else {
|
|
||||||
await execAsync(command);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user