Render playlist configs directly in-app; retire regen.sh and playlists.json (R3)
The app owns every input to a playlist's sldl .conf (playlists in its DB, credentials in its encrypted store), so the old DB -> playlists.json -> regen.sh subprocess -> regex credential patch-back chain was three serialization hops for no reason. Collapse it: - credential_service.render_playlist_confs(db) renders each <playlist>.conf from _template.conf in one pass: path placeholders substituted as literal text, then the four credential lines set, written 0600. This is now the single source of .conf rendering. - playlist_service loses _write_playlists_json and regenerate_confs; _sync_to_disk just calls render_playlist_confs and syncs the scheduler. No subprocess, no intermediate JSON file. - render_scope for spotify/soulseek re-renders confs via the same function (spotify still also writes _spotify.env for the python scripts). The dead _patch_conf_field / _every_playlist_conf helpers are removed. - Delete pipeline/configs/regen.sh and drop it from the Dockerfile chmod; update _template.conf's comments. Nothing outside playlist_service consumed regen.sh or playlists.json (verified). Also closes the last remnants of the S2/S3 injection surface: names are re-validated and path/credential values are substituted as literals, never through sed or a shell. Verified: end-to-end render in a throwaway DB (paths, creds incl. a password with shell metacharacters, 0600, bad-name rejection) and a live re-render of all 17 playlist configs with credentials preserved. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from sqlalchemy import select
|
||||
@@ -127,7 +126,6 @@ def delete(db: Session, playlist_id: int) -> None:
|
||||
db.commit()
|
||||
conf_path = settings.pipeline_config_dir / f"{name}.conf"
|
||||
conf_path.unlink(missing_ok=True)
|
||||
_write_playlists_json(db)
|
||||
|
||||
from app.services import scheduler_service
|
||||
|
||||
@@ -168,65 +166,18 @@ def seed_legacy(db: Session) -> int:
|
||||
return created
|
||||
|
||||
|
||||
def _write_playlists_json(db: Session) -> None:
|
||||
"""The file regen.sh reads. Every active-or-not playlist is included
|
||||
(see regen.sh's header comment) so a manual run is always possible;
|
||||
the scheduler is what actually skips inactive ones."""
|
||||
playlists = list_all(db)
|
||||
payload = [
|
||||
{
|
||||
"name": p.name,
|
||||
"spotify_url": p.spotify_url,
|
||||
"active": p.active,
|
||||
"no_m3u": p.no_m3u,
|
||||
}
|
||||
for p in playlists
|
||||
]
|
||||
settings.pipeline_config_dir.mkdir(parents=True, exist_ok=True)
|
||||
out_path = settings.pipeline_config_dir / "playlists.json"
|
||||
out_path.write_text(json.dumps(payload, indent=2))
|
||||
|
||||
|
||||
def regenerate_confs(db: Session) -> subprocess.CompletedProcess:
|
||||
"""Write playlists.json, then run regen.sh to render/refresh every
|
||||
playlist's .conf file. Newly-created .conf files still have unpatched
|
||||
SOULSEEK_USER/SOULSEEK_PASS/SPOTIFY_CLIENT_ID/SPOTIFY_CLIENT_SECRET
|
||||
placeholders at this point -- _sync_to_disk() (this function's caller)
|
||||
re-renders credentials into them immediately afterward."""
|
||||
_write_playlists_json(db)
|
||||
regen_script = settings.pipeline_dir / "configs" / "regen.sh"
|
||||
return subprocess.run(
|
||||
[str(regen_script)],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={
|
||||
"ALEMBIC_CONFIG_DIR": str(settings.alembic_config_dir),
|
||||
"MUSIC_DATA_DIR": str(settings.music_data_dir),
|
||||
"PIPELINE_DIR": str(settings.pipeline_dir),
|
||||
# Must include /opt/venv/bin: regen.sh shells out to `python3`
|
||||
# to parse playlists.json, and that's where this image's Python
|
||||
# actually lives (confirmed against the real container -- a
|
||||
# bare /usr/bin:/bin PATH made every regen.sh call silently
|
||||
# generate zero .conf files, no error surfaced to the caller).
|
||||
"PATH": "/opt/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _sync_to_disk(db: Session) -> None:
|
||||
"""Convenience wrapper used by create()/update(): regenerate confs,
|
||||
immediately re-patch credentials into any newly-rendered file, and push
|
||||
the change to the live scheduler if one is running -- this is what
|
||||
makes add/remove-in-the-UI take effect without a redeploy."""
|
||||
regenerate_confs(db)
|
||||
# Imported lazily to avoid a circular import (credential_service and
|
||||
# scheduler_service don't depend on playlist_service, but importing at
|
||||
# module load time would still work here — done lazily anyway to keep
|
||||
# the dependency direction obvious as all three services grow).
|
||||
"""Called by create()/update()/delete(): re-render every playlist's sldl
|
||||
.conf (with credentials substituted, in-app -- no regen.sh subprocess or
|
||||
playlists.json round-trip anymore) and push the change to the live
|
||||
scheduler if one is running. This is what makes add/remove-in-the-UI take
|
||||
effect without a redeploy.
|
||||
|
||||
Imported lazily to keep the dependency direction obvious (credential_service
|
||||
and scheduler_service don't import playlist_service at module load)."""
|
||||
from app.services import credential_service, scheduler_service
|
||||
|
||||
credential_service.render_scope(db, "soulseek")
|
||||
credential_service.render_scope(db, "spotify")
|
||||
credential_service.render_playlist_confs(db)
|
||||
|
||||
scheduler = scheduler_service.get_scheduler()
|
||||
if scheduler is not None:
|
||||
|
||||
Reference in New Issue
Block a user