Skip to content

Commit

Permalink
Modified LauncherConfiguration to include LauncherServiceUri;
Browse files Browse the repository at this point in the history
Implemented ConfigureKestrel method to handle Unix Domain Sockets and Named Pipes;
  • Loading branch information
pawelvds committed Jan 9, 2024
1 parent ead9319 commit 77cb9d6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
22 changes: 10 additions & 12 deletions src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public record LauncherConfiguration
private readonly object _rawAccessLock = new();

[JsonConstructor]
public LauncherConfiguration() { _rawAccess = false; }
public LauncherConfiguration()
{
_rawAccess = false;
_launcherServiceUri = OperatingSystem.IsWindows()
? $"net.pipe://localhost/fiskaltrust-{_cashboxId}"
: $"/tmp/fiskaltrust-{_cashboxId}.sock";
}

public T Raw<T>(System.Linq.Expressions.Expression<Func<LauncherConfiguration, T>> accessor)
{
Expand Down Expand Up @@ -77,9 +83,9 @@ private T WithDefault<T>(T value, Func<T> defaultValue)
[JsonPropertyName("accessToken")]
public string? AccessToken { get => _accessToken; set => _accessToken = value; }

private int? _launcherPort;
[JsonPropertyName("launcherPort")]
public int? LauncherPort { get => WithDefault(_launcherPort, 0); set => _launcherPort = value; }
private string? _launcherServiceUri;
[JsonPropertyName("launcherServiceUri")]
public string? LauncherServiceUri { get => _launcherServiceUri; set => _launcherServiceUri = value; }

private string? _serviceFolder;
[JsonPropertyName("serviceFolder")]
Expand Down Expand Up @@ -112,14 +118,6 @@ private T WithDefault<T>(T value, Func<T> defaultValue)
[AlternateName("verbosity")]
public LogLevel? LogLevel { get => WithDefault(_logLevel, Microsoft.Extensions.Logging.LogLevel.Information); set => _logLevel = value; }

private Uri? _grpcServiceUrl;
[JsonPropertyName("grpcServiceUrl")]
public Uri? GrpcServiceUrl
{
get => _grpcServiceUrl;
set => _grpcServiceUrl = value;
}

private Uri? _packagesUrl;
[JsonPropertyName("packagesUrl")]
public Uri? PackagesUrl { get => WithDefault(_packagesUrl, () => new Uri(Sandbox!.Value ? "https://packages-2-0-sandbox.fiskaltrust.cloud" : "https://packages-2-0.fiskaltrust.cloud")); set => _packagesUrl = value; }
Expand Down
4 changes: 2 additions & 2 deletions src/fiskaltrust.Launcher/Commands/DoctorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonPro

checkUp.Check("Setup monarch ProcessHostService", () =>
{
monarchBuilder.WebHost.ConfigureBinding(new Uri($"http://[::1]:{launcherConfiguration.LauncherPort}"), protocols: HttpProtocols.Http2);
monarchBuilder.WebHost.ConfigureBinding(new Uri($"http://[::1]:{launcherConfiguration.LauncherServiceUri}"), protocols: HttpProtocols.Http2);
monarchBuilder.Services.AddCodeFirstGrpc();
}, throws: true);
Expand Down Expand Up @@ -185,7 +185,7 @@ public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonPro
Version = "1.0.0"
};

IProcessHostService? processHostService = checkUp.Check("Start plebeian processhostservice client", () => GrpcChannel.ForAddress($"http://localhost:{launcherConfiguration.LauncherPort}").CreateGrpcService<IProcessHostService>());
IProcessHostService? processHostService = checkUp.Check("Start plebeian processhostservice client", () => GrpcChannel.ForAddress($"http://localhost:{launcherConfiguration.LauncherServiceUri}").CreateGrpcService<IProcessHostService>());

