refactor(cloud): add deploy to external API

This commit is contained in:
Mauricio Siu 2024-10-04 18:53:46 -06:00
parent 3df2f8e58c
commit 5cebf5540a
10 changed files with 92 additions and 3 deletions

View File

@ -2,6 +2,8 @@ import { db } from "@/server/db";
import { applications } from "@/server/db/schema";
import type { DeploymentJob } from "@/server/queues/deployments-queue";
import { myQueue } from "@/server/queues/queueSetup";
import { deploy } from "@/server/utils/deploy";
import { IS_CLOUD } from "@dokploy/builders";
import { eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next";
@ -89,6 +91,12 @@ export default async function handler(
applicationType: "application",
server: !!application.serverId,
};
if (IS_CLOUD && application.serverId) {
jobData.serverId = application.serverId;
await deploy(jobData);
return true;
}
await myQueue.add(
"deployments",
{ ...jobData },

View File

@ -9,6 +9,8 @@ import {
extractCommitMessage,
extractHash,
} from "../[refreshToken]";
import { IS_CLOUD } from "@dokploy/builders";
import { deploy } from "@/server/utils/deploy";
export default async function handler(
req: NextApiRequest,
@ -65,6 +67,12 @@ export default async function handler(
descriptionLog: `Hash: ${deploymentHash}`,
server: !!composeResult.serverId,
};
if (IS_CLOUD && composeResult.serverId) {
jobData.serverId = composeResult.serverId;
await deploy(jobData);
return true;
}
await myQueue.add(
"deployments",
{ ...jobData },

View File

@ -1,4 +1,4 @@
import { findAdmin } from "@dokploy/builders";
import { findAdmin, IS_CLOUD } from "@dokploy/builders";
import { db } from "@/server/db";
import { applications, compose, github } from "@/server/db/schema";
import type { DeploymentJob } from "@/server/queues/deployments-queue";
@ -7,6 +7,7 @@ import { Webhooks } from "@octokit/webhooks";
import { and, eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next";
import { extractCommitMessage, extractHash } from "./[refreshToken]";
import { deploy } from "@/server/utils/deploy";
export default async function handler(
req: NextApiRequest,
@ -88,6 +89,12 @@ export default async function handler(
applicationType: "application",
server: !!app.serverId,
};
if (IS_CLOUD && app.serverId) {
jobData.serverId = app.serverId;
await deploy(jobData);
return true;
}
await myQueue.add(
"deployments",
{ ...jobData },
@ -116,6 +123,12 @@ export default async function handler(
descriptionLog: `Hash: ${deploymentHash}`,
};
if (IS_CLOUD && composeApp.serverId) {
jobData.serverId = composeApp.serverId;
await deploy(jobData);
return true;
}
await myQueue.add(
"deployments",
{ ...jobData },

View File

@ -48,6 +48,7 @@ import {
addNewService,
checkServiceAccess,
IS_CLOUD,
findProjectById,
// uploadFileSchema
} from "@dokploy/builders";
import { uploadFileSchema } from "@/utils/schema";
@ -65,6 +66,14 @@ export const applicationRouter = createTRPCRouter({
if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newApplication = await createApplication(input);
if (ctx.user.rol === "user") {
@ -545,7 +554,6 @@ export const applicationRouter = createTRPCRouter({
});
await unzipDrop(zipFile, app);
const jobData: DeploymentJob = {
applicationId: app.applicationId,
titleLog: "Manual deployment",
@ -554,6 +562,12 @@ export const applicationRouter = createTRPCRouter({
applicationType: "application",
server: !!app.serverId,
};
if (IS_CLOUD && app.serverId) {
jobData.serverId = app.serverId;
await deploy(jobData);
return true;
}
await myQueue.add(
"deployments",
{ ...jobData },

View File

@ -64,6 +64,13 @@ export const composeRouter = createTRPCRouter({
if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newService = await createCompose(input);
if (ctx.user.rol === "user") {
@ -278,7 +285,6 @@ export const composeRouter = createTRPCRouter({
};
if (IS_CLOUD && compose.serverId) {
jobData.serverId = compose.serverId;
await deploy(jobData);
return true;
}

View File

@ -24,6 +24,7 @@ import {
addNewService,
checkServiceAccess,
createMount,
findProjectById,
} from "@dokploy/builders";
export const mariadbRouter = createTRPCRouter({
@ -35,6 +36,13 @@ export const mariadbRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newMariadb = await createMariadb(input);
if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newMariadb.mariadbId);

View File

@ -24,6 +24,7 @@ import {
startServiceRemote,
stopService,
stopServiceRemote,
findProjectById,
} from "@dokploy/builders";
export const mongoRouter = createTRPCRouter({
@ -35,6 +36,13 @@ export const mongoRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newMongo = await createMongo(input);
if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newMongo.mongoId);

View File

@ -26,6 +26,7 @@ import {
startServiceRemote,
stopService,
stopServiceRemote,
findProjectById,
} from "@dokploy/builders";
export const mysqlRouter = createTRPCRouter({
@ -36,6 +37,13 @@ export const mysqlRouter = createTRPCRouter({
if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newMysql = await createMysql(input);
if (ctx.user.rol === "user") {

View File

@ -24,6 +24,7 @@ import {
findPostgresById,
removePostgresById,
updatePostgresById,
findProjectById,
} from "@dokploy/builders";
export const postgresRouter = createTRPCRouter({
@ -35,6 +36,13 @@ export const postgresRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newPostgres = await createPostgres(input);
if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newPostgres.postgresId);

View File

@ -27,6 +27,7 @@ import {
removeRedisById,
updateRedisById,
IS_CLOUD,
findProjectById,
} from "@dokploy/builders";
export const redisRouter = createTRPCRouter({
@ -38,6 +39,13 @@ export const redisRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create");
}
const project = await findProjectById(input.projectId);
if (project.adminId !== ctx.user.adminId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this project",
});
}
const newRedis = await createRedis(input);
if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newRedis.redisId);