Show running Gateway commit and available updates

This commit is contained in:
jhartworks committed 2026-11-24 14:05:38 +01:00
1 parent 32b240469c
commit 36b2057991
12 files changed
+188 -21

No files matched your search

@@ -1,3 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text.Json;
using Gateway.Api.Options;
@@ -7,16 +9,22 @@ namespace Gateway.Api.Services;
public sealed record SystemUpdateRequest(string Password);
public sealed record SystemUpdateStatus(string Phase, string Message, string? Commit = null, string? Target = null,
DateTimeOffset? UpdatedAt = null, string? RunId = null);
DateTimeOffset? UpdatedAt = null, string? RunId = null, string? RunningCommit = null);
public sealed record SystemUpdateAvailability(string State, string Branch, string? RemoteCommit = null);
public sealed class SystemUpdateService(IOptions<GatewayOptions> options)
public sealed class SystemUpdateService(IOptions<GatewayOptions> options) : IDisposable
{
private static readonly JsonSerializerOptions StatusJson = new(JsonSerializerDefaults.Web);
private readonly SemaphoreSlim _availabilityLock = new(1, 1);
private SystemUpdateAvailability? _availability;
private DateTimeOffset _checkedAt;
public bool Enabled => OperatingSystem.IsLinux()
&& !string.IsNullOrWhiteSpace(options.Value.UpdateSocketPath)
&& !string.IsNullOrWhiteSpace(options.Value.UpdateStatusPath);
public string? RunningCommit => IsCommit(options.Value.RunningCommit) ? options.Value.RunningCommit!.ToLowerInvariant() : null;
public SystemUpdateStatus GetStatus()
{
if (!Enabled) return new("disabled", "Der lokale Systemupdate-Dienst ist für dieses Gateway nicht eingerichtet.");
@@ -33,6 +41,64 @@ public sealed class SystemUpdateService(IOptions<GatewayOptions> options)
return new("unavailable", "Der Update-Status konnte nicht gelesen werden.");
}
public async Task<SystemUpdateAvailability> CheckAvailabilityAsync(bool refresh, CancellationToken cancellationToken)
{
var branch = options.Value.UpdateBranch;
if (!Enabled || string.IsNullOrWhiteSpace(options.Value.UpdateAvailabilityCommand)) return new("disabled", branch);
if (RunningCommit is not { } running) return new("unknown", branch);
await _availabilityLock.WaitAsync(cancellationToken);
try
{
if (!refresh && _availability is not null && DateTimeOffset.UtcNow - _checkedAt < TimeSpan.FromSeconds(15))
return _availability;
_availability = await ProbeAsync(running, branch, cancellationToken);
_checkedAt = DateTimeOffset.UtcNow;
return _availability;
}
finally { _availabilityLock.Release(); }
}
private async Task<SystemUpdateAvailability> ProbeAsync(string running, string branch, CancellationToken cancellationToken)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(TimeSpan.FromSeconds(32));
var start = new ProcessStartInfo(options.Value.UpdateAvailabilityCommand!)
{
UseShellExecute = false,
RedirectStandardOutput = true
};
start.ArgumentList.Add(running);
try
{
using var process = Process.Start(start);
if (process is null) return new("unavailable", branch);
try
{
var output = await process.StandardOutput.ReadToEndAsync(timeout.Token);
await process.WaitForExitAsync(timeout.Token);
var parts = output.Trim().Split(' ');
if (process.ExitCode == 0 && parts is ["current" or "available" or "incompatible", var tip] && IsCommit(tip))
return new(parts[0], branch, tip);
}
catch (OperationCanceledException)
{
try { if (!process.HasExited) process.Kill(entireProcessTree: true); }
catch (InvalidOperationException) { }
catch (Win32Exception) { }
cancellationToken.ThrowIfCancellationRequested();
}
}
catch (Win32Exception) { }
catch (IOException) { }
return new("unavailable", branch);
}
private static bool IsCommit(string? value) => value is { Length: 40 } && value.All(Uri.IsHexDigit);
public void Dispose() => _availabilityLock.Dispose();
public async Task<bool> StartAsync(CancellationToken cancellationToken)
{
if (!Enabled) return false;