Breaking Changes: Docker Parity (#698)
* Refactored all the config files for Kavita to be loaded from config/. This will allow docker to just mount one folder and for Update functionality to be trivial. * Cleaned up documentation around new update method. * Updated docker files to support config directory * Removed entrypoint, no longer needed * Update appsettings to point to config directory for logs * Updated message for docker users that are upgrading * Ensure that docker users that have not updated their mount points from upgrade cannot start the server * Code smells * More cleanup * Added entrypoint to fix bind mount issues * Updated README with new folder structure * Fixed build system for new setup * Updated string path if user is docker * Updated the migration flow for docker to work properly and Fixed LogFile configuration updating. * Migrating docker images is now working 100% * Fixed config from bad code * Code cleanup Co-authored-by: Chris Plaatjes <kizaing@gmail.com>
This commit is contained in:
parent
66b79e8cbe
commit
a29b11c366
29 changed files with 670 additions and 438 deletions
|
|
@ -6,236 +6,349 @@ using Microsoft.Extensions.Hosting;
|
|||
|
||||
namespace Kavita.Common
|
||||
{
|
||||
public static class Configuration
|
||||
{
|
||||
private static readonly string AppSettingsFilename = GetAppSettingFilename();
|
||||
public static string Branch
|
||||
{
|
||||
get => GetBranch(GetAppSettingFilename());
|
||||
set => SetBranch(GetAppSettingFilename(), value);
|
||||
}
|
||||
public static class Configuration
|
||||
{
|
||||
private static readonly string AppSettingsFilename = Path.Join("config", GetAppSettingFilename());
|
||||
|
||||
public static int Port
|
||||
{
|
||||
get => GetPort(GetAppSettingFilename());
|
||||
set => SetPort(GetAppSettingFilename(), value);
|
||||
}
|
||||
public static string Branch
|
||||
{
|
||||
get => GetBranch(GetAppSettingFilename());
|
||||
set => SetBranch(GetAppSettingFilename(), value);
|
||||
}
|
||||
|
||||
public static string JwtToken
|
||||
{
|
||||
get => GetJwtToken(GetAppSettingFilename());
|
||||
set => SetJwtToken(GetAppSettingFilename(), value);
|
||||
}
|
||||
public static int Port
|
||||
{
|
||||
get => GetPort(GetAppSettingFilename());
|
||||
set => SetPort(GetAppSettingFilename(), value);
|
||||
}
|
||||
|
||||
public static string LogLevel
|
||||
{
|
||||
get => GetLogLevel(GetAppSettingFilename());
|
||||
set => SetLogLevel(GetAppSettingFilename(), value);
|
||||
}
|
||||
public static string JwtToken
|
||||
{
|
||||
get => GetJwtToken(GetAppSettingFilename());
|
||||
set => SetJwtToken(GetAppSettingFilename(), value);
|
||||
}
|
||||
|
||||
private static string GetAppSettingFilename()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(AppSettingsFilename))
|
||||
{
|
||||
return AppSettingsFilename;
|
||||
}
|
||||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
var isDevelopment = environment == Environments.Development;
|
||||
return "appsettings" + (isDevelopment ? ".Development" : "") + ".json";
|
||||
}
|
||||
public static string LogLevel
|
||||
{
|
||||
get => GetLogLevel(GetAppSettingFilename());
|
||||
set => SetLogLevel(GetAppSettingFilename(), value);
|
||||
}
|
||||
|
||||
#region JWT Token
|
||||
public static string LogPath
|
||||
{
|
||||
get => GetLoggingFile(GetAppSettingFilename());
|
||||
set => SetLoggingFile(GetAppSettingFilename(), value);
|
||||
}
|
||||
|
||||
private static string GetJwtToken(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "TokenKey";
|
||||
public static string DatabasePath
|
||||
{
|
||||
get => GetDatabasePath(GetAppSettingFilename());
|
||||
set => SetDatabasePath(GetAppSettingFilename(), value);
|
||||
}
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
private static string GetAppSettingFilename()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(AppSettingsFilename))
|
||||
{
|
||||
return tokenElement.GetString();
|
||||
return AppSettingsFilename;
|
||||
}
|
||||
|
||||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
var isDevelopment = environment == Environments.Development;
|
||||
return "appsettings" + (isDevelopment ? ".Development" : string.Empty) + ".json";
|
||||
}
|
||||
|
||||
#region JWT Token
|
||||
|
||||
private static string GetJwtToken(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "TokenKey";
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
{
|
||||
return tokenElement.GetString();
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error reading app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error reading app settings: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
private static void SetJwtToken(string filePath, string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentToken = GetJwtToken(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace("\"TokenKey\": \"" + currentToken, "\"TokenKey\": \"" + token);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow exception */
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetJwtToken(string filePath, string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentToken = GetJwtToken(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace("\"TokenKey\": \"" + currentToken, "\"TokenKey\": \"" + token);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow exception */
|
||||
}
|
||||
}
|
||||
public static bool CheckIfJwtTokenSet()
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetJwtToken(GetAppSettingFilename()) != "super secret unguessable key";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
public static bool CheckIfJwtTokenSet()
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetJwtToken(GetAppSettingFilename()) != "super secret unguessable key";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Port
|
||||
|
||||
#endregion
|
||||
private static void SetPort(string filePath, int port)
|
||||
{
|
||||
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
#region Port
|
||||
try
|
||||
{
|
||||
var currentPort = GetPort(filePath);
|
||||
var json = File.ReadAllText(filePath).Replace("\"Port\": " + currentPort, "\"Port\": " + port);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetPort(string filePath, int port)
|
||||
{
|
||||
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
|
||||
{
|
||||
return;
|
||||
}
|
||||
private static int GetPort(string filePath)
|
||||
{
|
||||
const int defaultPort = 5000;
|
||||
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
|
||||
{
|
||||
return defaultPort;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var currentPort = GetPort(filePath);
|
||||
var json = File.ReadAllText(filePath).Replace("\"Port\": " + currentPort, "\"Port\": " + port);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "Port";
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
{
|
||||
return tokenElement.GetInt32();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
private static int GetPort(string filePath)
|
||||
{
|
||||
const int defaultPort = 5000;
|
||||
if (new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker)
|
||||
{
|
||||
return defaultPort;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "Port";
|
||||
#endregion
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
#region LogLevel
|
||||
|
||||
private static void SetLogLevel(string filePath, string logLevel)
|
||||
{
|
||||
try
|
||||
{
|
||||
return tokenElement.GetInt32();
|
||||
var currentLevel = GetLogLevel(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace($"\"Default\": \"{currentLevel}\"", $"\"Default\": \"{logLevel}\"");
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return defaultPort;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LogLevel
|
||||
|
||||
private static void SetLogLevel(string filePath, string logLevel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentLevel = GetLogLevel(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace($"\"Default\": \"{currentLevel}\"", $"\"Default\": \"{logLevel}\"");
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetLogLevel(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
|
||||
if (jsonObj.TryGetProperty("Logging", out JsonElement tokenElement))
|
||||
catch (Exception)
|
||||
{
|
||||
foreach (var property in tokenElement.EnumerateObject())
|
||||
{
|
||||
if (!property.Name.Equals("LogLevel")) continue;
|
||||
foreach (var logProperty in property.Value.EnumerateObject())
|
||||
{
|
||||
if (logProperty.Name.Equals("Default"))
|
||||
{
|
||||
return logProperty.Value.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return "Information";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static string GetBranch(string filePath)
|
||||
{
|
||||
const string defaultBranch = "main";
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "Branch";
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
private static string GetLogLevel(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
return tokenElement.GetString();
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
|
||||
if (jsonObj.TryGetProperty("Logging", out JsonElement tokenElement))
|
||||
{
|
||||
foreach (var property in tokenElement.EnumerateObject())
|
||||
{
|
||||
if (!property.Name.Equals("LogLevel")) continue;
|
||||
foreach (var logProperty in property.Value.EnumerateObject())
|
||||
{
|
||||
if (logProperty.Name.Equals("Default"))
|
||||
{
|
||||
return logProperty.Value.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error reading app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return defaultBranch;
|
||||
}
|
||||
return "Information";
|
||||
}
|
||||
|
||||
private static void SetBranch(string filePath, string updatedBranch)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentBranch = GetBranch(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace("\"Branch\": " + currentBranch, "\"Branch\": " + updatedBranch);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private static string GetBranch(string filePath)
|
||||
{
|
||||
const string defaultBranch = "main";
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
const string key = "Branch";
|
||||
|
||||
if (jsonObj.TryGetProperty(key, out JsonElement tokenElement))
|
||||
{
|
||||
return tokenElement.GetString();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error reading app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return defaultBranch;
|
||||
}
|
||||
|
||||
private static void SetBranch(string filePath, string updatedBranch)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentBranch = GetBranch(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace("\"Branch\": " + currentBranch, "\"Branch\": " + updatedBranch);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetLoggingFile(string filePath)
|
||||
{
|
||||
const string defaultFile = "config/logs/kavita.log";
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
|
||||
if (jsonObj.TryGetProperty("Logging", out JsonElement tokenElement))
|
||||
{
|
||||
foreach (var property in tokenElement.EnumerateObject())
|
||||
{
|
||||
if (!property.Name.Equals("File")) continue;
|
||||
foreach (var logProperty in property.Value.EnumerateObject())
|
||||
{
|
||||
if (logProperty.Name.Equals("Path"))
|
||||
{
|
||||
return logProperty.Value.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return defaultFile;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This should NEVER be called except by <see cref="MigrateConfigFiles"/>
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="directory"></param>
|
||||
private static void SetLoggingFile(string filePath, string directory)
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentFile = GetLoggingFile(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace("\"Path\": \"" + currentFile + "\"", "\"Path\": \"" + directory + "\"");
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetDatabasePath(string filePath)
|
||||
{
|
||||
const string defaultFile = "config/kavita.db";
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
|
||||
|
||||
if (jsonObj.TryGetProperty("ConnectionStrings", out JsonElement tokenElement))
|
||||
{
|
||||
foreach (var property in tokenElement.EnumerateObject())
|
||||
{
|
||||
if (!property.Name.Equals("DefaultConnection")) continue;
|
||||
return property.Value.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error writing app settings: " + ex.Message);
|
||||
}
|
||||
|
||||
return defaultFile;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This should NEVER be called except by <see cref="MigrateConfigFiles"/>
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="updatedPath"></param>
|
||||
private static void SetDatabasePath(string filePath, string updatedPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var existingString = GetDatabasePath(filePath);
|
||||
var json = File.ReadAllText(filePath)
|
||||
.Replace(existingString,
|
||||
"Data source=" + updatedPath);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
/* Swallow Exception */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue