Skip to content

Commit

Permalink
Add 'uet cmake' command to distribute CMake-based builds over UBA (#34)
Browse files Browse the repository at this point in the history
* Add cmake-uba-run and cmake-uba-server commands for doing CMake work on UBA

* Make server auto-close after 60 seconds idle

* CMake UBA now mostly works

* Limit CMake UBA to only run clang-cl for now

* Use KubernetesClient.Aot to fix trim issues

* Add top-level 'uet cmake' command that puts all the internals together for you

* Try to fix YamlDotNet trim warnings caused by implicit upgrade
  • Loading branch information
hach-que authored Dec 25, 2024
1 parent 16f7ee0 commit 2e3e7dd
Show file tree
Hide file tree
Showing 20 changed files with 1,932 additions and 245 deletions.
440 changes: 254 additions & 186 deletions UET/Redpoint.Uba/DefaultUbaServer.cs

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions UET/Redpoint.Uba/DefaultUbaServerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ internal class DefaultUbaServerFactory : IUbaServerFactory
private readonly ILogger<DefaultUbaServer> _defaultUbaServerLogger;
private readonly ILogger<UbaLoggerForwarder> _ubaLoggerForwarderLogger;
private readonly IProcessArgumentParser _processArgumentParser;
private readonly IProcessExecutor _localProcessExecutor;

public DefaultUbaServerFactory(
ILogger<DefaultUbaServer> defaultUbaServerLogger,
ILogger<UbaLoggerForwarder> ubaLoggerForwarderLogger,
IProcessArgumentParser processArgumentParser,
IProcessExecutor localProcessExecutor)
IProcessArgumentParser processArgumentParser)
{
_defaultUbaServerLogger = defaultUbaServerLogger;
_ubaLoggerForwarderLogger = ubaLoggerForwarderLogger;
_processArgumentParser = processArgumentParser;
_localProcessExecutor = localProcessExecutor;
}

