import type { PluginContext } from "@termix/plugin-sdk/backend"; import { normalizeContainerRuntime, type ContainerRuntime, } from "./container-runtime.js"; export const PLUGIN_ID = "docker"; export interface DockerHostSettings { enabled: boolean; runtime: ContainerRuntime; } export async function readDockerHostSettings( ctx: PluginContext, hostId: number, ): Promise { const values = await ctx.settings.getAll("host", hostId); return { enabled: values.enableDocker === true, runtime: normalizeContainerRuntime(values.containerRuntime), }; } function asObject(value: unknown): Record | undefined { let parsed = value; if (typeof value === "string") { try { parsed = JSON.parse(value); } catch { return undefined; } } return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record) : undefined; } function asBool(value: unknown): boolean | undefined { if (typeof value === "boolean") return value; if (value === 1 || value === "1" || value === "true") return true; if (value === 0 || value === "0" || value === "false") return false; return undefined; } /** * The docker host settings in an imported or created host row: the nested * `pluginSettings.docker` of a Termix export first, then the flat * `enableDocker` and `dockerConfig.runtime` hosts had before 2.9.0 (older * exports, proxmox guests marked as Docker hosts). */ export function normalizeImportedHost( raw: Record, ): Record | null { const nested = asObject(asObject(raw.pluginSettings)?.[PLUGIN_ID]) ?? {}; const out: Record = {}; const enabled = asBool( nested.enableDocker !== undefined ? nested.enableDocker : raw.enableDocker, ); if (enabled !== undefined) out.enableDocker = enabled; const runtime = nested.containerRuntime ?? asObject(raw.dockerConfig)?.runtime; if (runtime === "docker" || runtime === "podman") { out.containerRuntime = runtime; } return Object.keys(out).length > 0 ? out : null; }