feat: entrypoint for safe config + permissions; optional counter seeding via USE_INITIAL_COUNTERS

This commit is contained in:
Andre Beging
2025-09-30 09:44:58 +02:00
parent 6d60fd813c
commit 5b2e652682
3 changed files with 395 additions and 369 deletions

View File

@@ -19,15 +19,17 @@ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
COPY bot.py ./ COPY bot.py ./
COPY config.example.yaml ./config.yaml COPY config.example.yaml ./config.example.yaml
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Datenverzeichnis # Datenverzeichnis
RUN mkdir -p /data && chown -R appuser:appuser /data && chown appuser:appuser /app # Pre-create data dir (ownership may be adjusted again at runtime by entrypoint)
RUN mkdir -p /data
VOLUME ["/data"] VOLUME ["/data"]
USER appuser
ENV DATA_DIR=/data \ ENV DATA_DIR=/data \
CONFIG_FILE=/app/config.yaml CONFIG_FILE=/app/config.yaml
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["python", "bot.py"] CMD ["python", "bot.py"]

8
bot.py
View File

@@ -245,8 +245,16 @@ def setup_logging(level: str):
def init_counters(existing: Dict[str, int], config: Dict[str, Any]) -> Dict[str, int]: def init_counters(existing: Dict[str, int], config: Dict[str, Any]) -> Dict[str, int]:
"""Return existing counters or (optionally) seed initial ones.
Seeding now only happens if BOTH conditions apply:
1) No existing counters file/content
2) Env USE_INITIAL_COUNTERS is truthy (1/true/yes)
"""
if existing: if existing:
return existing return existing
if os.environ.get("USE_INITIAL_COUNTERS", "false").lower() not in {"1", "true", "yes"}:
return {}
initial = config.get('initial_counters') or {} initial = config.get('initial_counters') or {}
normalized = {norm_key(k): int(v) for k, v in initial.items()} normalized = {norm_key(k): int(v) for k, v in initial.items()}
if normalized: if normalized:

16
docker-entrypoint.sh Normal file
View File

@@ -0,0 +1,16 @@
#!/bin/sh
set -e
# If running as root, fix ownership of /data, then drop privileges
if [ "$(id -u)" = "0" ]; then
mkdir -p /data
chown -R appuser:appuser /data || echo "Warn: could not chown /data"
# Copy example config only if missing target
if [ ! -f /app/config.yaml ] && [ -f /app/config.example.yaml ]; then
cp /app/config.example.yaml /app/config.yaml
chown appuser:appuser /app/config.yaml || true
fi
exec su -s /bin/sh appuser -c "$*"
else
exec "$@"
fi