public IUbaServer CreateServer(
Expand All @@ -46,18 +43,19 @@ public IUbaServer CreateServer(
Directory.CreateDirectory(rootStorageDirectoryPath);
Directory.CreateDirectory(Path.GetDirectoryName(ubaTraceFilePath)!);

var loggingForwarder = new UbaLoggerForwarder(_ubaLoggerForwarderLogger);
// @note: UbaLoggerForwarder must be static and hold a singleton log. Otherwise we can run into a crash if
// DefaultUbaServer is finalized after UbaLoggerForwarder.
var ubaLogger = UbaLoggerForwarder.GetUbaLogger(_ubaLoggerForwarderLogger);
var server = UbaServerDelayedImports.CreateServer(
loggingForwarder.Logger,
ubaLogger,
maxWorkers,
sendSize,
receiveTimeoutSeconds,
useQuic);
return new DefaultUbaServer(
_defaultUbaServerLogger,
_processArgumentParser,
_localProcessExecutor,
loggingForwarder,
ubaLogger,
server,
rootStorageDirectoryPath,
ubaTraceFilePath);
Expand Down
20 changes: 17 additions & 3 deletions UET/Redpoint.Uba/IUbaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
///
/// We don't support the UBA mode whereby the server listens on a port, and remote agents (clients) initiated a connection back to the server. That's because most developer machines are behind a firewall that remote agents won't be able to initiate a connection through.
///
/// Instead, we only support the model by which the remote agents (clients) listen on a port, and the server actively connects to them using <see cref="AddRemoteAgent(string, int, string)"/>.
/// Instead, we only support the model by which the remote agents (clients) listen on a port, and the server actively connects to them using <see cref="AddRemoteAgent(string, int)"/>.
/// </summary>
public interface IUbaServer : IProcessExecutor, IAsyncDisposable
{
Expand All @@ -19,8 +19,22 @@ public interface IUbaServer : IProcessExecutor, IAsyncDisposable
/// </summary>
/// <param name="ip">The IP address of the remote agent.</param>
/// <param name="port">The port of the remote agent.</param>
/// <param name="crypto">If the remote agent has the '-crypto' parameter set, this should be the 32 character hexadecimal value representing the symmetric cryptographic key.</param>
/// <returns>True if the agent was successfully added.</returns>
bool AddRemoteAgent(string ip, int port, string crypto = "");
bool AddRemoteAgent(string ip, int port);

/// <summary>
/// The number of processes currently in the queue to start execution.
/// </summary>
long ProcessesPendingInQueue { get; }

/// <summary>
/// The number of processes executing locally.
/// </summary>
long ProcessesExecutingLocally { get; }

/// <summary>
/// The number of processes executing on remote agents.
/// </summary>
long ProcessesExecutingRemotely { get; }
}
}
54 changes: 16 additions & 38 deletions UET/Redpoint.Uba/UbaLoggerForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System;
using System.Runtime.InteropServices;

internal partial class UbaLoggerForwarder : IDisposable
internal partial class UbaLoggerForwarder
{
private readonly ILogger<UbaLoggerForwarder> _logger;
private readonly nint _ubaLogger;
private bool _hasDisposed;
private static ILogger<UbaLoggerForwarder>? _logger;
private static nint? _ubaLogger;

#region Library Imports

Expand All @@ -36,8 +35,13 @@ private static partial void DestroyCallbackLogWriter(

#endregion

public UbaLoggerForwarder(ILogger<UbaLoggerForwarder> logger)
public static nint GetUbaLogger(ILogger<UbaLoggerForwarder> logger)
{
if (_ubaLogger.HasValue)
{
return _ubaLogger.Value;
}

_logger = logger;
_ubaLogger = CreateCallbackLogWriter(
BeginScope,
Expand All @@ -47,9 +51,9 @@ public UbaLoggerForwarder(ILogger<UbaLoggerForwarder> logger)
{
throw new InvalidOperationException("Unable to create UBA logger!");
}
}

public nint Logger => _ubaLogger;
return _ubaLogger.Value;
}

private static void BeginScope()
{
Expand All @@ -59,7 +63,7 @@ private static void EndScope()
{
}

private void Log(byte logEntryType, nint str, uint strLen, nint prefix, uint prefixLen)
private static void Log(byte logEntryType, nint str, uint strLen, nint prefix, uint prefixLen)
{
string message;
if (OperatingSystem.IsWindows())
Expand All @@ -74,46 +78,20 @@ private void Log(byte logEntryType, nint str, uint strLen, nint prefix, uint pre
switch (logEntryType)
{
case 0:
_logger.LogError(message);
_logger!.LogError(message);
break;
case 1:
_logger.LogWarning(message);
_logger!.LogWarning(message);
break;
case 2:
_logger.LogInformation(message);
_logger!.LogInformation(message);
break;
case 3:
case 4:
default:
_logger.LogDebug(message);
_logger!.LogDebug(message);
break;
}
}

protected virtual void Dispose(bool disposing)
{
if (!_hasDisposed)
{
if (_ubaLogger != nint.Zero)
{
DestroyCallbackLogWriter(_ubaLogger);
}

_hasDisposed = true;
}
}

~UbaLoggerForwarder()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
2 changes: 2 additions & 0 deletions UET/Redpoint.Uba/UbaProcessDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal class UbaProcessDescriptor

public required bool PreferRemote { get; set; }

public required bool AllowRemote { get; set; }

public required Gate CompletionGate { get; init; }

public int ExitCode { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions UET/Redpoint.Uba/UbaProcessSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public class UbaProcessSpecification : ProcessSpecification
/// remote agent.
/// </summary>
public bool PreferRemote { get; set; }

/// <summary>
/// If true, this command can be run remotely. Defaults to true if not set.
/// </summary>
public bool AllowRemote { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<Import Project="$(MSBuildThisFileDirectory)../Lib/Common.Build.props" />

<ItemGroup>
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="13.0.2" />
<PackageReference Include="YamlDotNet" Version="13.1.0" />
<PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 2e3e7dd

Please sign in to comment.