mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: mount volumes on startup
This commit is contained in:
@@ -2,6 +2,7 @@ import type { BackendStatus } from "@ironmount/schemas";
|
||||
import type { Volume } from "../../db/schema";
|
||||
import { makeDirectoryBackend } from "./directory/directory-backend";
|
||||
import { makeNfsBackend } from "./nfs/nfs-backend";
|
||||
import { config } from "../../core/config";
|
||||
|
||||
export type VolumeBackend = {
|
||||
mount: () => Promise<void>;
|
||||
@@ -10,17 +11,17 @@ export type VolumeBackend = {
|
||||
};
|
||||
|
||||
export const createVolumeBackend = (volume: Volume): VolumeBackend => {
|
||||
const { config, path } = volume;
|
||||
const path = `${config.volumeRootContainer}/${volume.name}/_data`;
|
||||
|
||||
switch (config.backend) {
|
||||
switch (volume.config.backend) {
|
||||
case "nfs": {
|
||||
return makeNfsBackend(config, path);
|
||||
return makeNfsBackend(volume.config, path);
|
||||
}
|
||||
case "directory": {
|
||||
return makeDirectoryBackend(config, path);
|
||||
return makeDirectoryBackend(volume.config, path);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Backend ${config.backend} not implemented`);
|
||||
throw new Error(`Backend ${volume.config.backend} not implemented`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -69,6 +69,11 @@ const unmount = async (path: string) => {
|
||||
logger.error(`Error unmounting NFS volume: ${stderr}`);
|
||||
return reject(new Error(`Failed to unmount NFS volume: ${stderr}`));
|
||||
}
|
||||
|
||||
fs.rmdir(path).catch((rmdirError) => {
|
||||
logger.error(`Failed to remove directory ${path}:`, rmdirError);
|
||||
});
|
||||
|
||||
logger.info(`NFS volume unmounted successfully: ${stdout}`);
|
||||
resolve();
|
||||
});
|
||||
|
||||
22
apps/server/src/modules/lifecycle/startup.ts
Normal file
22
apps/server/src/modules/lifecycle/startup.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../../db/db";
|
||||
import { logger } from "../../utils/logger";
|
||||
import { volumesTable } from "../../db/schema";
|
||||
import { createVolumeBackend } from "../backends/backend";
|
||||
|
||||
export const startup = async () => {
|
||||
logger.info("Mounting all volumes...");
|
||||
|
||||
const volumes = await db.query.volumesTable.findMany({ where: eq(volumesTable.status, "mounted") });
|
||||
|
||||
for (const volume of volumes) {
|
||||
try {
|
||||
const backend = createVolumeBackend(volume);
|
||||
await backend.mount();
|
||||
logger.info(`Mounted volume ${volume.name} successfully`);
|
||||
} catch (error) {
|
||||
logger.error(`Failed to mount volume ${volume.name}:`, error);
|
||||
await db.update(volumesTable).set({ status: "unmounted" }).where(eq(volumesTable.name, volume.name));
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -17,7 +17,6 @@ import {
|
||||
unmountVolumeDto,
|
||||
} from "./volume.dto";
|
||||
import { volumeService } from "./volume.service";
|
||||
import { logger } from "../../utils/logger";
|
||||
|
||||
export const volumeController = new Hono()
|
||||
.get("/", listVolumesDto, async (c) => {
|
||||
|
||||
Reference in New Issue
Block a user