2011-10-23 18:31:17 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Update.Providers
|
|
|
|
|
{
|
|
|
|
|
public class UpdateProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
|
|
|
|
private readonly ConsoleProvider _consoleProvider;
|
|
|
|
|
private readonly ServiceProvider _serviceProvider;
|
2011-10-23 18:31:17 +00:00
|
|
|
|
private readonly ProcessProvider _processProvider;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
public UpdateProvider(DiskProvider diskProvider, EnviromentProvider enviromentProvider, ConsoleProvider consoleProvider,
|
2011-10-23 18:31:17 +00:00
|
|
|
|
ServiceProvider serviceProvider, ProcessProvider processProvider)
|
2011-10-23 05:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_enviromentProvider = enviromentProvider;
|
|
|
|
|
_consoleProvider = consoleProvider;
|
|
|
|
|
_serviceProvider = serviceProvider;
|
2011-10-23 18:31:17 +00:00
|
|
|
|
_processProvider = processProvider;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-23 18:31:17 +00:00
|
|
|
|
public void Verify(string targetFolder)
|
2011-10-23 05:26:43 +00:00
|
|
|
|
{
|
2011-10-23 18:31:17 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2011-10-23 05:26:43 +00:00
|
|
|
|
var sandboxFolder = Path.Combine(_enviromentProvider.StartUpPath, "nzbdrone_update");
|
|
|
|
|
|
|
|
|
|
Logger.Info("Verifying Update Folder");
|
|
|
|
|
if (!_diskProvider.FolderExists(sandboxFolder))
|
2011-10-23 18:31:17 +00:00
|
|
|
|
throw new DirectoryNotFoundException("Update folder doesn't exist" + sandboxFolder);
|
2011-10-23 05:26:43 +00:00
|
|
|
|
|
2011-10-23 18:31:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start(string installationFolder)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Stopping all running services");
|
2011-10-23 05:26:43 +00:00
|
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider.Stop(ServiceProvider.NzbDroneServiceName);
|
|
|
|
|
}
|
|
|
|
|
|
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-10-23 05:26:43 +00:00
|
|
|
|
|
|
|
|
|
//Create backup of current folder
|
|
|
|
|
|
|
|
|
|
//Copy update folder on top of the existing folder
|
|
|
|
|
|
|
|
|
|
//Happy: Start Service, Process?
|
|
|
|
|
//Happy: Cleanup
|
|
|
|
|
|
|
|
|
|
//Sad: delete fucked up folder
|
|
|
|
|
//Sad: restore backup
|
|
|
|
|
//Sad: start service, process
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|