mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import * as fs from "node:fs/promises";
|
|
import { toMessage } from "../../../utils/errors";
|
|
import { logger } from "../../../utils/logger";
|
|
import { testMariaDBConnection } from "../../../utils/database-dump";
|
|
import type { VolumeBackend } from "../backend";
|
|
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
|
|
|
|
const mount = async (config: BackendConfig, volumePath: string) => {
|
|
if (config.backend !== "mariadb") {
|
|
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
|
}
|
|
|
|
logger.info(`Testing MariaDB connection to: ${config.host}:${config.port}`);
|
|
|
|
try {
|
|
await testMariaDBConnection(config);
|
|
await fs.mkdir(volumePath, { recursive: true });
|
|
|
|
logger.info("MariaDB connection successful");
|
|
return { status: BACKEND_STATUS.mounted };
|
|
} catch (error) {
|
|
logger.error("Failed to connect to MariaDB:", error);
|
|
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
|
}
|
|
};
|
|
|
|
const unmount = async (volumePath: string) => {
|
|
logger.info("Cleaning up MariaDB dump directory");
|
|
|
|
try {
|
|
await fs.rm(volumePath, { recursive: true, force: true });
|
|
return { status: BACKEND_STATUS.unmounted };
|
|
} catch (error) {
|
|
logger.warn(`Failed to clean up MariaDB dump directory: ${toMessage(error)}`);
|
|
return { status: BACKEND_STATUS.unmounted };
|
|
}
|
|
};
|
|
|
|
const checkHealth = async (config: BackendConfig) => {
|
|
if (config.backend !== "mariadb") {
|
|
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
|
}
|
|
|
|
try {
|
|
await testMariaDBConnection(config);
|
|
return { status: BACKEND_STATUS.mounted };
|
|
} catch (error) {
|
|
logger.error("MariaDB health check failed:", error);
|
|
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
|
}
|
|
};
|
|
|
|
export const makeMariaDBBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
|
|
mount: () => mount(config, volumePath),
|
|
unmount: () => unmount(volumePath),
|
|
checkHealth: () => checkHealth(config),
|
|
}); |