mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: edit volume
This commit is contained in:
@@ -15,7 +15,7 @@ export const createVolumeBackend = (volume: Volume): VolumeBackend => {
|
||||
return makeNfsBackend(config, path);
|
||||
}
|
||||
case "directory": {
|
||||
return makeDirectoryBackend();
|
||||
return makeDirectoryBackend(config, path);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Backend ${config.backend} not implemented`);
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import * as fs from "node:fs/promises";
|
||||
import type { BackendConfig } from "@ironmount/schemas";
|
||||
import type { VolumeBackend } from "../backend";
|
||||
|
||||
const mount = async () => {
|
||||
const mount = async (_config: BackendConfig, path: string) => {
|
||||
console.log("Mounting directory volume...");
|
||||
await fs.mkdir(path, { recursive: true });
|
||||
};
|
||||
|
||||
const unmount = async () => {
|
||||
console.log("Cannot unmount directory volume.");
|
||||
};
|
||||
|
||||
export const makeDirectoryBackend = (): VolumeBackend => ({
|
||||
mount,
|
||||
export const makeDirectoryBackend = (config: BackendConfig, path: string): VolumeBackend => ({
|
||||
mount: () => mount(config, path),
|
||||
unmount,
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { exec } from "node:child_process";
|
||||
import * as fs from "node:fs/promises";
|
||||
import * as os from "node:os";
|
||||
import type { BackendConfig } from "@ironmount/schemas";
|
||||
import type { VolumeBackend } from "../backend";
|
||||
@@ -13,6 +14,8 @@ const mount = async (config: BackendConfig, path: string) => {
|
||||
return;
|
||||
}
|
||||
|
||||
await fs.mkdir(path, { recursive: true });
|
||||
|
||||
const source = `${config.server}:${config.exportPath}`;
|
||||
const options = [`vers=${config.version}`, `port=${config.port}`];
|
||||
const cmd = `mount -t nfs -o ${options.join(",")} ${source} ${path}`;
|
||||
@@ -21,7 +24,7 @@ const mount = async (config: BackendConfig, path: string) => {
|
||||
exec(cmd, (error, stdout, stderr) => {
|
||||
console.log("Mount command executed:", { cmd, error, stdout, stderr });
|
||||
if (error) {
|
||||
console.error(`Error mounting NFS volume: ${stderr}`);
|
||||
// console.error(`Error mounting NFS volume: ${stderr}`);
|
||||
return reject(new Error(`Failed to mount NFS volume: ${stderr}`));
|
||||
}
|
||||
console.log(`NFS volume mounted successfully: ${stdout}`);
|
||||
@@ -30,11 +33,28 @@ const mount = async (config: BackendConfig, path: string) => {
|
||||
});
|
||||
};
|
||||
|
||||
const unmount = async () => {
|
||||
console.log("Unmounting nfs volume...");
|
||||
const unmount = async (path: string) => {
|
||||
if (os.platform() !== "linux") {
|
||||
console.error("NFS unmounting is only supported on Linux hosts.");
|
||||
return;
|
||||
}
|
||||
|
||||
const cmd = `umount -f ${path}`;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
exec(cmd, (error, stdout, stderr) => {
|
||||
console.log("Unmount command executed:", { cmd, error, stdout, stderr });
|
||||
if (error) {
|
||||
console.error(`Error unmounting NFS volume: ${stderr}`);
|
||||
return reject(new Error(`Failed to unmount NFS volume: ${stderr}`));
|
||||
}
|
||||
console.log(`NFS volume unmounted successfully: ${stdout}`);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const makeNfsBackend = (config: BackendConfig, path: string): VolumeBackend => ({
|
||||
mount: () => mount(config, path),
|
||||
unmount,
|
||||
unmount: () => unmount(path),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user