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),
|
||||
});
|
||||
|
||||
@@ -5,10 +5,13 @@ import {
|
||||
createVolumeBody,
|
||||
createVolumeDto,
|
||||
deleteVolumeDto,
|
||||
getVolumeDto,
|
||||
type ListVolumesResponseDto,
|
||||
listVolumesDto,
|
||||
testConnectionBody,
|
||||
testConnectionDto,
|
||||
updateVolumeBody,
|
||||
updateVolumeDto,
|
||||
} from "./volume.dto";
|
||||
import { volumeService } from "./volume.service";
|
||||
|
||||
@@ -18,8 +21,8 @@ export const volumeController = new Hono()
|
||||
|
||||
const response = {
|
||||
volumes: volumes.map((volume) => ({
|
||||
name: volume.name,
|
||||
path: volume.path,
|
||||
...volume,
|
||||
updatedAt: volume.updatedAt.getTime(),
|
||||
createdAt: volume.createdAt.getTime(),
|
||||
})),
|
||||
} satisfies ListVolumesResponseDto;
|
||||
@@ -54,12 +57,47 @@ export const volumeController = new Hono()
|
||||
|
||||
return c.json({ message: "Volume deleted" });
|
||||
})
|
||||
.get("/:name", (c) => {
|
||||
return c.json({ message: `Details of volume ${c.req.param("name")}` });
|
||||
.get("/:name", getVolumeDto, async (c) => {
|
||||
const { name } = c.req.param();
|
||||
const res = await volumeService.getVolume(name);
|
||||
|
||||
if (res.error) {
|
||||
const { message, status } = handleServiceError(res.error);
|
||||
return c.json(message, status);
|
||||
}
|
||||
|
||||
const response = {
|
||||
name: res.volume.name,
|
||||
path: res.volume.path,
|
||||
type: res.volume.type,
|
||||
createdAt: res.volume.createdAt.getTime(),
|
||||
updatedAt: res.volume.updatedAt.getTime(),
|
||||
config: res.volume.config,
|
||||
};
|
||||
|
||||
return c.json(response, 200);
|
||||
})
|
||||
.put("/:name", (c) => {
|
||||
return c.json({ message: `Update volume ${c.req.param("name")}` });
|
||||
})
|
||||
.delete("/:name", (c) => {
|
||||
return c.json({ message: `Delete volume ${c.req.param("name")}` });
|
||||
.put("/:name", updateVolumeDto, validator("json", updateVolumeBody), async (c) => {
|
||||
const { name } = c.req.param();
|
||||
const body = c.req.valid("json");
|
||||
const res = await volumeService.updateVolume(name, body.config);
|
||||
|
||||
if (res.error) {
|
||||
const { message, status } = handleServiceError(res.error);
|
||||
return c.json(message, status);
|
||||
}
|
||||
|
||||
const response = {
|
||||
message: "Volume updated",
|
||||
volume: {
|
||||
name: res.volume.name,
|
||||
path: res.volume.path,
|
||||
type: res.volume.type,
|
||||
createdAt: res.volume.createdAt.getTime(),
|
||||
updatedAt: res.volume.updatedAt.getTime(),
|
||||
config: res.volume.config,
|
||||
},
|
||||
};
|
||||
|
||||
return c.json(response, 200);
|
||||
});
|
||||
|
||||
@@ -3,15 +3,20 @@ import { type } from "arktype";
|
||||
import { describeRoute } from "hono-openapi";
|
||||
import { resolver } from "hono-openapi/arktype";
|
||||
|
||||
const volumeSchema = type({
|
||||
name: "string",
|
||||
path: "string",
|
||||
type: "string",
|
||||
createdAt: "number",
|
||||
updatedAt: "number",
|
||||
config: volumeConfigSchema,
|
||||
});
|
||||
|
||||
/**
|
||||
* List all volumes
|
||||
*/
|
||||
export const listVolumesResponse = type({
|
||||
volumes: type({
|
||||
name: "string",
|
||||
path: "string",
|
||||
createdAt: "number",
|
||||
}).array(),
|
||||
volumes: volumeSchema.array(),
|
||||
});
|
||||
export type ListVolumesResponseDto = typeof listVolumesResponse.infer;
|
||||
|
||||
@@ -90,6 +95,68 @@ export const deleteVolumeDto = describeRoute({
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Get a volume
|
||||
*/
|
||||
export const getVolumeDto = describeRoute({
|
||||
description: "Get a volume by name",
|
||||
operationId: "getVolume",
|
||||
validateResponse: true,
|
||||
tags: ["Volumes"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Volume details",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(volumeSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "Volume not found",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Update a volume
|
||||
*/
|
||||
export const updateVolumeBody = type({
|
||||
config: volumeConfigSchema,
|
||||
});
|
||||
|
||||
export const updateVolumeResponse = type({
|
||||
message: "string",
|
||||
volume: type({
|
||||
name: "string",
|
||||
path: "string",
|
||||
type: "string",
|
||||
createdAt: "number",
|
||||
updatedAt: "number",
|
||||
config: volumeConfigSchema,
|
||||
}),
|
||||
});
|
||||
|
||||
export const updateVolumeDto = describeRoute({
|
||||
description: "Update a volume's configuration",
|
||||
operationId: "updateVolume",
|
||||
validateResponse: true,
|
||||
tags: ["Volumes"],
|
||||
responses: {
|
||||
200: {
|
||||
description: "Volume updated successfully",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(updateVolumeResponse),
|
||||
},
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "Volume not found",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Test connection
|
||||
*/
|
||||
|
||||
@@ -86,6 +86,55 @@ const mountVolume = async (name: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getVolume = async (name: string) => {
|
||||
const volume = await db.query.volumesTable.findFirst({
|
||||
where: eq(volumesTable.name, name),
|
||||
});
|
||||
|
||||
if (!volume) {
|
||||
return { error: new NotFoundError("Volume not found") };
|
||||
}
|
||||
|
||||
return { volume };
|
||||
};
|
||||
|
||||
const updateVolume = async (name: string, backendConfig: BackendConfig) => {
|
||||
try {
|
||||
const existing = await db.query.volumesTable.findFirst({
|
||||
where: eq(volumesTable.name, name),
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return { error: new NotFoundError("Volume not found") };
|
||||
}
|
||||
|
||||
const oldBackend = createVolumeBackend(existing);
|
||||
await oldBackend.unmount();
|
||||
|
||||
const updated = await db
|
||||
.update(volumesTable)
|
||||
.set({
|
||||
config: backendConfig,
|
||||
type: backendConfig.backend,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(volumesTable.name, name))
|
||||
.returning();
|
||||
|
||||
// Mount with new configuration
|
||||
const newBackend = createVolumeBackend(updated[0]);
|
||||
await newBackend.mount();
|
||||
|
||||
return { volume: updated[0] };
|
||||
} catch (error) {
|
||||
return {
|
||||
error: new InternalServerError("Failed to update volume", {
|
||||
cause: error,
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const testConnection = async (backendConfig: BackendConfig) => {
|
||||
let tempDir: string | null = null;
|
||||
|
||||
@@ -134,5 +183,7 @@ export const volumeService = {
|
||||
createVolume,
|
||||
mountVolume,
|
||||
deleteVolume,
|
||||
getVolume,
|
||||
updateVolume,
|
||||
testConnection,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user