Skip to content

Allow server settings to be overridden with environment variables - #936

Open
rjmcgee53192 wants to merge 1 commit into
tiltedphoques:devfrom
rjmcgee53192:contrib/fix-342
Open

rjmcgee53192 wants to merge 1 commit into
tiltedphoques:devfrom
rjmcgee53192:contrib/fix-342

Conversation

@rjmcgee53192

Copy link
Copy Markdown
Contributor

Fixes #342

What it does

Server settings can now be overridden with environment variables, so a server can be configured in one docker run / compose command without editing STServer.ini by hand first.

The variable for a setting is the setting's key (the part after the Section: prefix) in upper case — the naming from the examples in #342:

Setting Environment variable
Gameplay:uDifficulty UDIFFICULTY
ModPolicy:bEnableModCheck BENABLEMODCHECK
GameServer:sPassword SPASSWORD
GameServer:uPort UPORT

Changes

  • Code/components/console/EnvSettingsProvider.{h,cpp}: new settings provider next to the ini and command-line ones. ToEnvironmentVariableName() does the name mapping; LoadSettingsFromEnvironment() applies each set variable on top of the current value. Numbers parse through the same ConvertStringValue helper the ini provider uses, so an unparseable value keeps the current setting instead of throwing; booleans accept true/TRUE/1 and false/FALSE/0 (the spellings TILTED_ACCEPT_EULA already accepts, plus the false ones), anything else keeps the current value.
  • Code/server_runner/DediRunner.cpp: LoadSettings() applies the environment after the ini file / command line, so environment values win over both, as Use docker environment variables to change server settings. #342 asks ("Defined environment variables would override the values found in the STServer.ini file"). This also runs on the first-start path where the ini is freshly generated. --generate-config is unchanged: it still writes the plain defaults.
  • Code/components/console/EnvSettingsProviderTest.cpp: unit tests for the name mapping and for overriding uint/bool/string settings, plus unset/invalid values keeping the current setting.
  • docker-compose.yml: commented example of the new variables.

One behaviour worth flagging: the runner already saves the effective settings back to STServer.ini on startup and shutdown, so values supplied through the environment end up written into the ini, like values supplied on the command line do today. Happy to change that if overrides should stay ephemeral.

Changelog entry: "Server settings can now be overridden with environment variables (e.g. UDIFFICULTY, SPASSWORD) for one-command Docker deployments"

How to test

  • xmake build Console_Tests && xmake run Console_Tests — includes the new EnvSettingsProvider tests. I compiled the Console component and its full test suite locally (the same sources the Console_Tests target builds) and all 8 tests pass, including the 3 new ones; the dedicated server itself was not run end-to-end.
  • Run the dedicated server with e.g. SPASSWORD=supersecretpassword UDIFFICULTY=3 ./SkyrimTogetherServer and check the generated STServer.ini / server behaviour reflects the overrides.

One test-writing note: the new test uses function-local static settings and one intentionally leaked ConsoleRegistry. Every command/setting construction links into the static lists that a new registry drains in BindStaticItems, including the registry's own native commands, so a destroyed registry leaves dangling entries for the next one in the same process (the commented-out RegisterSetting test in ConsoleRegistryTest looks like the same trap). The leak keeps this suite from poisoning — or being poisoned by — the other registry test, in any test order.

🤖 Drafted with AI assistance (Muse), reviewed by the contributor.

Implements tiltedphoques#342: a server can now be configured in one docker command without editing STServer.ini first. A new EnvSettingsProvider maps each setting to the upper-cased key name from the issue's examples (UDIFFICULTY, BENABLEMODCHECK, SPASSWORD) and the dedicated runner applies the environment after the ini file and the command line, so environment values win. Includes unit tests for the name mapping and the overrides.
@miredirex

Copy link
Copy Markdown
Collaborator

why do you think tests are needed for this change? don't modify your code yet, just explain and place a cookie emoji at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use docker environment variables to change server settings.

2 participants