42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
|
|
using System.Diagnostics;
|
||
|
|
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 = $"{remoteBasePath.Replace("\\", "/")}/attach-helper.ps1";
|
||
|
|
|
||
|
|
var paramsBlock = $"-WorkerName \"{worker.Name}\" -WorkerType \"{workerType}\"";
|
||
|
|
if (commandOnly)
|
||
|
|
{
|
||
|
|
paramsBlock += " -CommandOnly";
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrEmpty(command))
|
||
|
|
{
|
||
|
|
paramsBlock += $" -Command \"{command}\"";
|
||
|
|
}
|
||
|
|
|
||
|
|
var remoteCmd = $"powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -File \"{remoteHelper}\" {paramsBlock}";
|
||
|
|
|
||
|
|
_sshService.StartInteractiveSsh(worker, remoteCmd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|