feat: support custom server deployment drafts

This commit is contained in:
npc0-hue
2026-07-24 16:56:28 +08:00
parent 292b380f3c
commit 220ef91a8e
36 changed files with 1520 additions and 85 deletions
+115 -3
View File
@@ -21,6 +21,8 @@ import type {
ServerMemberResponse,
ServerMetricsResponse,
RuntimeBindingResponse,
RunEndpointResponse,
ServerDeploymentResponse,
ServerRuntimeActionsResponse,
MetricSampleResponse,
RemoteAdapterDeclarationResponse
@@ -46,7 +48,7 @@ import {
import { DiagnosticSummary, EmptyState, ErrorState, LoadingState, ResultBadge } from "../components/StateViews";
import type { PageComponentProps } from "../contracts/page";
import type { PluginBridgeAction, PluginBridgeManifestContract } from "../contracts/pluginBridge";
import { canStartServer, canStopServer, pluginLabel, runtimeBindingFields, serverMetadataFormFromInstance, type ServerMetadataFormState } from "../contracts/serverManagement";
import { canStartServer, canStopServer, endpointLabel, pluginLabel, runtimeBindingFields, serverMetadataFormFromInstance, type ServerMetadataFormState } from "../contracts/serverManagement";
import {
serverDetailSections,
serverIsOnline,
@@ -89,6 +91,8 @@ export function ServerDetailPage({ session, params, operations, onNavigate }: Pa
const [remoteAdapters, setRemoteAdapters] = useState<RemoteAdapterDeclarationResponse[]>([]);
const [runtimeActions, setRuntimeActions] = useState<LoadState<ServerRuntimeActionsResponse>>({ status: "loading" });
const [runtimeBinding, setRuntimeBinding] = useState<LoadState<RuntimeBindingResponse>>({ status: "loading" });
const [deployment, setDeployment] = useState<LoadState<ServerDeploymentResponse>>({ status: "loading" });
const [endpoints, setEndpoints] = useState<RunEndpointResponse[]>([]);
const [confirm, setConfirm] = useState<null | { title: string; description: string; danger?: boolean; run: () => Promise<void> }>(null);
const [confirmBusy, setConfirmBusy] = useState(false);
@@ -99,9 +103,10 @@ export function ServerDetailPage({ session, params, operations, onNavigate }: Pa
}
setInstance({ status: "loading" });
try {
const [detail, pluginResponse, jobResponse, runtimeResponse, bindingResponse, metricHistoryResponse, backupResponse, adapterResponse] = await Promise.all([
const [detail, pluginResponse, endpointResponse, jobResponse, runtimeResponse, bindingResponse, deploymentResponse, metricHistoryResponse, backupResponse, adapterResponse] = await Promise.all([
platformApiClient.getServerInstance(serverId),
platformApiClient.listGamePlugins(),
platformApiClient.listRunEndpoints(),
platformApiClient.listJobs(serverId),
platformApiClient
.getServerRuntimeActions(serverId)
@@ -111,15 +116,21 @@ export function ServerDetailPage({ session, params, operations, onNavigate }: Pa
.getServerRuntimeBinding(serverId)
.then((data): LoadState<RuntimeBindingResponse> => ({ status: "ready", data }))
.catch((error): LoadState<RuntimeBindingResponse> => ({ status: "error", reason: error instanceof Error ? error.message : "运行配置加载失败" })),
platformApiClient
.getServerDeployment(serverId)
.then((data): LoadState<ServerDeploymentResponse> => ({ status: "ready", data }))
.catch((error): LoadState<ServerDeploymentResponse> => ({ status: "error", reason: error instanceof Error ? error.message : "部署定义加载失败" })),
platformApiClient.listMetricHistory(serverId).catch(() => ({ items: [], count: 0 })),
platformApiClient.listBackups(serverId).catch(() => ({ items: [], count: 0 })),
platformApiClient.listRemoteAdapters(serverId).catch(() => ({ items: [], count: 0 }))
]);
setInstance({ status: "ready", data: detail });
setPlugins(pluginResponse.items);
setEndpoints(endpointResponse.items);
setJobs(jobResponse.items);
setRuntimeActions(runtimeResponse);
setRuntimeBinding(bindingResponse);
setDeployment(deploymentResponse);
setMetricHistory(metricHistoryResponse.items);
setBackups(backupResponse.items);
setRemoteAdapters(adapterResponse.items);
@@ -137,6 +148,7 @@ export function ServerDetailPage({ session, params, operations, onNavigate }: Pa
setArtifacts([]);
setRuntimeActions({ status: "error", reason: "运行分发状态加载失败" });
setRuntimeBinding({ status: "error", reason: "运行配置加载失败" });
setDeployment({ status: "error", reason: "部署定义加载失败" });
setMetricHistory([]);
setBackups([]);
setRemoteAdapters([]);
@@ -295,6 +307,7 @@ export function ServerDetailPage({ session, params, operations, onNavigate }: Pa
onChanged={() => void refresh()}
/>
)}
{section === "overview" && <ServerDeploymentSection instance={instance.data} deployment={deployment} endpoints={endpoints} session={session} operations={operations} onChanged={() => void refresh()} />}
{section === "overview" && <RuntimeDLLExtensionsPanel runtimeProfiles={plugins.find((plugin) => plugin.id === instance.data.pluginId)?.runtimeProfiles} />}
{section === "overview" && <SourceRCONCommandPanel serverId={instance.data.id} pluginId={instance.data.pluginId} />}
{section === "overview" && (
@@ -412,6 +425,104 @@ function ServerMetadataSection({ instance, session, operations, onChanged }: Ser
);
}
interface ServerDeploymentSectionProps {
instance: ServerInstanceResponse;
deployment: LoadState<ServerDeploymentResponse>;
endpoints: RunEndpointResponse[];
session: PageComponentProps["session"];
operations: PageComponentProps["operations"];
onChanged: () => void;
}
function ServerDeploymentSection({ instance, deployment, endpoints, session, operations, onChanged }: ServerDeploymentSectionProps) {
const [runEndpointId, setRunEndpointId] = useState(instance.runEndpointId);
const [mode, setMode] = useState<"guided-install" | "existing-server" | "custom-command">("guided-install");
const [serverRoot, setServerRoot] = useState("");
const [workingDirectory, setWorkingDirectory] = useState("");
const [installCommand, setInstallCommand] = useState("");
const [startCommand, setStartCommand] = useState("");
const [stopCommand, setStopCommand] = useState("");
const [statusCommand, setStatusCommand] = useState("");
const [shell, setShell] = useState<"" | "posix-sh" | "powershell" | "cmd">("");
const [busy, setBusy] = useState(false);
const [result, setResult] = useState<{ status: "succeeded" | "failed"; label: string } | null>(null);
useEffect(() => {
setRunEndpointId(instance.runEndpointId);
if (deployment.status === "ready") {
setMode(deployment.data.mode ?? "guided-install");
setShell(deployment.data.shell ?? "");
}
}, [deployment, instance.id, instance.runEndpointId]);
async function save(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setBusy(true);
setResult(null);
const operationId = operations.begin({ intent: "更新部署定义", targetKind: "server", targetId: instance.id, requester: session.displayName });
try {
await platformApiClient.updateServerDeployment(instance.id, { runEndpointId: runEndpointId || undefined, mode, serverRoot: serverRoot.trim() || undefined, workingDirectory: workingDirectory.trim() || undefined, installCommand: installCommand.trim() || undefined, startCommand: startCommand.trim() || undefined, stopCommand: stopCommand.trim() || undefined, statusCommand: statusCommand.trim() || undefined, shell: shell || undefined });
operations.succeed(operationId, "部署定义已保存;受保护路径和命令不会回显。");
setResult({ status: "succeeded", label: "部署定义已保存" });
setServerRoot(""); setWorkingDirectory(""); setInstallCommand(""); setStartCommand(""); setStopCommand(""); setStatusCommand("");
onChanged();
} catch (error) {
const label = error instanceof Error ? error.message : "部署定义保存失败";
operations.fail(operationId, label);
setResult({ status: "failed", label });
} finally { setBusy(false); }
}
async function deploy() {
setBusy(true);
setResult(null);
const operationId = operations.begin({ intent: "部署服务器", targetKind: "server", targetId: instance.id, requester: session.displayName });
try {
const response = await platformApiClient.deployServerInstance(instance.id, { expectedConfigVersion: instance.configVersion, idempotencyKey: `web:deploy:${instance.id}:${Date.now()}` });
operations.succeed(operationId, response.job.id ? "部署任务已进入队列,等待 Run 领取。" : "部署请求已接受。");
setResult({ status: "succeeded", label: deploymentProgressLabel(response.job.progress) });
onChanged();
} catch (error) {
const label = error instanceof Error ? error.message : "部署失败";
operations.fail(operationId, label);
setResult({ status: "failed", label });
} finally { setBusy(false); }
}
if (deployment.status === "loading") return <LoadingState label="正在加载部署定义…" compact />;
if (deployment.status === "error") return <ErrorState title="部署定义不可用" reason={deployment.reason} diagnosticId={`deployment:${instance.id}`} compact />;
const view = deployment.data;
return <article className="console-panel" aria-label="server deployment">
<div className="panel-header"><h2><PackageOpen size={16} style={{ verticalAlign: "-2px" }} /> </h2><span className="page-status">{view.mode || "未配置"} · {view.revision}</span></div>
<p className="section-copy"></p>
<div className="action-list"><span>{view.serverRootConfigured ? "已配置" : "未配置"}</span><span>{view.workingDirectoryConfigured ? "已配置" : "未配置"}</span><span> / {view.installCommandConfigured ? "已配置" : "未配置"} / {view.startCommandConfigured ? "已配置" : "未配置"}</span></div>
<form className="provider-form" style={{ marginTop: 12 }} onSubmit={(event) => void save(event)}>
<div className="form-grid">
<label><select value={runEndpointId} onChange={(event) => setRunEndpointId(event.target.value)}><option value=""></option>{endpoints.map((endpoint) => <option key={endpoint.id} value={endpoint.id}>{endpointLabel(endpoint, endpoint.id)}</option>)}</select></label>
<label><select value={mode} onChange={(event) => setMode(event.target.value as typeof mode)}><option value="guided-install"></option><option value="existing-server"></option><option value="custom-command"></option></select></label>
<label><input value={serverRoot} onChange={(event) => setServerRoot(event.target.value)} placeholder="完整绝对路径" autoComplete="off" /></label>
<label><input value={workingDirectory} onChange={(event) => setWorkingDirectory(event.target.value)} placeholder="完整绝对路径" autoComplete="off" /></label>
{mode === "custom-command" && <><label><select value={shell} onChange={(event) => setShell(event.target.value as typeof shell)}><option value=""> argv</option><option value="posix-sh">POSIX sh</option><option value="powershell">PowerShell</option><option value="cmd">Windows cmd</option></select></label><label><input value={installCommand} onChange={(event) => setInstallCommand(event.target.value)} autoComplete="off" /></label><label><input value={startCommand} onChange={(event) => setStartCommand(event.target.value)} autoComplete="off" /></label><label><input value={stopCommand} onChange={(event) => setStopCommand(event.target.value)} autoComplete="off" /></label><label><input value={statusCommand} onChange={(event) => setStatusCommand(event.target.value)} autoComplete="off" /></label></>}
</div>
<div className="action-strip"><button type="submit" className="primary-command" disabled={busy}><Pencil size={14} /><span>{busy ? "保存中…" : "保存部署定义"}</span></button>{instance.state === "draft" || instance.state === "failed" ? <button type="button" className="icon-command" disabled={busy || !runEndpointId} onClick={() => void deploy()}><Sparkles size={14} /><span> Run</span></button> : null}</div>
</form>
{result && <div style={{ marginTop: 10 }}><ResultBadge status={result.status} label={result.label} /></div>}
</article>;
}
function deploymentProgressLabel(progress: JobResponse["progress"]): string {
switch (progress.phase) {
case "queued": return "任务已排队,等待 Run 领取";
case "claimed": return "Run 已领取任务";
case "preflight": return "正在执行本机预检";
case "install": return "正在安装服务器";
case "configure": return "正在写入游戏配置";
case "start": return "正在启动服务器";
case "health": return "正在进行健康检查";
default: return "部署任务已提交";
}
}
interface ServerAdministratorsSectionProps {
instance: ServerInstanceResponse;
session: PageComponentProps["session"];
@@ -587,7 +698,7 @@ function OverviewSection({ instance, metrics, jobs, onOpenLogs }: OverviewSectio
{failed.length > 0 && <span> {failed.length} </span>}
{pending.length > 0 ? (
<span>
{pending[0].capability}{pending[0].state}{pending[0].progress.percent}%
{pending[0].capability}{deploymentProgressLabel(pending[0].progress)}{pending[0].progress.percent}%
</span>
) : (
<span></span>
@@ -2267,6 +2378,7 @@ function HistorySection({ serverId, serverOperations, jobs, artifacts, metricHis
<code>{job.id}</code>
</span>
<span> {job.progress.percent}%</span>
{job.progress.phase && <span>{deploymentProgressLabel(job.progress)}</span>}
<span>
{job.attempt}/{job.retryPolicy.maxAttempts}
</span>