Radarr/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs

220 lines
6.0 KiB
C#
Raw Normal View History

2018-04-21 04:59:31 +00:00
using System;
using System.Diagnostics;
2013-07-26 06:11:55 +00:00
using System.IO;
using System.Reflection;
using System.Security.Principal;
2013-11-26 06:53:36 +00:00
using System.ServiceProcess;
using NLog;
2014-01-14 01:35:16 +00:00
using NzbDrone.Common.Processes;
namespace NzbDrone.Common.EnvironmentInfo
{
2018-04-21 04:59:31 +00:00
public class RuntimeInfo : IRuntimeInfo
{
private readonly Logger _logger;
2018-11-23 07:03:32 +00:00
private readonly DateTime _startTime = DateTime.UtcNow;
2018-04-21 04:59:31 +00:00
public RuntimeInfo(IServiceProvider serviceProvider, Logger logger)
{
_logger = logger;
2013-11-26 06:53:36 +00:00
IsWindowsService = !IsUserInteractive &&
2014-01-13 01:14:57 +00:00
OsInfo.IsWindows &&
2018-11-23 07:03:32 +00:00
serviceProvider.ServiceExist(ServiceProvider.SERVICE_NAME) &&
serviceProvider.GetStatus(ServiceProvider.SERVICE_NAME) == ServiceControllerStatus.StartPending;
//Guarded to avoid issues when running in a non-managed process
2020-08-25 20:26:45 +00:00
var entry = Process.GetCurrentProcess().MainModule;
2014-02-10 19:40:13 +00:00
if (entry != null)
{
2020-08-25 20:26:45 +00:00
ExecutingApplication = entry.FileName;
IsWindowsTray = OsInfo.IsWindows && entry.ModuleName == $"{ProcessProvider.RADARR_PROCESS_NAME}.exe";
}
}
2018-04-21 04:59:31 +00:00
static RuntimeInfo()
{
var officialBuild = InternalIsOfficialBuild();
// An build running inside of the testing environment. (Analytics disabled)
IsTesting = InternalIsTesting();
// An official build running outside of the testing environment. (Analytics configurable)
IsProduction = !IsTesting && officialBuild;
// An unofficial build running outside of the testing environment. (Analytics enabled)
IsDevelopment = !IsTesting && !officialBuild && !InternalIsDebug();
}
2018-11-23 07:03:32 +00:00
public DateTime StartTime
{
get
{
return _startTime;
}
}
2016-12-09 06:54:15 +00:00
public static bool IsUserInteractive => Environment.UserInteractive;
2013-07-26 06:11:55 +00:00
2016-12-09 06:54:15 +00:00
bool IRuntimeInfo.IsUserInteractive => IsUserInteractive;
public bool IsAdmin
{
get
{
if (OsInfo.IsNotWindows)
{
return false;
}
try
{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (Exception ex)
{
2016-02-11 21:13:42 +00:00
_logger.Warn(ex, "Error checking if the current user is an administrator.");
return false;
}
}
}
2013-11-26 06:53:36 +00:00
public bool IsWindowsService { get; private set; }
2018-04-21 04:59:31 +00:00
public bool IsExiting { get; set; }
2018-11-23 07:03:32 +00:00
public bool IsTray
{
get
{
if (OsInfo.IsWindows)
{
return IsUserInteractive && Process.GetCurrentProcess().ProcessName.Equals(ProcessProvider.RADARR_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase);
}
return false;
}
}
public RuntimeMode Mode
{
get
{
if (IsWindowsService)
{
return RuntimeMode.Service;
}
if (IsTray)
{
return RuntimeMode.Tray;
}
return RuntimeMode.Console;
}
}
public bool RestartPending { get; set; }
2018-04-21 04:59:31 +00:00
public string ExecutingApplication { get; }
public static bool IsTesting { get; }
2018-04-21 04:59:31 +00:00
public static bool IsProduction { get; }
public static bool IsDevelopment { get; }
2013-07-26 06:11:55 +00:00
private static bool InternalIsTesting()
{
try
{
var lowerProcessName = Process.GetCurrentProcess().ProcessName.ToLower();
2019-12-22 22:08:53 +00:00
if (lowerProcessName.Contains("vshost"))
{
return true;
}
if (lowerProcessName.Contains("nunit"))
{
return true;
}
if (lowerProcessName.Contains("jetbrain"))
{
return true;
}
if (lowerProcessName.Contains("resharper"))
{
return true;
}
}
catch
{
}
2018-04-21 04:59:31 +00:00
try
{
var currentAssemblyLocation = typeof(RuntimeInfo).Assembly.Location;
2019-12-22 22:08:53 +00:00
if (currentAssemblyLocation.ToLower().Contains("_output"))
{
return true;
}
if (currentAssemblyLocation.ToLower().Contains("_tests"))
{
return true;
}
2018-04-21 04:59:31 +00:00
}
catch
{
}
2018-04-21 04:59:31 +00:00
var lowerCurrentDir = Directory.GetCurrentDirectory().ToLower();
2019-12-22 22:08:53 +00:00
if (lowerCurrentDir.Contains("vsts"))
{
return true;
}
if (lowerCurrentDir.Contains("buildagent"))
{
return true;
}
if (lowerCurrentDir.Contains("_output"))
{
return true;
}
if (lowerCurrentDir.Contains("_tests"))
{
return true;
}
return false;
}
private static bool InternalIsDebug()
{
2019-12-22 22:08:53 +00:00
if (BuildInfo.IsDebug || Debugger.IsAttached)
{
return true;
}
return false;
}
private static bool InternalIsOfficialBuild()
{
//Official builds will never have such a high revision
2019-12-22 22:08:53 +00:00
if (BuildInfo.Version.Major >= 10 || BuildInfo.Version.Revision > 10000)
{
return false;
}
2013-07-26 06:11:55 +00:00
return true;
}
2018-04-21 04:59:31 +00:00
public bool IsWindowsTray { get; private set; }
}
2019-12-22 21:24:10 +00:00
}