Files
UFL/Services/AttachService.cs
2025-12-17 16:51:41 -07:00

47 lines
1.7 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using UnifiedFarmLauncher.Models;
namespace UnifiedFarmLauncher.Services
{
public class AttachService
{
private readonly SshService _sshService;
private readonly WorkerControllerService _controllerService;
public AttachService(SshService sshService, WorkerControllerService controllerService)
{
_sshService = sshService;
_controllerService = controllerService;
}
public async Task AttachToWorkerAsync(WorkerConfig worker, string workerType, bool commandOnly = false, string? command = null)
{
await _controllerService.DeployAttachHelperAsync(worker);
var remoteBasePath = await _sshService.GetWorkerBasePathAsync(worker);
var remoteHelper = Path.Combine(remoteBasePath, "attach-helper.ps1");
var paramsBlock = $"-WorkerName \"{worker.Name}\" -WorkerType \"{workerType}\"";
if (commandOnly)
{
paramsBlock += " -CommandOnly";
}
if (!string.IsNullOrEmpty(command))
{
paramsBlock += $" -Command \"{command}\"";
}
// Use Windows path format (backslashes) and ensure it's properly quoted
// Add -NoExit to keep window open and ensure output is visible
// Use -Command with & to properly invoke the script and avoid pipeline input issues
var escapedHelper = remoteHelper.Replace("'", "''");
var remoteCmd = $"powershell.exe -NoLogo -NoProfile -NoExit -ExecutionPolicy Bypass -Command \"& '{escapedHelper}' {paramsBlock}\"";
_sshService.StartInteractiveSsh(worker, remoteCmd);
}
}
}