Changed IsDocker check (#1998)

* Refactored IsDocker to be completely static and changed to use an environment variable instead.

* Removed file from another branch
This commit is contained in:
Joe Milazzo 2023-05-15 17:57:57 -05:00 committed by GitHub
parent 57a6c769bc
commit be5b5d9251
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 20 additions and 77 deletions

View file

@ -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<IOsVersionAdapter> 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
{