Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes obsolete network settings from system.xml #5482

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8348145
New xml migration
BaronGreenback Mar 11, 2021
e96671b
Moved to program.cs
BaronGreenback Mar 11, 2021
6d4e544
Moved migration
BaronGreenback Mar 15, 2021
a9f4cf8
Update ApplicationHost.cs
BaronGreenback Mar 15, 2021
08aeb4f
Update ApplicationHost.cs
BaronGreenback Mar 15, 2021
d5051d7
Update Program.cs
BaronGreenback Mar 15, 2021
1b9b775
Changed to xmlTextReader
BaronGreenback Mar 15, 2021
a43ffc1
reworking
BaronGreenback Mar 22, 2021
14ce842
Simplified
BaronGreenback Mar 22, 2021
5d28ac0
removed unused file.
BaronGreenback Mar 22, 2021
6617fc3
sln correction
BaronGreenback Mar 22, 2021
72b3684
Merge branch 'master' into SettingsMigration
BaronGreenback Mar 23, 2021
23aad2d
Updated from master
BaronGreenback Apr 15, 2021
1bd8259
Used fixed config.
BaronGreenback Apr 15, 2021
1c95184
Merge branch 'master' into SettingsMigration
BaronGreenback Apr 21, 2021
26c6d90
fixed merge
BaronGreenback Apr 21, 2021
ed638d2
Update MigrationRunner.cs
BaronGreenback May 6, 2021
1fd43bc
Create delete me
BaronGreenback May 6, 2021
0d4e0bf
Rename Jellyfin.Server/Migrations/Legacy/V703/NetworkConfiguration.cs…
BaronGreenback May 6, 2021
b12443a
Update and rename Jellyfin.Server/Migrations/Legacy/V703/ServerConfig…
BaronGreenback May 6, 2021
13a8616
Delete delete me
BaronGreenback May 6, 2021
d391257
Update NetworkConfiguration.cs
BaronGreenback May 6, 2021
2d1452e
Improved
BaronGreenback May 7, 2021
6de8c96
changed path to configuration folder
BaronGreenback May 7, 2021
c09b0f7
Merge branch 'master' into SettingsMigration
BaronGreenback May 29, 2021
File filter
Filter file types
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

Improved
  • Loading branch information
BaronGreenback committed May 7, 2021
commit 2d1452e39a6ad96b5509118752af5e6069f6c792
@@ -2,7 +2,7 @@

using System;

