refactor(volumes): rework files volumes to be more simple and persistent

This commit is contained in:
Mauricio Siu
2024-07-21 18:55:57 -06:00
parent 2e79c7230f
commit 8454e4f781
11 changed files with 126 additions and 76 deletions

View File

@@ -9,7 +9,6 @@ import {
} from "@/server/db/schema";
import { createFile } from "@/server/utils/docker/utils";
import { removeFileOrDirectory } from "@/server/utils/filesystem/directory";
import { execAsync } from "@/server/utils/process/execAsync";
import { TRPCError } from "@trpc/server";
import { type SQL, eq, sql } from "drizzle-orm";
@@ -71,10 +70,10 @@ export const createMount = async (input: typeof apiCreateMount._type) => {
export const createFileMount = async (mountId: string) => {
try {
const mount = await findMountById(mountId);
const baseFilePath = await getBaseFilesMountPath(mountId);
const baseFilePath = await getBaseFilesPath(mountId);
await createFile(baseFilePath, mount.filePath || "", mount.content || "");
} catch (error) {
console.log(error);
console.log(`Error to create the file mount: ${error}`);
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the mount",
@@ -109,28 +108,29 @@ export const updateMount = async (
mountId: string,
mountData: Partial<Mount>,
) => {
const mount = await db
.update(mounts)
.set({
...mountData,
})
.where(eq(mounts.mountId, mountId))
.returning()
.then((value) => value[0]);
return await db.transaction(async (transaction) => {
const mount = await db
.update(mounts)
.set({
...mountData,
})
.where(eq(mounts.mountId, mountId))
.returning()
.then((value) => value[0]);
if (!mount) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Mount not found",
});
}
if (!mount) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Mount not found",
});
}
if (mount.type === "file") {
await deleteFileMount(mountId);
await createFileMount(mountId);
}
return mount;
if (mount.type === "file") {
await deleteFileMount(mountId);
await createFileMount(mountId);
}
return mount;
});
};
export const findMountsByApplicationId = async (
@@ -184,14 +184,15 @@ export const deleteMount = async (mountId: string) => {
export const deleteFileMount = async (mountId: string) => {
const mount = await findMountById(mountId);
const basePath = await getBaseFilesMountPath(mountId);
const fullPath = path.join(basePath, mount.filePath || "");
if (!mount.filePath) return;
const basePath = await getBaseFilesPath(mountId);
const fullPath = path.join(basePath, mount.filePath);
try {
await removeFileOrDirectory(fullPath);
} catch (error) {}
};
export const getBaseFilesMountPath = async (mountId: string) => {
export const getBaseFilesPath = async (mountId: string) => {
const mount = await findMountById(mountId);
let absoluteBasePath = path.resolve(APPLICATIONS_PATH);

View File

@@ -1,7 +1,7 @@
import fs from "node:fs";
import path, { join } from "node:path";
import path from "node:path";
import type { Readable } from "node:stream";
import { APPLICATIONS_PATH, COMPOSE_PATH, docker } from "@/server/constants";
import { APPLICATIONS_PATH, docker } from "@/server/constants";
import type { ContainerInfo, ResourceRequirements } from "dockerode";
import { parse } from "dotenv";
import type { ApplicationNested } from "../builders";
@@ -305,13 +305,13 @@ export const generateFileMounts = (
return mounts
.filter((mount) => mount.type === "file")
.map((mount) => {
const fileName = mount.mountPath.split("/").pop();
const fileName = mount.filePath;
const absoluteBasePath = path.resolve(APPLICATIONS_PATH);
const directory = path.join(absoluteBasePath, appName, "files");
const filePath = path.join(directory, fileName || "");
const sourcePath = path.join(directory, fileName || "");
return {
Type: "bind" as const,
Source: filePath,
Source: sourcePath,
Target: mount.mountPath,
};
});
@@ -323,27 +323,17 @@ export const createFile = async (
content: string,
) => {
try {
// Unir outputPath con filePath
const fullPath = path.join(outputPath, filePath);
// Verificar si la ruta termina en separador (indica que es un directorio)
if (fullPath.endsWith(path.sep) || filePath.endsWith("/")) {
fs.mkdirSync(fullPath, { recursive: true });
console.log(`Directorio creado: ${fullPath}`);
return;
}
// Para archivos, obtener el directorio del archivo
const directory = path.dirname(fullPath);
// Crear el directorio si no existe
fs.mkdirSync(directory, { recursive: true });
// Escribir el archivo
fs.writeFileSync(fullPath, content || "");
console.log(`Archivo creado: ${fullPath}`);
} catch (error) {
console.log(error);
throw error;
}
};