mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat(server): test mount endpoint
This commit is contained in:
@@ -12,3 +12,5 @@
|
|||||||
!apps/**/drizzle/**
|
!apps/**/drizzle/**
|
||||||
!apps/**/app/**
|
!apps/**/app/**
|
||||||
!apps/**/public/**
|
!apps/**/public/**
|
||||||
|
|
||||||
|
!packages/**/src/**
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ FROM oven/bun:1.2.20-alpine AS base
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY ./package.json ./bun.lock ./
|
COPY ./package.json ./bun.lock ./
|
||||||
|
COPY ./packages/schemas/package.json ./packages/schemas/package.json
|
||||||
COPY ./apps/client/package.json ./apps/client/package.json
|
COPY ./apps/client/package.json ./apps/client/package.json
|
||||||
COPY ./apps/server/package.json ./apps/server/package.json
|
COPY ./apps/server/package.json ./apps/server/package.json
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
// This file is auto-generated by @hey-api/openapi-ts
|
// This file is auto-generated by @hey-api/openapi-ts
|
||||||
|
|
||||||
import { type Options, listVolumes, createVolume, deleteVolume } from "../sdk.gen";
|
import { type Options, listVolumes, createVolume, testConnection, deleteVolume } from "../sdk.gen";
|
||||||
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
|
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
|
||||||
import type {
|
import type {
|
||||||
ListVolumesData,
|
ListVolumesData,
|
||||||
CreateVolumeData,
|
CreateVolumeData,
|
||||||
CreateVolumeResponse,
|
CreateVolumeResponse,
|
||||||
|
TestConnectionData,
|
||||||
|
TestConnectionResponse,
|
||||||
DeleteVolumeData,
|
DeleteVolumeData,
|
||||||
DeleteVolumeResponse,
|
DeleteVolumeResponse,
|
||||||
} from "../types.gen";
|
} from "../types.gen";
|
||||||
@@ -109,6 +111,46 @@ export const createVolumeMutation = (
|
|||||||
return mutationOptions;
|
return mutationOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const testConnectionQueryKey = (options?: Options<TestConnectionData>) =>
|
||||||
|
createQueryKey("testConnection", options);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test connection to backend
|
||||||
|
*/
|
||||||
|
export const testConnectionOptions = (options?: Options<TestConnectionData>) => {
|
||||||
|
return queryOptions({
|
||||||
|
queryFn: async ({ queryKey, signal }) => {
|
||||||
|
const { data } = await testConnection({
|
||||||
|
...options,
|
||||||
|
...queryKey[0],
|
||||||
|
signal,
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
queryKey: testConnectionQueryKey(options),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test connection to backend
|
||||||
|
*/
|
||||||
|
export const testConnectionMutation = (
|
||||||
|
options?: Partial<Options<TestConnectionData>>,
|
||||||
|
): UseMutationOptions<TestConnectionResponse, DefaultError, Options<TestConnectionData>> => {
|
||||||
|
const mutationOptions: UseMutationOptions<TestConnectionResponse, DefaultError, Options<TestConnectionData>> = {
|
||||||
|
mutationFn: async (localOptions) => {
|
||||||
|
const { data } = await testConnection({
|
||||||
|
...options,
|
||||||
|
...localOptions,
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return mutationOptions;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a volume
|
* Delete a volume
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import type {
|
|||||||
ListVolumesResponses,
|
ListVolumesResponses,
|
||||||
CreateVolumeData,
|
CreateVolumeData,
|
||||||
CreateVolumeResponses,
|
CreateVolumeResponses,
|
||||||
|
TestConnectionData,
|
||||||
|
TestConnectionResponses,
|
||||||
DeleteVolumeData,
|
DeleteVolumeData,
|
||||||
DeleteVolumeResponses,
|
DeleteVolumeResponses,
|
||||||
} from "./types.gen";
|
} from "./types.gen";
|
||||||
@@ -54,6 +56,22 @@ export const createVolume = <ThrowOnError extends boolean = false>(
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test connection to backend
|
||||||
|
*/
|
||||||
|
export const testConnection = <ThrowOnError extends boolean = false>(
|
||||||
|
options?: Options<TestConnectionData, ThrowOnError>,
|
||||||
|
) => {
|
||||||
|
return (options?.client ?? _heyApiClient).post<TestConnectionResponses, unknown, ThrowOnError>({
|
||||||
|
url: "/api/v1/volumes/test-connection",
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
...options?.headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a volume
|
* Delete a volume
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export type CreateVolumeData = {
|
|||||||
exportPath: string;
|
exportPath: string;
|
||||||
port: number;
|
port: number;
|
||||||
server: string;
|
server: string;
|
||||||
version: string;
|
version: "3" | "4" | "4.1";
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
backend: "smb";
|
backend: "smb";
|
||||||
@@ -61,6 +61,40 @@ export type CreateVolumeResponses = {
|
|||||||
|
|
||||||
export type CreateVolumeResponse = CreateVolumeResponses[keyof CreateVolumeResponses];
|
export type CreateVolumeResponse = CreateVolumeResponses[keyof CreateVolumeResponses];
|
||||||
|
|
||||||
|
export type TestConnectionData = {
|
||||||
|
body?: {
|
||||||
|
config:
|
||||||
|
| {
|
||||||
|
backend: "directory";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "nfs";
|
||||||
|
exportPath: string;
|
||||||
|
port: number;
|
||||||
|
server: string;
|
||||||
|
version: "3" | "4" | "4.1";
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
backend: "smb";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
path?: never;
|
||||||
|
query?: never;
|
||||||
|
url: "/api/v1/volumes/test-connection";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TestConnectionResponses = {
|
||||||
|
/**
|
||||||
|
* Connection test result
|
||||||
|
*/
|
||||||
|
200: {
|
||||||
|
message: string;
|
||||||
|
success: boolean;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TestConnectionResponse = TestConnectionResponses[keyof TestConnectionResponses];
|
||||||
|
|
||||||
export type DeleteVolumeData = {
|
export type DeleteVolumeData = {
|
||||||
body?: never;
|
body?: never;
|
||||||
path: {
|
path: {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import {
|
|||||||
deleteVolumeDto,
|
deleteVolumeDto,
|
||||||
type ListVolumesResponseDto,
|
type ListVolumesResponseDto,
|
||||||
listVolumesDto,
|
listVolumesDto,
|
||||||
|
testConnectionBody,
|
||||||
|
testConnectionDto,
|
||||||
} from "./volume.dto";
|
} from "./volume.dto";
|
||||||
import { volumeService } from "./volume.service";
|
import { volumeService } from "./volume.service";
|
||||||
|
|
||||||
@@ -35,6 +37,12 @@ export const volumeController = new Hono()
|
|||||||
|
|
||||||
return c.json({ message: "Volume created", volume: res.volume });
|
return c.json({ message: "Volume created", volume: res.volume });
|
||||||
})
|
})
|
||||||
|
.post("/test-connection", testConnectionDto, validator("json", testConnectionBody), async (c) => {
|
||||||
|
const body = c.req.valid("json");
|
||||||
|
const result = await volumeService.testConnection(body.config);
|
||||||
|
|
||||||
|
return c.json(result, 200);
|
||||||
|
})
|
||||||
.delete("/:name", deleteVolumeDto, async (c) => {
|
.delete("/:name", deleteVolumeDto, async (c) => {
|
||||||
const { name } = c.req.param();
|
const { name } = c.req.param();
|
||||||
const res = await volumeService.deleteVolume(name);
|
const res = await volumeService.deleteVolume(name);
|
||||||
|
|||||||
@@ -89,3 +89,32 @@ export const deleteVolumeDto = describeRoute({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test connection
|
||||||
|
*/
|
||||||
|
export const testConnectionBody = type({
|
||||||
|
config: volumeConfigSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const testConnectionResponse = type({
|
||||||
|
success: "boolean",
|
||||||
|
message: "string",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const testConnectionDto = describeRoute({
|
||||||
|
description: "Test connection to backend",
|
||||||
|
operationId: "testConnection",
|
||||||
|
validateResponse: true,
|
||||||
|
tags: ["Volumes"],
|
||||||
|
responses: {
|
||||||
|
200: {
|
||||||
|
description: "Connection test result",
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
schema: resolver(testConnectionResponse),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import * as fs from "node:fs/promises";
|
||||||
|
import * as os from "node:os";
|
||||||
import * as path from "node:path";
|
import * as path from "node:path";
|
||||||
import type { BackendConfig } from "@ironmount/schemas";
|
import type { BackendConfig } from "@ironmount/schemas";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
@@ -33,7 +35,7 @@ const createVolume = async (name: string, backendConfig: BackendConfig) => {
|
|||||||
name: slug,
|
name: slug,
|
||||||
config: backendConfig,
|
config: backendConfig,
|
||||||
path: path.join(volumePathHost, slug),
|
path: path.join(volumePathHost, slug),
|
||||||
type: "nfs",
|
type: backendConfig.backend,
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
@@ -84,9 +86,53 @@ const mountVolume = async (name: string) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const testConnection = async (backendConfig: BackendConfig) => {
|
||||||
|
let tempDir: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ironmount-test-"));
|
||||||
|
|
||||||
|
const mockVolume = {
|
||||||
|
id: 0,
|
||||||
|
name: "test-connection",
|
||||||
|
path: tempDir,
|
||||||
|
config: backendConfig,
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
type: backendConfig.backend,
|
||||||
|
};
|
||||||
|
|
||||||
|
const backend = createVolumeBackend(mockVolume);
|
||||||
|
|
||||||
|
await backend.mount();
|
||||||
|
await backend.unmount();
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "Connection successful",
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: error instanceof Error ? error.message : "Connection failed",
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
if (tempDir) {
|
||||||
|
try {
|
||||||
|
await fs.access(tempDir);
|
||||||
|
await fs.rm(tempDir, { recursive: true, force: true });
|
||||||
|
} catch (cleanupError) {
|
||||||
|
// Ignore cleanup errors if directory doesn't exist or can't be removed
|
||||||
|
console.warn("Failed to cleanup temp directory:", cleanupError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const volumeService = {
|
export const volumeService = {
|
||||||
listVolumes,
|
listVolumes,
|
||||||
createVolume,
|
createVolume,
|
||||||
mountVolume,
|
mountVolume,
|
||||||
deleteVolume,
|
deleteVolume,
|
||||||
|
testConnection,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ sync:
|
|||||||
- "tmp"
|
- "tmp"
|
||||||
- "logs"
|
- "logs"
|
||||||
- "mutagen.yml.lock"
|
- "mutagen.yml.lock"
|
||||||
- "data/ironmount.db"
|
- "data"
|
||||||
ironmount:
|
ironmount:
|
||||||
alpha: "."
|
alpha: "."
|
||||||
beta: "nicolas@192.168.2.42:/home/nicolas/ironmount"
|
beta: "nicolas@192.168.2.42:/home/nicolas/ironmount"
|
||||||
|
|||||||
Reference in New Issue
Block a user