Skip to content

Commit

Permalink
Proper management of environment (#316)
Browse files Browse the repository at this point in the history
* #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
  • Loading branch information
jf-06 authored Jun 17, 2024
1 parent a73eac9 commit f2e9068
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 36 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<RootDirectory Condition="'$(SolutionDir)' == ''">$(MSBuildThisFileDirectory)</RootDirectory>
</PropertyGroup>

<Import Project="$(RootDirectory)targets/git-metadata.targets" Condition="Exists('$(RootDirectory)targets/git-metadata.targets')" />

<PropertyGroup Label="PackageMetadata">
<Company>MFDLABS</Company>
<Copyright>Copyright © $(Company) $([System.DateTime]::Now.ToString(`yyyy`)). All rights reserved.</Copyright>
Expand All @@ -28,8 +30,9 @@
<RepositoryUrl>https://github.com/mfdlabs/grid-bot</RepositoryUrl>
<RepositoryType>git</RepositoryType>

<VersionPrefix>$([System.DateTime]::Now.ToString(`yyyy.MM.dd`))</VersionPrefix>
<Version>$([System.DateTime]::Now.ToString(`yyyy.MM.dd`))</Version>

<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<InformationalVersion Condition="'$(CI)' == 'true'">$(IMAGE_TAG)</InformationalVersion>
<InformationalVersion Condition="'$(CI)' != 'true'">dev</InformationalVersion>
</PropertyGroup>
Expand Down
5 changes: 3 additions & 2 deletions nomad/grid-bot.nomad
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ job "{{{NOMAD_JOB_NAME}}}" {
}

meta {
environment = "{{{NOMAD_ENVIRONMENT}}}"
ENVIRONMENT = "{{{NOMAD_ENVIRONMENT}}}"
}

group "grid-bot" {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion services/recovery/Grid.Bot.Recovery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup Label="PrivatePackages" Condition=" '$(LocalBuild)' != 'true' ">
<PackageReference Include="Configuration" Version="1.0.5" />
<PackageReference Include="Logging" Version="1.0.4" />
<PackageReference Include="Networking" Version="1.0.4" />
<PackageReference Include="Networking" Version="1.0.5" />
</ItemGroup>

<ItemGroup Label="PrivatePackages" Condition=" '$(LocalBuild)' == 'true' ">
Expand Down
3 changes: 2 additions & 1 deletion shared/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
<RepositoryUrl>https://github.com/mfdlabs/grid-bot</RepositoryUrl>
<RepositoryType>git</RepositoryType>

<VersionPrefix>$([System.DateTime]::Now.ToString(`yyyy.MM.dd`))</VersionPrefix>
<Version>$([System.DateTime]::Now.ToString(`yyyy.MM.dd`))</Version>

<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<InformationalVersion Condition="'$(CI)' == 'true'">$(IMAGE_TAG)</InformationalVersion>
<InformationalVersion Condition="'$(CI)' != 'true'">dev</InformationalVersion>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion shared/commands/Shared.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="FloodCheckers.Core" Version="1.0.4" />
<PackageReference Include="FloodCheckers.Redis" Version="1.0.4" />
<PackageReference Include="Text" Version="1.0.4" />
<PackageReference Include="Networking" Version="1.0.4" />
<PackageReference Include="Networking" Version="1.0.5" />
</ItemGroup>

<ItemGroup Label="PrivatePackages" Condition=" '$(LocalBuild)' == 'true' ">
Expand Down
23 changes: 23 additions & 0 deletions shared/settings/EnvironmentProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Grid.Bot;

using System;

/// <summary>
/// Provides the environment name for the settings provider.
/// </summary>
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;

/// <summary>
/// Gets the environment name for the settings provider.
/// </summary>
public static string EnvironmentName => _providerEnvironmentName;
}
31 changes: 14 additions & 17 deletions shared/settings/SettingsProvidersDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion shared/utility/Shared.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReference Include="Users.Client" Version="1.0.4" />
<PackageReference Include="Grid.ProcessManagement.Docker" Version="1.0.4" />
<PackageReference Include="Grid.ProcessManagement.Core" Version="1.0.4" />
<PackageReference Include="Networking" Version="1.0.4" />
<PackageReference Include="Networking" Version="1.0.5" />
<PackageReference Include="Thumbnails.Client" Version="1.0.4" />
</ItemGroup>

Expand Down
18 changes: 6 additions & 12 deletions src/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -120,13 +114,13 @@ private static ServiceProvider InitializeServices()
services.AddSingleton<ILogger>(logger);

var informationalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
var metadataAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyMetadataAttribute>();

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()
{
Expand Down Expand Up @@ -288,7 +282,7 @@ private static void SetupFloodCheckersRedis(ServiceCollection services, FloodChe
logger,
consulClientProvider,
floodCheckerSettings.ToSingleSetting(s => s.FloodCheckersConsulServiceName),
_environmentName,
EnvironmentProvider.EnvironmentName,
floodCheckerSettings.FloodCheckersRedisUseServiceDiscovery
);

Expand Down
32 changes: 32 additions & 0 deletions targets/git-metadata.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project>
<Target Name="AddGitMetadaAssemblyAttributes"
BeforeTargets="GetAssemblyAttributes">

<!--Executes the Git Commands to get the Hash and Branch-->
<Exec Command="git rev-parse HEAD" ConsoleToMSBuild="true"
StandardOutputImportance="low" IgnoreExitCode="true" Condition=" '$(CommitHash)' == '' "
WorkingDirectory="$(RootDirectory)">
<Output TaskParameter="ConsoleOutput" PropertyName="CommitHash" />
</Exec>
<Exec Command="git rev-parse --short HEAD" ConsoleToMSBuild="true"
StandardOutputImportance="low" IgnoreExitCode="true"
Condition=" '$(CommitShortHash)' == '' " WorkingDirectory="$(RootDirectory)">
<Output TaskParameter="ConsoleOutput" PropertyName="CommitShortHash" />
</Exec>
<Exec Command="git rev-parse --abbrev-ref HEAD" ConsoleToMSBuild="true"
StandardOutputImportance="low" IgnoreExitCode="true"
Condition=" '$(CommitBranch)' == '' " WorkingDirectory="$(RootDirectory)">
<Output TaskParameter="ConsoleOutput" PropertyName="CommitBranch" />
</Exec>

<ItemGroup>
<AssemblyMetadata Include="BuildTimestamp"
Value="$([System.DateTime]::UtcNow.ToString(yyyy-MM-ddTHH:mm:ssK))" />
<AssemblyMetadata Condition=" $(CommitHash) != '' " Include="GitHash"
Value="$(CommitHash)" />
<AssemblyMetadata Condition=" $(CommitBranch) != '' " Include="GitBranch"
Value="$(CommitBranch)" />
</ItemGroup>
</Target>

</Project>

0 comments on commit f2e9068

Please sign in to comment.