refactor: use rr actions/loader

refactor: use rr actions
This commit is contained in:
Nicolas Meienberger
2025-08-31 19:22:55 +02:00
parent 23f47bbb6b
commit 37effcb4e3
21 changed files with 454 additions and 219 deletions

View File

@@ -0,0 +1,24 @@
import type { Volume } from "../../db/schema";
import { makeDirectoryBackend } from "./directory/directory-backend";
import { makeNfsBackend } from "./nfs/nfs-backend";
export type VolumeBackend = {
mount: () => Promise<void>;
unmount: () => Promise<void>;
};
export const createVolumeBackend = (volume: Volume): VolumeBackend => {
const { config, path } = volume;
switch (config.backend) {
case "nfs": {
return makeNfsBackend(config, path);
}
case "directory": {
return makeDirectoryBackend();
}
default: {
throw new Error(`Backend ${config.backend} not implemented`);
}
}
};

View File

@@ -0,0 +1,14 @@
import type { VolumeBackend } from "../backend";
const mount = async () => {
console.log("Mounting directory volume...");
};
const unmount = async () => {
console.log("Cannot unmount directory volume.");
};
export const makeDirectoryBackend = (): VolumeBackend => ({
mount,
unmount,
});

View File

@@ -0,0 +1,39 @@
import { exec } from "node:child_process";
import * as os from "node:os";
import type { BackendConfig } from "../../../db/schema";
import type { VolumeBackend } from "../backend";
const mount = async (config: BackendConfig, path: string) => {
if (config.backend !== "nfs") {
throw new Error("Invalid backend config for NFS");
}
if (os.platform() !== "linux") {
console.error("NFS mounting is only supported on Linux hosts.");
return;
}
const source = `${config.server}:${config.exportPath}`;
const options = [`vers=${config.version}`, `port=${config.port}`];
const cmd = `mount -t nfs -o ${options.join(",")} ${source} ${path}`;
return new Promise<void>((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error mounting NFS volume: ${stderr}`);
return reject(new Error(`Failed to mount NFS volume: ${stderr}`));
}
console.log(`NFS volume mounted successfully: ${stdout}`);
resolve();
});
});
};
const unmount = async () => {
console.log("Unmounting nfs volume...");
};
export const makeNfsBackend = (config: BackendConfig, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount,
});