fix: support PK01 Codex pool sync
This commit is contained in:
@@ -28,6 +28,59 @@ import { codexPoolRuntimeTarget } from "./runtime-target";
|
||||
import { sub2apiConfigPath } from "./types";
|
||||
|
||||
export async function fetchPoolApiKey(config: UniDeskConfig, pool: CodexPoolConfig, target = codexPoolRuntimeTarget()): Promise<{ apiKey: string | null; error: string | null }> {
|
||||
if (target.runtimeMode === "host-docker") {
|
||||
const envPath = target.hostDockerEnvPath;
|
||||
if (envPath === null) return { apiKey: null, error: "host-docker envPath missing" };
|
||||
const result = await capture(config, target.route, ["sh"], `
|
||||
set -u
|
||||
python3 - <<'PY'
|
||||
import base64
|
||||
import json
|
||||
import subprocess
|
||||
path = ${JSON.stringify(envPath)}
|
||||
key = ${JSON.stringify(pool.apiKeySecretKey)}
|
||||
values = {}
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as handle:
|
||||
lines = handle.read().splitlines()
|
||||
except FileNotFoundError:
|
||||
print(json.dumps({"ok": False, "error": "env-source-missing", "path": path, "valuesPrinted": False}))
|
||||
raise SystemExit(1)
|
||||
except PermissionError:
|
||||
proc = subprocess.run(["sudo", "-n", "cat", path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if proc.returncode != 0:
|
||||
print(json.dumps({"ok": False, "error": "env-source-unreadable", "path": path, "stderrTail": proc.stderr.decode("utf-8", errors="replace")[-500:], "valuesPrinted": False}))
|
||||
raise SystemExit(1)
|
||||
lines = proc.stdout.decode("utf-8", errors="replace").splitlines()
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith("#") or "=" not in stripped:
|
||||
continue
|
||||
current_key, value = stripped.split("=", 1)
|
||||
current_key = current_key.strip()
|
||||
value = value.strip()
|
||||
if len(value) >= 2 and value[0] == value[-1] and value[0] in ("'", '"'):
|
||||
value = value[1:-1]
|
||||
values[current_key] = value
|
||||
value = values.get(key)
|
||||
if not value:
|
||||
print(json.dumps({"ok": False, "error": "api-key-missing", "path": path, "key": key, "valuesPrinted": False}))
|
||||
raise SystemExit(1)
|
||||
print(json.dumps({"ok": True, "apiKeyB64": base64.b64encode(value.encode()).decode(), "path": path, "key": key, "valuesPrinted": False}))
|
||||
PY
|
||||
`);
|
||||
if (result.exitCode !== 0) return { apiKey: null, error: `read host pool API key source failed: ${result.stderr.slice(-1000) || result.stdout.slice(-1000)}` };
|
||||
const parsed = parseJsonOutput(result.stdout);
|
||||
if (!isRecord(parsed) || parsed.ok !== true || typeof parsed.apiKeyB64 !== "string") {
|
||||
return { apiKey: null, error: `${envPath}.${pool.apiKeySecretKey} missing` };
|
||||
}
|
||||
try {
|
||||
const apiKey = Buffer.from(parsed.apiKeyB64, "base64").toString("utf8");
|
||||
return apiKey.length > 0 ? { apiKey, error: null } : { apiKey: null, error: "decoded API key is empty" };
|
||||
} catch (error) {
|
||||
return { apiKey: null, error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
}
|
||||
const result = await capture(config, target.route, ["sh"], `
|
||||
set -u
|
||||
kubectl -n ${target.namespace} get secret ${pool.apiKeySecretName} -o json
|
||||
|
||||
Reference in New Issue
Block a user