aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Updates/InstallationManager.cs
diff options
context:
space:
mode:
authorBond-009 <bond.009@outlook.com>2019-12-06 12:06:13 +0100
committerBond-009 <bond.009@outlook.com>2019-12-06 12:06:13 +0100
commita2c35e6dba02f068a3f06e5a4e4964e6539069d1 (patch)
treee75984ab85fedceaf96150ad9d5241cf88230a60 /Emby.Server.Implementations/Updates/InstallationManager.cs
parent94edb5b9f98cf3b06144255eccc988712332f0a8 (diff)
parent935525e77a18061195dea786be71d38fffe82a10 (diff)
Merge remote-tracking branch 'upstream/master' into random
Diffstat (limited to 'Emby.Server.Implementations/Updates/InstallationManager.cs')
-rw-r--r--Emby.Server.Implementations/Updates/InstallationManager.cs308
1 files changed, 105 insertions, 203 deletions
diff --git a/Emby.Server.Implementations/Updates/InstallationManager.cs b/Emby.Server.Implementations/Updates/InstallationManager.cs
index 0c0c77cda..09a5a0dca 100644
--- a/Emby.Server.Implementations/Updates/InstallationManager.cs
+++ b/Emby.Server.Implementations/Updates/InstallationManager.cs
@@ -19,52 +19,18 @@ using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Updates;
using Microsoft.Extensions.Logging;
-using static MediaBrowser.Common.HexHelper;
namespace Emby.Server.Implementations.Updates
{
/// <summary>
- /// Manages all install, uninstall and update operations (both plugins and system)
+ /// Manages all install, uninstall and update operations (both plugins and system).
/// </summary>
public class InstallationManager : IInstallationManager
{
- public event EventHandler<InstallationEventArgs> PackageInstalling;
- public event EventHandler<InstallationEventArgs> PackageInstallationCompleted;
- public event EventHandler<InstallationFailedEventArgs> PackageInstallationFailed;
- public event EventHandler<InstallationEventArgs> PackageInstallationCancelled;
-
- /// <summary>
- /// The current installations
- /// </summary>
- private List<(InstallationInfo info, CancellationTokenSource token)> _currentInstallations { get; set; }
-
/// <summary>
- /// The completed installations
- /// </summary>
- private ConcurrentBag<InstallationInfo> _completedInstallationsInternal;
-
- public IEnumerable<InstallationInfo> CompletedInstallations => _completedInstallationsInternal;
-
- /// <summary>
- /// Occurs when [plugin uninstalled].
- /// </summary>
- public event EventHandler<GenericEventArgs<IPlugin>> PluginUninstalled;
-
- /// <summary>
- /// Occurs when [plugin updated].
- /// </summary>
- public event EventHandler<GenericEventArgs<(IPlugin, PackageVersionInfo)>> PluginUpdated;
-
- /// <summary>
- /// Occurs when [plugin updated].
- /// </summary>
- public event EventHandler<GenericEventArgs<PackageVersionInfo>> PluginInstalled;
-
- /// <summary>
- /// The _logger
+ /// The _logger.
/// </summary>
private readonly ILogger _logger;
-
private readonly IApplicationPaths _appPaths;
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
@@ -79,6 +45,18 @@ namespace Emby.Server.Implementations.Updates
private readonly IZipClient _zipClient;
+ private readonly object _currentInstallationsLock = new object();
+
+ /// <summary>
+ /// The current installations.
+ /// </summary>
+ private readonly List<(InstallationInfo info, CancellationTokenSource token)> _currentInstallations;
+
+ /// <summary>
+ /// The completed installations.
+ /// </summary>
+ private readonly ConcurrentBag<InstallationInfo> _completedInstallationsInternal;
+
public InstallationManager(
ILogger<InstallationManager> logger,
IApplicationHost appHost,
@@ -107,26 +85,32 @@ namespace Emby.Server.Implementations.Updates
_zipClient = zipClient;
}
- /// <summary>
- /// Gets all available packages.
- /// </summary>
- /// <returns>Task{List{PackageInfo}}.</returns>
- public async Task<List<PackageInfo>> GetAvailablePackages(
- CancellationToken cancellationToken,
- bool withRegistration = true,
- string packageType = null,
- Version applicationVersion = null)
- {
- var packages = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
- return FilterPackages(packages, packageType, applicationVersion);
- }
+ /// <inheritdoc />
+ public event EventHandler<InstallationEventArgs> PackageInstalling;
- /// <summary>
- /// Gets all available packages.
- /// </summary>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task{List{PackageInfo}}.</returns>
- public async Task<List<PackageInfo>> GetAvailablePackagesWithoutRegistrationInfo(CancellationToken cancellationToken)
+ /// <inheritdoc />
+ public event EventHandler<InstallationEventArgs> PackageInstallationCompleted;
+
+ /// <inheritdoc />
+ public event EventHandler<InstallationFailedEventArgs> PackageInstallationFailed;
+
+ /// <inheritdoc />
+ public event EventHandler<InstallationEventArgs> PackageInstallationCancelled;
+
+ /// <inheritdoc />
+ public event EventHandler<GenericEventArgs<IPlugin>> PluginUninstalled;
+
+ /// <inheritdoc />
+ public event EventHandler<GenericEventArgs<(IPlugin, PackageVersionInfo)>> PluginUpdated;
+
+ /// <inheritdoc />
+ public event EventHandler<GenericEventArgs<PackageVersionInfo>> PluginInstalled;
+
+ /// <inheritdoc />
+ public IEnumerable<InstallationInfo> CompletedInstallations => _completedInstallationsInternal;
+
+ /// <inheritdoc />
+ public async Task<IReadOnlyList<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken = default)
{
using (var response = await _httpClient.SendAsync(
new HttpRequestOptions
@@ -134,178 +118,95 @@ namespace Emby.Server.Implementations.Updates
Url = "https://repo.jellyfin.org/releases/plugin/manifest.json",
CancellationToken = cancellationToken,
CacheMode = CacheMode.Unconditional,
- CacheLength = GetCacheLength()
+ CacheLength = TimeSpan.FromMinutes(3)
},
HttpMethod.Get).ConfigureAwait(false))
using (Stream stream = response.Content)
{
- return FilterPackages(await _jsonSerializer.DeserializeFromStreamAsync<PackageInfo[]>(stream).ConfigureAwait(false));
+ return await _jsonSerializer.DeserializeFromStreamAsync<IReadOnlyList<PackageInfo>>(
+ stream).ConfigureAwait(false);
}
}
- private static TimeSpan GetCacheLength()
- {
- return TimeSpan.FromMinutes(3);
- }
-
- protected List<PackageInfo> FilterPackages(IEnumerable<PackageInfo> packages)
+ /// <inheritdoc />
+ public IEnumerable<PackageInfo> FilterPackages(
+ IEnumerable<PackageInfo> availablePackages,
+ string name = null,
+ Guid guid = default)
{
- var list = new List<PackageInfo>();
-
- foreach (var package in packages)
+ if (name != null)
{
- var versions = new List<PackageVersionInfo>();
- foreach (var version in package.versions)
- {
- if (string.IsNullOrEmpty(version.sourceUrl))
- {
- continue;
- }
-
- versions.Add(version);
- }
-
- package.versions = versions
- .OrderByDescending(x => x.Version)
- .ToArray();
-
- if (package.versions.Length == 0)
- {
- continue;
- }
-
- list.Add(package);
+ availablePackages = availablePackages.Where(x => x.name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
- // Remove packages with no versions
- return list;
- }
-
- protected List<PackageInfo> FilterPackages(IEnumerable<PackageInfo> packages, string packageType, Version applicationVersion)
- {
- var packagesList = FilterPackages(packages);
-
- var returnList = new List<PackageInfo>();
-
- var filterOnPackageType = !string.IsNullOrEmpty(packageType);
-
- foreach (var p in packagesList)
+ if (guid != Guid.Empty)
{
- if (filterOnPackageType && !string.Equals(p.type, packageType, StringComparison.OrdinalIgnoreCase))
- {
- continue;
- }
-
- // If an app version was supplied, filter the versions for each package to only include supported versions
- if (applicationVersion != null)
- {
- p.versions = p.versions.Where(v => IsPackageVersionUpToDate(v, applicationVersion)).ToArray();
- }
-
- if (p.versions.Length == 0)
- {
- continue;
- }
-
- returnList.Add(p);
+ var strGuid = guid.ToString("N", CultureInfo.InvariantCulture);
+ availablePackages = availablePackages.Where(x => x.guid.Equals(strGuid, StringComparison.OrdinalIgnoreCase));
}
- return returnList;
+ return availablePackages;
}
- /// <summary>
- /// Determines whether [is package version up to date] [the specified package version info].
- /// </summary>
- /// <param name="packageVersionInfo">The package version info.</param>
- /// <param name="currentServerVersion">The current server version.</param>
- /// <returns><c>true</c> if [is package version up to date] [the specified package version info]; otherwise, <c>false</c>.</returns>
- private static bool IsPackageVersionUpToDate(PackageVersionInfo packageVersionInfo, Version currentServerVersion)
+ /// <inheritdoc />
+ public IEnumerable<PackageVersionInfo> GetCompatibleVersions(
+ IEnumerable<PackageVersionInfo> availableVersions,
+ Version minVersion = null,
+ PackageVersionClass classification = PackageVersionClass.Release)
{
- if (string.IsNullOrEmpty(packageVersionInfo.requiredVersionStr))
+ var appVer = _applicationHost.ApplicationVersion;
+ availableVersions = availableVersions
+ .Where(x => x.classification == classification
+ && Version.Parse(x.requiredVersionStr) <= appVer);
+
+ if (minVersion != null)
{
- return true;
+ availableVersions = availableVersions.Where(x => x.Version >= minVersion);
}
- return Version.TryParse(packageVersionInfo.requiredVersionStr, out var requiredVersion) && currentServerVersion >= requiredVersion;
+ return availableVersions.OrderByDescending(x => x.Version);
}
- /// <summary>
- /// Gets the package.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <param name="guid">The assembly guid</param>
- /// <param name="classification">The classification.</param>
- /// <param name="version">The version.</param>
- /// <returns>Task{PackageVersionInfo}.</returns>
- public async Task<PackageVersionInfo> GetPackage(string name, string guid, PackageVersionClass classification, Version version)
+ /// <inheritdoc />
+ public IEnumerable<PackageVersionInfo> GetCompatibleVersions(
+ IEnumerable<PackageInfo> availablePackages,
+ string name = null,
+ Guid guid = default,
+ Version minVersion = null,
+ PackageVersionClass classification = PackageVersionClass.Release)
{
- var packages = await GetAvailablePackages(CancellationToken.None, false).ConfigureAwait(false);
-
- var package = packages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
- ?? packages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
+ var package = FilterPackages(availablePackages, name, guid).FirstOrDefault();
+ // Package not found.
if (package == null)
{
- return null;
+ return Enumerable.Empty<PackageVersionInfo>();
}
- return package.versions.FirstOrDefault(v => v.Version == version && v.classification == classification);
- }
-
- /// <summary>
- /// Gets the latest compatible version.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <param name="guid">The assembly guid if this is a plug-in</param>
- /// <param name="currentServerVersion">The current server version.</param>
- /// <param name="classification">The classification.</param>
- /// <returns>Task{PackageVersionInfo}.</returns>
- public async Task<PackageVersionInfo> GetLatestCompatibleVersion(string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
- {
- var packages = await GetAvailablePackages(CancellationToken.None, false).ConfigureAwait(false);
-
- return GetLatestCompatibleVersion(packages, name, guid, currentServerVersion, classification);
+ return GetCompatibleVersions(
+ package.versions,
+ minVersion,
+ classification);
}
- /// <summary>
- /// Gets the latest compatible version.
- /// </summary>
- /// <param name="availablePackages">The available packages.</param>
- /// <param name="name">The name.</param>
- /// <param name="currentServerVersion">The current server version.</param>
- /// <param name="classification">The classification.</param>
- /// <returns>PackageVersionInfo.</returns>
- public PackageVersionInfo GetLatestCompatibleVersion(IEnumerable<PackageInfo> availablePackages, string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
- {
- var package = availablePackages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
- ?? availablePackages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
-
- return package?.versions
- .OrderByDescending(x => x.Version)
- .FirstOrDefault(v => v.classification <= classification && IsPackageVersionUpToDate(v, currentServerVersion));
- }
-
- /// <summary>
- /// Gets the available plugin updates.
- /// </summary>
- /// <param name="applicationVersion">The current server version.</param>
- /// <param name="withAutoUpdateEnabled">if set to <c>true</c> [with auto update enabled].</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task{IEnumerable{PackageVersionInfo}}.</returns>
- public async Task<IEnumerable<PackageVersionInfo>> GetAvailablePluginUpdates(Version applicationVersion, bool withAutoUpdateEnabled, CancellationToken cancellationToken)
+ /// <inheritdoc />
+ public async IAsyncEnumerable<PackageVersionInfo> GetAvailablePluginUpdates(CancellationToken cancellationToken = default)
{
- var catalog = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
+ var catalog = await GetAvailablePackages(cancellationToken).ConfigureAwait(false);
var systemUpdateLevel = _applicationHost.SystemUpdateLevel;
// Figure out what needs to be installed
- return _applicationHost.Plugins.Select(p =>
+ foreach (var plugin in _applicationHost.Plugins)
{
- var latestPluginInfo = GetLatestCompatibleVersion(catalog, p.Name, p.Id.ToString(), applicationVersion, systemUpdateLevel);
-
- return latestPluginInfo != null && latestPluginInfo.Version > p.Version ? latestPluginInfo : null;
- }).Where(i => i != null)
- .Where(p => !string.IsNullOrEmpty(p.sourceUrl) && !CompletedInstallations.Any(i => string.Equals(i.AssemblyGuid, p.guid, StringComparison.OrdinalIgnoreCase)));
+ var compatibleversions = GetCompatibleVersions(catalog, plugin.Name, plugin.Id, plugin.Version, systemUpdateLevel);
+ var version = compatibleversions.FirstOrDefault(y => y.Version > plugin.Version);
+ if (version != null
+ && !CompletedInstallations.Any(x => string.Equals(x.AssemblyGuid, version.guid, StringComparison.OrdinalIgnoreCase)))
+ {
+ yield return version;
+ }
+ }
}
/// <inheritdoc />
@@ -330,7 +231,7 @@ namespace Emby.Server.Implementations.Updates
var tuple = (installationInfo, innerCancellationTokenSource);
// Add it to the in-progress list
- lock (_currentInstallations)
+ lock (_currentInstallationsLock)
{
_currentInstallations.Add(tuple);
}
@@ -349,7 +250,7 @@ namespace Emby.Server.Implementations.Updates
{
await InstallPackageInternal(package, linkedToken).ConfigureAwait(false);
- lock (_currentInstallations)
+ lock (_currentInstallationsLock)
{
_currentInstallations.Remove(tuple);
}
@@ -360,7 +261,7 @@ namespace Emby.Server.Implementations.Updates
}
catch (OperationCanceledException)
{
- lock (_currentInstallations)
+ lock (_currentInstallationsLock)
{
_currentInstallations.Remove(tuple);
}
@@ -375,7 +276,7 @@ namespace Emby.Server.Implementations.Updates
{
_logger.LogError(ex, "Package installation failed");
- lock (_currentInstallations)
+ lock (_currentInstallationsLock)
{
_currentInstallations.Remove(tuple);
}
@@ -391,7 +292,7 @@ namespace Emby.Server.Implementations.Updates
finally
{
// Dispose the progress object and remove the installation from the in-progress list
- tuple.Item2.Dispose();
+ tuple.innerCancellationTokenSource.Dispose();
}
}
@@ -439,7 +340,7 @@ namespace Emby.Server.Implementations.Updates
// Always override the passed-in target (which is a file) and figure it out again
string targetDir = Path.Combine(_appPaths.PluginsPath, package.name);
-// CA5351: Do Not Use Broken Cryptographic Algorithms
+ // CA5351: Do Not Use Broken Cryptographic Algorithms
#pragma warning disable CA5351
using (var res = await _httpClient.SendAsync(
new HttpRequestOptions
@@ -455,7 +356,7 @@ namespace Emby.Server.Implementations.Updates
{
cancellationToken.ThrowIfCancellationRequested();
- var hash = ToHexString(md5.ComputeHash(stream));
+ var hash = Hex.Encode(md5.ComputeHash(stream));
if (!string.Equals(package.checksum, hash, StringComparison.OrdinalIgnoreCase))
{
_logger.LogError(
@@ -535,20 +436,21 @@ namespace Emby.Server.Implementations.Updates
/// <inheritdoc/>
public bool CancelInstallation(Guid id)
{
- lock (_currentInstallations)
+ lock (_currentInstallationsLock)
{
- var install = _currentInstallations.Find(x => x.Item1.Id == id);
+ var install = _currentInstallations.Find(x => x.info.Id == id);
if (install == default((InstallationInfo, CancellationTokenSource)))
{
return false;
}
- install.Item2.Cancel();
+ install.token.Cancel();
_currentInstallations.Remove(install);
return true;
}
}
+ /// <inheritdoc />
public void Dispose()
{
Dispose(true);
@@ -563,11 +465,11 @@ namespace Emby.Server.Implementations.Updates
{
if (dispose)
{
- lock (_currentInstallations)
+ lock (_currentInstallationsLock)
{
foreach (var tuple in _currentInstallations)
{
- tuple.Item2.Dispose();
+ tuple.token.Dispose();
}
_currentInstallations.Clear();