sonarr-repo-only/NzbDrone.Update/Providers/UpdateProvider.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2011-10-23 18:31:17 +00:00
using System;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
namespace NzbDrone.Update.Providers
{
public class UpdateProvider
{
private readonly DiskProvider _diskProvider;
private readonly EnviromentProvider _enviromentProvider;
private readonly ServiceProvider _serviceProvider;
2011-10-23 18:31:17 +00:00
private readonly ProcessProvider _processProvider;
2011-11-13 04:07:06 +00:00
private readonly PathProvider _pathProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-10-24 05:54:09 +00:00
public UpdateProvider(DiskProvider diskProvider, EnviromentProvider enviromentProvider,
2011-11-13 04:07:06 +00:00
ServiceProvider serviceProvider, ProcessProvider processProvider, PathProvider pathProvider)
{
_diskProvider = diskProvider;
_enviromentProvider = enviromentProvider;
_serviceProvider = serviceProvider;
2011-10-23 18:31:17 +00:00
_processProvider = processProvider;
2011-11-13 04:07:06 +00:00
_pathProvider = pathProvider;
}
2011-11-13 04:07:06 +00:00
private void Verify(string installationFolder)
{
2011-10-23 18:31:17 +00:00
Logger.Info("Verifying requirements before update...");
2011-11-13 04:07:06 +00:00
if (String.IsNullOrWhiteSpace(installationFolder))
2011-10-23 18:31:17 +00:00
throw new ArgumentException("Target folder can not be null or empty");
2011-11-13 04:07:06 +00:00
if (!_diskProvider.FolderExists(installationFolder))
throw new DirectoryNotFoundException("Target folder doesn't exist" + installationFolder);
Logger.Info("Verifying Update Folder");
2011-11-13 04:07:06 +00:00
if (!_diskProvider.FolderExists(_pathProvider.UpdatePackageFolder))
throw new DirectoryNotFoundException("Update folder doesn't exist" + _pathProvider.UpdateSandboxFolder);
2011-10-23 18:31:17 +00:00
}
2011-11-13 04:07:06 +00:00
public void Start(string targetFolder)
2011-10-23 18:31:17 +00:00
{
2011-11-13 04:07:06 +00:00
Verify(targetFolder);
bool isService = false;
2011-10-23 18:31:17 +00:00
Logger.Info("Stopping all running services");
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
2011-11-13 04:07:06 +00:00
if (_serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
isService = true;
}
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
2011-10-23 18:31:17 +00:00
Logger.Info("Killing all running processes");
var processes = _processProvider.GetProcessByName(ProcessProvider.NzbDroneProccessName);
foreach (var processInfo in processes)
{
_processProvider.Kill(processInfo.Id);
}
2011-11-13 04:07:06 +00:00
Logger.Info("Creating backup of existing installation");
_diskProvider.CopyDirectory(targetFolder, _pathProvider.UpdateBackUpFolder);
2011-11-13 04:07:06 +00:00
Logger.Info("Copying update package to target");
try
{
_diskProvider.CopyDirectory(_pathProvider.UpdatePackageFolder, targetFolder);
}
catch (Exception e)
{
RollBack(targetFolder);
Logger.Fatal("Failed to copy upgrade package to target folder.", e);
}
finally
{
StartNzbDrone(isService, targetFolder);
}
}
2011-11-13 04:07:06 +00:00
private void RollBack(string targetFolder)
{
Logger.Info("Attempting to rollback upgrade");
_diskProvider.CopyDirectory(_pathProvider.UpdateBackUpFolder, targetFolder);
}
2011-11-13 04:07:06 +00:00
private void StartNzbDrone(bool isService, string targetFolder)
{
if (isService)
{
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
else
{
_processProvider.Start(Path.Combine(targetFolder, "nzbdrone.exe"));
}
}
}
}