2013-08-03 23:15:31 +02:00
|
|
|
|
using System;
|
2013-06-28 02:04:52 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
|
using NzbDrone.Core.Update.Commands;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Update
|
|
|
|
|
{
|
|
|
|
|
public class InstallUpdateService : IExecute<ApplicationUpdateCommand>
|
|
|
|
|
{
|
|
|
|
|
private readonly ICheckUpdateService _checkUpdateService;
|
|
|
|
|
private readonly Logger _logger;
|
2013-07-05 06:43:28 +02:00
|
|
|
|
private readonly IAppFolderInfo _appFolderInfo;
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-07-19 01:36:49 +02:00
|
|
|
|
private readonly IArchiveService _archiveService;
|
2013-06-28 02:04:52 +02:00
|
|
|
|
private readonly IProcessProvider _processProvider;
|
|
|
|
|
|
|
|
|
|
|
2013-07-05 06:43:28 +02:00
|
|
|
|
public InstallUpdateService(ICheckUpdateService checkUpdateService, IAppFolderInfo appFolderInfo,
|
2013-06-28 02:04:52 +02:00
|
|
|
|
IDiskProvider diskProvider, IHttpProvider httpProvider,
|
2013-07-19 01:36:49 +02:00
|
|
|
|
IArchiveService archiveService, IProcessProvider processProvider, Logger logger)
|
2013-06-28 02:04:52 +02:00
|
|
|
|
{
|
|
|
|
|
_checkUpdateService = checkUpdateService;
|
2013-07-05 06:43:28 +02:00
|
|
|
|
_appFolderInfo = appFolderInfo;
|
2013-06-28 02:04:52 +02:00
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_httpProvider = httpProvider;
|
2013-07-19 01:36:49 +02:00
|
|
|
|
_archiveService = archiveService;
|
2013-06-28 02:04:52 +02:00
|
|
|
|
_processProvider = processProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-30 04:12:16 +02:00
|
|
|
|
|
2013-06-28 02:04:52 +02:00
|
|
|
|
public void Execute(ApplicationUpdateCommand message)
|
|
|
|
|
{
|
|
|
|
|
var latestAvailable = _checkUpdateService.AvailableUpdate();
|
|
|
|
|
|
|
|
|
|
if (latestAvailable != null)
|
|
|
|
|
{
|
|
|
|
|
InstallUpdate(latestAvailable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstallUpdate(UpdatePackage updatePackage)
|
|
|
|
|
{
|
2013-08-03 23:15:31 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var updateSandboxFolder = _appFolderInfo.GetUpdateSandboxFolder();
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName);
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
if (_diskProvider.FolderExists(updateSandboxFolder))
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Deleting old update files");
|
|
|
|
|
_diskProvider.DeleteFolder(updateSandboxFolder, true);
|
|
|
|
|
}
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
_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);
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
_logger.Info("Extracting Update package");
|
|
|
|
|
_archiveService.Extract(packageDestination, updateSandboxFolder);
|
|
|
|
|
_logger.Info("Update package extracted successfully");
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
_logger.Info("Preparing client");
|
|
|
|
|
_diskProvider.MoveFolder(_appFolderInfo.GetUpdateClientFolder(),
|
|
|
|
|
updateSandboxFolder);
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-07 07:54:50 +02:00
|
|
|
|
_logger.Info("Starting update client {0}", _appFolderInfo.GetUpdateClientExePath());
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-14 07:20:24 +02:00
|
|
|
|
var process = _processProvider.Start(_appFolderInfo.GetUpdateClientExePath(), _processProvider.GetCurrentProcess().Id.ToString());
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
_processProvider.WaitForExit(process);
|
2013-06-28 02:04:52 +02:00
|
|
|
|
|
2013-08-03 23:15:31 +02:00
|
|
|
|
_logger.Error("Update process failed");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Update process failed", ex);
|
|
|
|
|
}
|
2013-06-28 02:04:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-07 07:54:50 +02:00
|
|
|
|
}
|