worker prints start success but no attachment yet

This commit is contained in:
Nathan
2025-12-17 16:32:39 -07:00
parent 4628fafc79
commit 0460865ebc
10 changed files with 7160 additions and 45 deletions

View File

@@ -197,12 +197,15 @@ namespace UnifiedFarmLauncher.Services
var sshArgs = BuildSshArgs(parts, interactive);
sshArgs.Add(command);
var argsString = string.Join(" ", sshArgs.Select(arg => $"\"{arg.Replace("\"", "\\\"")}\""));
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = GetSshExecutable(),
Arguments = string.Join(" ", sshArgs.Select(arg => $"\"{arg.Replace("\"", "\\\"")}\"")),
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Arguments = argsString,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
@@ -230,6 +233,54 @@ namespace UnifiedFarmLauncher.Services
return output.ToString();
}
public async Task<string> ExecuteRemoteScriptAsync(WorkerConfig worker, string script, bool interactive = false)
{
var parts = ParseConnectionParts(worker.Ssh.Args, worker.Ssh.Host);
var sshArgs = BuildSshArgs(parts, interactive);
// Add PowerShell command as a single quoted argument for remote execution
sshArgs.Add("powershell -NoLogo -NoProfile -NonInteractive -OutputFormat Text -ExecutionPolicy Bypass -Command -");
var argsString = string.Join(" ", sshArgs.Select(arg => $"\"{arg.Replace("\"", "\\\"")}\""));
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = GetSshExecutable(),
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Arguments = argsString,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = !interactive
}
};
var output = new StringBuilder();
var error = new StringBuilder();
process.OutputDataReceived += (s, e) => { if (e.Data != null) output.AppendLine(e.Data); };
process.ErrorDataReceived += (s, e) => { if (e.Data != null) error.AppendLine(e.Data); };
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Write script to stdin
await process.StandardInput.WriteAsync(script);
process.StandardInput.Close();
await process.WaitForExitAsync();
if (process.ExitCode != 0 && !interactive)
{
throw new InvalidOperationException($"SSH script execution failed with exit code {process.ExitCode}: {error}");
}
return output.ToString();
}
public async Task<string> GetWorkerBasePathAsync(WorkerConfig worker)
{
if (_workerBasePathCache.TryGetValue(worker.Name, out var cached))

View File

@@ -65,7 +65,7 @@ $controllerPath = Join-Path $dataRoot 'controller.ps1'
[IO.File]::WriteAllBytes($controllerPath, [Convert]::FromBase64String('{controllerBase64}'))
";
await _sshService.ExecuteRemoteCommandAsync(worker, $"powershell -NoLogo -NoProfile -NonInteractive -OutputFormat Text -ExecutionPolicy Bypass -EncodedCommand {Convert.ToBase64String(Encoding.Unicode.GetBytes(script))}");
await _sshService.ExecuteRemoteScriptAsync(worker, script);
}
public async Task DeployAttachHelperAsync(WorkerConfig worker)
@@ -79,7 +79,7 @@ $attachPath = Join-Path $dataRoot 'attach-helper.ps1'
[IO.File]::WriteAllBytes($attachPath, [Convert]::FromBase64String('{helperBase64}'))
";
await _sshService.ExecuteRemoteCommandAsync(worker, $"powershell -NoLogo -NoProfile -NonInteractive -OutputFormat Text -ExecutionPolicy Bypass -EncodedCommand {Convert.ToBase64String(Encoding.Unicode.GetBytes(script))}");
await _sshService.ExecuteRemoteScriptAsync(worker, script);
}
public string GenerateSheepItPayload(WorkerConfig worker)
@@ -171,7 +171,10 @@ catch {{
if (worker.WorkerTypes.Flamenco == null)
throw new InvalidOperationException("Worker does not have Flamenco configuration");
var config = _configService.Load();
var flamenco = worker.WorkerTypes.Flamenco;
var globalSettings = config.GlobalSettings;
var workerPath = globalSettings.FlamencoWorkerPath.Replace("'", "''");
var drives = string.Join(", ", Array.ConvertAll(flamenco.NetworkDrives.ToArray(), d => $"'{d}'"));
var paths = string.Join(", ", Array.ConvertAll(flamenco.NetworkPaths.ToArray(), p => $"'{p.Replace("\\", "\\\\")}'"));
@@ -200,7 +203,7 @@ try {{
# Start worker
Write-Host ""Starting Flamenco worker..."" -ForegroundColor Cyan
Set-Location '{flamenco.WorkerPath}'
Set-Location '{workerPath}'
if (Test-Path 'flamenco-worker.exe') {{
Write-Host ""Running flamenco-worker.exe..."" -ForegroundColor Green
$workerProcess = Start-Process -FilePath '.\flamenco-worker.exe' -NoNewWindow -PassThru -Wait
@@ -208,8 +211,8 @@ try {{
Write-Host ""Flamenco worker process has terminated with exit code: $exitCode"" -ForegroundColor Yellow
exit $exitCode
}} else {{
Write-Host ""Error: flamenco-worker.exe not found in {flamenco.WorkerPath}"" -ForegroundColor Red
[Console]::Error.WriteLine(""Error: flamenco-worker.exe not found in {flamenco.WorkerPath}"")
Write-Host ""Error: flamenco-worker.exe not found in {workerPath}"" -ForegroundColor Red
[Console]::Error.WriteLine(""Error: flamenco-worker.exe not found in {workerPath}"")
exit 1
}}
}}
@@ -327,7 +330,8 @@ if ($shouldStart) {{
}}
";
await _sshService.ExecuteRemoteCommandAsync(worker, $"powershell -NoLogo -NoProfile -NonInteractive -OutputFormat Text -ExecutionPolicy Bypass -EncodedCommand {Convert.ToBase64String(Encoding.Unicode.GetBytes(ensureScript))}");
// Pipe script through stdin to avoid command line length limits
await _sshService.ExecuteRemoteScriptAsync(worker, ensureScript);
}
public async Task StopWorkerAsync(WorkerConfig worker, string workerType)