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