namespace Jellyfin.Server.Migrations.Legacy.V10703
namespace Jellyfin.Server.Migrations.Legacy.V10v7
{
/// <summary>
/// Defines the <see cref="NetworkConfiguration" />.
@@ -0,0 +1,28 @@
using System.IO;
using MediaBrowser.Controller;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Server.Migrations.Legacy.V10v7
{
/// <summary>
/// Defines the settings migration for version 10.7.
/// </summary>
public class RunMigration
{
/// <summary>
/// Initializes a new instance of the <see cref="RunMigration"/> class.
/// </summary>
/// <param name="appPaths">The <see cref="IServerApplicationPaths"/>.</param>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public RunMigration(IServerApplicationPaths appPaths, ILogger logger)
{
var destFile = Path.Combine(appPaths.ConfigurationDirectoryPath, "network.xml");
if (File.Exists(destFile))
{
// Migrate version 10.7 network configuration.
MigrationRunner.RunSettingMigration<ServerConfiguration, NetworkConfiguration>(logger, appPaths.SystemConfigurationFilePath, destFile);
logger.LogDebug("Migrated to network configuration 7.0.3");
}
}
}
}
@@ -2,7 +2,7 @@

using System;

namespace Jellyfin.Server.Migrations.Legacy.V10703
namespace Jellyfin.Server.Migrations.Legacy.V10v7
{
/// <summary>
/// Defines the legacy <see cref="ServerConfiguration" />.
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Emby.Server.Implementations.Serialization;
using Jellyfin.Networking.Configuration;
using MediaBrowser.Common.Configuration;
@@ -15,6 +18,8 @@ namespace Jellyfin.Server.Migrations
/// </summary>
public sealed class MigrationRunner
{
private const string MigrationNamespace = "Jellyfin.Server.Migrations.Legacy.V";

/// <summary>
/// The list of known migrations, in order of applicability.
/// </summary>
@@ -87,18 +92,62 @@ public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
}

/// <summary>
/// Performs a network setting migration.
/// Performs the setting migrations.
///
/// Namespaces with the prefix 'Jellyfin.Server.Migrations.Legacy.V{x}v{y}[r{y}] should contain a method called RunMigration.
/// This method is responsible for each individual migration.
/// Each migrations is applied in version order, starting with the earliest.
///
/// </summary>
/// <param name="appPaths">The <see cref="IServerApplicationPaths"/>.</param>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public static void RunSettingsMigration(IServerApplicationPaths appPaths, ILogger logger)
{
var destFile = Path.Combine(appPaths.ConfigurationDirectoryPath, "network.xml");
if (!File.Exists(destFile))
var previous = Array.Empty<string>();
var migrationHistory = Path.Combine(appPaths.ProgramDataPath, "settingsMigrations");
if (File.Exists(migrationHistory))
{
// Migrate version 10.7.0.3 network configuration.
RunSettingMigration<Legacy.V71003.ServerConfiguration, Legacy.V10703.NetworkConfiguration>(logger, appPaths.SystemConfigurationFilePath, destFile);
logger.LogDebug("Migrated to network configuration 7.0.3");
try
{
previous = File.ReadAllLines(migrationHistory);
}
catch (Exception ex)
{
logger.LogError(ex, "Unable to read migration history. Re-applying all.");
}
}

var completedMigrations = new List<string>(previous);

var migrations = GetMigrationClasses(MigrationNamespace);
bool changed = false;
foreach (string migration in migrations)
{
var method = migration + ".RunMigration";
if (!completedMigrations.Contains(migration))
{
// Each migration namespace needs has a migrateSettings class.
var migrationMethodType = Type.GetType(method);
if (migrationMethodType != null)
{
// Execute migration.
_ = Activator.CreateInstance(migrationMethodType, appPaths, logger);
completedMigrations.Add(migration);
changed = true;
}
}
}

if (changed)
{
try
{
File.WriteAllLines(migrationHistory, completedMigrations);
}
catch (Exception ex)
{
logger.LogError(ex, "Unable to store migration history.");
}
}
}

@@ -110,7 +159,7 @@ public static void RunSettingsMigration(IServerApplicationPaths appPaths, ILogge
/// <param name="logger">The <see cref="ILogger"/>.</param>
/// <param name="sourceFile">Source filename.</param>
/// <param name="destFile">Destination filename.</param>
private static void RunSettingMigration<T, TU>(ILogger logger, string sourceFile, string destFile)
public static void RunSettingMigration<T, TU>(ILogger logger, string sourceFile, string destFile)
where TU : new()
{
try
@@ -145,5 +194,40 @@ public static void RunSettingsMigration(IServerApplicationPaths appPaths, ILogge
logger.LogDebug(ex, "Exception occurred migrating settings.");
}
}

private static string GetNamespace(string ns)
{
var i = ns.LastIndexOf('.');
if (i != -1)
{
return ns.Substring(0, i);
}

return ns;
}

private static Version GetVersion(string ns)
{
// extrapolate version number from the namespace.
return Version.Parse(ns[MigrationNamespace.Length..]
.Replace("v", ".", StringComparison.Ordinal)
.Replace("r", ".", StringComparison.Ordinal));
}

/// <summary>
/// Returns a list of namespace classes in the format 'Legacy.V{x}r{y}'.
/// </summary>
/// <param name="nameSpace">Namespace to enumerate.</param>
/// <returns>List of namespaces.</returns>
private static IEnumerable<string> GetMigrationClasses(string nameSpace)
{
var asm = Assembly.GetEntryAssembly();
return asm!.GetTypes()
.Where(type => type.Namespace != null
&& type.Namespace!.StartsWith(nameSpace, StringComparison.OrdinalIgnoreCase))
.Select(type => GetNamespace(type.FullName!))
.Distinct()
.OrderBy(fn => GetVersion(fn));
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.