From be5b5d92510268aaaac1262459b70bc433be592a Mon Sep 17 00:00:00 2001 From: Joe Milazzo Date: Mon, 15 May 2023 17:57:57 -0500 Subject: [PATCH] Changed IsDocker check (#1998) * Refactored IsDocker to be completely static and changed to use an environment variable instead. * Removed file from another branch --- API/Controllers/SettingsController.cs | 3 + API/DTOs/Settings/ServerSettingDTO.cs | 1 + API/Program.cs | 2 +- API/Services/Tasks/StatsService.cs | 2 +- API/Services/Tasks/VersionUpdaterService.cs | 2 +- API/Startup.cs | 4 +- Dockerfile | 2 + Kavita.Common/Configuration.cs | 10 +-- Kavita.Common/EnvironmentInfo/IOsInfo.cs | 69 +-------------------- openapi.json | 2 +- 10 files changed, 20 insertions(+), 77 deletions(-) diff --git a/API/Controllers/SettingsController.cs b/API/Controllers/SettingsController.cs index ef73fdda6..2b950f00f 100644 --- a/API/Controllers/SettingsController.cs +++ b/API/Controllers/SettingsController.cs @@ -15,6 +15,7 @@ using API.Services.Tasks.Scanner; using AutoMapper; using Flurl.Http; using Kavita.Common; +using Kavita.Common.EnvironmentInfo; using Kavita.Common.Extensions; using Kavita.Common.Helpers; using Microsoft.AspNetCore.Authorization; @@ -183,6 +184,7 @@ public class SettingsController : BaseApiController if (setting.Key == ServerSettingKey.Port && updateSettingsDto.Port + string.Empty != setting.Value) { + if (OsInfo.IsDocker) break; setting.Value = updateSettingsDto.Port + string.Empty; // Port is managed in appSetting.json Configuration.Port = updateSettingsDto.Port; @@ -191,6 +193,7 @@ public class SettingsController : BaseApiController if (setting.Key == ServerSettingKey.IpAddresses && updateSettingsDto.IpAddresses != setting.Value) { + if (OsInfo.IsDocker) break; // Validate IP addresses foreach (var ipAddress in updateSettingsDto.IpAddresses.Split(',')) { diff --git a/API/DTOs/Settings/ServerSettingDTO.cs b/API/DTOs/Settings/ServerSettingDTO.cs index c5bd0470b..c7d81b5a0 100644 --- a/API/DTOs/Settings/ServerSettingDTO.cs +++ b/API/DTOs/Settings/ServerSettingDTO.cs @@ -5,6 +5,7 @@ namespace API.DTOs.Settings; public class ServerSettingDto { + public string CacheDirectory { get; set; } = default!; public string TaskScan { get; set; } = default!; /// diff --git a/API/Program.cs b/API/Program.cs index e04c47732..1bebc5c36 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -174,7 +174,7 @@ public class Program webBuilder.UseKestrel((opts) => { var ipAddresses = Configuration.IpAddresses; - if (new OsInfo().IsDocker || string.IsNullOrEmpty(ipAddresses) || ipAddresses.Equals(Configuration.DefaultIpAddresses)) + if (OsInfo.IsDocker || string.IsNullOrEmpty(ipAddresses) || ipAddresses.Equals(Configuration.DefaultIpAddresses)) { opts.ListenAnyIP(HttpPort, options => { options.Protocols = HttpProtocols.Http1AndHttp2; }); } diff --git a/API/Services/Tasks/StatsService.cs b/API/Services/Tasks/StatsService.cs index 42b7ddfc2..b257aa9a6 100644 --- a/API/Services/Tasks/StatsService.cs +++ b/API/Services/Tasks/StatsService.cs @@ -119,7 +119,7 @@ public class StatsService : IStatsService Os = RuntimeInformation.OSDescription, KavitaVersion = serverSettings.InstallVersion, DotnetVersion = Environment.Version.ToString(), - IsDocker = new OsInfo().IsDocker, + IsDocker = OsInfo.IsDocker, NumOfCores = Math.Max(Environment.ProcessorCount, 1), UsersWithEmulateComicBook = await _context.AppUserPreferences.CountAsync(p => p.EmulateBook), TotalReadingHours = await _statisticService.TimeSpentReadingForUsersAsync(ArraySegment.Empty, ArraySegment.Empty), diff --git a/API/Services/Tasks/VersionUpdaterService.cs b/API/Services/Tasks/VersionUpdaterService.cs index 3abbb0868..bfb4a34c7 100644 --- a/API/Services/Tasks/VersionUpdaterService.cs +++ b/API/Services/Tasks/VersionUpdaterService.cs @@ -100,7 +100,7 @@ public class VersionUpdaterService : IVersionUpdaterService UpdateBody = _markdown.Transform(update.Body.Trim()), UpdateTitle = update.Name, UpdateUrl = update.Html_Url, - IsDocker = new OsInfo(Array.Empty()).IsDocker, + IsDocker = OsInfo.IsDocker, PublishDate = update.Published_At }; } diff --git a/API/Startup.cs b/API/Startup.cs index 109e018eb..7f6d582d9 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -415,7 +415,9 @@ public class Startup } catch (Exception ex) { - if ((ex.Message.Contains("Permission denied") || ex.Message.Contains("UnauthorizedAccessException")) && baseUrl.Equals(Configuration.DefaultBaseUrl) && new OsInfo().IsDocker) + if ((ex.Message.Contains("Permission denied") + || ex.Message.Contains("UnauthorizedAccessException")) + && baseUrl.Equals(Configuration.DefaultBaseUrl) && OsInfo.IsDocker) { // Swallow the exception as the install is non-root and Docker return; diff --git a/Dockerfile b/Dockerfile index 19eb5e1df..a9eafdd7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,5 +32,7 @@ WORKDIR /kavita HEALTHCHECK --interval=30s --timeout=15s --start-period=30s --retries=3 CMD curl --fail http://localhost:5000/api/health || exit 1 +ENV DOTNET_RUNNING_IN_CONTAINER=true + ENTRYPOINT [ "/bin/bash" ] CMD ["/entrypoint.sh"] diff --git a/Kavita.Common/Configuration.cs b/Kavita.Common/Configuration.cs index 6ea7441d5..e855aef3d 100644 --- a/Kavita.Common/Configuration.cs +++ b/Kavita.Common/Configuration.cs @@ -113,7 +113,7 @@ public static class Configuration private static void SetPort(string filePath, int port) { - if (new OsInfo(Array.Empty()).IsDocker) + if (OsInfo.IsDocker) { return; } @@ -135,7 +135,7 @@ public static class Configuration private static int GetPort(string filePath) { const int defaultPort = 5000; - if (new OsInfo(Array.Empty()).IsDocker) + if (OsInfo.IsDocker) { return defaultPort; } @@ -165,7 +165,7 @@ public static class Configuration private static void SetIpAddresses(string filePath, string ipAddresses) { - if (new OsInfo(Array.Empty()).IsDocker) + if (OsInfo.IsDocker) { return; } @@ -186,7 +186,7 @@ public static class Configuration private static string GetIpAddresses(string filePath) { - if (new OsInfo(Array.Empty()).IsDocker) + if (OsInfo.IsDocker) { return string.Empty; } @@ -276,7 +276,7 @@ public static class Configuration #region XFrameOrigins private static string GetXFrameOptions(string filePath) { - if (new OsInfo(Array.Empty()).IsDocker) + if (OsInfo.IsDocker) { return DefaultBaseUrl; } diff --git a/Kavita.Common/EnvironmentInfo/IOsInfo.cs b/Kavita.Common/EnvironmentInfo/IOsInfo.cs index 54ec1500c..605da5592 100644 --- a/Kavita.Common/EnvironmentInfo/IOsInfo.cs +++ b/Kavita.Common/EnvironmentInfo/IOsInfo.cs @@ -6,21 +6,14 @@ using System.Linq; namespace Kavita.Common.EnvironmentInfo; -public class OsInfo : IOsInfo +public static class OsInfo { public static Os Os { get; } - public static bool IsNotWindows => !IsWindows; public static bool IsLinux => Os is Os.Linux or Os.LinuxMusl or Os.Bsd; public static bool IsOsx => Os == Os.Osx; public static bool IsWindows => Os == Os.Windows; - - // this needs to not be static so we can mock it - public bool IsDocker { get; private set; } - - public string Version { get; } - public string Name { get; } - public string FullName { get; } + public static bool IsDocker => Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true"; static OsInfo() { @@ -43,56 +36,6 @@ public class OsInfo : IOsInfo } } - public OsInfo(IEnumerable versionAdapters) - { - OsVersionModel osInfo = null; - - foreach (var osVersionAdapter in versionAdapters.Where(c => c.Enabled)) - { - try - { - osInfo = osVersionAdapter.Read(); - } - catch (Exception e) - { - Console.WriteLine("Couldn't get OS Version info: " + e.Message); - } - - if (osInfo != null) - { - break; - } - } - - if (osInfo != null) - { - Name = osInfo.Name; - Version = osInfo.Version; - FullName = osInfo.FullName; - } - else - { - Name = Os.ToString(); - FullName = Name; - } - - if (IsLinux && File.Exists("/proc/1/cgroup") && File.ReadAllText("/proc/1/cgroup").Contains("/docker/")) - { - IsDocker = true; - } - } - - public OsInfo() - { - Name = Os.ToString(); - FullName = Name; - - if (IsLinux && File.Exists("/proc/1/cgroup") && File.ReadAllText("/proc/1/cgroup").Contains("/docker/")) - { - IsDocker = true; - } - } - private static Os GetPosixFlavour() { var output = RunAndCapture("uname", "-s"); @@ -139,14 +82,6 @@ public class OsInfo : IOsInfo } } -public interface IOsInfo -{ - string Version { get; } - string Name { get; } - string FullName { get; } - - bool IsDocker { get; } -} public enum Os { diff --git a/openapi.json b/openapi.json index 3ed643b75..36eb05fcf 100644 --- a/openapi.json +++ b/openapi.json @@ -7,7 +7,7 @@ "name": "GPL-3.0", "url": "https://github.com/Kareadita/Kavita/blob/develop/LICENSE" }, - "version": "0.7.2.8" + "version": "0.7.2.10" }, "servers": [ {