mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-06-06 07:04:31 -04:00
Added Setup overlay app to communicate status of startup
This commit is contained in:
parent
3ceb8337e7
commit
1e7acec017
@ -7,10 +7,13 @@ using System.Reflection;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
using Emby.Server.Implementations;
|
using Emby.Server.Implementations;
|
||||||
|
using Jellyfin.Networking.Manager;
|
||||||
using Jellyfin.Server.Extensions;
|
using Jellyfin.Server.Extensions;
|
||||||
using Jellyfin.Server.Helpers;
|
using Jellyfin.Server.Helpers;
|
||||||
using Jellyfin.Server.Implementations;
|
using Jellyfin.Server.Implementations;
|
||||||
|
using Jellyfin.Server.ServerSetupApp;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller;
|
using MediaBrowser.Controller;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@ -42,6 +45,9 @@ namespace Jellyfin.Server
|
|||||||
public const string LoggingConfigFileSystem = "logging.json";
|
public const string LoggingConfigFileSystem = "logging.json";
|
||||||
|
|
||||||
private static readonly SerilogLoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
private static readonly SerilogLoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
||||||
|
private static SetupServer? _setupServer = new();
|
||||||
|
|
||||||
|
private static IHost? _jfHost = null;
|
||||||
private static long _startTimestamp;
|
private static long _startTimestamp;
|
||||||
private static ILogger _logger = NullLogger.Instance;
|
private static ILogger _logger = NullLogger.Instance;
|
||||||
private static bool _restartOnShutdown;
|
private static bool _restartOnShutdown;
|
||||||
@ -68,6 +74,7 @@ namespace Jellyfin.Server
|
|||||||
{
|
{
|
||||||
_startTimestamp = Stopwatch.GetTimestamp();
|
_startTimestamp = Stopwatch.GetTimestamp();
|
||||||
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
|
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
|
||||||
|
await _setupServer!.RunAsync(static () => _jfHost?.Services?.GetService<INetworkManager>(), appPaths).ConfigureAwait(false);
|
||||||
|
|
||||||
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
|
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
|
||||||
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
|
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
|
||||||
@ -122,6 +129,8 @@ namespace Jellyfin.Server
|
|||||||
if (_restartOnShutdown)
|
if (_restartOnShutdown)
|
||||||
{
|
{
|
||||||
_startTimestamp = Stopwatch.GetTimestamp();
|
_startTimestamp = Stopwatch.GetTimestamp();
|
||||||
|
_setupServer = new SetupServer();
|
||||||
|
await _setupServer.RunAsync(static () => _jfHost?.Services?.GetService<INetworkManager>(), appPaths).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
} while (_restartOnShutdown);
|
} while (_restartOnShutdown);
|
||||||
}
|
}
|
||||||
@ -133,11 +142,9 @@ namespace Jellyfin.Server
|
|||||||
_loggerFactory,
|
_loggerFactory,
|
||||||
options,
|
options,
|
||||||
startupConfig);
|
startupConfig);
|
||||||
|
|
||||||
IHost? host = null;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
host = Host.CreateDefaultBuilder()
|
_jfHost = Host.CreateDefaultBuilder()
|
||||||
.UseConsoleLifetime()
|
.UseConsoleLifetime()
|
||||||
.ConfigureServices(services => appHost.Init(services))
|
.ConfigureServices(services => appHost.Init(services))
|
||||||
.ConfigureWebHostDefaults(webHostBuilder =>
|
.ConfigureWebHostDefaults(webHostBuilder =>
|
||||||
@ -154,14 +161,18 @@ namespace Jellyfin.Server
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Re-use the host service provider in the app host since ASP.NET doesn't allow a custom service collection.
|
// Re-use the host service provider in the app host since ASP.NET doesn't allow a custom service collection.
|
||||||
appHost.ServiceProvider = host.Services;
|
appHost.ServiceProvider = _jfHost.Services;
|
||||||
|
|
||||||
await appHost.InitializeServices().ConfigureAwait(false);
|
await appHost.InitializeServices().ConfigureAwait(false);
|
||||||
Migrations.MigrationRunner.Run(appHost, _loggerFactory);
|
Migrations.MigrationRunner.Run(appHost, _loggerFactory);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await host.StartAsync().ConfigureAwait(false);
|
await Task.Delay(50000).ConfigureAwait(false);
|
||||||
|
await _setupServer!.StopAsync().ConfigureAwait(false);
|
||||||
|
_setupServer.Dispose();
|
||||||
|
_setupServer = null!;
|
||||||
|
await _jfHost.StartAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
if (!OperatingSystem.IsWindows() && startupConfig.UseUnixSocket())
|
if (!OperatingSystem.IsWindows() && startupConfig.UseUnixSocket())
|
||||||
{
|
{
|
||||||
@ -180,7 +191,7 @@ namespace Jellyfin.Server
|
|||||||
|
|
||||||
_logger.LogInformation("Startup complete {Time:g}", Stopwatch.GetElapsedTime(_startTimestamp));
|
_logger.LogInformation("Startup complete {Time:g}", Stopwatch.GetElapsedTime(_startTimestamp));
|
||||||
|
|
||||||
await host.WaitForShutdownAsync().ConfigureAwait(false);
|
await _jfHost.WaitForShutdownAsync().ConfigureAwait(false);
|
||||||
_restartOnShutdown = appHost.ShouldRestart;
|
_restartOnShutdown = appHost.ShouldRestart;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -205,7 +216,7 @@ namespace Jellyfin.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
host?.Dispose();
|
_jfHost?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
131
Jellyfin.Server/ServerSetupApp/SetupServer.cs
Normal file
131
Jellyfin.Server/ServerSetupApp/SetupServer.cs
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Networking.Manager;
|
||||||
|
using MediaBrowser.Common.Configuration;
|
||||||
|
using MediaBrowser.Common.Net;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using SQLitePCL;
|
||||||
|
|
||||||
|
namespace Jellyfin.Server.ServerSetupApp;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a fake application pipeline that will only exist for as long as the main app is not started.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class SetupServer : IDisposable
|
||||||
|
{
|
||||||
|
private IHost? _startupServer;
|
||||||
|
private bool _disposed;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Starts the Bind-All Setup aspcore server to provide a reflection on the current core setup.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="networkManagerFactory">The networkmanager.</param>
|
||||||
|
/// <param name="applicationPaths">The application paths.</param>
|
||||||
|
/// <returns>A Task.</returns>
|
||||||
|
public async Task RunAsync(Func<INetworkManager?> networkManagerFactory, IApplicationPaths applicationPaths)
|
||||||
|
{
|
||||||
|
ThrowIfDisposed();
|
||||||
|
_startupServer = Host.CreateDefaultBuilder()
|
||||||
|
.UseConsoleLifetime()
|
||||||
|
.ConfigureServices(serv =>
|
||||||
|
{
|
||||||
|
serv.AddHealthChecks()
|
||||||
|
.AddCheck<SetupHealthcheck>("StartupCheck");
|
||||||
|
})
|
||||||
|
.ConfigureWebHostDefaults(webHostBuilder =>
|
||||||
|
{
|
||||||
|
webHostBuilder
|
||||||
|
.UseKestrel()
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseHealthChecks("/health");
|
||||||
|
|
||||||
|
app.Map("/startup/logger", loggerRoute =>
|
||||||
|
{
|
||||||
|
loggerRoute.Run(async context =>
|
||||||
|
{
|
||||||
|
var networkManager = networkManagerFactory();
|
||||||
|
if (context.Connection.RemoteIpAddress is null || networkManager is null || !networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var logfilePath = Directory.EnumerateFiles(applicationPaths.LogDirectoryPath).Select(e => new FileInfo(e)).OrderBy(f => f.CreationTimeUtc).FirstOrDefault()?.FullName;
|
||||||
|
if (logfilePath is not null)
|
||||||
|
{
|
||||||
|
await context.Response.SendFileAsync(logfilePath, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.Run((context) =>
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
|
||||||
|
context.Response.Headers.RetryAfter = new Microsoft.Extensions.Primitives.StringValues("60");
|
||||||
|
context.Response.WriteAsync("<p>Jellyfin Server still starting. Please wait.</p>");
|
||||||
|
var networkManager = networkManagerFactory();
|
||||||
|
if (networkManager is not null && context.Connection.RemoteIpAddress is not null && networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
|
||||||
|
{
|
||||||
|
context.Response.WriteAsync("<p>You can download the current logfiles <a href='/startup/logger'>here</a>.</p>");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
await _startupServer.StartAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Stops the Setup server.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task. Duh.</returns>
|
||||||
|
public async Task StopAsync()
|
||||||
|
{
|
||||||
|
ThrowIfDisposed();
|
||||||
|
if (_startupServer is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Tried to stop a non existing startup server");
|
||||||
|
}
|
||||||
|
|
||||||
|
await _startupServer.StopAsync().ConfigureAwait(false);
|
||||||
|
_startupServer.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (_disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_disposed = true;
|
||||||
|
_startupServer?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThrowIfDisposed()
|
||||||
|
{
|
||||||
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SetupHealthcheck : IHealthCheck
|
||||||
|
{
|
||||||
|
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return Task.FromResult(HealthCheckResult.Degraded("Server is still starting up."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -921,6 +921,19 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool IsInLocalNetwork(IPAddress address)
|
public bool IsInLocalNetwork(IPAddress address)
|
||||||
|
{
|
||||||
|
return NetworkManager.IsInLocalNetwork(address, TrustAllIPv6Interfaces, _lanSubnets, _excludedSubnets);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks a ip address to match any lansubnet given but not to be in any excluded subnet.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">The IP address to checl.</param>
|
||||||
|
/// <param name="trustAllIpv6">Whenever all IPV6 subnet address shall be permitted.</param>
|
||||||
|
/// <param name="lanSubnets">The list of subnets to permit.</param>
|
||||||
|
/// <param name="excludedSubnets">The list of subnets to never permit.</param>
|
||||||
|
/// <returns>The check if the given IP address is in any provided subnet.</returns>
|
||||||
|
public static bool IsInLocalNetwork(IPAddress address, bool trustAllIpv6, IReadOnlyList<IPNetwork> lanSubnets, IReadOnlyList<IPNetwork> excludedSubnets)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(address);
|
ArgumentNullException.ThrowIfNull(address);
|
||||||
|
|
||||||
@ -930,23 +943,23 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
address = address.MapToIPv4();
|
address = address.MapToIPv4();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((TrustAllIPv6Interfaces && address.AddressFamily == AddressFamily.InterNetworkV6)
|
if ((trustAllIpv6 && address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
|| IPAddress.IsLoopback(address))
|
|| IPAddress.IsLoopback(address))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// As private addresses can be redefined by Configuration.LocalNetworkAddresses
|
// As private addresses can be redefined by Configuration.LocalNetworkAddresses
|
||||||
return CheckIfLanAndNotExcluded(address);
|
return CheckIfLanAndNotExcluded(address, lanSubnets, excludedSubnets);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CheckIfLanAndNotExcluded(IPAddress address)
|
private static bool CheckIfLanAndNotExcluded(IPAddress address, IReadOnlyList<IPNetwork> lanSubnets, IReadOnlyList<IPNetwork> excludedSubnets)
|
||||||
{
|
{
|
||||||
foreach (var lanSubnet in _lanSubnets)
|
foreach (var lanSubnet in lanSubnets)
|
||||||
{
|
{
|
||||||
if (lanSubnet.Contains(address))
|
if (lanSubnet.Contains(address))
|
||||||
{
|
{
|
||||||
foreach (var excludedSubnet in _excludedSubnets)
|
foreach (var excludedSubnet in excludedSubnets)
|
||||||
{
|
{
|
||||||
if (excludedSubnet.Contains(address))
|
if (excludedSubnet.Contains(address))
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user