feat: add application and databases external servers

This commit is contained in:
Mauricio Siu
2024-09-09 15:58:58 -06:00
parent 5afe1645a0
commit 95f75fdccb
16 changed files with 385 additions and 233 deletions

View File

@@ -1,5 +1,4 @@
import { rmdir, stat, unlink } from "node:fs/promises";
import path, { join } from "node:path";
import path from "node:path";
import { APPLICATIONS_PATH, COMPOSE_PATH } from "@/server/constants";
import { db } from "@/server/db";
import {
@@ -7,10 +6,11 @@ import {
type apiCreateMount,
mounts,
} from "@/server/db/schema";
import { createFile } from "@/server/utils/docker/utils";
import { createFile, getCreateFileCommand } from "@/server/utils/docker/utils";
import { removeFileOrDirectory } from "@/server/utils/filesystem/directory";
import { TRPCError } from "@trpc/server";
import { type SQL, eq, sql } from "drizzle-orm";
import { execAsyncRemote } from "@/server/utils/process/execAsync";
export type Mount = typeof mounts.$inferSelect;
@@ -71,7 +71,19 @@ export const createFileMount = async (mountId: string) => {
try {
const mount = await findMountById(mountId);
const baseFilePath = await getBaseFilesPath(mountId);
await createFile(baseFilePath, mount.filePath || "", mount.content || "");
const serverId = await getServerId(mount);
if (serverId) {
const command = getCreateFileCommand(
baseFilePath,
mount.filePath || "",
mount.content || "",
);
await execAsyncRemote(serverId, command);
} else {
await createFile(baseFilePath, mount.filePath || "", mount.content || "");
}
} catch (error) {
console.log(`Error to create the file mount: ${error}`);
throw new TRPCError({
@@ -186,9 +198,16 @@ export const deleteFileMount = async (mountId: string) => {
const mount = await findMountById(mountId);
if (!mount.filePath) return;
const basePath = await getBaseFilesPath(mountId);
const fullPath = path.join(basePath, mount.filePath);
try {
await removeFileOrDirectory(fullPath);
const serverId = await getServerId(mount);
if (serverId) {
const command = `rm -rf ${fullPath}`;
await execAsyncRemote(serverId, command);
} else {
await removeFileOrDirectory(fullPath);
}
} catch (error) {}
};
@@ -219,3 +238,30 @@ export const getBaseFilesPath = async (mountId: string) => {
return directoryPath;
};
type MountNested = Awaited<ReturnType<typeof findMountById>>;
export const getServerId = async (mount: MountNested) => {
if (mount.serviceType === "application" && mount?.application?.serverId) {
return mount.application.serverId;
}
if (mount.serviceType === "postgres" && mount?.postgres?.serverId) {
return mount.postgres.serverId;
}
if (mount.serviceType === "mariadb" && mount?.mariadb?.serverId) {
return mount.mariadb.serverId;
}
if (mount.serviceType === "mongo" && mount?.mongo?.serverId) {
return mount.mongo.serverId;
}
if (mount.serviceType === "mysql" && mount?.mysql?.serverId) {
return mount.mysql.serverId;
}
if (mount.serviceType === "redis" && mount?.redis?.serverId) {
return mount.redis.serverId;
}
if (mount.serviceType === "compose" && mount?.compose?.serverId) {
return mount.compose.serverId;
}
return null;
};