mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: update volume
This commit is contained in:
@@ -32,10 +32,10 @@ export const scalarDescriptor = Scalar({
|
||||
const driver = new Hono().use(honoLogger()).route("/", driverController);
|
||||
const app = new Hono()
|
||||
.use(honoLogger())
|
||||
.get("/healthcheck", (c) => c.json({ status: "ok" }))
|
||||
.route("/api/v1/volumes", volumeController)
|
||||
.get("/assets/*", serveStatic({ root: "./assets/frontend" }))
|
||||
.get("*", serveStatic({ path: "./assets/frontend/index.html" }));
|
||||
.get("*", serveStatic({ root: "./assets/frontend" }))
|
||||
.get("healthcheck", (c) => c.json({ status: "ok" }))
|
||||
.basePath("/api/v1")
|
||||
.route("/volumes", volumeController);
|
||||
|
||||
app.get("/openapi.json", generalDescriptor(app));
|
||||
app.get("/docs", scalarDescriptor);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Hono } from "hono";
|
||||
import { VOLUME_MOUNT_BASE } from "~/core/constants";
|
||||
import { VOLUME_MOUNT_BASE } from "../../core/constants";
|
||||
import { volumeService } from "../volumes/volume.service";
|
||||
|
||||
export const driverController = new Hono()
|
||||
|
||||
@@ -33,7 +33,7 @@ const createVolume = async (name: string, backendConfig: BackendConfig) => {
|
||||
|
||||
const volumePathHost = path.join(VOLUME_MOUNT_BASE);
|
||||
|
||||
const val = await db
|
||||
const [created] = await db
|
||||
.insert(volumesTable)
|
||||
.values({
|
||||
name: slug,
|
||||
@@ -43,7 +43,19 @@ const createVolume = async (name: string, backendConfig: BackendConfig) => {
|
||||
})
|
||||
.returning();
|
||||
|
||||
return { volume: val[0], status: 201 };
|
||||
if (!created) {
|
||||
throw new InternalServerError("Failed to create volume");
|
||||
}
|
||||
|
||||
const backend = createVolumeBackend(created);
|
||||
const { error, status } = await backend.mount();
|
||||
|
||||
await db
|
||||
.update(volumesTable)
|
||||
.set({ status, lastError: error ?? null, lastHealthCheck: new Date() })
|
||||
.where(eq(volumesTable.name, slug));
|
||||
|
||||
return { volume: created, status: 201 };
|
||||
};
|
||||
|
||||
const deleteVolume = async (name: string) => {
|
||||
@@ -123,6 +135,15 @@ const updateVolume = async (name: string, volumeData: UpdateVolumeBody) => {
|
||||
throw new NotFoundError("Volume not found");
|
||||
}
|
||||
|
||||
const configChanged =
|
||||
JSON.stringify(existing.config) !== JSON.stringify(volumeData.config) && volumeData.config !== undefined;
|
||||
|
||||
if (configChanged) {
|
||||
console.log("Unmounting existing volume before applying new config");
|
||||
const backend = createVolumeBackend(existing);
|
||||
await backend.unmount();
|
||||
}
|
||||
|
||||
const [updated] = await db
|
||||
.update(volumesTable)
|
||||
.set({
|
||||
@@ -138,6 +159,15 @@ const updateVolume = async (name: string, volumeData: UpdateVolumeBody) => {
|
||||
throw new InternalServerError("Failed to update volume");
|
||||
}
|
||||
|
||||
if (configChanged) {
|
||||
const backend = createVolumeBackend(updated);
|
||||
const { error, status } = await backend.mount();
|
||||
await db
|
||||
.update(volumesTable)
|
||||
.set({ status, lastError: error ?? null, lastHealthCheck: new Date() })
|
||||
.where(eq(volumesTable.name, name));
|
||||
}
|
||||
|
||||
return { volume: updated };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user