2013-06-28 00:04:52 +00:00
|
|
|
|
using System;
|
2017-01-04 02:36:47 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.EnvironmentInfo
|
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
public class OsInfo : IOsInfo
|
2013-06-28 00:04:52 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
public static Os Os { get; }
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
public static bool IsNotWindows => !IsWindows;
|
|
|
|
|
public static bool IsLinux => Os == Os.Linux;
|
|
|
|
|
public static bool IsOsx => Os == Os.Osx;
|
|
|
|
|
public static bool IsWindows => Os == Os.Windows;
|
2019-04-05 15:47:26 +00:00
|
|
|
|
|
|
|
|
|
// this needs to not be static so we can mock it
|
|
|
|
|
public bool IsDocker { get; }
|
2019-04-04 15:15:43 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
public string Version { get; }
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string FullName { get; }
|
2013-06-28 00:04:52 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
static OsInfo()
|
|
|
|
|
{
|
|
|
|
|
var platform = Environment.OSVersion.Platform;
|
2014-03-09 05:28:40 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
switch (platform)
|
2014-03-09 05:28:40 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
{
|
|
|
|
|
Os = Os.Windows;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PlatformID.MacOSX:
|
|
|
|
|
case PlatformID.Unix:
|
|
|
|
|
{
|
|
|
|
|
// Sometimes Mac OS reports itself as Unix
|
|
|
|
|
if (Directory.Exists("/System/Library/CoreServices/") &&
|
|
|
|
|
(File.Exists("/System/Library/CoreServices/SystemVersion.plist") ||
|
|
|
|
|
File.Exists("/System/Library/CoreServices/ServerVersion.plist"))
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
Os = Os.Osx;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Os = Os.Linux;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-03-09 05:28:40 +00:00
|
|
|
|
}
|
2013-06-28 00:04:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
public OsInfo(IEnumerable<IOsVersionAdapter> versionAdapters, Logger logger)
|
2013-06-28 00:04:52 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
OsVersionModel osInfo = null;
|
2014-03-09 05:28:40 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
foreach (var osVersionAdapter in versionAdapters.Where(c => c.Enabled))
|
2013-06-28 00:04:52 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
try
|
2014-03-09 05:28:40 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
osInfo = osVersionAdapter.Read();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error(e, "Couldn't get OS Version info");
|
|
|
|
|
}
|
2013-10-02 19:41:21 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
if (osInfo != null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
2014-03-09 05:28:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-04 02:36:47 +00:00
|
|
|
|
|
|
|
|
|
if (osInfo != null)
|
2013-10-02 19:41:21 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
Name = osInfo.Name;
|
|
|
|
|
Version = osInfo.Version;
|
|
|
|
|
FullName = osInfo.FullName;
|
2013-10-02 19:41:21 +00:00
|
|
|
|
}
|
2017-01-04 02:36:47 +00:00
|
|
|
|
else
|
2014-03-09 05:28:40 +00:00
|
|
|
|
{
|
2017-01-04 02:36:47 +00:00
|
|
|
|
Name = Os.ToString();
|
|
|
|
|
FullName = Name;
|
2014-03-09 05:28:40 +00:00
|
|
|
|
}
|
2017-01-04 08:53:09 +00:00
|
|
|
|
|
2019-04-05 15:47:26 +00:00
|
|
|
|
if (IsLinux && File.Exists("/proc/1/cgroup") && File.ReadAllText("/proc/1/cgroup").Contains("/docker/"))
|
|
|
|
|
{
|
|
|
|
|
IsDocker = true;
|
|
|
|
|
}
|
2013-10-02 19:41:21 +00:00
|
|
|
|
}
|
2013-06-28 00:04:52 +00:00
|
|
|
|
}
|
2014-03-09 05:28:40 +00:00
|
|
|
|
|
2017-01-04 02:36:47 +00:00
|
|
|
|
public interface IOsInfo
|
|
|
|
|
{
|
|
|
|
|
string Version { get; }
|
|
|
|
|
string Name { get; }
|
|
|
|
|
string FullName { get; }
|
2019-04-05 15:47:26 +00:00
|
|
|
|
|
|
|
|
|
bool IsDocker { get; }
|
2017-01-04 02:36:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-09 05:28:40 +00:00
|
|
|
|
public enum Os
|
|
|
|
|
{
|
|
|
|
|
Windows,
|
|
|
|
|
Linux,
|
|
|
|
|
Osx
|
|
|
|
|
}
|
2019-04-04 15:15:43 +00:00
|
|
|
|
}
|