Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import signal
import subprocess
import sys
import threading
import time
import types
import urllib.error
Expand Down Expand Up @@ -3352,6 +3353,7 @@ def _load_config():

Priority: env vars > beatscheck.conf > defaults.
"""
_snapshot_docker_env()
if len(sys.argv) == 4:
input_folder = sys.argv[1].rstrip("/")
output_folder = sys.argv[2].rstrip("/")
Expand All @@ -3362,7 +3364,6 @@ def _load_config():
# Write default config template on first run, then load values.
# Config file values fill in for any env vars not already set.
_write_default_config(log_dir)
_snapshot_docker_env()
_apply_config_file(log_dir)
input_folder = os.environ.get("MUSIC_DIR", "/data").rstrip("/")
output_folder = os.environ.get("OUTPUT_DIR", "/data/corrupted").rstrip("/")
Expand Down Expand Up @@ -3403,28 +3404,46 @@ def _load_config():
)


_config_sync_lock = threading.Lock()


def _sync_env_from_config_file(config_dir):
"""Copy beatscheck.conf into os.environ except Docker-set vars; returns keys Docker overrides, None if no readable file."""
# One at a time, or an older read of the file can be applied after a newer one.
with _config_sync_lock:
return _copy_config_file_to_env(os.path.join(config_dir, "beatscheck.conf"))


def _copy_config_file_to_env(path):
"""_sync_env_from_config_file's work, run under _config_sync_lock."""
if not os.path.isfile(path):
return None
try:
file_vals = _parse_config_lines(path)
except OSError:
return None
overridden = []
for key, value in file_vals.items():
env_name = _CONFIG_KEY_MAP.get(key)
if not env_name:
continue
if env_name not in _docker_env_vars:
os.environ[env_name] = value
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
elif os.environ.get(env_name) != value:
overridden.append(key)
return overridden


def _reload_config(cfg):
"""Re-read beatscheck.conf and update cfg for the next scan.

Docker env vars (set before startup) always win. Config file values
update the os.environ entries that _apply_config_file originally set,
then we refresh the cfg namespace from os.environ.
"""
path = os.path.join(cfg.log_dir, "beatscheck.conf")
try:
file_vals = {}
for key, value in _parse_config_lines(path).items():
env_name = _CONFIG_KEY_MAP.get(key)
if env_name:
file_vals[env_name] = value
except OSError:
if _sync_env_from_config_file(cfg.log_dir) is None:
return

# Update env vars from config file (skip Docker-set vars)
for env_name, value in file_vals.items():
if env_name not in _docker_env_vars:
os.environ[env_name] = value

