2011-10-23 11:31:17 -07:00
|
|
|
using System;
|
2011-11-18 22:49:12 -08:00
|
|
|
using System.Collections.Generic;
|
2011-10-23 11:31:17 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2011-10-22 22:26:43 -07:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
|
|
|
namespace NzbDrone.Update.Providers
|
|
|
|
{
|
|
|
|
public class UpdateProvider
|
|
|
|
{
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
private readonly ServiceProvider _serviceProvider;
|
2011-10-23 11:31:17 -07:00
|
|
|
private readonly ProcessProvider _processProvider;
|
2011-11-12 23:27:16 -08:00
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-11-20 17:13:58 -08:00
|
|
|
private readonly IISProvider _iisProvider;
|
2011-11-12 20:16:54 -08:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-10-22 22:26:43 -07:00
|
|
|
|
2011-11-13 12:31:02 -08:00
|
|
|
public UpdateProvider(DiskProvider diskProvider, ServiceProvider serviceProvider,
|
2011-11-20 17:13:58 -08:00
|
|
|
ProcessProvider processProvider, EnviromentProvider enviromentProvider, IISProvider iisProvider)
|
2011-10-22 22:26:43 -07:00
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_serviceProvider = serviceProvider;
|
2011-10-23 11:31:17 -07:00
|
|
|
_processProvider = processProvider;
|
2011-11-12 23:27:16 -08:00
|
|
|
_enviromentProvider = enviromentProvider;
|
2011-11-20 17:13:58 -08:00
|
|
|
_iisProvider = iisProvider;
|
2011-10-22 22:26:43 -07:00
|
|
|
}
|
|
|
|
|
2011-11-13 12:31:02 -08:00
|
|
|
public UpdateProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-12 21:19:19 -08:00
|
|
|
private void Verify(string targetFolder)
|
2011-10-22 22:26:43 -07:00
|
|
|
{
|
2011-11-12 20:16:54 -08:00
|
|
|
logger.Info("Verifying requirements before update...");
|
2011-10-23 11:31:17 -07:00
|
|
|
|
2011-11-12 21:19:19 -08:00
|
|
|
if (String.IsNullOrWhiteSpace(targetFolder))
|
2011-10-23 11:31:17 -07:00
|
|
|
throw new ArgumentException("Target folder can not be null or empty");
|
|
|
|
|
2011-11-12 21:19:19 -08:00
|
|
|
if (!_diskProvider.FolderExists(targetFolder))
|
|
|
|
throw new DirectoryNotFoundException("Target folder doesn't exist " + targetFolder);
|
2011-10-22 22:26:43 -07:00
|
|
|
|
2011-11-12 20:16:54 -08:00
|
|
|
logger.Info("Verifying Update Folder");
|
2011-11-12 23:27:16 -08:00
|
|
|
if (!_diskProvider.FolderExists(_enviromentProvider.GetUpdatePackageFolder()))
|
|
|
|
throw new DirectoryNotFoundException("Update folder doesn't exist " + _enviromentProvider.GetUpdatePackageFolder());
|
2011-10-22 22:26:43 -07:00
|
|
|
|
2011-10-23 11:31:17 -07:00
|
|
|
}
|
|
|
|
|
2011-11-13 12:31:02 -08:00
|
|
|
public virtual void Start(string targetFolder)
|
2011-10-23 11:31:17 -07:00
|
|
|
{
|
2011-11-12 20:07:06 -08:00
|
|
|
Verify(targetFolder);
|
|
|
|
bool isService = false;
|
|
|
|
|
2011-11-12 20:16:54 -08:00
|
|
|
logger.Info("Stopping all running services");
|
2011-11-20 17:13:58 -08:00
|
|
|
|
2011-11-14 18:38:15 -08:00
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)
|
|
|
|
&& _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-22 22:26:43 -07:00
|
|
|
{
|
2011-11-14 18:38:15 -08:00
|
|
|
isService = true;
|
2011-10-26 10:15:47 -07:00
|
|
|
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-22 22:26:43 -07:00
|
|
|
}
|
2011-11-20 17:13:58 -08:00
|
|
|
|
2011-11-25 15:46:29 -08:00
|
|
|
//TODO:Should be able to restart service if anything beyond this point fails
|
|
|
|
|
2011-11-12 20:16:54 -08:00
|
|
|
logger.Info("Killing all running processes");
|
2011-10-23 11:31:17 -07:00
|
|
|
var processes = _processProvider.GetProcessByName(ProcessProvider.NzbDroneProccessName);
|
|
|
|
foreach (var processInfo in processes)
|
|
|
|
{
|
|
|
|
_processProvider.Kill(processInfo.Id);
|
|
|
|
}
|
|
|
|
|
2011-11-20 17:13:58 -08:00
|
|
|
logger.Info("Killing all orphan IISExpress processes");
|
|
|
|
_iisProvider.StopServer();
|
|
|
|
|
2011-11-12 20:16:54 -08:00
|
|
|
logger.Info("Creating backup of existing installation");
|
2011-11-12 23:27:16 -08:00
|
|
|
_diskProvider.CopyDirectory(targetFolder, _enviromentProvider.GetUpdateBackUpFolder());
|
2011-11-12 20:07:06 -08:00
|
|
|
|
2011-10-22 22:26:43 -07:00
|
|
|
|
2011-11-17 23:31:40 -08:00
|
|
|
logger.Info("Moving update package to target");
|
2011-11-12 20:07:06 -08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-11-21 16:39:56 -08:00
|
|
|
_diskProvider.CopyDirectory(_enviromentProvider.GetUpdatePackageFolder(), targetFolder);
|
2011-11-21 19:46:58 -08:00
|
|
|
|
|
|
|
logger.Trace("Deleting Update Package.");
|
2011-11-21 18:53:22 -08:00
|
|
|
_diskProvider.DeleteFolder(_enviromentProvider.GetUpdatePackageFolder(), true);
|
2011-11-12 20:07:06 -08:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
RollBack(targetFolder);
|
2011-11-18 22:49:12 -08:00
|
|
|
|
2011-11-20 17:13:58 -08:00
|
|
|
foreach (var key in e.Data.Keys)
|
2011-11-18 22:49:12 -08:00
|
|
|
{
|
|
|
|
logger.Trace("Key: {0}, Value: {1}", key, e.Data[key]);
|
|
|
|
}
|
|
|
|
|
2011-11-13 12:42:12 -08:00
|
|
|
logger.FatalException("Failed to copy upgrade package to target folder.", e);
|
2011-11-12 20:07:06 -08:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
StartNzbDrone(isService, targetFolder);
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 22:26:43 -07:00
|
|
|
|
2011-11-12 20:07:06 -08:00
|
|
|
private void RollBack(string targetFolder)
|
|
|
|
{
|
2011-11-25 15:46:29 -08:00
|
|
|
//TODO:this should ignore single file failures.
|
2011-11-12 20:16:54 -08:00
|
|
|
logger.Info("Attempting to rollback upgrade");
|
2011-11-12 23:27:16 -08:00
|
|
|
_diskProvider.CopyDirectory(_enviromentProvider.GetUpdateBackUpFolder(), targetFolder);
|
2011-11-12 20:07:06 -08:00
|
|
|
}
|
2011-10-22 22:26:43 -07:00
|
|
|
|
|
|
|
|
2011-11-12 20:07:06 -08:00
|
|
|
private void StartNzbDrone(bool isService, string targetFolder)
|
|
|
|
{
|
|
|
|
if (isService)
|
|
|
|
{
|
|
|
|
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_processProvider.Start(Path.Combine(targetFolder, "nzbdrone.exe"));
|
|
|
|
}
|
2011-10-22 22:26:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|