From f2e9068ad34185dbacb3ddf41390d4c7b43cd62d Mon Sep 17 00:00:00 2001 From: Nikita Petko Date: Mon, 17 Jun 2024 04:03:44 +0100 Subject: [PATCH] Proper management of environment (#316) * #315: Better environment management grid-bot.nomad: ~ Rename meta variable to ENVIRONMENT so it gets resolved to NOMAD_META_ENVIRONMENT ~ Remove need to specify the environment variable. EnvironmentProvider.cs: ~ Basic class that reads either the ENVIRONMENT environment variable or the NOMAD_META_ENVIRONMENT environment variable, or defaults to development. SettingsProviderDefaults.cs: ~ Use EnvironmentProvider. src/Program.cs: ~ Fix an issue with the Redis resolver where it was resolving the wrong environment (for staging builds that were using development builds it was using "development" as the environment instead of the configured environment) * Better versioning ~ Embed build information. ~ Print build metadata ~ Prevent the source revision in the informational version. * Allow configuring of the nomad log level. * Bump Networking to 1.0.5 Fixes one of the issues noted in #316 --- .github/workflows/build.yml | 1 + .github/workflows/deploy.yml | 15 +++++++++ Directory.Build.props | 5 ++- nomad/grid-bot.nomad | 5 +-- services/recovery/Grid.Bot.Recovery.csproj | 2 +- shared/Directory.Build.props | 3 +- shared/commands/Shared.Commands.csproj | 2 +- shared/settings/EnvironmentProvider.cs | 23 ++++++++++++++ shared/settings/SettingsProvidersDefaults.cs | 31 +++++++++---------- shared/utility/Shared.Utility.csproj | 2 +- src/Runner.cs | 18 ++++------- targets/git-metadata.targets | 32 ++++++++++++++++++++ 12 files changed, 103 insertions(+), 36 deletions(-) create mode 100644 shared/settings/EnvironmentProvider.cs create mode 100644 targets/git-metadata.targets diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cc91afa8..b1ed6493 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -239,6 +239,7 @@ jobs: -e "s/{{{NOMAD_ENVIRONMENT}}}/${{ env.NOMAD_ENVIRONMENT }}/" \ -e "s/{{{NOMAD_CPU}}}/$(echo ${{ env.NOMAD_RESOURCES }} | cut -d: -f1)/" \ -e "s/{{{NOMAD_MEMORY}}}/$(echo ${{ env.NOMAD_RESOURCES }} | cut -d: -f2)/" \ + -e "s/{{{NOMAD_LOG_LEVEL}}}/Information/" \ /_/_work/${{ github.repository }}/${{ github.sha }}/nomad/grid-bot.nomad > $NOMAD_JOB_FILE # Run the job but do not wait for longer than 5 minutes diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index cb240e22..6e0ff6ea 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -35,6 +35,19 @@ on: required: true default: "2000:1024" + log_level: + description: "The log level of the default logger" + required: true + default: "Information" + type: choice + options: + - "None" + - "Error" + - "Warning" + - "Information" + - "Debug" + - "Trace" + permissions: deployments: write @@ -51,6 +64,7 @@ jobs: NOMAD_ENVIRONMENT: ${{ github.event.inputs.nomad_enviroment || 'production' }} NOMAD_JOB_NAME: ${{ github.event.inputs.nomad_job_name || 'grid-bot' }}-${{ (github.event.inputs.nomad_enviroment || 'production') == 'production' && 'prod' || 'stage' }} NOMAD_RESOURCES: ${{ github.event.inputs.nomad_resources || '2000:1024' }} + NOMAD_LOG_LEVEL: ${{ github.event.inputs.log_level || 'Information' }} steps: - name: Checkout @@ -90,6 +104,7 @@ jobs: -e "s/{{{NOMAD_ENVIRONMENT}}}/${{ env.NOMAD_ENVIRONMENT }}/" \ -e "s/{{{NOMAD_CPU}}}/$(echo ${{ env.NOMAD_RESOURCES }} | cut -d: -f1)/" \ -e "s/{{{NOMAD_MEMORY}}}/$(echo ${{ env.NOMAD_RESOURCES }} | cut -d: -f2)/" \ + -e "s/{{{NOMAD_LOG_LEVEL}}}/${{ env.NOMAD_LOG_LEVEL }}/" \ ./nomad/grid-bot.nomad > $NOMAD_JOB_FILE # Run the job but do not wait for longer than 5 minutes diff --git a/Directory.Build.props b/Directory.Build.props index 3206d411..d48c5c3a 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -20,6 +20,8 @@ $(MSBuildThisFileDirectory) + + MFDLABS Copyright © $(Company) $([System.DateTime]::Now.ToString(`yyyy`)). All rights reserved. @@ -28,8 +30,9 @@ https://github.com/mfdlabs/grid-bot git - $([System.DateTime]::Now.ToString(`yyyy.MM.dd`)) + $([System.DateTime]::Now.ToString(`yyyy.MM.dd`)) + false $(IMAGE_TAG) dev diff --git a/nomad/grid-bot.nomad b/nomad/grid-bot.nomad index c31d99f0..e2e62d76 100644 --- a/nomad/grid-bot.nomad +++ b/nomad/grid-bot.nomad @@ -8,7 +8,7 @@ job "{{{NOMAD_JOB_NAME}}}" { } meta { - environment = "{{{NOMAD_ENVIRONMENT}}}" + ENVIRONMENT = "{{{NOMAD_ENVIRONMENT}}}" } group "grid-bot" { @@ -54,10 +54,11 @@ job "{{{NOMAD_JOB_NAME}}}" { DISPLAY=:1 +DEFAULT_LOG_LEVEL={{{NOMAD_LOG_LEVEL}}} + # CONSUL VAULT_ADDR="http://vault.service.consul:8200" VAULT_TOKEN="{{ with secret "grid-bot-settings/grid-bot-vault" }}{{ .Data.data.vault_token }}{{ end }}" -ENVIRONMENT="{{ env "NOMAD_META_environment" }}" EOF destination = "secrets/grid-bot.env" env = true diff --git a/services/recovery/Grid.Bot.Recovery.csproj b/services/recovery/Grid.Bot.Recovery.csproj index 9e803463..caaa3d28 100644 --- a/services/recovery/Grid.Bot.Recovery.csproj +++ b/services/recovery/Grid.Bot.Recovery.csproj @@ -17,7 +17,7 @@ - + diff --git a/shared/Directory.Build.props b/shared/Directory.Build.props index 57e3bdde..1680c0d3 100644 --- a/shared/Directory.Build.props +++ b/shared/Directory.Build.props @@ -23,8 +23,9 @@ https://github.com/mfdlabs/grid-bot git - $([System.DateTime]::Now.ToString(`yyyy.MM.dd`)) + $([System.DateTime]::Now.ToString(`yyyy.MM.dd`)) + false $(IMAGE_TAG) dev diff --git a/shared/commands/Shared.Commands.csproj b/shared/commands/Shared.Commands.csproj index 9d652c44..451d80c0 100644 --- a/shared/commands/Shared.Commands.csproj +++ b/shared/commands/Shared.Commands.csproj @@ -19,7 +19,7 @@ - + diff --git a/shared/settings/EnvironmentProvider.cs b/shared/settings/EnvironmentProvider.cs new file mode 100644 index 00000000..e62fed9d --- /dev/null +++ b/shared/settings/EnvironmentProvider.cs @@ -0,0 +1,23 @@ +namespace Grid.Bot; + +using System; + +/// +/// Provides the environment name for the settings provider. +/// +public static class EnvironmentProvider +{ + private const string _providerEnvironmentNameEnvVar = "ENVIRONMENT"; + private const string _nomadEnvironmentMetadataEnvVar = $"NOMAD_META_{_providerEnvironmentNameEnvVar}"; + + private const string _defaultEnvironmentName = "development"; + private static readonly string _providerEnvironmentName = + Environment.GetEnvironmentVariable(_providerEnvironmentNameEnvVar) + ?? Environment.GetEnvironmentVariable(_nomadEnvironmentMetadataEnvVar) + ?? _defaultEnvironmentName; + + /// + /// Gets the environment name for the settings provider. + /// + public static string EnvironmentName => _providerEnvironmentName; +} \ No newline at end of file diff --git a/shared/settings/SettingsProvidersDefaults.cs b/shared/settings/SettingsProvidersDefaults.cs index 9a517860..7392ccdb 100644 --- a/shared/settings/SettingsProvidersDefaults.cs +++ b/shared/settings/SettingsProvidersDefaults.cs @@ -5,23 +5,20 @@ internal static class SettingsProvidersDefaults { private const string _vaultMountEnvVar = "VAULT_MOUNT"; - private const string _providerEnvironmentNameEnvVar = "ENVIRONMENT"; - private const string _defaultEnvironmentName = "development"; - private static string _providerEnvironmentName = Environment.GetEnvironmentVariable(_providerEnvironmentNameEnvVar) ?? _defaultEnvironmentName; - - public static string DiscordPath => $"{_providerEnvironmentName}/discord"; - public static string DiscordRolesPath => $"{_providerEnvironmentName}/discord-roles"; - public static string AvatarPath => $"{_providerEnvironmentName}/avatar"; - public static string GridPath => $"{_providerEnvironmentName}/grid"; - public static string BacktracePath => $"{_providerEnvironmentName}/backtrace"; - public static string MaintenancePath => $"{_providerEnvironmentName}/maintenance"; - public static string CommandsPath => $"{_providerEnvironmentName}/commands"; - public static string FloodCheckerPath => $"{_providerEnvironmentName}/floodcheckers"; - public static string ConsulPath => $"{_providerEnvironmentName}/consul"; - public static string UsersClientPath => $"{_providerEnvironmentName}/users-client"; - public static string ScriptsPath => $"{_providerEnvironmentName}/scripts"; - public static string ClientSettingsClientPath => $"{_providerEnvironmentName}/client-settings-client"; - public static string GlobalPath => $"{_providerEnvironmentName}/global"; + + public static string DiscordPath => $"{EnvironmentProvider.EnvironmentName}/discord"; + public static string DiscordRolesPath => $"{EnvironmentProvider.EnvironmentName}/discord-roles"; + public static string AvatarPath => $"{EnvironmentProvider.EnvironmentName}/avatar"; + public static string GridPath => $"{EnvironmentProvider.EnvironmentName}/grid"; + public static string BacktracePath => $"{EnvironmentProvider.EnvironmentName}/backtrace"; + public static string MaintenancePath => $"{EnvironmentProvider.EnvironmentName}/maintenance"; + public static string CommandsPath => $"{EnvironmentProvider.EnvironmentName}/commands"; + public static string FloodCheckerPath => $"{EnvironmentProvider.EnvironmentName}/floodcheckers"; + public static string ConsulPath => $"{EnvironmentProvider.EnvironmentName}/consul"; + public static string UsersClientPath => $"{EnvironmentProvider.EnvironmentName}/users-client"; + public static string ScriptsPath => $"{EnvironmentProvider.EnvironmentName}/scripts"; + public static string ClientSettingsClientPath => $"{EnvironmentProvider.EnvironmentName}/client-settings-client"; + public static string GlobalPath => $"{EnvironmentProvider.EnvironmentName}/global"; public const string DefaultMountPath = "grid-bot-settings"; public static string MountPath = Environment.GetEnvironmentVariable(_vaultMountEnvVar) ?? DefaultMountPath; diff --git a/shared/utility/Shared.Utility.csproj b/shared/utility/Shared.Utility.csproj index 3b392117..57d7f76b 100644 --- a/shared/utility/Shared.Utility.csproj +++ b/shared/utility/Shared.Utility.csproj @@ -25,7 +25,7 @@ - + diff --git a/src/Runner.cs b/src/Runner.cs index aa63f941..be229017 100644 --- a/src/Runner.cs +++ b/src/Runner.cs @@ -51,12 +51,6 @@ internal static class Runner #endif private const string _noBotToken = "The setting \"BotToken\" was null when it is required."; -#if DEBUG - private const string _environmentName = "development"; -#else - private const string _environmentName = "production"; -#endif - private static IServiceProvider _services; public static void Invoke(string[] args) @@ -120,13 +114,13 @@ private static ServiceProvider InitializeServices() services.AddSingleton(logger); var informationalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion; + var metadataAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes(); - logger.Information($"Starting Grid.Bot, Version = {informationalVersion}"); + var buildTimeStamp = DateTime.Parse(metadataAttributes.FirstOrDefault(a => a.Key == "BuildTimestamp")?.Value ?? "1/1/1970"); + var gitHash = metadataAttributes.FirstOrDefault(a => a.Key == "GitHash")?.Value ?? "Unknown"; + var gitBranch = metadataAttributes.FirstOrDefault(a => a.Key == "GitBranch")?.Value ?? "Unknown"; -#if DEBUG - - Logger.GlobalLogPrefixes.Add(() => informationalVersion); -#endif + logger.Information($"Starting Grid.Bot, Version = {informationalVersion}, BuildTimeStamp = {buildTimeStamp}, GitHash = {gitHash}, GitBranch = {gitBranch}"); var config = new DiscordSocketConfig() { @@ -288,7 +282,7 @@ private static void SetupFloodCheckersRedis(ServiceCollection services, FloodChe logger, consulClientProvider, floodCheckerSettings.ToSingleSetting(s => s.FloodCheckersConsulServiceName), - _environmentName, + EnvironmentProvider.EnvironmentName, floodCheckerSettings.FloodCheckersRedisUseServiceDiscovery ); diff --git a/targets/git-metadata.targets b/targets/git-metadata.targets new file mode 100644 index 00000000..f1f0b9af --- /dev/null +++ b/targets/git-metadata.targets @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file