Radarr/src/NzbDrone.Update/UpdateApp.cs

132 lines
4.4 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
2011-11-14 03:09:34 +00:00
using System.IO;
using System.Linq;
using DryIoc;
2011-11-13 20:31:02 +00:00
using NLog;
using NzbDrone.Common.Composition.Extensions;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
2013-06-06 04:33:16 +00:00
using NzbDrone.Common.Instrumentation;
using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Common.Processes;
2013-05-21 04:03:05 +00:00
using NzbDrone.Update.UpdateEngine;
namespace NzbDrone.Update
{
public class UpdateApp
{
2013-05-21 04:03:05 +00:00
private readonly IInstallUpdateService _installUpdateService;
2013-05-10 23:53:50 +00:00
private readonly IProcessProvider _processProvider;
2014-12-17 07:12:26 +00:00
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(UpdateApp));
2011-11-13 20:31:02 +00:00
public UpdateApp(IInstallUpdateService installUpdateService, IProcessProvider processProvider)
2011-11-13 20:31:02 +00:00
{
2013-05-21 04:03:05 +00:00
_installUpdateService = installUpdateService;
2011-11-13 20:31:02 +00:00
_processProvider = processProvider;
}
public static void Main(string[] args)
{
try
{
2013-11-26 06:53:36 +00:00
var startupArgument = new StartupContext(args);
2014-12-17 07:12:26 +00:00
NzbDroneLogger.Register(startupArgument, true, true);
2013-08-31 01:42:30 +00:00
Logger.Info("Starting Radarr Update Client");
2013-08-16 03:32:23 +00:00
var container = new Container(rules => rules.WithNzbDroneRules())
.AutoAddServices(new List<string> { "Radarr.Update" })
.AddNzbDroneLogger()
.AddStartupContext(startupArgument);
container.Resolve<InitializeLogger>().Initialize();
container.Resolve<UpdateApp>().Start(args);
2014-12-17 16:26:28 +00:00
2015-06-28 01:20:51 +00:00
Logger.Info("Update completed successfully");
2011-11-13 20:31:02 +00:00
}
catch (Exception e)
{
2016-02-11 21:13:42 +00:00
Logger.Fatal(e, "An error has occurred while applying update package.");
2011-11-13 20:31:02 +00:00
}
}
2011-11-13 20:31:02 +00:00
public void Start(string[] args)
{
var startupContext = ParseArgs(args);
2014-06-05 00:10:33 +00:00
var targetFolder = GetInstallationDirectory(startupContext);
2014-12-17 07:12:26 +00:00
_installUpdateService.Start(targetFolder, startupContext.ProcessId);
2011-11-13 20:31:02 +00:00
}
private UpdateStartupContext ParseArgs(string[] args)
{
if (args == null || !args.Any())
{
throw new ArgumentOutOfRangeException("args", "args must be specified");
}
var startupContext = new UpdateStartupContext
{
ProcessId = ParseProcessId(args[0])
};
2014-12-08 06:05:27 +00:00
if (OsInfo.IsNotWindows)
2014-06-05 23:28:28 +00:00
{
switch (args.Length)
2014-06-05 23:28:28 +00:00
{
2014-12-07 07:23:11 +00:00
case 1:
return startupContext;
default:
{
2014-12-17 07:12:26 +00:00
Logger.Debug("Arguments:");
2014-06-05 23:28:28 +00:00
2014-12-17 07:12:26 +00:00
foreach (var arg in args)
{
Logger.Debug(" {0}", arg);
}
2014-06-05 23:28:28 +00:00
startupContext.UpdateLocation = args[1];
startupContext.ExecutingApplication = args[2];
2014-12-17 07:12:26 +00:00
break;
2014-12-17 07:12:26 +00:00
}
2014-06-05 23:28:28 +00:00
}
}
return startupContext;
}
private int ParseProcessId(string arg)
2011-11-13 20:31:02 +00:00
{
if (!int.TryParse(arg, out var id) || id <= 0)
2011-11-13 20:31:02 +00:00
{
throw new ArgumentOutOfRangeException("arg", "Invalid process ID");
2011-11-13 20:31:02 +00:00
}
2022-02-12 03:00:17 +00:00
Logger.Debug("Radarr process ID: {0}", id);
2011-11-13 20:31:02 +00:00
return id;
}
2014-06-05 00:10:33 +00:00
private string GetInstallationDirectory(UpdateStartupContext startupContext)
{
if (startupContext.ExecutingApplication.IsNullOrWhiteSpace())
{
2014-12-17 07:12:26 +00:00
Logger.Debug("Using process ID to find installation directory: {0}", startupContext.ProcessId);
2014-06-05 00:10:33 +00:00
var exeFileInfo = new FileInfo(_processProvider.GetProcessById(startupContext.ProcessId).StartPath);
2014-12-17 07:12:26 +00:00
Logger.Debug("Executable location: {0}", exeFileInfo.FullName);
2014-06-05 00:10:33 +00:00
return exeFileInfo.DirectoryName;
}
else
{
2014-12-17 07:12:26 +00:00
Logger.Debug("Using executing application: {0}", startupContext.ExecutingApplication);
2014-06-05 00:10:33 +00:00
var exeFileInfo = new FileInfo(startupContext.ExecutingApplication);
2014-12-17 07:12:26 +00:00
Logger.Debug("Executable location: {0}", exeFileInfo.FullName);
2014-06-05 00:10:33 +00:00
return exeFileInfo.DirectoryName;
}
}
}
}