parent
663aa462b0
commit
d0e2cf9571
@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Update;
|
||||
|
||||
namespace NzbDrone.Core.Configuration
|
||||
{
|
||||
public interface IDeploymentInfoProvider
|
||||
{
|
||||
string PackageVersion { get; }
|
||||
string PackageAuthor { get; }
|
||||
string PackageBranch { get; }
|
||||
UpdateMechanism PackageUpdateMechanism { get; }
|
||||
|
||||
string ReleaseVersion { get; }
|
||||
string ReleaseBranch { get; }
|
||||
|
||||
bool IsExternalUpdateMechanism { 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 = "aphrodite";
|
||||
|
||||
if (Path.GetFileName(bin) == "bin" && diskProvider.FileExists(packageInfoPath))
|
||||
{
|
||||
var data = diskProvider.ReadAllText(packageInfoPath);
|
||||
|
||||
PackageVersion = ReadValue(data, "PackageVersion");
|
||||
PackageAuthor = ReadValue(data, "PackageAuthor");
|
||||
PackageUpdateMechanism = ReadEnumValue(data, "UpdateMethod", UpdateMechanism.BuiltIn);
|
||||
PackageBranch = ReadValue(data, "Branch");
|
||||
|
||||
ReleaseVersion = ReadValue(data, "ReleaseVersion");
|
||||
|
||||
if (PackageBranch.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
DefaultBranch = PackageBranch;
|
||||
}
|
||||
}
|
||||
|
||||
if (diskProvider.FileExists(releaseInfoPath))
|
||||
{
|
||||
var data = diskProvider.ReadAllText(releaseInfoPath);
|
||||
|
||||
ReleaseVersion = ReadValue(data, "ReleaseVersion", ReleaseVersion);
|
||||
ReleaseBranch = ReadValue(data, "Branch");
|
||||
|
||||
if (ReleaseBranch.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
DefaultBranch = ReleaseBranch;
|
||||
}
|
||||
}
|
||||
|
||||
DefaultUpdateMechanism = PackageUpdateMechanism;
|
||||
}
|
||||
|
||||
private static string ReadValue(string fileData, string key, string defaultValue = null)
|
||||
{
|
||||
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);
|
||||
if (value != null && Enum.TryParse<T>(value, true, out var result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public string PackageVersion { get; private set; }
|
||||
public string PackageAuthor { get; private set; }
|
||||
public string PackageBranch { get; private set; }
|
||||
public UpdateMechanism PackageUpdateMechanism { get; private set; }
|
||||
|
||||
public string ReleaseVersion { get; private set; }
|
||||
public string ReleaseBranch { get; set; }
|
||||
|
||||
public bool IsExternalUpdateMechanism => PackageUpdateMechanism >= UpdateMechanism.External;
|
||||
public UpdateMechanism DefaultUpdateMechanism { get; private set; }
|
||||
public string DefaultBranch { get; private set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Lifecycle;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
|
||||
namespace NzbDrone.Core.Update
|
||||
{
|
||||
public interface IUpdaterConfigProvider
|
||||
{
|
||||
}
|
||||
|
||||
public class UpdaterConfigProvider : IUpdaterConfigProvider, IHandle<ApplicationStartedEvent>
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly IConfigFileProvider _configFileProvider;
|
||||
private readonly 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 => v >= 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;
|
||||
}
|
||||
}
|
||||
|
||||
if (_deploymentInfoProvider.IsExternalUpdateMechanism)
|
||||
{
|
||||
var currentBranch = _configFileProvider.Branch;
|
||||
var packageBranch = _deploymentInfoProvider.PackageBranch;
|
||||
if (packageBranch.IsNotNullOrWhiteSpace() && packageBranch != currentBranch)
|
||||
{
|
||||
_logger.Info("External updater uses branch {0} instead of the currently selected {1}, changing to {0}.", packageBranch, currentBranch);
|
||||
ChangeBranch(packageBranch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangeUpdateMechanism(UpdateMechanism updateMechanism)
|
||||
{
|
||||
var config = new Dictionary<string, object>
|
||||
{
|
||||
[nameof(_configFileProvider.UpdateMechanism)] = updateMechanism
|
||||
};
|
||||
_configFileProvider.SaveConfigDictionary(config);
|
||||
}
|
||||
|
||||
private void ChangeBranch(string branch)
|
||||
{
|
||||
var config = new Dictionary<string, object>
|
||||
{
|
||||
[nameof(_configFileProvider.Branch)] = branch
|
||||
};
|
||||
_configFileProvider.SaveConfigDictionary(config);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue