diff --git a/factorio.yaml b/factorio.yaml index bfd3f05..22841fc 100644 --- a/factorio.yaml +++ b/factorio.yaml @@ -17,7 +17,7 @@ spec: metadata: labels: *id001 annotations: - factorio.brunner.ninja/config-revision: {{ printf "%s%s%s%s%s%s" (.Files.Get "scripts/start.sh") (.Files.Get "scripts/health.sh") (.Files.Get "scripts/collect.sh") (.Files.Get "scripts/collector-health.sh") (.Files.Get "server-settings.json") (.Files.Get "server-adminlist.json") | sha256sum }} + factorio.brunner.ninja/config-revision: {{ printf "%s%s%s%s%s%s%s" (.Files.Get "scripts/start.sh") (.Files.Get "scripts/disable-recycler.sh") (.Files.Get "scripts/health.sh") (.Files.Get "scripts/collect.sh") (.Files.Get "scripts/collector-health.sh") (.Files.Get "server-settings.json") (.Files.Get "server-adminlist.json") | sha256sum }} spec: automountServiceAccountToken: false terminationGracePeriodSeconds: 180 diff --git a/scripts/disable-recycler.sh b/scripts/disable-recycler.sh new file mode 100644 index 0000000..5be9d1e --- /dev/null +++ b/scripts/disable-recycler.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -euo pipefail + +mod_list=${1:-/factorio/mods/mod-list.json} +mkdir -p "$(dirname "$mod_list")" + +if [[ ! -s "$mod_list" ]]; then + printf '{"mods":[{"name":"base","enabled":true}]}\n' > "$mod_list" +fi + +tmp_file="${mod_list}.tmp" +jq ' + if (.mods | map(.name) | index("recycler")) then + .mods |= map(if .name == "recycler" then .enabled = false else . end) + else + .mods += [{"name":"recycler","enabled":false}] + end +' "$mod_list" > "$tmp_file" +mv "$tmp_file" "$mod_list" diff --git a/scripts/start.sh b/scripts/start.sh index 0a1044c..61f8bfb 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -13,6 +13,9 @@ cp /settings/server-settings.json /factorio/config/server-settings.json cp /settings/server-adminlist.json /factorio/config/server-adminlist.json cp /credentials/rcon-password /factorio/config/rconpw chmod 600 /factorio/config/rconpw +# Recycler is shipped with Factorio 2.1 and is auto-enabled if omitted from +# mod-list.json. This vanilla server does not use the Space Age expansion. +/scripts/disable-recycler.sh /factorio/mods/mod-list.json # Upstream uses bash xtrace, which would otherwise log the RCON password. exec 3>/dev/null export BASH_XTRACEFD=3 diff --git a/templates/configmaps.yaml b/templates/configmaps.yaml index 686f261..e6ca3ad 100644 --- a/templates/configmaps.yaml +++ b/templates/configmaps.yaml @@ -8,6 +8,8 @@ metadata: data: start.sh: | {{ .Files.Get "scripts/start.sh" | indent 4 }} + disable-recycler.sh: | +{{ .Files.Get "scripts/disable-recycler.sh" | indent 4 }} health.sh: | {{ .Files.Get "scripts/health.sh" | indent 4 }} collect.sh: | diff --git a/tests/test_mod_list.py b/tests/test_mod_list.py new file mode 100644 index 0000000..262bc16 --- /dev/null +++ b/tests/test_mod_list.py @@ -0,0 +1,40 @@ +import json +import subprocess +import tempfile +import unittest +from pathlib import Path + + +SCRIPT = Path(__file__).resolve().parents[1] / "scripts/disable-recycler.sh" + + +class ModListTests(unittest.TestCase): + def test_disables_recycler_without_changing_other_mods(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "mod-list.json" + path.write_text(json.dumps({"mods": [ + {"name": "base", "enabled": True}, + {"name": "recycler", "enabled": True}, + {"name": "my-mod", "enabled": True}, + ]})) + + subprocess.run(["bash", str(SCRIPT), str(path)], check=True) + mods = {mod["name"]: mod["enabled"] for mod in json.loads(path.read_text())["mods"]} + + self.assertFalse(mods["recycler"]) + self.assertTrue(mods["base"]) + self.assertTrue(mods["my-mod"]) + + def test_adds_recycler_disabled_to_new_mod_list(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "mods" / "mod-list.json" + + subprocess.run(["bash", str(SCRIPT), str(path)], check=True) + mods = {mod["name"]: mod["enabled"] for mod in json.loads(path.read_text())["mods"]} + + self.assertTrue(mods["base"]) + self.assertFalse(mods["recycler"]) + + +if __name__ == "__main__": + unittest.main()