var plebeianBuilder = Host.CreateDefaultBuilder()
.UseSerilog(new LoggerConfiguration().CreateLogger())
Expand Down
3 changes: 2 additions & 1 deletion src/fiskaltrust.Launcher/Commands/HostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public static async Task<int> HandleAsync(HostOptions hostOptions, HostServices
IProcessHostService? processHostService = null;
if (!hostOptions.NoProcessHostService)
{
string grpcAddress = launcherConfiguration.GrpcServiceUrl?.ToString() ?? $"http://localhost:{launcherConfiguration.LauncherPort}";
string grpcAddress = launcherConfiguration.LauncherServiceUri?.ToString();
processHostService = GrpcChannel.ForAddress(grpcAddress).CreateGrpcService<IProcessHostService>();

}

Log.Logger = new LoggerConfiguration()
Expand Down
2 changes: 1 addition & 1 deletion src/fiskaltrust.Launcher/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonPro
services.AddSingleton(_ => runServices.LauncherExecutablePath);
});

builder.WebHost.ConfigureBinding(new Uri($"http://[::1]:{commonProperties.LauncherConfiguration.LauncherPort}"), protocols: HttpProtocols.Http2);
builder.WebHost.ConfigureBinding(new Uri($"http://[::1]:{commonProperties.LauncherConfiguration.LauncherServiceUri}"), protocols: HttpProtocols.Http2);

builder.Services.AddCodeFirstGrpc();

Expand Down
32 changes: 26 additions & 6 deletions src/fiskaltrust.Launcher/ProcessHost/ProcessHostMonarcStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using fiskaltrust.storage.serialization.V0;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting.WindowsServices;

namespace fiskaltrust.Launcher.ProcessHost
Expand All @@ -26,6 +27,7 @@ public class AlreadyLoggedException : Exception { }
private readonly ILifetime _lifetime;
private readonly LauncherExecutablePath _launcherExecutablePath;
private readonly TaskCompletionSource<Uri> _kestrelReady;
private readonly WebApplicationBuilder _webHostBuilder;

public ProcessHostMonarcStartup(ILoggerFactory loggerFactory, ILogger<ProcessHostMonarcStartup> logger, Dictionary<Guid, IProcessHostMonarch> hosts, LauncherConfiguration launcherConfiguration, ftCashBoxConfiguration cashBoxConfiguration, PackageDownloader downloader, ILifetime lifetime, LauncherExecutablePath launcherExecutablePath, IHostApplicationLifetime hostApplicationLifetime, IServer server)
{
Expand All @@ -38,6 +40,7 @@ public ProcessHostMonarcStartup(ILoggerFactory loggerFactory, ILogger<ProcessHos
_lifetime = lifetime;
_launcherExecutablePath = launcherExecutablePath;
_kestrelReady = new TaskCompletionSource<Uri>();
_webHostBuilder = WebApplication.CreateBuilder();

hostApplicationLifetime.ApplicationStarted.Register(() =>
{
Expand All @@ -51,22 +54,21 @@ public ProcessHostMonarcStartup(ILoggerFactory loggerFactory, ILogger<ProcessHos
}
});
}



protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
_lifetime.ApplicationLifetime.ApplicationStopping.Register(() => _logger.LogInformation("Shutting down launcher."));
cancellationToken.Register(() => _kestrelReady.TrySetCanceled());

StartupLogging();

if (_launcherConfiguration.LauncherPort == 0)
if (string.IsNullOrEmpty(_launcherConfiguration.LauncherServiceUri))
{
try
{
var url = await _kestrelReady.Task.ConfigureAwait(false);
_launcherConfiguration.LauncherPort = url.Port;
_logger.LogInformation("ProcessHostService running on {url}", url);
var uri = new Uri(_launcherConfiguration.LauncherServiceUri);
ConfigureKestrel(uri, _webHostBuilder);
_logger.LogInformation("ProcessHostService running on {uri}", uri);
}
catch (Exception e)
{
Expand Down Expand Up @@ -138,6 +140,24 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
_lifetime.ApplicationLifetime.StopApplication();
}

private void ConfigureKestrel(Uri launcherServiceUri, WebApplicationBuilder builder)
{
if (OperatingSystem.IsWindows())
{
builder.WebHost.UseKestrel(serverOptions =>
{
serverOptions.ListenNamedPipe(launcherServiceUri.ToString());
});
}
else
{
builder.WebHost.UseKestrel(serverOptions =>
{
serverOptions.ListenUnixSocket(launcherServiceUri.ToString());
});
}
}

private async Task StartProcessHostMonarch(PackageConfiguration configuration, PackageType packageType, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
Expand Down

0 comments on commit 77cb9d6

Please sign in to comment.