You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Radarr/src/NzbDrone.Update/UpdateApp.cs

135 lines
4.6 KiB

using System;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common.Composition;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation;
using NzbDrone.Common.Processes;
using NzbDrone.Common.Security;
using NzbDrone.Update.UpdateEngine;
namespace NzbDrone.Update
{
public class UpdateApp
{
private readonly IInstallUpdateService _installUpdateService;
private readonly IProcessProvider _processProvider;
private static IContainer _container;
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(UpdateApp));
public UpdateApp(IInstallUpdateService installUpdateService, IProcessProvider processProvider)
{
_installUpdateService = installUpdateService;
_processProvider = processProvider;
}
public static void Main(string[] args)
{
try
{
var startupArgument = new StartupContext(args);
NzbDroneLogger.Register(startupArgument, true, true);
Console.WriteLine("Starting NzbDrone Update Client");
X509CertificateValidationPolicy.Register();
_container = UpdateContainerBuilder.Build(startupArgument);
Logger.Info("Updating NzbDrone to version {0}", BuildInfo.Version);
_container.Resolve<UpdateApp>().Start(args);
}
catch (Exception e)
{
Logger.FatalException("An error has occurred while applying update package.", e);
}
}
public void Start(string[] args)
{
var startupContext = ParseArgs(args);
var targetFolder = GetInstallationDirectory(startupContext);
Logger.Info("Starting update process. Target Path:{0}", targetFolder);
_installUpdateService.Start(targetFolder, startupContext.ProcessId);
}
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])
};
10 years ago
if (OsInfo.IsNotWindows)
11 years ago
{
switch (args.Count())
11 years ago
{
case 1:
return startupContext;
case 3:
startupContext.UpdateLocation = args[1];
startupContext.ExecutingApplication = args[2];
break;
default:
{
Logger.Debug("Arguments:");
11 years ago
foreach (var arg in args)
{
Logger.Debug(" {0}", arg);
}
11 years ago
var message = String.Format("Number of arguments are unexpected, expected: 3, found: {0}", args.Count());
throw new ArgumentOutOfRangeException("args", message);
}
11 years ago
}
}
return startupContext;
}
private int ParseProcessId(string arg)
{
int id;
if (!Int32.TryParse(arg, out id) || id <= 0)
{
throw new ArgumentOutOfRangeException("arg", "Invalid process ID");
}
Logger.Debug("NzbDrone process ID: {0}", id);
return id;
}
private string GetInstallationDirectory(UpdateStartupContext startupContext)
{
if (startupContext.ExecutingApplication.IsNullOrWhiteSpace())
{
Logger.Debug("Using process ID to find installation directory: {0}", startupContext.ProcessId);
var exeFileInfo = new FileInfo(_processProvider.GetProcessById(startupContext.ProcessId).StartPath);
Logger.Debug("Executable location: {0}", exeFileInfo.FullName);
return exeFileInfo.DirectoryName;
}
else
{
Logger.Debug("Using executing application: {0}", startupContext.ExecutingApplication);
var exeFileInfo = new FileInfo(startupContext.ExecutingApplication);
Logger.Debug("Executable location: {0}", exeFileInfo.FullName);
return exeFileInfo.DirectoryName;
}
}
}
}