# Refresh cfg attributes from env vars
cfg.input_folder = os.environ.get(
"MUSIC_DIR", cfg.input_folder).rstrip("/")
Expand Down
7 changes: 6 additions & 1 deletion app/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,12 @@ async function saveConfig(e) {
if (res && res.ok) {
status.textContent = 'Saved!';
status.className = 'form-status';
showToast('Configuration saved', 'success');
const dockerSet = Array.isArray(res.docker_set) ? res.docker_set : [];
if (dockerSet.length) {
showToast('Configuration saved. Set by a Docker variable, so not applied: ' + dockerSet.join(', '), 'warning');
} else {
showToast('Configuration saved', 'success');
}
configSnapshot = buildConfigSnapshot();
updateUnsavedIndicator();
} else {
Expand Down
17 changes: 9 additions & 8 deletions app/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ def _login_record_success(key):
# allowed-keys list and parser stay in sync without duplication.
from main import ( # noqa: E402
_CONFIG_KEY_MAP, _acquire_scan_lock, _load_corrupt_details,
_parse_config_lines, _release_scan_lock, write_corrupt_list,
write_json_atomic, write_text_atomic,
_parse_config_lines, _release_scan_lock, _sync_env_from_config_file,
write_corrupt_list, write_json_atomic, write_text_atomic,
)
import reason_text # noqa: E402
_ALLOWED_CONFIG_KEYS = frozenset(_CONFIG_KEY_MAP)
Expand Down Expand Up @@ -1223,12 +1223,13 @@ def do_POST(self):
del updates['lidarr_api_key']
ok = _write_config_file(config_dir, updates)
if ok:
# Sync updated values to os.environ so other
# operations (e.g. delete) see them immediately.
for key, val in updates.items():
env_name = key.upper()
os.environ[env_name] = str(val)
self._json_response({"ok": True})
# Deletes read os.environ, so apply the save now; Docker-set vars still win.
overridden = _sync_env_from_config_file(config_dir)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if overridden is None:
self._json_response(
{"error": "config saved, but it could not be read back to apply"}, 500)
else:
self._json_response({"ok": True, "docker_set": [k for k in overridden if k in updates]})
else:
self._json_response(
{"error": "config file not found"}, 500)
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ On first run, BeatsCheck creates `/config/beatscheck.conf` with all options and
| `webui` | `false` | Enable the built-in [web interface](webui.md). Requires a published port |
| `webui_port` | `8484` | Port for the web interface |

Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set. A value saved in the WebUI overrides the environment variable until the container restarts.
Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set, including values saved in the WebUI. A WebUI save that a variable overrides says so, and the saved value applies once you remove the variable and recreate the container.

## Container environment variables

Expand Down
116 changes: 116 additions & 0 deletions tests/test_config_save_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""A WebUI settings save applies at once, but a Docker-set variable still wins and the reply names it."""

import json
import os
import sys
import threading
import urllib.error
import urllib.request

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "app"))

import main # noqa: E402
import webui # noqa: E402


@pytest.fixture
def save(webui_server, tmp_path, monkeypatch):
"""POST /api/config with the login off and MODE=report set by Docker; returns the reply."""
monkeypatch.setattr(os, "environ", {"MODE": "report"})
monkeypatch.setattr(main, "_docker_env_vars", {"MODE"})
(tmp_path / "beatscheck.conf").write_text('mode = report\nlidarr_url = ""\n')
webui._save_auth_off(str(tmp_path))

def post(config):
req = urllib.request.Request(
webui_server + "/api/config", data=json.dumps({"config": config}).encode(),
headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=10) as resp: # nosec B310 # the fixture's http://127.0.0.1 URL
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return json.loads(resp.read())
return post


def test_a_save_keeps_a_docker_set_variable_and_names_it(save):
reply = save({"mode": "setup", "lidarr_url": "http://lidarr:8686"})
assert os.environ["MODE"] == "report"
assert reply["docker_set"] == ["mode"]


def test_a_save_applies_a_setting_no_docker_variable_sets(save):
save({"lidarr_url": "http://lidarr:8686"})
assert os.environ["LIDARR_URL"] == "http://lidarr:8686"


def test_a_docker_set_variable_the_save_agrees_with_is_not_named(save):
reply = save({"mode": "report"})
assert reply["docker_set"] == []


def test_a_save_names_only_the_docker_set_keys_it_saved(save, tmp_path):
(tmp_path / "beatscheck.conf").write_text('mode = setup\nlidarr_url = ""\n')
reply = save({"lidarr_url": "http://lidarr:8686"})
assert reply["docker_set"] == []


def test_a_save_it_cannot_read_back_replies_with_an_error(save, monkeypatch):
def unreadable(path):
raise OSError("unreadable")
monkeypatch.setattr(main, "_parse_config_lines", unreadable)
with pytest.raises(urllib.error.HTTPError) as err:
save({"lidarr_url": "http://lidarr:8686"})
assert err.value.code == 500
assert "LIDARR_URL" not in os.environ


def test_a_save_whose_file_is_gone_before_it_applies_replies_with_an_error(save, monkeypatch):
write = webui._write_config_file

def write_then_remove(config_dir, updates):
ok = write(config_dir, updates)
os.remove(os.path.join(config_dir, "beatscheck.conf"))
return ok
monkeypatch.setattr(webui, "_write_config_file", write_then_remove)
with pytest.raises(urllib.error.HTTPError) as err:
save({"lidarr_url": "http://lidarr:8686"})
assert err.value.code == 500
assert "LIDARR_URL" not in os.environ


@pytest.mark.parametrize("older", ["save", "reload"])
def test_an_older_read_of_the_file_is_not_applied_after_a_newer_save(save, tmp_path, monkeypatch, older):
parse, paused, go = main._parse_config_lines, threading.Event(), threading.Event()

def pause_first_read(path):
vals = parse(path)
if not paused.is_set():
paused.set()
go.wait(5)
return vals
monkeypatch.setattr(main, "_parse_config_lines", pause_first_read)
if older == "save":
first = threading.Thread(target=save, args=({"lidarr_url": "http://old"},))
else:
first = threading.Thread(target=main._sync_env_from_config_file, args=(str(tmp_path),))
first.start()
reached = paused.wait(5)
second = threading.Thread(target=save, args=({"lidarr_url": "http://new"},))
second.start()
second.join(0.5)
go.set()
first.join(5)
second.join(5)
assert reached
assert os.environ["LIDARR_URL"] == "http://new"


@pytest.mark.parametrize("paths", [[], ["music", "out", "beats_check.log"]], ids=["docker", "path-arguments"])
def test_the_config_reload_keeps_a_docker_set_variable(tmp_path, monkeypatch, paths):
monkeypatch.setattr(os, "environ", {"CONFIG_DIR": str(tmp_path), "MODE": "report"})
monkeypatch.setattr(main, "_docker_env_vars", set())
monkeypatch.setattr(sys, "argv", ["main.py"] + [str(tmp_path / p) for p in paths])
cfg = main._load_config()
(tmp_path / "beatscheck.conf").write_text("mode = move\n")
main._reload_config(cfg)
assert cfg.saved_mode == "report"