diff --git a/scripts/src/platform-infra-k3s-cluster.ts b/scripts/src/platform-infra-k3s-cluster.ts index 08847f9e..2fb2b106 100644 --- a/scripts/src/platform-infra-k3s-cluster.ts +++ b/scripts/src/platform-infra-k3s-cluster.ts @@ -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'}",