From cc6128997ebcedea712a439c80e0df3b8689ada8 Mon Sep 17 00:00:00 2001 From: Veovis Date: Wed, 27 Jul 2022 05:00:27 +0000 Subject: [PATCH] NEW: add NadekoBot__creds env to specify alternative creds.yml --- Dockerfile | 1 + docker-entrypoint.sh | 10 ++++- src/NadekoBot/Bot.cs | 4 +- src/NadekoBot/Program.cs | 6 +-- .../Services/Impl/BotCredsProvider.cs | 38 ++++++++++++------- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index e1b00d8f5..00b43a920 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,6 +39,7 @@ COPY docker-entrypoint.sh /usr/local/sbin ENV shard_id=0 ENV total_shards=1 +ENV NadekoBot__creds=/app/data/creds.yml VOLUME [ "/app/data" ] ENTRYPOINT [ "/usr/local/sbin/docker-entrypoint.sh" ] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 3ea89cd1d..3f37d4dd1 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -13,7 +13,15 @@ do fi done -# fix folder permissions +# creds.yml migration +if [ -f /app/creds.yml ]; then + echo "Default location for creds.yml is now /app/data/creds.yml." + echo "Please move your creds.yml and update your docker-compose.yml accordingly." + + export Nadeko_creds=/app/creds.yml +fi + +# ensure nadeko can write on /app/data chown -R nadeko:nadeko "$data" # drop to regular user and launch command diff --git a/src/NadekoBot/Bot.cs b/src/NadekoBot/Bot.cs index 5a723b516..a2d386d46 100644 --- a/src/NadekoBot/Bot.cs +++ b/src/NadekoBot/Bot.cs @@ -34,13 +34,13 @@ public sealed class Bot private readonly IBotCredsProvider _credsProvider; // private readonly InteractionService _interactionService; - public Bot(int shardId, int? totalShards) + public Bot(int shardId, int? totalShards, string credPath = null) { if (shardId < 0) throw new ArgumentOutOfRangeException(nameof(shardId)); ShardId = shardId; - _credsProvider = new BotCredsProvider(totalShards); + _credsProvider = new BotCredsProvider(totalShards, credPath); _creds = _credsProvider.GetCreds(); _db = new(_credsProvider); diff --git a/src/NadekoBot/Program.cs b/src/NadekoBot/Program.cs index dc89a4a8f..ee3e55b4b 100644 --- a/src/NadekoBot/Program.cs +++ b/src/NadekoBot/Program.cs @@ -23,6 +23,6 @@ if (args.Length > 0 && args[0] != "run") } LogSetup.SetupLogger(shardId); -Log.Information("Pid: {ProcessId}", pid); - -await new Bot(shardId, totalShards).RunAndBlockAsync(); \ No newline at end of file +Log.Information("Pid: {ProcessId}", pid); + +await new Bot(shardId, totalShards, Environment.GetEnvironmentVariable("NadekoBot__creds")).RunAndBlockAsync(); \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/BotCredsProvider.cs b/src/NadekoBot/Services/Impl/BotCredsProvider.cs index 73fb19051..2243666ec 100644 --- a/src/NadekoBot/Services/Impl/BotCredsProvider.cs +++ b/src/NadekoBot/Services/Impl/BotCredsProvider.cs @@ -18,11 +18,9 @@ public sealed class BotCredsProvider : IBotCredsProvider private const string CREDS_FILE_NAME = "creds.yml"; private const string CREDS_EXAMPLE_FILE_NAME = "creds_example.yml"; - private string CredsPath - => Path.Combine(Directory.GetCurrentDirectory(), CREDS_FILE_NAME); + private string CredsPath { get; } - private string CredsExamplePath - => Path.Combine(Directory.GetCurrentDirectory(), CREDS_EXAMPLE_FILE_NAME); + private string CredsExamplePath { get; } private readonly int? _totalShards; @@ -34,9 +32,21 @@ public sealed class BotCredsProvider : IBotCredsProvider private readonly object _reloadLock = new(); private readonly IDisposable _changeToken; - public BotCredsProvider(int? totalShards = null) + public BotCredsProvider(int? totalShards = null, string credPath = null) { - _totalShards = totalShards; + _totalShards = totalShards; + + if (!string.IsNullOrWhiteSpace(credPath)) + { + CredsPath = credPath; + CredsExamplePath = Path.Combine(Path.GetDirectoryName(credPath), CREDS_EXAMPLE_FILE_NAME); + } + else + { + CredsPath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_FILE_NAME); + CredsExamplePath = Path.Combine(Directory.GetCurrentDirectory(), CREDS_EXAMPLE_FILE_NAME); + } + try { if (!File.Exists(CredsExamplePath)) @@ -59,8 +69,8 @@ public sealed class BotCredsProvider : IBotCredsProvider _config = new ConfigurationBuilder().AddYamlFile(CredsPath, false, true) .AddEnvironmentVariables("NadekoBot_") - .Build(); - + .Build(); + _changeToken = ChangeToken.OnChange(() => _config.GetReloadToken(), Reload); Reload(); } @@ -121,14 +131,14 @@ public sealed class BotCredsProvider : IBotCredsProvider ymlData = Yaml.Serializer.Serialize(creds); File.WriteAllText(CREDS_FILE_NAME, ymlData); - } - + } + private string OldCredsJsonPath => Path.Combine(Directory.GetCurrentDirectory(), "credentials.json"); private string OldCredsJsonBackupPath - => Path.Combine(Directory.GetCurrentDirectory(), "credentials.json.bak"); - + => Path.Combine(Directory.GetCurrentDirectory(), "credentials.json.bak"); + private void MigrateCredentials() { if (File.Exists(OldCredsJsonPath)) @@ -167,8 +177,8 @@ public sealed class BotCredsProvider : IBotCredsProvider Log.Warning( "Data from credentials.json has been moved to creds.yml\nPlease inspect your creds.yml for correctness"); - } - + } + if (File.Exists(CREDS_FILE_NAME)) { var creds = Yaml.Deserializer.Deserialize(File.ReadAllText(CREDS_FILE_NAME));