fix(k3s): stream worker control scripts over ssh

This commit is contained in:
pikastech
2026-07-20 08:04:28 +02:00
parent 4193b6b749
commit cebd55e28e
+36 -10
View File
@@ -719,6 +719,34 @@ function runControlPlane(target: K3sClusterTarget, script: string, timeoutMs: nu
function runWorker(target: K3sClusterTarget, script: string, copyArtifacts: boolean, timeoutMs: number) {
const encoded = Buffer.from(script, "utf8").toString("base64");
const keyPath = `${target.worker.stateDir}\\id_ed25519`;
if (!copyArtifacts) {
const lines = [
"$ErrorActionPreference='Stop'",
`$key=${psQuote(keyPath)}`,
`$guest=${psQuote(`${target.worker.guestUser}@${target.worker.guestAddress}`)}`,
`$payload=${psQuote(encoded)}`,
"$start=[Diagnostics.ProcessStartInfo]::new()",
"$start.FileName='ssh.exe'",
'$start.Arguments="-i `"$key`" -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=15 $guest `"sudo sh -s`""',
"$start.UseShellExecute=$false",
"$start.RedirectStandardInput=$true",
"$start.RedirectStandardOutput=$true",
"$start.RedirectStandardError=$true",
"$process=[Diagnostics.Process]::new()",
"$process.StartInfo=$start",
"if(!$process.Start()){throw 'worker-inline-ssh-start-failed'}",
"$stdout=$process.StandardOutput.ReadToEndAsync()",
"$stderr=$process.StandardError.ReadToEndAsync()",
"$bytes=[Convert]::FromBase64String($payload)",
"$process.StandardInput.BaseStream.Write($bytes,0,$bytes.Length)",
"$process.StandardInput.Close()",
"$process.WaitForExit()",
"[Console]::Out.Write($stdout.Result)",
"[Console]::Error.Write($stderr.Result)",
"exit $process.ExitCode",
];
return runCommand(["trans", target.worker.windowsRoute, "ps"], rootPath(), { input: lines.join("\n") + "\n", timeoutMs });
}
const remoteScript = `/tmp/unidesk-k3s-cluster-${target.id}.sh`;
const lines = [
"$ErrorActionPreference='Stop'",
@@ -727,16 +755,14 @@ function runWorker(target: K3sClusterTarget, script: string, copyArtifacts: bool
`$temp=Join-Path $env:TEMP ${psQuote(`unidesk-k3s-cluster-${target.id}.sh`)}`,
`[IO.File]::WriteAllBytes($temp,[Convert]::FromBase64String(${psQuote(encoded)}))`,
];
if (copyArtifacts) {
lines.push(
`$binary=${psQuote(`${target.worker.stateDir}\\k3s`)}`,
`$installer=${psQuote(`${target.worker.stateDir}\\install-k3s.sh`)}`,
"& scp.exe -i $key -o BatchMode=yes -o StrictHostKeyChecking=accept-new $binary \"$($guest):/tmp/k3s\"",
"if($LASTEXITCODE -ne 0){throw 'worker-k3s-binary-scp-failed'}",
"& scp.exe -i $key -o BatchMode=yes -o StrictHostKeyChecking=accept-new $installer \"$($guest):/tmp/install-k3s.sh\"",
"if($LASTEXITCODE -ne 0){throw 'worker-k3s-installer-scp-failed'}",
);
}
lines.push(
`$binary=${psQuote(`${target.worker.stateDir}\\k3s`)}`,
`$installer=${psQuote(`${target.worker.stateDir}\\install-k3s.sh`)}`,
"& scp.exe -i $key -o BatchMode=yes -o StrictHostKeyChecking=accept-new $binary \"$($guest):/tmp/k3s\"",
"if($LASTEXITCODE -ne 0){throw 'worker-k3s-binary-scp-failed'}",
"& scp.exe -i $key -o BatchMode=yes -o StrictHostKeyChecking=accept-new $installer \"$($guest):/tmp/install-k3s.sh\"",
"if($LASTEXITCODE -ne 0){throw 'worker-k3s-installer-scp-failed'}",
);
lines.push(
`& scp.exe -i $key -o BatchMode=yes -o StrictHostKeyChecking=accept-new $temp "$($guest):${remoteScript}"`,
"if($LASTEXITCODE -ne 0){throw 'worker-script-scp-failed'}",