new abstract method for volumepath

This commit is contained in:
Renan Bernordi
2025-11-16 17:47:23 -03:00
parent ff16c6914d
commit 14dadc85e7
14 changed files with 112 additions and 84 deletions

View File

@@ -1,6 +1,6 @@
import type { BackendStatus } from "~/schemas/volumes";
import type { Volume } from "../../db/schema";
import { getVolumePath } from "../volumes/helpers";
import { VOLUME_MOUNT_BASE } from "../../core/constants";
import { makeDirectoryBackend } from "./directory/directory-backend";
import { makeNfsBackend } from "./nfs/nfs-backend";
import { makeSmbBackend } from "./smb/smb-backend";
@@ -18,32 +18,38 @@ export type VolumeBackend = {
mount: () => Promise<OperationResult>;
unmount: () => Promise<OperationResult>;
checkHealth: () => Promise<OperationResult>;
getVolumePath: () => string;
isDatabaseBackend: () => boolean;
getDumpPath: () => string | null;
getDumpFilePath: (timestamp: number) => string | null;
};
export const createVolumeBackend = (volume: Volume): VolumeBackend => {
const path = getVolumePath(volume);
const path = volume.config.backend === "directory"
? volume.config.path
: `${VOLUME_MOUNT_BASE}/${volume.name}/_data`;
switch (volume.config.backend) {
case "nfs": {
return makeNfsBackend(volume.config, path);
return makeNfsBackend(volume.config, volume.name, path);
}
case "smb": {
return makeSmbBackend(volume.config, path);
return makeSmbBackend(volume.config, volume.name, path);
}
case "directory": {
return makeDirectoryBackend(volume.config, path);
return makeDirectoryBackend(volume.config, volume.name, path);
}
case "webdav": {
return makeWebdavBackend(volume.config, path);
return makeWebdavBackend(volume.config, volume.name, path);
}
case "mariadb": {
return makeMariaDBBackend(volume.config, path);
return makeMariaDBBackend(volume.config, volume.name, path);
}
case "mysql": {
return makeMySQLBackend(volume.config, path);
return makeMySQLBackend(volume.config, volume.name, path);
}
case "postgres": {
return makePostgresBackend(volume.config, path);
return makePostgresBackend(volume.config, volume.name, path);
}
default: {
throw new Error(`Unsupported backend type: ${(volume.config as any).backend}`);

View File

@@ -52,8 +52,12 @@ const checkHealth = async (config: BackendConfig) => {
}
};
export const makeDirectoryBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
export const makeDirectoryBackend = (config: BackendConfig, volumeName: string, volumePath: string): VolumeBackend => ({
mount: () => mount(config, volumePath),
unmount,
checkHealth: () => checkHealth(config),
getVolumePath: () => config.backend === "directory" ? config.path : volumePath,
isDatabaseBackend: () => false,
getDumpPath: () => null,
getDumpFilePath: () => null,
});

View File

@@ -4,6 +4,7 @@ import { logger } from "../../../utils/logger";
import { testMariaDBConnection } from "../../../utils/database-dump";
import type { VolumeBackend } from "../backend";
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
import { VOLUME_MOUNT_BASE } from "../../../core/constants";
const mount = async (config: BackendConfig, volumePath: string) => {
if (config.backend !== "mariadb") {
@@ -50,8 +51,15 @@ const checkHealth = async (config: BackendConfig) => {
}
};
export const makeMariaDBBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
export const makeMariaDBBackend = (config: BackendConfig, volumeName: string, volumePath: string): VolumeBackend => ({
mount: () => mount(config, volumePath),
unmount: () => unmount(volumePath),
checkHealth: () => checkHealth(config),
getVolumePath: () => volumePath,
isDatabaseBackend: () => true,
getDumpPath: () => `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`,
getDumpFilePath: (timestamp: number) => {
const dumpDir = `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`;
return `${dumpDir}/${volumeName}-${timestamp}.sql`;
},
});

View File

@@ -4,6 +4,7 @@ import { logger } from "../../../utils/logger";
import { testMySQLConnection } from "../../../utils/database-dump";
import type { VolumeBackend } from "../backend";
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
import { VOLUME_MOUNT_BASE } from "../../../core/constants";
const mount = async (config: BackendConfig, volumePath: string) => {
if (config.backend !== "mysql") {
@@ -50,8 +51,15 @@ const checkHealth = async (config: BackendConfig) => {
}
};
export const makeMySQLBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
export const makeMySQLBackend = (config: BackendConfig, volumeName: string, volumePath: string): VolumeBackend => ({
mount: () => mount(config, volumePath),
unmount: () => unmount(volumePath),
checkHealth: () => checkHealth(config),
getVolumePath: () => volumePath,
isDatabaseBackend: () => true,
getDumpPath: () => `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`,
getDumpFilePath: (timestamp: number) => {
const dumpDir = `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`;
return `${dumpDir}/${volumeName}-${timestamp}.sql`;
},
});

View File

@@ -114,8 +114,12 @@ const checkHealth = async (path: string, readOnly: boolean) => {
}
};
export const makeNfsBackend = (config: BackendConfig, path: string): VolumeBackend => ({
export const makeNfsBackend = (config: BackendConfig, volumeName: string, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount: () => unmount(path),
checkHealth: () => checkHealth(path, config.readOnly ?? false),
getVolumePath: () => path,
isDatabaseBackend: () => false,
getDumpPath: () => null,
getDumpFilePath: () => null,
});

View File

@@ -4,6 +4,7 @@ import { logger } from "../../../utils/logger";
import { testPostgresConnection } from "../../../utils/database-dump";
import type { VolumeBackend } from "../backend";
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
import { VOLUME_MOUNT_BASE } from "../../../core/constants";
const mount = async (config: BackendConfig, volumePath: string) => {
if (config.backend !== "postgres") {
@@ -50,8 +51,19 @@ const checkHealth = async (config: BackendConfig) => {
}
};
export const makePostgresBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
export const makePostgresBackend = (config: BackendConfig, volumeName: string, volumePath: string): VolumeBackend => ({
mount: () => mount(config, volumePath),
unmount: () => unmount(volumePath),
checkHealth: () => checkHealth(config),
getVolumePath: () => volumePath,
isDatabaseBackend: () => true,
getDumpPath: () => `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`,
getDumpFilePath: (timestamp: number) => {
const dumpDir = `${VOLUME_MOUNT_BASE}/${volumeName}/dumps`;
const extension = config.backend === "postgres" &&
(config as Extract<BackendConfig, { backend: "postgres" }>).dumpFormat !== "plain"
? "dump"
: "sql";
return `${dumpDir}/${volumeName}-${timestamp}.${extension}`;
},
});

View File

@@ -127,8 +127,12 @@ const checkHealth = async (path: string, readOnly: boolean) => {
}
};
export const makeSmbBackend = (config: BackendConfig, path: string): VolumeBackend => ({
export const makeSmbBackend = (config: BackendConfig, volumeName: string, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount: () => unmount(path),
checkHealth: () => checkHealth(path, config.readOnly ?? false),
getVolumePath: () => path,
isDatabaseBackend: () => false,
getDumpPath: () => null,
getDumpFilePath: () => null,
});

View File

@@ -161,8 +161,12 @@ const checkHealth = async (path: string, readOnly: boolean) => {
}
};
export const makeWebdavBackend = (config: BackendConfig, path: string): VolumeBackend => ({
export const makeWebdavBackend = (config: BackendConfig, volumeName: string, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount: () => unmount(path),
checkHealth: () => checkHealth(path, config.readOnly ?? false),
getVolumePath: () => path,
isDatabaseBackend: () => false,
getDumpPath: () => null,
getDumpFilePath: () => null,
});