2025-12-17 15:34:34 -07:00
|
|
|
using System.Diagnostics;
|
2025-12-17 16:46:35 -07:00
|
|
|
using System.IO;
|
2025-12-17 15:34:34 -07:00
|
|
|
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);
|
2025-12-17 16:46:35 -07:00
|
|
|
var remoteHelper = Path.Combine(remoteBasePath, "attach-helper.ps1");
|
2025-12-17 15:34:34 -07:00
|
|
|
|
|
|
|
|
var paramsBlock = $"-WorkerName \"{worker.Name}\" -WorkerType \"{workerType}\"";
|
|
|
|
|
if (commandOnly)
|
|
|
|
|
{
|
|
|
|
|
paramsBlock += " -CommandOnly";
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(command))
|
|
|
|
|
{
|
|
|
|
|
paramsBlock += $" -Command \"{command}\"";
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 16:46:35 -07:00
|
|
|
// Use Windows path format (backslashes) and ensure it's properly quoted
|
|
|
|
|
// Add -NoExit to keep window open and ensure output is visible
|
2025-12-17 16:51:41 -07:00
|
|
|
// 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}\"";
|
2025-12-17 15:34:34 -07:00
|
|
|
|
|
|
|
|
_sshService.StartInteractiveSsh(worker, remoteCmd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|