feat: mount volumes on startup

This commit is contained in:
Nicolas Meienberger
2025-09-23 19:15:28 +02:00
parent f67152146d
commit 3734ba2925
14 changed files with 159 additions and 18 deletions

View File

@@ -2,16 +2,14 @@ import { type } from "arktype";
import "dotenv/config";
const envSchema = type({
NODE_ENV: type
.enumerated("development", "production", "test")
.default("development"),
NODE_ENV: type.enumerated("development", "production", "test").default("development"),
VOLUME_ROOT: "string",
}).pipe((s) => ({
__prod__: s.NODE_ENV === "production",
environment: s.NODE_ENV,
dbFileName: "/data/ironmount.db",
volumeRootHost: s.VOLUME_ROOT,
volumeRootContainer: "/mnt/volumes",
volumeRootContainer: "/mounts",
}));
const parseConfig = (env: unknown) => {

View File

@@ -7,6 +7,7 @@ import { runDbMigrations } from "./db/db";
import { driverController } from "./modules/driver/driver.controller";
import { volumeController } from "./modules/volumes/volume.controller";
import { logger } from "./utils/logger";
import { startup } from "./modules/lifecycle/startup";
export const generalDescriptor = (app: Hono) =>
openAPISpecs(app, {
@@ -57,6 +58,8 @@ const socketPath = "/run/docker/plugins/ironmount.sock";
fetch: app.fetch,
});
await startup();
logger.info(`Server is running at http://localhost:8080 and unix socket at ${socketPath}`);
})();

View File

@@ -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`);
}
}
};

View File

@@ -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();
});

View 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));
}
}
};

View File

@@ -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) => {