feat(cli): migrate child json recovery callers
This commit is contained in:
@@ -10,6 +10,7 @@ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "
|
||||
import { dirname, join } from "node:path";
|
||||
import { repoRoot, rootPath, type Config } from "../config";
|
||||
import { runCommand, type CommandResult } from "../command";
|
||||
import { resolveCliChildJsonCommandResult } from "../cli-child-json-recovery";
|
||||
import { startJob } from "../jobs";
|
||||
import { classifySshTcpPoolFailure } from "../ssh";
|
||||
import { HWLAB_NODE_CONTROL_PLANE_CONFIG_PATH, hwlabNodeControlPlaneInfraHelp, runHwlabNodeControlPlaneInfra } from "../hwlab-node-control-plane";
|
||||
@@ -36,15 +37,6 @@ import { parseNodeScopedDelegatedOptions } from "./plan";
|
||||
import { compactRuntimeCommand, compactRuntimeCommandStats, nodeRuntimeUnsupportedAction, runNodeHostScript, transPath } from "./runtime-common";
|
||||
import { optionValue, positiveIntegerOption, shellQuote, statusText } from "./utils";
|
||||
|
||||
export function parseJsonObject(text: string): Record<string, unknown> {
|
||||
try {
|
||||
const parsed = JSON.parse(text) as unknown;
|
||||
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed as Record<string, unknown> : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function commandResultFromAsync(spec: HwlabRuntimeLaneSpec, payload: Record<string, unknown>, statusPath: string, timedOut: boolean): CommandResult {
|
||||
return {
|
||||
command: [transPath(), spec.nodeRoute, "sh", "--", "<remote async script>"],
|
||||
@@ -102,7 +94,7 @@ export function nodeRuntimeGitopsRoot(spec: HwlabRuntimeLaneSpec): string {
|
||||
return spec.gitopsRoot;
|
||||
}
|
||||
|
||||
export function resolveNodeRuntimeLaneHead(spec: HwlabRuntimeLaneSpec): { sourceCommit: string | null; result: CommandResult } {
|
||||
export function resolveNodeRuntimeLaneHead(spec: HwlabRuntimeLaneSpec): { sourceCommit: string | null; result: CommandResult; stdoutRecovery?: Record<string, unknown> } {
|
||||
const mirror = nodeRuntimeSourceMirrorTarget(spec);
|
||||
const script = [
|
||||
"set +e",
|
||||
@@ -135,10 +127,25 @@ export function resolveNodeRuntimeLaneHead(spec: HwlabRuntimeLaneSpec): { source
|
||||
"NODE",
|
||||
].join("\n");
|
||||
const result = runNodeK3sScript(spec, script, 45);
|
||||
const payload = parseJsonObject(result.stdout);
|
||||
const payloadResolution = resolveRuntimeCleanupJson(result, "node runtime source snapshot JSON");
|
||||
const payload = payloadResolution.parsed ?? {};
|
||||
const payloadCommit = typeof payload.sourceCommit === "string" && /^[0-9a-f]{40}$/iu.test(payload.sourceCommit) ? payload.sourceCommit.toLowerCase() : null;
|
||||
const match = payloadCommit ?? /[0-9a-f]{40}/iu.exec(statusText(result))?.[0].toLowerCase() ?? null;
|
||||
return { sourceCommit: result.exitCode === 0 ? match : null, result };
|
||||
return { sourceCommit: result.exitCode === 0 ? match : null, result, stdoutRecovery: payloadResolution.diagnostics };
|
||||
}
|
||||
|
||||
function resolveRuntimeCleanupJson(result: CommandResult, requestedStdoutType: string): { parsed: Record<string, unknown> | null; diagnostics: Record<string, unknown> } {
|
||||
const resolved = resolveCliChildJsonCommandResult({
|
||||
result,
|
||||
requestedStdoutType,
|
||||
acceptParsed: (value) => typeof value.ok === "boolean"
|
||||
|| typeof value.mode === "string"
|
||||
|| typeof value.sourceCommit === "string"
|
||||
|| typeof value.degradedReason === "string"
|
||||
|| typeof value.error === "string"
|
||||
|| Object.keys(value).length > 1,
|
||||
});
|
||||
return { parsed: resolved.parsed, diagnostics: resolved.diagnostics };
|
||||
}
|
||||
|
||||
interface NodeRuntimeSourceMirrorTarget {
|
||||
@@ -688,7 +695,8 @@ export function nodeRuntimeCleanupLegacyDockerImages(scoped: ReturnType<typeof p
|
||||
"UNIDESK_LEGACY_DOCKER_IMAGE_GC",
|
||||
].join("\n");
|
||||
const result = runNodeHostScript(scoped.spec, script, scoped.timeoutSeconds);
|
||||
const payload = parseJsonObject(result.stdout);
|
||||
const payloadResolution = resolveRuntimeCleanupJson(result, "legacy docker image cleanup JSON");
|
||||
const payload = payloadResolution.parsed ?? {};
|
||||
const ok = isCommandSuccess(result) && payload.ok !== false;
|
||||
return {
|
||||
ok,
|
||||
@@ -711,7 +719,7 @@ export function nodeRuntimeCleanupLegacyDockerImages(scoped: ReturnType<typeof p
|
||||
valuesRedacted: true,
|
||||
},
|
||||
cleanup: options.dryRun || ok ? compactNodeRuntimeLegacyDockerImageCleanupPayload(payload) : payload,
|
||||
result: ok ? compactRuntimeCommandStats(result) : compactRuntimeCommand(result),
|
||||
result: { ...(ok ? compactRuntimeCommandStats(result) : compactRuntimeCommand(result)), stdoutRecovery: payloadResolution.diagnostics },
|
||||
degradedReason: ok ? undefined : "legacy-docker-image-cleanup-failed",
|
||||
next: options.dryRun
|
||||
? { confirm: `${command} --min-age-hours ${options.minAgeHours} --keep-per-repository ${options.keepPerRepository} --limit ${options.limit} --confirm` }
|
||||
@@ -769,7 +777,8 @@ export function nodeRuntimeCleanupLegacyDockerRegistryVolume(scoped: ReturnType<
|
||||
"UNIDESK_LEGACY_DOCKER_REGISTRY_VOLUME_GC",
|
||||
].join("\n");
|
||||
const result = runNodeHostScript(scoped.spec, script, scoped.timeoutSeconds);
|
||||
const payload = parseJsonObject(result.stdout);
|
||||
const payloadResolution = resolveRuntimeCleanupJson(result, "legacy docker registry volume cleanup JSON");
|
||||
const payload = payloadResolution.parsed ?? {};
|
||||
const ok = isCommandSuccess(result) && payload.ok !== false;
|
||||
return {
|
||||
ok,
|
||||
@@ -784,7 +793,7 @@ export function nodeRuntimeCleanupLegacyDockerRegistryVolume(scoped: ReturnType<
|
||||
},
|
||||
target,
|
||||
cleanup: compactNodeRuntimeLegacyDockerRegistryVolumeCleanupPayload(payload),
|
||||
result: ok ? compactRuntimeCommandStats(result) : compactRuntimeCommand(result),
|
||||
result: { ...(ok ? compactRuntimeCommandStats(result) : compactRuntimeCommand(result)), stdoutRecovery: payloadResolution.diagnostics },
|
||||
degradedReason: ok ? undefined : "legacy-docker-registry-volume-cleanup-failed",
|
||||
next: dryRun
|
||||
? { confirm: `${command} --confirm --wait` }
|
||||
|
||||
Reference in New Issue
Block a user