parent
2573558321
commit
99f269cd95
@ -0,0 +1,38 @@
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
|
||||
namespace NzbDrone.Update.UpdateEngine
|
||||
{
|
||||
public interface IBackupAndRestore
|
||||
{
|
||||
void BackUp(string source);
|
||||
void Restore(string target);
|
||||
}
|
||||
|
||||
public class BackupAndRestore : IBackupAndRestore
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IEnvironmentProvider _environmentProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public BackupAndRestore(IDiskProvider diskProvider, IEnvironmentProvider environmentProvider, Logger logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_environmentProvider = environmentProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void BackUp(string source)
|
||||
{
|
||||
_logger.Info("Creating backup of existing installation");
|
||||
_diskProvider.CopyDirectory(source, _environmentProvider.GetUpdateBackUpFolder());
|
||||
}
|
||||
|
||||
public void Restore(string target)
|
||||
{
|
||||
//TODO:this should ignore single file failures.
|
||||
_logger.Info("Attempting to rollback upgrade");
|
||||
_diskProvider.CopyDirectory(_environmentProvider.GetUpdateBackUpFolder(), target);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
using System.Linq;
|
||||
using NzbDrone.Common;
|
||||
|
||||
namespace NzbDrone.Update.UpdateEngine
|
||||
{
|
||||
public interface IDetectApplicationType
|
||||
{
|
||||
AppType GetAppType();
|
||||
}
|
||||
|
||||
public class DetectApplicationType : IDetectApplicationType
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IProcessProvider _processProvider;
|
||||
|
||||
public DetectApplicationType(IServiceProvider serviceProvider, IProcessProvider processProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_processProvider = processProvider;
|
||||
}
|
||||
|
||||
public AppType GetAppType()
|
||||
{
|
||||
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)
|
||||
&& _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
||||
{
|
||||
return AppType.Service;
|
||||
}
|
||||
|
||||
if (_processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProcessName).Any())
|
||||
{
|
||||
return AppType.Console;
|
||||
}
|
||||
|
||||
return AppType.Normal;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
|
||||
namespace NzbDrone.Update.UpdateEngine
|
||||
{
|
||||
public interface IInstallUpdateService
|
||||
{
|
||||
void Start(string installationFolder);
|
||||
}
|
||||
|
||||
public class InstallUpdateService : IInstallUpdateService
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IDetectApplicationType _detectApplicationType;
|
||||
private readonly ITerminateNzbDrone _terminateNzbDrone;
|
||||
private readonly IEnvironmentProvider _environmentProvider;
|
||||
private readonly IBackupAndRestore _backupAndRestore;
|
||||
private readonly IStartNzbDrone _startNzbDrone;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public InstallUpdateService(IDiskProvider diskProvider, IDetectApplicationType detectApplicationType, ITerminateNzbDrone terminateNzbDrone,
|
||||
IProcessProvider processProvider, IEnvironmentProvider environmentProvider, IBackupAndRestore backupAndRestore, IStartNzbDrone startNzbDrone, Logger logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_detectApplicationType = detectApplicationType;
|
||||
_terminateNzbDrone = terminateNzbDrone;
|
||||
_environmentProvider = environmentProvider;
|
||||
_backupAndRestore = backupAndRestore;
|
||||
_startNzbDrone = startNzbDrone;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private void Verify(string targetFolder)
|
||||
{
|
||||
_logger.Info("Verifying requirements before update...");
|
||||
|
||||
if (String.IsNullOrWhiteSpace(targetFolder))
|
||||
throw new ArgumentException("Target folder can not be null or empty");
|
||||
|
||||
if (!_diskProvider.FolderExists(targetFolder))
|
||||
throw new DirectoryNotFoundException("Target folder doesn't exist " + targetFolder);
|
||||
|
||||
_logger.Info("Verifying Update Folder");
|
||||
if (!_diskProvider.FolderExists(_environmentProvider.GetUpdatePackageFolder()))
|
||||
throw new DirectoryNotFoundException("Update folder doesn't exist " + _environmentProvider.GetUpdatePackageFolder());
|
||||
}
|
||||
|
||||
public void Start(string installationFolder)
|
||||
{
|
||||
Verify(installationFolder);
|
||||
|
||||
var appType = _detectApplicationType.GetAppType();
|
||||
|
||||
try
|
||||
{
|
||||
_terminateNzbDrone.Terminate();
|
||||
|
||||
_backupAndRestore.BackUp(installationFolder);
|
||||
|
||||
_logger.Info("Moving update package to target");
|
||||
|
||||
try
|
||||
{
|
||||
_diskProvider.CopyDirectory(_environmentProvider.GetUpdatePackageFolder(), installationFolder);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_backupAndRestore.Restore(installationFolder);
|
||||
_logger.FatalException("Failed to copy upgrade package to target folder.", e);
|
||||
}
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
_startNzbDrone.Start(appType, installationFolder);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
using System.IO;
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
|
||||
namespace NzbDrone.Update.UpdateEngine
|
||||
{
|
||||
public interface IStartNzbDrone
|
||||
{
|
||||
void Start(AppType appType, string installationFolder);
|
||||
}
|
||||
|
||||
public class StartNzbDrone : IStartNzbDrone
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IProcessProvider _processProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public StartNzbDrone(IServiceProvider serviceProvider, IProcessProvider processProvider, Logger logger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_processProvider = processProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void Start(AppType appType, string installationFolder)
|
||||
{
|
||||
_logger.Info("Starting NzbDrone");
|
||||
if (appType == AppType.Service)
|
||||
{
|
||||
_logger.Info("Starting NzbDrone service");
|
||||
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
||||
}
|
||||
else if (appType == AppType.Console)
|
||||
{
|
||||
_logger.Info("Starting NzbDrone with Console");
|
||||
_processProvider.Start(Path.Combine(installationFolder, "NzbDrone.Console.exe"));
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info("Starting NzbDrone without Console");
|
||||
_processProvider.Start(Path.Combine(installationFolder, "NzbDrone.exe"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common;
|
||||
using IServiceProvider = NzbDrone.Common.IServiceProvider;
|
||||
|
||||
namespace NzbDrone.Update.UpdateEngine
|
||||
{
|
||||
public interface ITerminateNzbDrone
|
||||
{
|
||||
void Terminate();
|
||||
}
|
||||
|
||||
public class TerminateNzbDrone : ITerminateNzbDrone
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IProcessProvider _processProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public TerminateNzbDrone(IServiceProvider serviceProvider, IProcessProvider processProvider, Logger logger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_processProvider = processProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
_logger.Info("Stopping all running services");
|
||||
|
||||
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)
|
||||
&& _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
||||
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
_logger.WarnException("couldn't stop service", e);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO:Should be able to restart service if anything beyond this point fails
|
||||
_logger.Info("Killing all running processes");
|
||||
|
||||
if (_processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProcessName).Any())
|
||||
{
|
||||
_processProvider.KillAll(ProcessProvider.NzbDroneConsoleProcessName);
|
||||
}
|
||||
|
||||
_processProvider.KillAll(ProcessProvider.NzbDroneProcessName);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue