1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-27 02:43:57 +02:00

Update fixes.

This commit is contained in:
kay.one 2011-11-20 18:59:42 -08:00
parent b74ef12a4c
commit 508be34ebc
9 changed files with 87 additions and 70 deletions

1
.gitignore vendored
View File

@ -42,3 +42,4 @@ _rawPackage/
NzbDrone.zip NzbDrone.zip
NzbDrone.sln.DotSettings.user* NzbDrone.sln.DotSettings.user*
config.xml config.xml
UpdateLogs/

View File

@ -40,6 +40,7 @@
<Reference Include="Moq"> <Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath> <HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference> </Reference>
<Reference Include="Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL" />
<Reference Include="NLog"> <Reference Include="NLog">
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath> <HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference> </Reference>

View File

@ -1,20 +1,21 @@
using System; using System.Linq;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using Ninject.Activation.Strategies;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests namespace NzbDrone.Core.Test.JobTests
{ {
[TestFixture] [TestFixture]
internal class StartUpdateFixture : CoreTest internal class AppUpdateJobFixture : CoreTest
{ {
private const string SANDBOX_FOLDER = @"C:\Temp\nzbdrone_update\"; private const string SANDBOX_FOLDER = @"C:\Temp\nzbdrone_update\";
@ -32,6 +33,7 @@ public void Setup()
{ {
Mocker.GetMock<EnviromentProvider>().SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); Mocker.GetMock<EnviromentProvider>().SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
Mocker.GetMock<ConfigFileProvider>().SetupGet(c => c.Guid).Returns(_clientGuid); Mocker.GetMock<ConfigFileProvider>().SetupGet(c => c.Guid).Returns(_clientGuid);
Mocker.GetMock<UpdateProvider>().Setup(c => c.GetAvilableUpdate()).Returns(updatePackage);
} }
@ -41,7 +43,7 @@ public void should_delete_sandbox_before_update_if_folder_exists()
Mocker.GetMock<DiskProvider>().Setup(c => c.FolderExists(SANDBOX_FOLDER)).Returns(true); Mocker.GetMock<DiskProvider>().Setup(c => c.FolderExists(SANDBOX_FOLDER)).Returns(true);
//Act //Act
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
//Assert //Assert
Mocker.GetMock<DiskProvider>().Verify(c => c.DeleteFolder(SANDBOX_FOLDER, true)); Mocker.GetMock<DiskProvider>().Verify(c => c.DeleteFolder(SANDBOX_FOLDER, true));
@ -53,7 +55,7 @@ public void should_not_delete_sandbox_before_update_if_folder_doesnt_exists()
Mocker.GetMock<DiskProvider>().Setup(c => c.FolderExists(SANDBOX_FOLDER)).Returns(false); Mocker.GetMock<DiskProvider>().Setup(c => c.FolderExists(SANDBOX_FOLDER)).Returns(false);
//Act //Act
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
//Assert //Assert
Mocker.GetMock<DiskProvider>().Verify(c => c.DeleteFolder(SANDBOX_FOLDER, true), Times.Never()); Mocker.GetMock<DiskProvider>().Verify(c => c.DeleteFolder(SANDBOX_FOLDER, true), Times.Never());
@ -65,7 +67,7 @@ public void Should_download_update_package()
var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName); var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName);
//Act //Act
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
//Assert //Assert
Mocker.GetMock<HttpProvider>().Verify( Mocker.GetMock<HttpProvider>().Verify(
@ -78,7 +80,7 @@ public void Should_extract_update_package()
var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName); var updateArchive = Path.Combine(SANDBOX_FOLDER, updatePackage.FileName);
//Act //Act
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
//Assert //Assert
Mocker.GetMock<ArchiveProvider>().Verify( Mocker.GetMock<ArchiveProvider>().Verify(
@ -91,7 +93,7 @@ public void Should_copy_update_client_to_root_of_sandbox()
var updateClientFolder = Mocker.GetMock<EnviromentProvider>().Object.GetUpdateClientFolder(); var updateClientFolder = Mocker.GetMock<EnviromentProvider>().Object.GetUpdateClientFolder();
//Act //Act
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
//Assert //Assert
Mocker.GetMock<DiskProvider>().Verify( Mocker.GetMock<DiskProvider>().Verify(
@ -108,7 +110,7 @@ public void should_start_update_client()
.SetupGet(c => c.NzbDroneProcessIdFromEnviroment).Returns(12); .SetupGet(c => c.NzbDroneProcessIdFromEnviroment).Returns(12);
//Act //Act
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
//Assert //Assert
Mocker.GetMock<ProcessProvider>().Verify( Mocker.GetMock<ProcessProvider>().Verify(
@ -134,7 +136,7 @@ public void Should_download_and_extract_to_temp_folder()
Mocker.Resolve<HttpProvider>(); Mocker.Resolve<HttpProvider>();
Mocker.Resolve<DiskProvider>(); Mocker.Resolve<DiskProvider>();
Mocker.Resolve<ArchiveProvider>(); Mocker.Resolve<ArchiveProvider>();
Mocker.Resolve<UpdateProvider>().StartUpdate(updatePackage); StartUpdate();
updateSubFolder.Refresh(); updateSubFolder.Refresh();
//Assert //Assert
@ -143,5 +145,10 @@ public void Should_download_and_extract_to_temp_folder()
updateSubFolder.GetDirectories().Should().HaveCount(1); updateSubFolder.GetDirectories().Should().HaveCount(1);
updateSubFolder.GetFiles().Should().HaveCount(1); updateSubFolder.GetFiles().Should().HaveCount(1);
} }
private void StartUpdate()
{
Mocker.Resolve<AppUpdateJob>().Start(MockNotification, 0, 0);
}
} }
} }

View File

@ -95,7 +95,7 @@
<Compile Include="JobTests\SearchJobTest.cs" /> <Compile Include="JobTests\SearchJobTest.cs" />
<Compile Include="ProviderTests\PostDownloadProviderTests\ProcessDownloadFixture.cs" /> <Compile Include="ProviderTests\PostDownloadProviderTests\ProcessDownloadFixture.cs" />
<Compile Include="ProviderTests\JobProviderTests\TestJobs.cs" /> <Compile Include="ProviderTests\JobProviderTests\TestJobs.cs" />
<Compile Include="ProviderTests\UpdateProviderTests\StartUpdateFixture.cs" /> <Compile Include="JobTests\AppUpdateJobFixture.cs" />
<Compile Include="ProviderTests\UpdateProviderTests\GetAvilableUpdateFixture.cs" /> <Compile Include="ProviderTests\UpdateProviderTests\GetAvilableUpdateFixture.cs" />
<Compile Include="SortHelperTest.cs" /> <Compile Include="SortHelperTest.cs" />
<Compile Include="ProviderTests\EpisodeProviderTest_DeleteInvalidEpisodes.cs" /> <Compile Include="ProviderTests\EpisodeProviderTest_DeleteInvalidEpisodes.cs" />

View File

@ -1,14 +1,35 @@
using NzbDrone.Core.Model.Notification; using System.Diagnostics;
using System.IO;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
namespace NzbDrone.Core.Providers.Jobs namespace NzbDrone.Core.Providers.Jobs
{ {
public class AppUpdateJob : IJob public class AppUpdateJob : IJob
{ {
private readonly UpdateProvider _updateProvider; private readonly UpdateProvider _updateProvider;
private readonly EnviromentProvider _enviromentProvider;
private readonly DiskProvider _diskProvider;
private readonly HttpProvider _httpProvider;
private readonly ProcessProvider _processProvider;
private readonly ArchiveProvider _archiveProvider;
private readonly ConfigFileProvider _configFileProvider;
public AppUpdateJob(UpdateProvider updateProvider) private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public AppUpdateJob(UpdateProvider updateProvider, EnviromentProvider enviromentProvider, DiskProvider diskProvider,
HttpProvider httpProvider, ProcessProvider processProvider, ArchiveProvider archiveProvider, ConfigFileProvider configFileProvider)
{ {
_updateProvider = updateProvider; _updateProvider = updateProvider;
_enviromentProvider = enviromentProvider;
_diskProvider = diskProvider;
_httpProvider = httpProvider;
_processProvider = processProvider;
_archiveProvider = archiveProvider;
_configFileProvider = configFileProvider;
} }
public string Name public string Name
@ -27,7 +48,38 @@ public virtual void Start(ProgressNotification notification, int targetId, int s
var updatePackage = _updateProvider.GetAvilableUpdate(); var updatePackage = _updateProvider.GetAvilableUpdate();
_updateProvider.StartUpdate(updatePackage); var packageDestination = Path.Combine(_enviromentProvider.GetUpdateSandboxFolder(), updatePackage.FileName);
if (_diskProvider.FolderExists(_enviromentProvider.GetUpdateSandboxFolder()))
{
logger.Info("Deleting old update files");
_diskProvider.DeleteFolder(_enviromentProvider.GetUpdateSandboxFolder(), true);
}
logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination);
notification.CurrentMessage = "Downloading Update " + updatePackage.Version;
_httpProvider.DownloadFile(updatePackage.Url, packageDestination);
logger.Info("Download completed for update package from [{0}]", updatePackage.FileName);
logger.Info("Extracting Update package");
notification.CurrentMessage = "Extracting Update";
_archiveProvider.ExtractArchive(packageDestination, _enviromentProvider.GetUpdateSandboxFolder());
logger.Info("Update package extracted successfully");
logger.Info("Preparing client");
notification.CurrentMessage = "Preparing to start Update";
_diskProvider.CopyDirectory(_enviromentProvider.GetUpdateClientFolder(), _enviromentProvider.GetUpdateSandboxFolder());
logger.Info("Starting update client");
var startInfo = new ProcessStartInfo()
{
FileName = _enviromentProvider.GetUpdateClientExePath(),
Arguments = string.Format("{0} {1}", _enviromentProvider.NzbDroneProcessIdFromEnviroment, _configFileProvider.Guid)
};
var updateProcess = _processProvider.Start(startInfo);
logger.Info("Update in progress. NzbDrone will restart shortly.");
} }
} }
} }

View File

@ -77,39 +77,5 @@ public virtual UpdatePackage GetAvilableUpdate()
logger.Trace("No updates available"); logger.Trace("No updates available");
return null; return null;
} }
public virtual void StartUpdate(UpdatePackage updatePackage)
{
var packageDestination = Path.Combine(_enviromentProvider.GetUpdateSandboxFolder(), updatePackage.FileName);
if (_diskProvider.FolderExists(_enviromentProvider.GetUpdateSandboxFolder()))
{
logger.Info("Deleting old update files");
_diskProvider.DeleteFolder(_enviromentProvider.GetUpdateSandboxFolder(), true);
}
logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination);
_httpProvider.DownloadFile(updatePackage.Url, packageDestination);
logger.Info("Download completed for update package from [{0}]", updatePackage.FileName);
logger.Info("Extracting Update package");
_archiveProvider.ExtractArchive(packageDestination, _enviromentProvider.GetUpdateSandboxFolder());
logger.Info("Update package extracted successfully");
logger.Info("Preparing client");
_diskProvider.CopyDirectory(_enviromentProvider.GetUpdateClientFolder(), _enviromentProvider.GetUpdateSandboxFolder());
logger.Info("Starting update client");
var startInfo = new ProcessStartInfo()
{
FileName = _enviromentProvider.GetUpdateClientExePath(),
Arguments = string.Format("{0} {1}", _enviromentProvider.NzbDroneProcessIdFromEnviroment, _configFileProvider.Guid)
};
_processProvider.Start(startInfo);
}
} }
} }

View File

@ -35,6 +35,7 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL" />
<Reference Include="NLog"> <Reference Include="NLog">
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath> <HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference> </Reference>

View File

@ -4,6 +4,7 @@
using NLog; using NLog;
using NLog.Config; using NLog.Config;
using NLog.Targets; using NLog.Targets;
using Ninject;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Update.Providers; using NzbDrone.Update.Providers;
@ -13,6 +14,7 @@ public class Program
{ {
private readonly UpdateProvider _updateProvider; private readonly UpdateProvider _updateProvider;
private readonly ProcessProvider _processProvider; private readonly ProcessProvider _processProvider;
private static StandardKernel _kernel;
private static readonly Logger logger = LogManager.GetCurrentClassLogger(); private static readonly Logger logger = LogManager.GetCurrentClassLogger();
@ -29,18 +31,10 @@ public static void Main(string[] args)
Console.WriteLine("Starting NzbDrone Update Client"); Console.WriteLine("Starting NzbDrone Update Client");
InitLoggers(); InitLoggers();
logger.Info("Initializing update application"); logger.Info("Initializing update application");
var enviromentProvider = new EnviromentProvider(); _kernel = new StandardKernel();
var processProvider = new ProcessProvider(); _kernel.Get<Program>().Start(args);
var serviceProvider = new ServiceProvider();
var diskProvider = new DiskProvider();
var iisProvider = new IISProvider();
var updateProvider = new UpdateProvider(diskProvider, serviceProvider, processProvider, enviromentProvider, iisProvider);
new Program(updateProvider, processProvider).Start(args);
} }
catch (Exception e) catch (Exception e)
{ {
@ -55,8 +49,8 @@ private static void TransferUpdateLogs()
{ {
try try
{ {
var enviromentProvider = new EnviromentProvider(); var enviromentProvider = _kernel.Get<EnviromentProvider>();
var diskProvider = new DiskProvider(); var diskProvider = _kernel.Get<DiskProvider>();
logger.Info("Copying log tiles to application directory."); logger.Info("Copying log tiles to application directory.");
diskProvider.CopyDirectory(enviromentProvider.GetSandboxLogFolder(), enviromentProvider.GetUpdateLogFolder()); diskProvider.CopyDirectory(enviromentProvider.GetSandboxLogFolder(), enviromentProvider.GetUpdateLogFolder());
} }
@ -70,18 +64,19 @@ private static void InitLoggers()
{ {
LogConfiguration.RegisterConsoleLogger(LogLevel.Trace); LogConfiguration.RegisterConsoleLogger(LogLevel.Trace);
LogConfiguration.RegisterUdpLogger(); LogConfiguration.RegisterUdpLogger();
LogConfiguration.RegisterExceptioneer();
var lastUpgradeLog = new FileTarget(); var lastUpgradeLog = new FileTarget();
lastUpgradeLog.AutoFlush = true; lastUpgradeLog.AutoFlush = true;
lastUpgradeLog.ConcurrentWrites = false; lastUpgradeLog.ConcurrentWrites = false;
lastUpgradeLog.FileName = Path.Combine(PathExtentions.UPDATE_LOG_FOLDER_NAME, DateTime.Now.ToString("yyyy.MM.dd-H-mm") + ".txt"); lastUpgradeLog.FileName = Path.Combine(PathExtentions.UPDATE_LOG_FOLDER_NAME, DateTime.Now.ToString("yyyy.MM.dd-H-mm") + ".txt");
lastUpgradeLog.KeepFileOpen = false; lastUpgradeLog.KeepFileOpen = false;
lastUpgradeLog.Layout = "${longdate} - ${logger}: ${message} ${exception}"; lastUpgradeLog.Layout = "${longdate} - ${logger}: ${message} ${exception:format=ToString}";
LogManager.Configuration.AddTarget(lastUpgradeLog.GetType().Name, lastUpgradeLog); LogManager.Configuration.AddTarget(lastUpgradeLog.GetType().Name, lastUpgradeLog);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, lastUpgradeLog)); LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, lastUpgradeLog));
LogConfiguration.RegisterExceptioneer();
LogConfiguration.Reload(); LogConfiguration.Reload();
} }

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Config>
<Port>8989</Port>
<AuthenticationType>0</AuthenticationType>
<LaunchBrowser>true</LaunchBrowser>
</Config>