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