2019-04-13 03:25:58 +00:00
|
|
|
using System;
|
2021-10-21 20:04:26 +00:00
|
|
|
using System.Collections.Generic;
|
2011-11-14 03:09:34 +00:00
|
|
|
using System.IO;
|
2014-04-28 06:34:29 +00:00
|
|
|
using System.Linq;
|
2021-10-21 20:04:26 +00:00
|
|
|
using DryIoc;
|
2011-11-13 20:31:02 +00:00
|
|
|
using NLog;
|
2021-10-21 20:04:26 +00:00
|
|
|
using NzbDrone.Common.Composition.Extensions;
|
2013-06-28 00:04:52 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2013-06-06 04:33:16 +00:00
|
|
|
using NzbDrone.Common.Instrumentation;
|
2021-10-21 20:04:26 +00:00
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
2013-09-20 23:56:17 +00:00
|
|
|
using NzbDrone.Common.Processes;
|
2013-05-21 04:03:05 +00:00
|
|
|
using NzbDrone.Update.UpdateEngine;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Update
|
|
|
|
{
|
2013-08-16 02:20:54 +00:00
|
|
|
public class UpdateApp
|
2011-10-23 05:26:43 +00:00
|
|
|
{
|
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
|
|
|
|
2013-08-16 02:20:54 +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
|
|
|
|
2017-01-08 17:47:48 +00:00
|
|
|
Logger.Info("Starting Radarr Update Client");
|
2013-08-16 03:32:23 +00:00
|
|
|
|
2021-10-21 20:04:26 +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-21 02:13:10 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:31:02 +00:00
|
|
|
public void Start(string[] args)
|
|
|
|
{
|
2014-04-28 06:34:29 +00:00
|
|
|
var startupContext = ParseArgs(args);
|
2014-06-05 00:10:33 +00:00
|
|
|
var targetFolder = GetInstallationDirectory(startupContext);
|
2014-12-17 07:12:26 +00:00
|
|
|
|
2014-05-24 19:06:32 +00:00
|
|
|
_installUpdateService.Start(targetFolder, startupContext.ProcessId);
|
2011-11-13 20:31:02 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 06:34:29 +00:00
|
|
|
private UpdateStartupContext ParseArgs(string[] args)
|
|
|
|
{
|
|
|
|
if (args == null || !args.Any())
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("args", "args must be specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
var startupContext = new UpdateStartupContext
|
2017-01-08 17:47:48 +00:00
|
|
|
{
|
|
|
|
ProcessId = ParseProcessId(args[0])
|
|
|
|
};
|
2014-04-28 06:34:29 +00:00
|
|
|
|
2014-12-08 06:05:27 +00:00
|
|
|
if (OsInfo.IsNotWindows)
|
2014-06-05 23:28:28 +00:00
|
|
|
{
|
2020-10-02 18:05:34 +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
|
|
|
|
2014-12-21 16:42:50 +00:00
|
|
|
startupContext.UpdateLocation = args[1];
|
|
|
|
startupContext.ExecutingApplication = args[2];
|
2014-12-17 07:12:26 +00:00
|
|
|
|
2014-12-21 16:42:50 +00:00
|
|
|
break;
|
2014-12-17 07:12:26 +00:00
|
|
|
}
|
2014-06-05 23:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-02 17:41:34 +00:00
|
|
|
|
2014-04-28 06:34:29 +00:00
|
|
|
return startupContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int ParseProcessId(string arg)
|
2011-11-13 20:31:02 +00:00
|
|
|
{
|
2023-05-23 10:48:37 +00:00
|
|
|
if (!int.TryParse(arg, out var id) || id <= 0)
|
2011-11-13 20:31:02 +00:00
|
|
|
{
|
2014-04-28 06:34:29 +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;
|
|
|
|
}
|
|
|
|
}
|
2011-10-23 05:26:43 +00:00
|
|
|
}
|
|
|
|
}
|