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,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) => {
|
||||
|
||||
@@ -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}`);
|
||||
})();
|
||||
|
||||
|
||||
@@ -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