Parse the package_info file to determine whether the UpdateMechanism is configured properly.

This commit is contained in:
Taloth Saldono 2018-11-23 16:59:31 +01:00
parent f87e2dd717
commit 066a9de7ae
5 changed files with 209 additions and 2 deletions

View File

@ -0,0 +1,118 @@
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Update;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NzbDrone.Core.Configuration
{
public interface IDeploymentInfoProvider
{
Version PackageVersion { get; }
string PackageBranch { get; }
UpdateMechanism PackageUpdateMechanism { get; }
Version ReleaseVersion { get; }
string ReleaseBranch { get; }
bool BuiltInUpdaterAllowed { get; }
UpdateMechanism DefaultUpdateMechanism { get; }
string DefaultBranch { get; }
}
public class DeploymentInfoProvider : IDeploymentInfoProvider
{
public DeploymentInfoProvider(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider)
{
var bin = appFolderInfo.StartUpFolder;
var packageInfoPath = Path.Combine(bin, "..", "package_info");
var releaseInfoPath = Path.Combine(bin, "release_info");
PackageUpdateMechanism = UpdateMechanism.BuiltIn;
DefaultBranch = "master";
if (Path.GetFileName(bin) == "bin" && diskProvider.FileExists(packageInfoPath))
{
var data = diskProvider.ReadAllText(packageInfoPath);
PackageVersion = ReadVersion(data, "PackageVersion");
PackageUpdateMechanism = ReadEnumValue(data, "UpdateMethod", UpdateMechanism.BuiltIn);
PackageBranch = ReadValue(data, "Branch", null);
ReleaseVersion = ReadVersion(data, "ReleaseVersion");
if (PackageBranch.IsNotNullOrWhiteSpace())
{
DefaultBranch = PackageBranch;
}
}
if (diskProvider.FileExists(releaseInfoPath))
{
var data = diskProvider.ReadAllText(releaseInfoPath);
ReleaseVersion = ReadVersion(data, "ReleaseVersion", ReleaseVersion);
ReleaseBranch = ReadValue(data, "Branch", null);
if (ReleaseBranch.IsNotNullOrWhiteSpace())
{
DefaultBranch = ReleaseBranch;
}
}
DefaultUpdateMechanism = PackageUpdateMechanism;
}
private static string ReadValue(string fileData, string key, string defaultValue)
{
var match = Regex.Match(fileData, "^" + key + "=(.*)$", RegexOptions.Multiline);
if (match.Success)
{
return match.Groups[1].Value.Trim();
}
return defaultValue;
}
private static T ReadEnumValue<T>(string fileData, string key, T defaultValue)
where T : struct
{
var value = ReadValue(fileData, key, null);
if (value != null && Enum.TryParse<T>(value, true, out var result))
{
return result;
}
return defaultValue;
}
private static Version ReadVersion(string fileData, string key, Version defaultValue = null)
{
var value = ReadValue(fileData, key, null);
if (value != null && Version.TryParse(value, out var result))
{
return result;
}
return defaultValue;
}
public Version PackageVersion { get; private set; }
public string PackageBranch { get; private set; }
public UpdateMechanism PackageUpdateMechanism { get; private set; }
public Version ReleaseVersion { get; set; }
public string ReleaseBranch { get; set; }
public bool BuiltInUpdaterAllowed => PackageUpdateMechanism == UpdateMechanism.BuiltIn;
public UpdateMechanism DefaultUpdateMechanism { get; private set; }
public string DefaultBranch { get; private set; }
}
}

View File

@ -129,6 +129,7 @@
<Compile Include="Configuration\ConfigFileProvider.cs" />
<Compile Include="Configuration\ConfigRepository.cs" />
<Compile Include="Configuration\ConfigService.cs" />
<Compile Include="Configuration\DeploymentInfoProvider.cs" />
<Compile Include="Configuration\Events\ConfigFileSavedEvent.cs" />
<Compile Include="Configuration\Events\ConfigSavedEvent.cs" />
<Compile Include="Configuration\IConfigService.cs" />
@ -1225,6 +1226,7 @@
<Compile Include="Tv\SeriesTypes.cs" />
<Compile Include="Tv\ShouldRefreshSeries.cs" />
<Compile Include="Update\Commands\ApplicationUpdateCommand.cs" />
<Compile Include="Update\ConfigureUpdateMechanism.cs" />
<Compile Include="Update\InstallUpdateService.cs" />
<Compile Include="Update\RecentUpdateProvider.cs" />
<Compile Include="Update\UpdateAbortedException.cs" />

