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

View File

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

View File

@@ -48,6 +48,7 @@ import {
addNewService, addNewService,
checkServiceAccess, checkServiceAccess,
IS_CLOUD, IS_CLOUD,
findProjectById,
// uploadFileSchema // uploadFileSchema
} from "@dokploy/builders"; } from "@dokploy/builders";
import { uploadFileSchema } from "@/utils/schema"; import { uploadFileSchema } from "@/utils/schema";
@@ -65,6 +66,14 @@ export const applicationRouter = createTRPCRouter({
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newApplication = await createApplication(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
@@ -545,7 +554,6 @@ export const applicationRouter = createTRPCRouter({
}); });
await unzipDrop(zipFile, app); await unzipDrop(zipFile, app);
const jobData: DeploymentJob = { const jobData: DeploymentJob = {
applicationId: app.applicationId, applicationId: app.applicationId,
titleLog: "Manual deployment", titleLog: "Manual deployment",
@@ -554,6 +562,12 @@ export const applicationRouter = createTRPCRouter({
applicationType: "application", applicationType: "application",
server: !!app.serverId, server: !!app.serverId,
}; };
if (IS_CLOUD && app.serverId) {
jobData.serverId = app.serverId;
await deploy(jobData);
return true;
}
await myQueue.add( await myQueue.add(
"deployments", "deployments",
{ ...jobData }, { ...jobData },

View File

@@ -64,6 +64,13 @@ export const composeRouter = createTRPCRouter({
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newService = await createCompose(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
@@ -278,7 +285,6 @@ export const composeRouter = createTRPCRouter({
}; };
if (IS_CLOUD && compose.serverId) { if (IS_CLOUD && compose.serverId) {
jobData.serverId = compose.serverId; jobData.serverId = compose.serverId;
await deploy(jobData); await deploy(jobData);
return true; return true;
} }

View File

@@ -24,6 +24,7 @@ import {
addNewService, addNewService,
checkServiceAccess, checkServiceAccess,
createMount, createMount,
findProjectById,
} from "@dokploy/builders"; } from "@dokploy/builders";
export const mariadbRouter = createTRPCRouter({ export const mariadbRouter = createTRPCRouter({
@@ -35,6 +36,13 @@ export const mariadbRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newMariadb = await createMariadb(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newMariadb.mariadbId); await addNewService(ctx.user.authId, newMariadb.mariadbId);

View File

@@ -24,6 +24,7 @@ import {
startServiceRemote, startServiceRemote,
stopService, stopService,
stopServiceRemote, stopServiceRemote,
findProjectById,
} from "@dokploy/builders"; } from "@dokploy/builders";
export const mongoRouter = createTRPCRouter({ export const mongoRouter = createTRPCRouter({
@@ -35,6 +36,13 @@ export const mongoRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newMongo = await createMongo(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newMongo.mongoId); await addNewService(ctx.user.authId, newMongo.mongoId);

View File

@@ -26,6 +26,7 @@ import {
startServiceRemote, startServiceRemote,
stopService, stopService,
stopServiceRemote, stopServiceRemote,
findProjectById,
} from "@dokploy/builders"; } from "@dokploy/builders";
export const mysqlRouter = createTRPCRouter({ export const mysqlRouter = createTRPCRouter({
@@ -36,6 +37,13 @@ export const mysqlRouter = createTRPCRouter({
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newMysql = await createMysql(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {

View File

@@ -24,6 +24,7 @@ import {
findPostgresById, findPostgresById,
removePostgresById, removePostgresById,
updatePostgresById, updatePostgresById,
findProjectById,
} from "@dokploy/builders"; } from "@dokploy/builders";
export const postgresRouter = createTRPCRouter({ export const postgresRouter = createTRPCRouter({
@@ -35,6 +36,13 @@ export const postgresRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newPostgres = await createPostgres(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newPostgres.postgresId); await addNewService(ctx.user.authId, newPostgres.postgresId);

View File

@@ -27,6 +27,7 @@ import {
removeRedisById, removeRedisById,
updateRedisById, updateRedisById,
IS_CLOUD, IS_CLOUD,
findProjectById,
} from "@dokploy/builders"; } from "@dokploy/builders";
export const redisRouter = createTRPCRouter({ export const redisRouter = createTRPCRouter({
@@ -38,6 +39,13 @@ export const redisRouter = createTRPCRouter({
await checkServiceAccess(ctx.user.authId, input.projectId, "create"); 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); const newRedis = await createRedis(input);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await addNewService(ctx.user.authId, newRedis.redisId); await addNewService(ctx.user.authId, newRedis.redisId);