2013-05-21 07:03:05 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
2013-06-28 03:04:52 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-21 07:03:05 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Update.UpdateEngine
|
|
|
|
{
|
|
|
|
public interface IInstallUpdateService
|
|
|
|
{
|
|
|
|
void Start(string installationFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class InstallUpdateService : IInstallUpdateService
|
|
|
|
{
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IDetectApplicationType _detectApplicationType;
|
|
|
|
private readonly ITerminateNzbDrone _terminateNzbDrone;
|
2013-07-05 07:43:28 +03:00
|
|
|
private readonly IAppFolderInfo _appFolderInfo;
|
2013-05-21 07:03:05 +03:00
|
|
|
private readonly IBackupAndRestore _backupAndRestore;
|
|
|
|
private readonly IStartNzbDrone _startNzbDrone;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public InstallUpdateService(IDiskProvider diskProvider, IDetectApplicationType detectApplicationType, ITerminateNzbDrone terminateNzbDrone,
|
2013-07-05 07:43:28 +03:00
|
|
|
IAppFolderInfo appFolderInfo, IBackupAndRestore backupAndRestore, IStartNzbDrone startNzbDrone, Logger logger)
|
2013-05-21 07:03:05 +03:00
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_detectApplicationType = detectApplicationType;
|
|
|
|
_terminateNzbDrone = terminateNzbDrone;
|
2013-07-05 07:43:28 +03:00
|
|
|
_appFolderInfo = appFolderInfo;
|
2013-05-21 07:03:05 +03:00
|
|
|
_backupAndRestore = backupAndRestore;
|
|
|
|
_startNzbDrone = startNzbDrone;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Verify(string targetFolder)
|
|
|
|
{
|
|
|
|
_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);
|
|
|
|
|
|
|
|
_logger.Info("Verifying Update Folder");
|
2013-07-05 07:43:28 +03:00
|
|
|
if (!_diskProvider.FolderExists(_appFolderInfo.GetUpdatePackageFolder()))
|
|
|
|
throw new DirectoryNotFoundException("Update folder doesn't exist " + _appFolderInfo.GetUpdatePackageFolder());
|
2013-05-21 07:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(string installationFolder)
|
|
|
|
{
|
|
|
|
Verify(installationFolder);
|
|
|
|
|
|
|
|
var appType = _detectApplicationType.GetAppType();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_terminateNzbDrone.Terminate();
|
|
|
|
|
|
|
|
_backupAndRestore.BackUp(installationFolder);
|
|
|
|
|
|
|
|
_logger.Info("Moving update package to target");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2013-09-08 20:13:46 +03:00
|
|
|
_diskProvider.EmptyFolder(installationFolder);
|
2013-07-05 07:43:28 +03:00
|
|
|
_diskProvider.CopyFolder(_appFolderInfo.GetUpdatePackageFolder(), installationFolder);
|
2013-05-21 07:03:05 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_backupAndRestore.Restore(installationFolder);
|
|
|
|
_logger.FatalException("Failed to copy upgrade package to target folder.", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_startNzbDrone.Start(appType, installationFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|