2013-08-04 00:15:31 +03:00
|
|
|
using System;
|
2013-06-28 03:04:52 +03:00
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-09-21 02:56:17 +03:00
|
|
|
using NzbDrone.Common.Processes;
|
2013-09-14 09:36:07 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-06-28 03:04:52 +03:00
|
|
|
using NzbDrone.Core.Update.Commands;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Core.Instrumentation;
|
2013-06-28 03:04:52 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Update
|
|
|
|
{
|
2013-10-05 11:15:48 +03:00
|
|
|
public interface IInstallUpdates
|
|
|
|
{
|
|
|
|
void InstallUpdate(UpdatePackage updatePackage);
|
|
|
|
}
|
|
|
|
|
2013-10-19 03:48:14 +03:00
|
|
|
public class InstallUpdateService : IInstallUpdates, IExecute<ApplicationUpdateCommand>, IExecute<InstallUpdateCommand>
|
2013-06-28 03:04:52 +03:00
|
|
|
{
|
|
|
|
private readonly ICheckUpdateService _checkUpdateService;
|
|
|
|
private readonly Logger _logger;
|
2013-07-05 07:43:28 +03:00
|
|
|
private readonly IAppFolderInfo _appFolderInfo;
|
2013-06-28 03:04:52 +03:00
|
|
|
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-07-19 02:36:49 +03:00
|
|
|
private readonly IArchiveService _archiveService;
|
2013-06-28 03:04:52 +03:00
|
|
|
private readonly IProcessProvider _processProvider;
|
|
|
|
|
|
|
|
|
2013-07-05 07:43:28 +03:00
|
|
|
public InstallUpdateService(ICheckUpdateService checkUpdateService, IAppFolderInfo appFolderInfo,
|
2013-06-28 03:04:52 +03:00
|
|
|
IDiskProvider diskProvider, IHttpProvider httpProvider,
|
2013-07-19 02:36:49 +03:00
|
|
|
IArchiveService archiveService, IProcessProvider processProvider, Logger logger)
|
2013-06-28 03:04:52 +03:00
|
|
|
{
|
|
|
|
_checkUpdateService = checkUpdateService;
|
2013-07-05 07:43:28 +03:00
|
|
|
_appFolderInfo = appFolderInfo;
|
2013-06-28 03:04:52 +03:00
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_httpProvider = httpProvider;
|
2013-07-19 02:36:49 +03:00
|
|
|
_archiveService = archiveService;
|
2013-06-28 03:04:52 +03:00
|
|
|
_processProvider = processProvider;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2013-10-05 11:15:48 +03:00
|
|
|
public void InstallUpdate(UpdatePackage updatePackage)
|
2013-06-28 03:04:52 +03:00
|
|
|
{
|
2013-08-04 00:15:31 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var updateSandboxFolder = _appFolderInfo.GetUpdateSandboxFolder();
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-08-04 00:15:31 +03:00
|
|
|
var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName);
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-08-04 00:15:31 +03:00
|
|
|
if (_diskProvider.FolderExists(updateSandboxFolder))
|
|
|
|
{
|
|
|
|
_logger.Info("Deleting old update files");
|
|
|
|
_diskProvider.DeleteFolder(updateSandboxFolder, true);
|
|
|
|
}
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-09-19 08:33:37 +03:00
|
|
|
_logger.ProgressInfo("Downloading update {0} [{1}]", updatePackage.Version, updatePackage.Branch);
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.Debug("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination);
|
2013-08-04 00:15:31 +03:00
|
|
|
_httpProvider.DownloadFile(updatePackage.Url, packageDestination);
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.ProgressInfo("Extracting Update package");
|
2013-08-04 00:15:31 +03:00
|
|
|
_archiveService.Extract(packageDestination, updateSandboxFolder);
|
|
|
|
_logger.Info("Update package extracted successfully");
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-08-04 00:15:31 +03:00
|
|
|
_logger.Info("Preparing client");
|
|
|
|
_diskProvider.MoveFolder(_appFolderInfo.GetUpdateClientFolder(),
|
|
|
|
updateSandboxFolder);
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-08-07 08:54:50 +03:00
|
|
|
_logger.Info("Starting update client {0}", _appFolderInfo.GetUpdateClientExePath());
|
2013-06-28 03:04:52 +03:00
|
|
|
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.ProgressInfo("NzbDrone will restart shortly.");
|
|
|
|
|
2013-08-19 02:43:13 +03:00
|
|
|
_processProvider.Start(_appFolderInfo.GetUpdateClientExePath(), _processProvider.GetCurrentProcess().Id.ToString());
|
2013-08-04 00:15:31 +03:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Update process failed", ex);
|
|
|
|
}
|
2013-06-28 03:04:52 +03:00
|
|
|
}
|
2013-10-05 11:15:48 +03:00
|
|
|
|
|
|
|
public void Execute(ApplicationUpdateCommand message)
|
|
|
|
{
|
|
|
|
_logger.ProgressDebug("Checking for updates");
|
|
|
|
var latestAvailable = _checkUpdateService.AvailableUpdate();
|
|
|
|
|
|
|
|
if (latestAvailable != null)
|
|
|
|
{
|
|
|
|
InstallUpdate(latestAvailable);
|
|
|
|
}
|
|
|
|
}
|
2013-10-19 03:48:14 +03:00
|
|
|
|
|
|
|
public void Execute(InstallUpdateCommand message)
|
|
|
|
{
|
|
|
|
InstallUpdate(message.UpdatePackage);
|
|
|
|
}
|
2013-06-28 03:04:52 +03:00
|
|
|
}
|
2013-08-07 08:54:50 +03:00
|
|
|
}
|