2011-10-21 05:04:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-11-14 00:57:03 +00:00
|
|
|
|
using System.Diagnostics;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using NLog;
|
|
|
|
|
using Ninject;
|
2011-10-29 04:54:33 +00:00
|
|
|
|
using NzbDrone.Common;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-11-13 04:07:06 +00:00
|
|
|
|
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
class UpdateProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpProvider _httpProvider;
|
|
|
|
|
private readonly ConfigProvider _configProvider;
|
2011-11-14 00:57:03 +00:00
|
|
|
|
private readonly ConfigFileProvider _configFileProvider;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-11-13 04:07:06 +00:00
|
|
|
|
private readonly ArchiveProvider _archiveProvider;
|
2011-11-14 00:57:03 +00:00
|
|
|
|
private readonly ProcessProvider _processProvider;
|
2011-11-14 01:27:11 +00:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2011-11-13 07:27:16 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
private static readonly Regex parseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
2011-11-03 05:04:14 +00:00
|
|
|
|
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
|
|
|
|
[Inject]
|
2011-11-14 00:57:03 +00:00
|
|
|
|
public UpdateProvider(HttpProvider httpProvider, ConfigProvider configProvider, ConfigFileProvider configFileProvider,
|
2011-11-14 01:27:11 +00:00
|
|
|
|
EnviromentProvider enviromentProvider, ArchiveProvider archiveProvider, ProcessProvider processProvider, DiskProvider diskProvider)
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
|
_configProvider = configProvider;
|
2011-11-14 00:57:03 +00:00
|
|
|
|
_configFileProvider = configFileProvider;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
_enviromentProvider = enviromentProvider;
|
2011-11-13 04:07:06 +00:00
|
|
|
|
_archiveProvider = archiveProvider;
|
2011-11-14 00:57:03 +00:00
|
|
|
|
_processProvider = processProvider;
|
2011-11-14 01:27:11 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2011-10-21 05:04:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UpdateProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<UpdatePackage> GetAvailablePackages()
|
|
|
|
|
{
|
|
|
|
|
var updateList = new List<UpdatePackage>();
|
|
|
|
|
var rawUpdateList = _httpProvider.DownloadString(_configProvider.UpdateUrl);
|
2011-11-13 07:27:16 +00:00
|
|
|
|
var matches = parseRegex.Matches(rawUpdateList);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
|
|
|
|
foreach (Match match in matches)
|
|
|
|
|
{
|
|
|
|
|
var updatePackage = new UpdatePackage();
|
|
|
|
|
updatePackage.FileName = match.Groups["filename"].Value;
|
|
|
|
|
updatePackage.Url = _configProvider.UpdateUrl + updatePackage.FileName;
|
|
|
|
|
updatePackage.Version = new Version(match.Groups["version"].Value);
|
|
|
|
|
updateList.Add(updatePackage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual UpdatePackage GetAvilableUpdate()
|
|
|
|
|
{
|
|
|
|
|
var latestAvailable = GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (latestAvailable != null && latestAvailable.Version > _enviromentProvider.Version)
|
|
|
|
|
{
|
2011-11-13 07:27:16 +00:00
|
|
|
|
logger.Debug("An update is available ({0}) => ({1})", _enviromentProvider.Version, latestAvailable.Version);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
return latestAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
logger.Trace("No updates available");
|
2011-10-21 05:04:26 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 04:07:06 +00:00
|
|
|
|
public virtual void StartUpgrade(UpdatePackage updatePackage)
|
2011-10-21 05:04:26 +00:00
|
|
|
|
{
|
2011-11-13 07:27:16 +00:00
|
|
|
|
var packageDestination = Path.Combine(_enviromentProvider.GetUpdateSandboxFolder(), updatePackage.FileName);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
_httpProvider.DownloadFile(updatePackage.Url, packageDestination);
|
2011-11-13 07:27:16 +00:00
|
|
|
|
logger.Info("Download completed for update package from [{0}]", updatePackage.FileName);
|
2011-10-21 05:04:26 +00:00
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
logger.Info("Extracting Update package");
|
|
|
|
|
_archiveProvider.ExtractArchive(packageDestination, _enviromentProvider.GetUpdateSandboxFolder());
|
|
|
|
|
logger.Info("Update package extracted successfully");
|
2011-11-14 00:57:03 +00:00
|
|
|
|
|
2011-11-14 01:27:11 +00:00
|
|
|
|
logger.Info("Preparing client");
|
|
|
|
|
_diskProvider.CopyDirectory(_enviromentProvider.GetUpdateClientFolder(), _enviromentProvider.GetUpdateSandboxFolder());
|
|
|
|
|
|
2011-11-14 00:57:03 +00:00
|
|
|
|
|
2011-11-14 01:27:11 +00:00
|
|
|
|
logger.Info("Starting update client");
|
2011-11-14 00:57:03 +00:00
|
|
|
|
var startInfo = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = _enviromentProvider.GetUpdateClientExePath(),
|
|
|
|
|
Arguments = string.Format("/{0} /{1}", _enviromentProvider.NzbDroneProcessIdFromEnviroment, _configFileProvider.Guid)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_processProvider.Start(startInfo);
|
|
|
|
|
|
2011-10-21 05:04:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|