Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast actions #478

Merged
merged 22 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ jobs:
with:
name: nupkg
path: src/Runner.Client/nupkg
- name: Windows Container Change Firewall Settings
if: contains(matrix.os, 'windows') && !job.container
run: |
docker pull mcr.microsoft.com/windows/servercore:${{matrix.tag}}&
docker pull mcr.microsoft.com/windows/servercore:ltsc2022&
docker pull mcr.microsoft.com/powershell:lts-windowsservercore-ltsc2022&
shell: bash
- name: Install
run: |
dotnet tool install -g Runner.Client --add-source src/Runner.Client/nupkg --version ${{needs.package.outputs.version}}
Expand Down
165 changes: 165 additions & 0 deletions src/Runner.Client/ExternalQueueService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using GitHub.DistributedTask.WebApi;
using System.Threading;
using GitHub.Runner.Common;
using GitHub.Services.Common;
using GitHub.DistributedTask.Pipelines;

namespace Runner.Client
{
partial class Program
{
private class ExternalQueueService : Runner.Server.IQueueService, IRunnerServer
{
private string customConfigDir;

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / package

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (alpine-x64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (alpine-x64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (win-x86, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (win-x86, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, false)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, true)

The field 'Program.ExternalQueueService.customConfigDir' is never used

Check warning on line 17 in src/Runner.Client/ExternalQueueService.cs

View workflow job for this annotation

GitHub Actions / deploy

The field 'Program.ExternalQueueService.customConfigDir' is never used
private Parameters parameters;
private SemaphoreSlim semaphore;

public ExternalQueueService(Parameters parameters)
{
this.parameters = parameters;
semaphore = new SemaphoreSlim(parameters.Parallel ?? 1, parameters.Parallel ?? 1);
}

public string Prefix { get; private set; }
public string Suffix { get; private set; }

public Task<TaskAgent> AddAgentAsync(int agentPoolId, TaskAgent agent)
{
throw new NotImplementedException();
}

public Task ConnectAsync(Uri serverUrl, VssCredentials credentials)
{
throw new NotImplementedException();
}

public Task<TaskAgentSession> CreateAgentSessionAsync(int poolId, TaskAgentSession session, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task DeleteAgentAsync(int agentPoolId, ulong agentId)
{
throw new NotImplementedException();
}

public Task DeleteAgentAsync(ulong agentId)
{
throw new NotImplementedException();
}

public Task DeleteAgentMessageAsync(int poolId, long messageId, Guid sessionId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task DeleteAgentSessionAsync(int poolId, Guid sessionId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<TaskAgentJobRequest> FinishAgentRequestAsync(int poolId, long requestId, Guid lockToken, DateTime finishTime, TaskResult result, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<TaskAgentMessage> GetAgentMessageAsync(int poolId, Guid sessionId, long? lastMessageId, TaskAgentStatus status, string runnerVersion, string os, string architecture, bool disableUpdate, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<List<TaskAgentPool>> GetAgentPoolsAsync(string agentPoolName = null, TaskAgentPoolType poolType = TaskAgentPoolType.Automation)
{
throw new NotImplementedException();
}

public Task<TaskAgentJobRequest> GetAgentRequestAsync(int poolId, long requestId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<List<TaskAgent>> GetAgentsAsync(int agentPoolId, string agentName = null)
{
throw new NotImplementedException();
}

public Task<List<TaskAgent>> GetAgentsAsync(string agentName)
{
throw new NotImplementedException();
}

public Task<PackageMetadata> GetPackageAsync(string packageType, string platform, string version, bool includeToken, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task<List<PackageMetadata>> GetPackagesAsync(string packageType, string platform, int top, bool includeToken, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public void Initialize(IHostContext context)
{
throw new NotImplementedException();
}

public async void PickJob(AgentJobRequestMessage message, CancellationToken token, string[] labels)
{
semaphore.Wait();
try {
CreateExternalRunnerDirectory(parameters, out _, out var prefix, out var suffix, out _, out var tmpdir);
this.Prefix = prefix;
this.Suffix = suffix;
File.WriteAllText(Path.Join(tmpdir, ".agent"), "{\"isHostedServer\": false, \"agentName\": \"my-runner\", \"workFolder\": \"_work\"}");
File.WriteAllText(Path.Join(tmpdir, ".runner"), "{\"isHostedServer\": false, \"agentName\": \"my-runner\", \"workFolder\": \"_work\"}");
var ctx = new HostContext("EXTERNALRUNNERCLIENT", customConfigDir: tmpdir);
ctx.PutService<IRunnerServer>(this);
ctx.PutService<ExternalQueueService>(this);
ctx.PutService<ITerminal>(new Terminal());
ctx.PutServiceFactory<IProcessInvoker, WrapProcService>();
var dispatcher = new GitHub.Runner.Listener.JobDispatcher();
dispatcher.Initialize(ctx);
dispatcher.Run(message, true);
await dispatcher.WaitAsync(token);
await dispatcher.ShutdownAsync();
try {
Directory.Delete(tmpdir, true);
} catch {

}
} finally {
semaphore.Release();
}
}

public Task RefreshConnectionAsync(RunnerConnectionType connectionType, TimeSpan timeout)
{
throw new NotImplementedException();
}

public Task<TaskAgentJobRequest> RenewAgentRequestAsync(int poolId, long requestId, Guid lockToken, string orchestrationId, CancellationToken cancellationToken)
{
return Task.FromResult(new TaskAgentJobRequest());
}

public Task<TaskAgent> ReplaceAgentAsync(int agentPoolId, TaskAgent agent)
{
throw new NotImplementedException();
}

public void SetConnectionTimeout(RunnerConnectionType connectionType, TimeSpan timeout)
{
throw new NotImplementedException();
}

public Task<TaskAgent> UpdateAgentUpdateStateAsync(int agentPoolId, ulong agentId, string currentState, string trace)
{
throw new NotImplementedException();
}
}
}
}
83 changes: 83 additions & 0 deletions src/Runner.Client/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Concurrent;
using GitHub.Runner.Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Runner.Client
{
public class JsonLogger : ILogger
{
public IDisposable BeginScope<TState>(TState state) where TState : notnull => default!;

public bool IsEnabled(LogLevel logLevel) => true;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if(!IsEnabled(logLevel)) {
return;
}
Console.WriteLine(JsonConvert.SerializeObject(new { level = "trace", msg = formatter(state, exception), time = DateTime.Now }));
}
}

public class JsonLoggerProvider : ILoggerProvider
{
private readonly ConcurrentDictionary<string, JsonLogger> _loggers = new (StringComparer.OrdinalIgnoreCase);
public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, name => new JsonLogger());

public void Dispose()
{
_loggers.Clear();
}
}

public sealed class Terminal : GitHub.Runner.Common.RunnerService, ITerminal
{
public bool Silent { get; set; }

public event EventHandler CancelKeyPress;

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / package

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (alpine-arm64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (alpine-x64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (alpine-x64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (win-x86, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (win-x86, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, false)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, true)

The event 'Terminal.CancelKeyPress' is never used

Check warning on line 39 in src/Runner.Client/Logger.cs

View workflow job for this annotation

GitHub Actions / deploy

The event 'Terminal.CancelKeyPress' is never used

public void Dispose()
{
}

public string ReadLine()
{
throw new NotImplementedException();
}

public string ReadSecret()
{
throw new NotImplementedException();
}

public void Write(string message, ConsoleColor? colorCode = null)
{
}

public void WriteError(Exception ex)
{
}

public void WriteError(string line)
{
}

public void WriteLine()
{
}

public void WriteLine(string line, ConsoleColor? colorCode = null, bool skipTracing = false)
{
}

public void WriteSection(string message)
{
}

public void WriteSuccessMessage(string message)
{
}
}
}
121 changes: 121 additions & 0 deletions src/Runner.Client/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace Runner.Client
{
partial class Program
{
public static class NativeMethods
{
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

private const uint FILE_READ_EA = 0x0008;
private const uint FILE_FLAG_BACKUP_SEMANTICS = 0x2000000;

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint GetFinalPathNameByHandle(IntPtr hFile, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpszFilePath, uint cchFilePath, uint dwFlags);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);

public static Int32 FSCTL_GET_REPARSE_POINT = ( ((0x00000009) << 16) | ((0) << 14) | ((42) << 2) | (0) );
public static uint FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000;

[StructLayout(LayoutKind.Sequential)]
class REPARSE_DATA_BUFFER {
public uint ReparseTag;
public ushort ReparseDataLength;
public ushort Reserved;
public ushort SubstituteNameOffset;
public ushort SubstituteNameLength;
public ushort PrintNameOffset;
public ushort PrintNameLength;
public uint Flags;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3FF0)]
public byte[] PathBuffer;
}


[DllImport("kernel32.dll")]
public static extern byte DeviceIoControl(IntPtr hDevice, Int32 dwIoControlCode, IntPtr lpInBuffer, Int32 nInBufferSize, IntPtr lpOutBuffer, Int32 nOutBufferSize, ref Int32 lpBytesReturned, IntPtr lpOverlapped);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateFile(
[MarshalAs(UnmanagedType.LPTStr)] string filename,
[MarshalAs(UnmanagedType.U4)] uint access,
[MarshalAs(UnmanagedType.U4)] FileShare share,
IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] uint flagsAndAttributes,
IntPtr templateFile);

public static string GetFinalPathName(string path)
{
var h = CreateFile(path,
FILE_READ_EA,
FileShare.ReadWrite | FileShare.Delete,
IntPtr.Zero,
FileMode.Open,
FILE_FLAG_BACKUP_SEMANTICS,
IntPtr.Zero);
if (h == INVALID_HANDLE_VALUE)
throw new Win32Exception();

try
{
var sb = new StringBuilder(1024);
var res = GetFinalPathNameByHandle(h, sb, 1024, 0);
if (res == 0)
throw new Win32Exception();

return sb.ToString();
}
finally
{
CloseHandle(h);
}
}

private static uint IO_REPARSE_TAG_SYMLINK = 0xA000000C;

public static string ReadSymlink(string path)
{
var h = CreateFile(path,
FILE_READ_EA,
FileShare.ReadWrite | FileShare.Delete,
IntPtr.Zero,
FileMode.Open,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
IntPtr.Zero);
if (h == INVALID_HANDLE_VALUE)
throw new Win32Exception();

try
{
REPARSE_DATA_BUFFER rdb = new REPARSE_DATA_BUFFER();
var buf = Marshal.AllocHGlobal(Marshal.SizeOf(rdb));
int size = 0;
var res = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, IntPtr.Zero, 0, buf, Marshal.SizeOf(rdb), ref size, IntPtr.Zero);
if (res == 0)
throw new Win32Exception();
Marshal.PtrToStructure<REPARSE_DATA_BUFFER>(buf, rdb);
if(rdb.ReparseTag != IO_REPARSE_TAG_SYMLINK) {
throw new Exception("Invalid reparse point, only symlinks are supported");
}
//var sres = Encoding.Unicode.GetString(rdb.PathBuffer, rdb.PrintNameOffset, rdb.PrintNameLength);
var sres2 = Encoding.Unicode.GetString(rdb.PathBuffer, rdb.SubstituteNameOffset, rdb.SubstituteNameLength);
return sres2;
}
finally
{
CloseHandle(h);
}
}
}
}
}
Loading
Loading