View File

@ -0,0 +1,62 @@
using NLog;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NzbDrone.Core.Update
{
public interface IUpdaterConfigProvider
{
}
public class UpdaterConfigProvider : IUpdaterConfigProvider, IHandle<ApplicationStartedEvent>
{
private Logger _logger;
private IConfigFileProvider _configFileProvider;
private IDeploymentInfoProvider _deploymentInfoProvider;
public UpdaterConfigProvider(IDeploymentInfoProvider deploymentInfoProvider, IConfigFileProvider configFileProvider, Logger logger)
{
_deploymentInfoProvider = deploymentInfoProvider;
_configFileProvider = configFileProvider;
_logger = logger;
}
public void Handle(ApplicationStartedEvent message)
{
var updateMechanism = _configFileProvider.UpdateMechanism;
var packageUpdateMechanism = _deploymentInfoProvider.PackageUpdateMechanism;
var externalMechanisms = Enum.GetValues(typeof(UpdateMechanism))
.Cast<UpdateMechanism>()
.Where(v => (int)v >= (int)UpdateMechanism.External)
.ToArray();
foreach (var externalMechanism in externalMechanisms)
{
if (packageUpdateMechanism != externalMechanism && updateMechanism == externalMechanism ||
packageUpdateMechanism == externalMechanism && updateMechanism == UpdateMechanism.BuiltIn)
{
_logger.Info("Update mechanism {0} not supported in the current configuration, changing to {1}.", updateMechanism, packageUpdateMechanism);
ChangeUpdateMechanism(packageUpdateMechanism);
break;
}
}
}
private void ChangeUpdateMechanism(UpdateMechanism updateMechanism)
{
var config = new Dictionary<string, object>
{
[nameof(_configFileProvider.UpdateMechanism)] = updateMechanism
};
_configFileProvider.SaveConfigDictionary(config);
}
}
}

View File

@ -29,6 +29,7 @@ namespace NzbDrone.Core.Update
private readonly IProcessProvider _processProvider;
private readonly IVerifyUpdates _updateVerifier;
private readonly IStartupContext _startupContext;
private readonly IDeploymentInfoProvider _deploymentInfoProvider;
private readonly IConfigFileProvider _configFileProvider;
private readonly IRuntimeInfo _runtimeInfo;
private readonly IBackupService _backupService;
@ -43,6 +44,7 @@ namespace NzbDrone.Core.Update
IProcessProvider processProvider,
IVerifyUpdates updateVerifier,
IStartupContext startupContext,
IDeploymentInfoProvider deploymentInfoProvider,
IConfigFileProvider configFileProvider,
IRuntimeInfo runtimeInfo,
IBackupService backupService,
@ -61,6 +63,7 @@ namespace NzbDrone.Core.Update
_processProvider = processProvider;
_updateVerifier = updateVerifier;
_startupContext = startupContext;
_deploymentInfoProvider = deploymentInfoProvider;
_configFileProvider = configFileProvider;
_runtimeInfo = runtimeInfo;
_backupService = backupService;
@ -87,6 +90,12 @@ namespace NzbDrone.Core.Update
}
}
if (_appFolderInfo.StartUpFolder.EndsWith("_output"))
{
_logger.ProgressDebug("Running in developer environment, not updating.");
return;
}
var updateSandboxFolder = _appFolderInfo.GetUpdateSandboxFolder();
var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName);
@ -209,6 +218,19 @@ namespace NzbDrone.Core.Update
return;
}
// Safety net, ConfigureUpdateMechanism should take care of invalid settings
if (_configFileProvider.UpdateMechanism == UpdateMechanism.BuiltIn && !_deploymentInfoProvider.BuiltInUpdaterAllowed)
{
_logger.ProgressDebug("Built-In updater disabled, please use {0} to install", _deploymentInfoProvider.PackageUpdateMechanism);
return;
}
else if (_configFileProvider.UpdateMechanism != UpdateMechanism.Script && !_deploymentInfoProvider.BuiltInUpdaterAllowed)
{
_logger.ProgressDebug("Update available, please use {0} to install", _deploymentInfoProvider.PackageUpdateMechanism);
return;
}
try
{
InstallUpdate(latestAvailable);

View File

@ -1,8 +1,11 @@
namespace NzbDrone.Core.Update
namespace NzbDrone.Core.Update
{
public enum UpdateMechanism
{
BuiltIn = 0,
Script = 1
Script = 1,
External = 10,
Apt = 11,
Docker = 12
}
}