Skip to content

Commit

Permalink
Pipe scripts into SSH instead of running them
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamOlech committed Jul 27, 2021
1 parent a5c2197 commit f66d53c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/Runner.Worker/Handlers/ScriptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Channels;
using System.Linq;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.Runner.Common;
Expand Down Expand Up @@ -175,6 +176,8 @@ public async Task RunAsync(ActionRunStage stage)
}
}
var workspaceDir = githubContext["workspace"] as StringContextData;
Trace.Info($"Workspace from githubContext is {workspaceDir}");
Trace.Info($"Working directory from Inputs is {workingDirectory}");
workingDirectory = Path.Combine(workspaceDir, workingDirectory ?? string.Empty);

string shell = null;
Expand Down Expand Up @@ -254,8 +257,11 @@ public async Task RunAsync(ActionRunStage stage)
// Don't add a BOM. It causes the script to fail on some operating systems (e.g. on Ubuntu 14).
var encoding = new UTF8Encoding(false);
#endif
// Script is written to local path (ie host) but executed relative to the StepHost, which may be a container
File.WriteAllText(scriptFilePath, contents, encoding);
// Script is written to local path (ie host) but executed relative to the StepHost, which may be a containe
if (isContainerStepHost)
{
File.WriteAllText(scriptFilePath, contents, encoding);
}

// Prepend PATH
AddPrependPathToEnvironment();
Expand Down Expand Up @@ -292,14 +298,19 @@ public async Task RunAsync(ActionRunStage stage)
var sshIp = githubContext["qemu_ip"];

fileName = "/usr/bin/ssh";
arguments = $"-q -o \"UserKnownHostsFile /dev/null\" -o \"StrictHostKeyChecking no\" scalerunner@{sshIp} sudo singularity exec instance://i bash -e /9p/_temp/{scriptName}";
arguments = $"-q -o \"UserKnownHostsFile /dev/null\" -o \"StrictHostKeyChecking no\" scalerunner@{sshIp} sudo singularity exec instance://i bash";

using (var stdoutManager = new OutputManager(ExecutionContext, ActionCommandManager))
using (var stderrManager = new OutputManager(ExecutionContext, ActionCommandManager))
{
StepHost.OutputDataReceived += stdoutManager.OnDataReceived;
StepHost.ErrorDataReceived += stderrManager.OnDataReceived;

var input = Channel.CreateBounded<string>(new BoundedChannelOptions(1) { SingleReader = true, SingleWriter = true });
input.Writer.TryWrite(contents);

StepHost.StandardInChannel = input;

// Execute
int exitCode = await StepHost.ExecuteAsync(workingDirectory: StepHost.ResolvePathForStepHost(workingDirectory),
fileName: fileName,
Expand All @@ -317,6 +328,8 @@ public async Task RunAsync(ActionRunStage stage)
ExecutionContext.Error($"Process completed with exit code {exitCode}.");
ExecutionContext.Result = TaskResult.Failed;
}

StepHost.StandardInChannel = null;
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/Runner.Worker/Handlers/StepHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IStepHost : IRunnerService
event EventHandler<ProcessDataReceivedEventArgs> OutputDataReceived;
event EventHandler<ProcessDataReceivedEventArgs> ErrorDataReceived;

public Channel<string> StandardInChannel { get; set; }

string ResolvePathForStepHost(string path);

Task<string> DetermineNodeRuntimeVersion(IExecutionContext executionContext);
Expand Down Expand Up @@ -53,6 +55,8 @@ public sealed class DefaultStepHost : RunnerService, IDefaultStepHost
public event EventHandler<ProcessDataReceivedEventArgs> OutputDataReceived;
public event EventHandler<ProcessDataReceivedEventArgs> ErrorDataReceived;

public Channel<string> StandardInChannel { get; set; }

public string ResolvePathForStepHost(string path)
{
return path;
Expand Down Expand Up @@ -85,7 +89,7 @@ public async Task<int> ExecuteAsync(string workingDirectory,
requireExitCodeZero: requireExitCodeZero,
outputEncoding: outputEncoding,
killProcessOnCancel: killProcessOnCancel,
redirectStandardIn: null,
redirectStandardIn: StandardInChannel,
inheritConsoleHandler: inheritConsoleHandler,
cancellationToken: cancellationToken);
}
Expand All @@ -99,6 +103,8 @@ public sealed class ContainerStepHost : RunnerService, IContainerStepHost
public event EventHandler<ProcessDataReceivedEventArgs> OutputDataReceived;
public event EventHandler<ProcessDataReceivedEventArgs> ErrorDataReceived;

public Channel<string> StandardInChannel { get; set; }

public string ResolvePathForStepHost(string path)
{
// make sure container exist.
Expand Down

0 comments on commit f66d53c

Please sign in to comment.