2019-01-06 15:50:43 -05:00
|
|
|
using System;
|
2016-10-29 01:40:15 -04:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2021-12-18 11:49:33 -05:00
|
|
|
using System.IO.Compression;
|
2016-10-29 01:40:15 -04:00
|
|
|
using System.Linq;
|
2019-06-14 12:38:14 -04:00
|
|
|
using System.Net.Http;
|
2020-11-22 14:35:45 -05:00
|
|
|
using System.Net.Http.Json;
|
2019-07-29 17:47:25 -04:00
|
|
|
using System.Security.Cryptography;
|
2020-11-22 14:35:45 -05:00
|
|
|
using System.Text.Json;
|
2016-10-29 01:40:15 -04:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-09-05 18:58:16 -04:00
|
|
|
using Jellyfin.Data.Events;
|
2021-06-19 12:02:33 -04:00
|
|
|
using Jellyfin.Extensions.Json;
|
2021-10-02 13:45:11 -04:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-02-20 20:33:05 -05:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Common.Plugins;
|
2013-02-26 15:21:43 -05:00
|
|
|
using MediaBrowser.Common.Updates;
|
2020-09-30 19:37:30 -04:00
|
|
|
using MediaBrowser.Controller;
|
2019-01-13 14:23:38 -05:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2020-09-05 18:58:16 -04:00
|
|
|
using MediaBrowser.Controller.Events;
|
|
|
|
using MediaBrowser.Controller.Events.Updates;
|
2021-04-06 15:59:47 -04:00
|
|
|
using MediaBrowser.Model.Plugins;
|
2013-02-20 20:33:05 -05:00
|
|
|
using MediaBrowser.Model.Updates;
|
2019-01-13 14:23:38 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2016-11-02 16:53:50 -04:00
|
|
|
namespace Emby.Server.Implementations.Updates
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-25 11:58:39 -05:00
|
|
|
/// Manages all install, uninstall, and update operations for the system and individual plugins.
|
2013-02-20 20:33:05 -05:00
|
|
|
/// </summary>
|
2013-03-04 23:25:27 -05:00
|
|
|
public class InstallationManager : IInstallationManager
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-25 11:58:39 -05:00
|
|
|
/// The logger.
|
2013-02-21 16:39:53 -05:00
|
|
|
/// </summary>
|
2020-06-05 20:15:56 -04:00
|
|
|
private readonly ILogger<InstallationManager> _logger;
|
2013-08-07 15:15:55 -04:00
|
|
|
private readonly IApplicationPaths _appPaths;
|
2020-09-05 18:58:16 -04:00
|
|
|
private readonly IEventManager _eventManager;
|
2020-08-31 13:30:05 -04:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2018-09-12 13:26:21 -04:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2020-11-22 14:35:45 -05:00
|
|
|
private readonly JsonSerializerOptions _jsonSerializerOptions;
|
2020-12-06 18:48:54 -05:00
|
|
|
private readonly IPluginManager _pluginManager;
|
2013-02-24 19:13:45 -05:00
|
|
|
|
2013-02-25 22:43:04 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the application host.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The application host.</value>
|
2020-09-30 19:37:30 -04:00
|
|
|
private readonly IServerApplicationHost _applicationHost;
|
2019-09-23 17:10:51 -04:00
|
|
|
private readonly object _currentInstallationsLock = new object();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The current installations.
|
|
|
|
/// </summary>
|
2021-12-24 16:18:24 -05:00
|
|
|
private readonly List<(InstallationInfo Info, CancellationTokenSource Token)> _currentInstallations;
|
2019-09-23 17:10:51 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The completed installations.
|
|
|
|
/// </summary>
|
2019-11-07 04:55:02 -05:00
|
|
|
private readonly ConcurrentBag<InstallationInfo> _completedInstallationsInternal;
|
2019-09-23 17:10:51 -04:00
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="InstallationManager"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The <see cref="ILogger{InstallationManager}"/>.</param>
|
|
|
|
/// <param name="appHost">The <see cref="IServerApplicationHost"/>.</param>
|
|
|
|
/// <param name="appPaths">The <see cref="IApplicationPaths"/>.</param>
|
|
|
|
/// <param name="eventManager">The <see cref="IEventManager"/>.</param>
|
|
|
|
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
|
|
|
|
/// <param name="config">The <see cref="IServerConfigurationManager"/>.</param>
|
|
|
|
/// <param name="pluginManager">The <see cref="IPluginManager"/>.</param>
|
2019-01-04 16:42:56 -05:00
|
|
|
public InstallationManager(
|
2019-06-14 12:38:14 -04:00
|
|
|
ILogger<InstallationManager> logger,
|
2020-09-30 19:37:30 -04:00
|
|
|
IServerApplicationHost appHost,
|
2019-01-04 16:42:56 -05:00
|
|
|
IApplicationPaths appPaths,
|
2020-09-05 18:58:16 -04:00
|
|
|
IEventManager eventManager,
|
2020-08-31 13:30:05 -04:00
|
|
|
IHttpClientFactory httpClientFactory,
|
2019-01-04 16:42:56 -05:00
|
|
|
IServerConfigurationManager config,
|
2020-12-06 18:48:54 -05:00
|
|
|
IPluginManager pluginManager)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2019-06-14 12:38:14 -04:00
|
|
|
_currentInstallations = new List<(InstallationInfo, CancellationTokenSource)>();
|
2019-03-25 17:25:32 -04:00
|
|
|
_completedInstallationsInternal = new ConcurrentBag<InstallationInfo>();
|
2013-08-07 15:15:55 -04:00
|
|
|
|
2019-06-14 12:38:14 -04:00
|
|
|
_logger = logger;
|
2013-08-07 15:15:55 -04:00
|
|
|
_applicationHost = appHost;
|
|
|
|
_appPaths = appPaths;
|
2020-09-05 18:58:16 -04:00
|
|
|
_eventManager = eventManager;
|
2020-08-31 13:30:05 -04:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2013-08-30 10:05:44 -04:00
|
|
|
_config = config;
|
2021-03-08 23:57:38 -05:00
|
|
|
_jsonSerializerOptions = JsonDefaults.Options;
|
2020-12-06 18:48:54 -05:00
|
|
|
_pluginManager = pluginManager;
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2019-11-07 04:55:02 -05:00
|
|
|
/// <inheritdoc />
|
2019-09-23 17:10:51 -04:00
|
|
|
public IEnumerable<InstallationInfo> CompletedInstallations => _completedInstallationsInternal;
|
|
|
|
|
2019-10-08 14:51:11 -04:00
|
|
|
/// <inheritdoc />
|
2021-06-06 12:11:51 -04:00
|
|
|
public async Task<PackageInfo[]> GetPackages(string manifestName, string manifest, bool filterIncompatible, CancellationToken cancellationToken = default)
|
2013-06-27 15:08:57 -04:00
|
|
|
{
|
2020-04-05 14:34:41 -04:00
|
|
|
try
|
|
|
|
{
|
2021-06-06 12:11:51 -04:00
|
|
|
PackageInfo[]? packages = await _httpClientFactory.CreateClient(NamedClient.Default)
|
|
|
|
.GetFromJsonAsync<PackageInfo[]>(new Uri(manifest), _jsonSerializerOptions, cancellationToken).ConfigureAwait(false);
|
2020-12-14 20:13:11 -05:00
|
|
|
|
2022-12-05 09:00:20 -05:00
|
|
|
if (packages is null)
|
2020-08-31 13:30:05 -04:00
|
|
|
{
|
2020-11-22 14:35:45 -05:00
|
|
|
return Array.Empty<PackageInfo>();
|
|
|
|
}
|
2020-11-19 08:34:09 -05:00
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
var minimumVersion = new Version(0, 0, 0, 1);
|
2020-11-22 14:35:45 -05:00
|
|
|
// Store the repository and repository url with each version, as they may be spread apart.
|
|
|
|
foreach (var entry in packages)
|
|
|
|
{
|
2020-12-06 18:48:54 -05:00
|
|
|
for (int a = entry.Versions.Count - 1; a >= 0; a--)
|
2020-11-19 08:34:09 -05:00
|
|
|
{
|
2020-12-06 18:48:54 -05:00
|
|
|
var ver = entry.Versions[a];
|
|
|
|
ver.RepositoryName = manifestName;
|
|
|
|
ver.RepositoryUrl = manifest;
|
|
|
|
|
|
|
|
if (!filterIncompatible)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Version.TryParse(ver.TargetAbi, out var targetAbi))
|
|
|
|
{
|
|
|
|
targetAbi = minimumVersion;
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:55:50 -05:00
|
|
|
// Only show plugins that are greater than or equal to targetAbi.
|
2020-12-18 04:44:57 -05:00
|
|
|
if (_applicationHost.ApplicationVersion >= targetAbi)
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not compatible with this version so remove it.
|
|
|
|
entry.Versions.Remove(ver);
|
2020-11-19 08:34:09 -05:00
|
|
|
}
|
2020-04-05 14:34:41 -04:00
|
|
|
}
|
2020-11-22 14:35:45 -05:00
|
|
|
|
|
|
|
return packages;
|
|
|
|
}
|
2020-12-14 20:13:11 -05:00
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "Cannot locate the plugin manifest {Manifest}", manifest);
|
|
|
|
return Array.Empty<PackageInfo>();
|
|
|
|
}
|
2020-11-22 14:35:45 -05:00
|
|
|
catch (JsonException ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "Failed to deserialize the plugin manifest retrieved from {Manifest}", manifest);
|
|
|
|
return Array.Empty<PackageInfo>();
|
2020-04-05 14:34:41 -04:00
|
|
|
}
|
|
|
|
catch (UriFormatException ex)
|
2016-01-02 16:54:37 -05:00
|
|
|
{
|
2020-06-04 10:57:57 -04:00
|
|
|
_logger.LogError(ex, "The URL configured for the plugin repository manifest URL is not valid: {Manifest}", manifest);
|
2020-06-22 09:35:53 -04:00
|
|
|
return Array.Empty<PackageInfo>();
|
2020-06-06 13:08:05 -04:00
|
|
|
}
|
2020-07-16 10:28:31 -04:00
|
|
|
catch (HttpRequestException ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "An error occurred while accessing the plugin manifest: {Manifest}", manifest);
|
|
|
|
return Array.Empty<PackageInfo>();
|
|
|
|
}
|
2016-01-02 16:54:37 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:57:57 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task<IReadOnlyList<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var result = new List<PackageInfo>();
|
|
|
|
foreach (RepositoryInfo repository in _config.Configuration.PluginRepositories)
|
|
|
|
{
|
2022-12-05 09:01:13 -05:00
|
|
|
if (repository.Enabled && repository.Url is not null)
|
2020-07-17 11:08:29 -04:00
|
|
|
{
|
2020-12-06 18:48:54 -05:00
|
|
|
// Where repositories have the same content, the details from the first is taken.
|
|
|
|
foreach (var package in await GetPackages(repository.Name ?? "Unnamed Repo", repository.Url, true, cancellationToken).ConfigureAwait(true))
|
2020-11-19 08:34:09 -05:00
|
|
|
{
|
2021-06-06 12:11:51 -04:00
|
|
|
var existing = FilterPackages(result, package.Name, package.Id).FirstOrDefault();
|
2020-12-06 18:48:54 -05:00
|
|
|
|
|
|
|
// Remove invalid versions from the valid package.
|
|
|
|
for (var i = package.Versions.Count - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
var version = package.Versions[i];
|
|
|
|
|
2021-06-06 12:11:51 -04:00
|
|
|
var plugin = _pluginManager.GetPlugin(package.Id, version.VersionNumber);
|
2022-12-05 09:01:13 -05:00
|
|
|
if (plugin is not null)
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
2023-04-09 12:53:09 -04:00
|
|
|
await _pluginManager.PopulateManifest(package, version.VersionNumber, plugin.Path, plugin.Manifest.Status).ConfigureAwait(false);
|
2020-12-06 18:48:54 -05:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:33:10 -05:00
|
|
|
// Remove versions with a target ABI greater then the current application version.
|
2020-12-18 04:44:57 -05:00
|
|
|
if (Version.TryParse(version.TargetAbi, out var targetAbi) && _applicationHost.ApplicationVersion < targetAbi)
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
|
|
|
package.Versions.RemoveAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't add a package that doesn't have any compatible versions.
|
2020-12-14 20:23:52 -05:00
|
|
|
if (package.Versions.Count == 0)
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-05 09:01:13 -05:00
|
|
|
if (existing is not null)
|
2020-11-19 08:34:09 -05:00
|
|
|
{
|
|
|
|
// Assumption is both lists are ordered, so slot these into the correct place.
|
2020-12-06 18:48:54 -05:00
|
|
|
MergeSortedList(existing.Versions, package.Versions);
|
2020-11-19 08:34:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.Add(package);
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 11:08:29 -04:00
|
|
|
}
|
2020-06-04 10:57:57 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 09:35:53 -04:00
|
|
|
return result;
|
2020-06-04 10:57:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:51:11 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public IEnumerable<PackageInfo> FilterPackages(
|
|
|
|
IEnumerable<PackageInfo> availablePackages,
|
2020-12-06 18:48:54 -05:00
|
|
|
string? name = null,
|
2021-06-06 12:11:51 -04:00
|
|
|
Guid id = default,
|
2020-12-06 18:48:54 -05:00
|
|
|
Version? specificVersion = null)
|
2013-09-29 21:29:38 -04:00
|
|
|
{
|
2022-12-05 09:01:13 -05:00
|
|
|
if (name is not null)
|
2013-09-29 21:29:38 -04:00
|
|
|
{
|
2020-12-06 18:48:54 -05:00
|
|
|
availablePackages = availablePackages.Where(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
|
2013-09-29 21:29:38 -04:00
|
|
|
}
|
|
|
|
|
2022-02-21 08:15:09 -05:00
|
|
|
if (!id.Equals(default))
|
2013-02-27 07:49:55 -05:00
|
|
|
{
|
2022-02-21 08:15:09 -05:00
|
|
|
availablePackages = availablePackages.Where(x => x.Id.Equals(id));
|
2017-08-24 15:52:19 -04:00
|
|
|
}
|
2017-08-20 15:10:00 -04:00
|
|
|
|
2022-12-05 09:01:13 -05:00
|
|
|
if (specificVersion is not null)
|
2020-11-19 08:34:09 -05:00
|
|
|
{
|
2020-12-06 18:48:54 -05:00
|
|
|
availablePackages = availablePackages.Where(x => x.Versions.Any(y => y.VersionNumber.Equals(specificVersion)));
|
2020-11-19 08:34:09 -05:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:51:11 -04:00
|
|
|
return availablePackages;
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 12:08:41 -04:00
|
|
|
/// <inheritdoc />
|
2020-05-24 02:04:10 -04:00
|
|
|
public IEnumerable<InstallationInfo> GetCompatibleVersions(
|
2019-10-08 14:51:11 -04:00
|
|
|
IEnumerable<PackageInfo> availablePackages,
|
2020-12-06 18:48:54 -05:00
|
|
|
string? name = null,
|
2021-06-06 12:11:51 -04:00
|
|
|
Guid id = default,
|
2020-12-06 18:48:54 -05:00
|
|
|
Version? minVersion = null,
|
|
|
|
Version? specificVersion = null)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2020-12-18 16:11:29 -05:00
|
|
|
var package = FilterPackages(availablePackages, name, id, specificVersion).FirstOrDefault();
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2020-02-25 11:58:39 -05:00
|
|
|
// Package not found in repository
|
2022-12-05 09:00:20 -05:00
|
|
|
if (package is null)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2020-05-24 02:04:10 -04:00
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var appVer = _applicationHost.ApplicationVersion;
|
2020-12-06 18:48:54 -05:00
|
|
|
var availableVersions = package.Versions
|
2020-12-18 04:44:57 -05:00
|
|
|
.Where(x => string.IsNullOrEmpty(x.TargetAbi) || Version.Parse(x.TargetAbi) <= appVer);
|
2020-05-24 02:04:10 -04:00
|
|
|
|
2022-12-05 09:01:13 -05:00
|
|
|
if (specificVersion is not null)
|
2020-08-14 10:54:21 -04:00
|
|
|
{
|
2020-11-19 08:34:09 -05:00
|
|
|
availableVersions = availableVersions.Where(x => x.VersionNumber.Equals(specificVersion));
|
2020-08-14 10:54:21 -04:00
|
|
|
}
|
2022-12-05 09:01:13 -05:00
|
|
|
else if (minVersion is not null)
|
2020-05-24 02:04:10 -04:00
|
|
|
{
|
2020-11-19 08:34:09 -05:00
|
|
|
availableVersions = availableVersions.Where(x => x.VersionNumber >= minVersion);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 08:34:09 -05:00
|
|
|
foreach (var v in availableVersions.OrderByDescending(x => x.VersionNumber))
|
2020-05-24 02:04:10 -04:00
|
|
|
{
|
|
|
|
yield return new InstallationInfo
|
|
|
|
{
|
2020-12-06 18:48:54 -05:00
|
|
|
Changelog = v.Changelog,
|
2021-06-06 12:11:51 -04:00
|
|
|
Id = package.Id,
|
2020-12-06 18:48:54 -05:00
|
|
|
Name = package.Name,
|
2020-11-19 08:34:09 -05:00
|
|
|
Version = v.VersionNumber,
|
2020-12-06 18:48:54 -05:00
|
|
|
SourceUrl = v.SourceUrl,
|
2021-02-12 08:33:10 -05:00
|
|
|
Checksum = v.Checksum,
|
|
|
|
PackageInfo = package
|
2020-05-24 02:04:10 -04:00
|
|
|
};
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:51:11 -04:00
|
|
|
/// <inheritdoc />
|
2020-05-24 02:04:10 -04:00
|
|
|
public async Task<IEnumerable<InstallationInfo>> GetAvailablePluginUpdates(CancellationToken cancellationToken = default)
|
2013-06-27 15:08:57 -04:00
|
|
|
{
|
2019-10-08 14:51:11 -04:00
|
|
|
var catalog = await GetAvailablePackages(cancellationToken).ConfigureAwait(false);
|
2020-04-06 22:04:24 -04:00
|
|
|
return GetAvailablePluginUpdates(catalog);
|
|
|
|
}
|
2017-08-19 15:43:35 -04:00
|
|
|
|
2019-07-29 17:47:25 -04:00
|
|
|
/// <inheritdoc />
|
2020-05-24 02:04:10 -04:00
|
|
|
public async Task InstallPackage(InstallationInfo package, CancellationToken cancellationToken)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2022-10-06 14:21:23 -04:00
|
|
|
ArgumentNullException.ThrowIfNull(package);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
var innerCancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
2020-05-24 02:04:10 -04:00
|
|
|
var tuple = (package, innerCancellationTokenSource);
|
2013-02-24 16:53:54 -05:00
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
// Add it to the in-progress list
|
2019-09-23 17:10:51 -04:00
|
|
|
lock (_currentInstallationsLock)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2019-06-14 12:38:14 -04:00
|
|
|
_currentInstallations.Add(tuple);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:43:00 -05:00
|
|
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, innerCancellationTokenSource.Token);
|
|
|
|
var linkedToken = linkedTokenSource.Token;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2020-09-05 18:58:16 -04:00
|
|
|
await _eventManager.PublishAsync(new PluginInstallingEventArgs(package)).ConfigureAwait(false);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-09-05 18:58:16 -04:00
|
|
|
var isUpdate = await InstallPackageInternal(package, linkedToken).ConfigureAwait(false);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2019-09-23 17:10:51 -04:00
|
|
|
lock (_currentInstallationsLock)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2019-06-14 12:38:14 -04:00
|
|
|
_currentInstallations.Remove(tuple);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2020-05-24 02:04:10 -04:00
|
|
|
_completedInstallationsInternal.Add(package);
|
2023-11-25 19:25:52 -05:00
|
|
|
|
|
|
|
if (isUpdate)
|
|
|
|
{
|
|
|
|
await _eventManager.PublishAsync(new PluginUpdatedEventArgs(package)).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await _eventManager.PublishAsync(new PluginInstalledEventArgs(package)).ConfigureAwait(false);
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2020-09-05 18:58:16 -04:00
|
|
|
_applicationHost.NotifyPendingRestart();
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
2019-09-23 17:10:51 -04:00
|
|
|
lock (_currentInstallationsLock)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2019-06-14 12:38:14 -04:00
|
|
|
_currentInstallations.Remove(tuple);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2020-05-24 02:04:10 -04:00
|
|
|
_logger.LogInformation("Package installation cancelled: {0} {1}", package.Name, package.Version);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2020-09-05 18:58:16 -04:00
|
|
|
await _eventManager.PublishAsync(new PluginInstallationCancelledEventArgs(package)).ConfigureAwait(false);
|
2013-02-24 16:53:54 -05:00
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
throw;
|
|
|
|
}
|
2013-03-16 12:41:49 -04:00
|
|
|
catch (Exception ex)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
_logger.LogError(ex, "Package installation failed");
|
2013-03-16 12:41:49 -04:00
|
|
|
|
2019-09-23 17:10:51 -04:00
|
|
|
lock (_currentInstallationsLock)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2019-06-14 12:38:14 -04:00
|
|
|
_currentInstallations.Remove(tuple);
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
2013-02-24 16:53:54 -05:00
|
|
|
|
2020-09-05 18:58:16 -04:00
|
|
|
await _eventManager.PublishAsync(new InstallationFailedEventArgs
|
2013-07-06 17:23:32 -04:00
|
|
|
{
|
2020-05-24 02:04:10 -04:00
|
|
|
InstallationInfo = package,
|
2013-07-06 17:23:32 -04:00
|
|
|
Exception = ex
|
2020-09-05 18:58:16 -04:00
|
|
|
}).ConfigureAwait(false);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
// Dispose the progress object and remove the installation from the in-progress list
|
2019-10-08 14:51:11 -04:00
|
|
|
tuple.innerCancellationTokenSource.Dispose();
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-12-06 18:48:54 -05:00
|
|
|
/// Uninstalls a plugin.
|
2013-02-20 20:33:05 -05:00
|
|
|
/// </summary>
|
2020-12-06 18:48:54 -05:00
|
|
|
/// <param name="plugin">The <see cref="LocalPlugin"/> to uninstall.</param>
|
|
|
|
public void UninstallPlugin(LocalPlugin plugin)
|
2013-02-20 20:33:05 -05:00
|
|
|
{
|
2022-12-05 09:00:20 -05:00
|
|
|
if (plugin is null)
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-06-14 12:38:14 -04:00
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
if (plugin.Instance?.CanUninstall == false)
|
|
|
|
{
|
2020-12-23 05:26:20 -05:00
|
|
|
_logger.LogWarning("Attempt to delete non removable plugin {PluginName}, ignoring request", plugin.Name);
|
2020-12-06 18:48:54 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-02-27 08:34:24 -05:00
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
plugin.Instance?.OnUninstalling();
|
2019-06-14 12:38:14 -04:00
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
// Remove it the quick way for now
|
|
|
|
_pluginManager.RemovePlugin(plugin);
|
|
|
|
|
|
|
|
_eventManager.Publish(new PluginUninstalledEventArgs(plugin.GetPluginInfo()));
|
|
|
|
|
|
|
|
_applicationHost.NotifyPendingRestart();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public bool CancelInstallation(Guid id)
|
|
|
|
{
|
|
|
|
lock (_currentInstallationsLock)
|
|
|
|
{
|
2022-02-21 08:15:09 -05:00
|
|
|
var install = _currentInstallations.Find(x => x.Info.Id.Equals(id));
|
2020-12-06 18:48:54 -05:00
|
|
|
if (install == default((InstallationInfo, CancellationTokenSource)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-24 16:18:24 -05:00
|
|
|
install.Token.Cancel();
|
2020-12-06 18:48:54 -05:00
|
|
|
_currentInstallations.Remove(install);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and optionally managed resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources or <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
{
|
|
|
|
if (dispose)
|
|
|
|
{
|
|
|
|
lock (_currentInstallationsLock)
|
|
|
|
{
|
|
|
|
foreach (var (info, token) in _currentInstallations)
|
|
|
|
{
|
|
|
|
token.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_currentInstallations.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Merges two sorted lists.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">The source <see cref="IList{VersionInfo}"/> instance to merge.</param>
|
|
|
|
/// <param name="dest">The destination <see cref="IList{VersionInfo}"/> instance to merge with.</param>
|
|
|
|
private static void MergeSortedList(IList<VersionInfo> source, IList<VersionInfo> dest)
|
|
|
|
{
|
|
|
|
int sLength = source.Count - 1;
|
|
|
|
int dLength = dest.Count;
|
|
|
|
int s = 0, d = 0;
|
|
|
|
var sourceVersion = source[0].VersionNumber;
|
|
|
|
var destVersion = dest[0].VersionNumber;
|
|
|
|
|
|
|
|
while (d < dLength)
|
|
|
|
{
|
|
|
|
if (sourceVersion.CompareTo(destVersion) >= 0)
|
|
|
|
{
|
|
|
|
if (s < sLength)
|
|
|
|
{
|
|
|
|
sourceVersion = source[++s].VersionNumber;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Append all of destination to the end of source.
|
|
|
|
while (d < dLength)
|
|
|
|
{
|
|
|
|
source.Add(dest[d++]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source.Insert(s++, dest[d++]);
|
|
|
|
if (d >= dLength)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sLength++;
|
|
|
|
destVersion = dest[d].VersionNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<InstallationInfo> GetAvailablePluginUpdates(IReadOnlyList<PackageInfo> pluginCatalog)
|
|
|
|
{
|
|
|
|
var plugins = _pluginManager.Plugins;
|
|
|
|
foreach (var plugin in plugins)
|
|
|
|
{
|
2021-04-06 15:59:47 -04:00
|
|
|
// Don't auto update when plugin marked not to, or when it's disabled.
|
2021-04-08 09:38:25 -04:00
|
|
|
if (plugin.Manifest?.AutoUpdate == false || plugin.Manifest?.Status == PluginStatus.Disabled)
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var compatibleVersions = GetCompatibleVersions(pluginCatalog, plugin.Name, plugin.Id, minVersion: plugin.Version);
|
|
|
|
var version = compatibleVersions.FirstOrDefault(y => y.Version > plugin.Version);
|
|
|
|
|
2022-12-05 09:01:13 -05:00
|
|
|
if (version is not null && CompletedInstallations.All(x => !x.Id.Equals(version.Id)))
|
2020-12-06 18:48:54 -05:00
|
|
|
{
|
|
|
|
yield return version;
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 15:59:47 -04:00
|
|
|
private async Task PerformPackageInstallation(InstallationInfo package, PluginStatus status, CancellationToken cancellationToken)
|
2013-08-07 15:15:55 -04:00
|
|
|
{
|
2023-10-05 18:40:09 -04:00
|
|
|
if (!Path.GetExtension(package.SourceUrl.AsSpan()).Equals(".zip", StringComparison.OrdinalIgnoreCase))
|
2019-02-11 12:52:09 -05:00
|
|
|
{
|
2020-05-24 02:53:17 -04:00
|
|
|
_logger.LogError("Only zip packages are supported. {SourceUrl} is not a zip archive.", package.SourceUrl);
|
2019-02-11 12:52:09 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-02-11 12:53:35 -05:00
|
|
|
|
2019-04-03 19:43:02 -04:00
|
|
|
// Always override the passed-in target (which is a file) and figure it out again
|
2020-05-24 02:04:10 -04:00
|
|
|
string targetDir = Path.Combine(_appPaths.PluginsPath, package.Name);
|
2013-08-07 15:15:55 -04:00
|
|
|
|
2020-08-31 13:30:05 -04:00
|
|
|
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
2020-11-19 09:12:33 -05:00
|
|
|
.GetAsync(new Uri(package.SourceUrl), cancellationToken).ConfigureAwait(false);
|
2020-12-08 10:28:19 -05:00
|
|
|
response.EnsureSuccessStatusCode();
|
2020-11-17 13:43:00 -05:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-31 13:30:05 -04:00
|
|
|
|
2019-11-07 04:50:55 -05:00
|
|
|
// CA5351: Do Not Use Broken Cryptographic Algorithms
|
2019-07-29 17:47:25 -04:00
|
|
|
#pragma warning disable CA5351
|
2020-08-31 13:30:05 -04:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-08-07 15:15:55 -04:00
|
|
|
|
2023-10-11 12:32:57 -04:00
|
|
|
var hash = Convert.ToHexString(await MD5.HashDataAsync(stream, cancellationToken).ConfigureAwait(false));
|
2020-08-31 13:30:05 -04:00
|
|
|
if (!string.Equals(package.Checksum, hash, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
_logger.LogError(
|
|
|
|
"The checksums didn't match while installing {Package}, expected: {Expected}, got: {Received}",
|
|
|
|
package.Name,
|
|
|
|
package.Checksum,
|
|
|
|
hash);
|
|
|
|
throw new InvalidDataException("The checksum of the received data doesn't match.");
|
|
|
|
}
|
2013-08-07 15:15:55 -04:00
|
|
|
|
2020-09-05 15:49:04 -04:00
|
|
|
// Version folder as they cannot be overwritten in Windows.
|
2020-09-08 12:50:19 -04:00
|
|
|
targetDir += "_" + package.Version;
|
2013-08-07 15:15:55 -04:00
|
|
|
|
2020-08-31 13:30:05 -04:00
|
|
|
if (Directory.Exists(targetDir))
|
|
|
|
{
|
2020-09-05 15:49:04 -04:00
|
|
|
try
|
2019-07-29 17:47:25 -04:00
|
|
|
{
|
2020-09-05 15:49:04 -04:00
|
|
|
Directory.Delete(targetDir, true);
|
2019-07-29 17:47:25 -04:00
|
|
|
}
|
2020-12-06 18:48:54 -05:00
|
|
|
#pragma warning disable CA1031 // Do not catch general exception types
|
2020-09-05 15:49:04 -04:00
|
|
|
catch
|
2020-12-06 18:48:54 -05:00
|
|
|
#pragma warning restore CA1031 // Do not catch general exception types
|
2019-02-10 17:29:55 -05:00
|
|
|
{
|
2020-09-05 15:49:04 -04:00
|
|
|
// Ignore any exceptions.
|
2013-08-07 15:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 16:04:31 -04:00
|
|
|
|
2020-08-31 13:30:05 -04:00
|
|
|
stream.Position = 0;
|
2023-11-15 18:49:23 -05:00
|
|
|
ZipFile.ExtractToDirectory(stream, targetDir, true);
|
2023-04-09 12:53:09 -04:00
|
|
|
|
|
|
|
// Ensure we create one or populate existing ones with missing data.
|
2023-10-11 12:32:57 -04:00
|
|
|
await _pluginManager.PopulateManifest(package.PackageInfo, package.Version, targetDir, status).ConfigureAwait(false);
|
2023-04-09 12:53:09 -04:00
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
_pluginManager.ImportPluginFrom(targetDir);
|
2013-08-07 15:15:55 -04:00
|
|
|
}
|
|
|
|
|
2020-12-06 18:48:54 -05:00
|
|
|
private async Task<bool> InstallPackageInternal(InstallationInfo package, CancellationToken cancellationToken)
|
2019-06-14 12:38:14 -04:00
|
|
|
{
|
2020-12-18 15:37:35 -05:00
|
|
|
LocalPlugin? plugin = _pluginManager.Plugins.FirstOrDefault(p => p.Id.Equals(package.Id) && p.Version.Equals(package.Version))
|
2020-12-06 18:48:54 -05:00
|
|
|
?? _pluginManager.Plugins.FirstOrDefault(p => p.Name.Equals(package.Name, StringComparison.OrdinalIgnoreCase) && p.Version.Equals(package.Version));
|
2019-06-14 12:38:14 -04:00
|
|
|
|
2021-04-06 15:59:47 -04:00
|
|
|
await PerformPackageInstallation(package, plugin?.Manifest.Status ?? PluginStatus.Active, cancellationToken).ConfigureAwait(false);
|
2022-12-05 09:00:20 -05:00
|
|
|
_logger.LogInformation("Plugin {Action}: {PluginName} {PluginVersion}", plugin is null ? "installed" : "updated", package.Name, package.Version);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
2022-12-05 09:01:13 -05:00
|
|
|
return plugin is not null;
|
2013-03-04 23:25:27 -05:00
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
}
|
|
|
|
}
|