From a8477591f020c0198c73d0f9dcd5768b27c19c26 Mon Sep 17 00:00:00 2001 From: Ivan Schaller Date: Mon, 18 Jul 2022 18:41:41 +0200 Subject: [PATCH] add options to configure the default schedule in the docker container via environment variables --- CHANGELOG.md | 7 +++ docker/README.md | 30 +++++++-- docker/docker-compose.yml | 8 +-- .../rootfs/app/schedules/{daily => daily.sh} | 0 .../cont-init.d/{20-setenv => 20-setenv.sh} | 10 ++- .../rootfs/etc/cont-init.d/51-fix-schedule.sh | 28 +++++++++ .../rootfs/etc/cont-init.d/52-set-schedule.sh | 62 +++++++++++++++++++ .../{80-fix-perms => 80-fix-perms.sh} | 2 +- docker/rootfs/etc/cron.d/mangadlp | 2 +- 9 files changed, 134 insertions(+), 15 deletions(-) rename docker/rootfs/app/schedules/{daily => daily.sh} (100%) rename docker/rootfs/etc/cont-init.d/{20-setenv => 20-setenv.sh} (60%) create mode 100644 docker/rootfs/etc/cont-init.d/51-fix-schedule.sh create mode 100644 docker/rootfs/etc/cont-init.d/52-set-schedule.sh rename docker/rootfs/etc/cont-init.d/{80-fix-perms => 80-fix-perms.sh} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef2fa84..e862225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,14 +15,21 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - The `--read` option now filters empty lines, so it will not generate an error anymore - An error which was caused by the interactive input method when you did not specify a chapter or to list them +- Some typos ### Added +- Options to configure the default schedule in the docker container via environment variables +- Section the the docker [README.md](docker/README.md) for the new environment variables - `autoflake` test in `justfile` - Some more things which get logged ### Changed +- **BREAKING**: renamed the default schedule from `daily` to `daily.sh`. Don't forget to fix your bind-mounts to + overwrite + the default schedule +- Added the `.sh` suffix to the s6 init scripts for better compatibility - Adjusted the new logging implementation. It shows now more info about the module the log is from, and some other improvements diff --git a/docker/README.md b/docker/README.md index 5ddf6aa..420a0b6 100644 --- a/docker/README.md +++ b/docker/README.md @@ -31,6 +31,26 @@ environment: docker run -e PUID= -e PGID= ``` +## Environment variables + +You can configure the default schedule via environment variables. Don't forget to set `MDLP_GENERATE_SCHEDULE` to "true" +, else +it will not generate it (it will just use the default one). + +For more info's about the options, you can look in the main scripts [README.md](../README.md) + +| ENV Variable | Default | manga-dlp option | Info | +|:-----------------------|:----------------|:-------------------------|--------------------------------------------------------------------------| +| MDLP_GENERATE_SCHEDULE | false | none | Has to be set to "true" to generate the config via environment variables | +| MDLP_PATH | /app/downloads | --path | | +| MDLP_READ | /app/mangas.txt | --read | | +| MDLP_LANGUAGE | en | --language | | +| MDLP_CHAPTERS | all | --chapter | | +| MDLP_FILE_FORMAT | cbz | --format | | +| MDLP_WAIT | 0.5 | --wait | | +| MDLP_FORCEVOL | false | --forcevol | | +| MDLP_LOG_LEVEL | lean | --lean/--verbose/--debug | Can either be set to: "lean", "verbose" or "debug" | + ## Run commands in container > You don't need to use the full path of manga-dlp.py because `/app` already is the working directory @@ -68,15 +88,15 @@ To use your own schedule you need to mount (override) the default schedule or ad volumes: - ./crontab:/etc/cron.d/mangadlp # overwrites the default crontab - ./crontab2:/etc/cron.d/something # adds a new one crontab file - - ./schedule1:/app/schedules/daily # overwrites the default schedule - - ./schedule2:/app/schedules/weekly # adds a new schedule + - ./schedule1.sh:/app/schedules/daily.sh # overwrites the default schedule + - ./schedule2.sh:/app/schedules/weekly.sh # adds a new schedule ``` ```sh docker run -v ./crontab:/etc/cron.d/mangadlp # overwrites the default crontab docker run -v ./crontab2:/etc/cron.d/something # adds a new one crontab file -docker run -v ./schedule1:/app/schedules/daily # overwrites the default schedule -docker run -v ./schedule2:/app/schedules/weekly # adds a new schedule +docker run -v ./schedule1.sh:/app/schedules/daily.sh # overwrites the default schedule +docker run -v ./schedule2.sh:/app/schedules/weekly.sh # adds a new schedule ``` #### The default crontab file: @@ -91,7 +111,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # "/proc/1/fd/1 2>&1" is to show the logs in the container # "s6-setuidgid abc" is used to set the permissions -0 12 * * * root s6-setuidgid abc /app/schedules/daily > /proc/1/fd/1 2>&1 +0 12 * * * root s6-setuidgid abc /app/schedules/daily.sh > /proc/1/fd/1 2>&1 ``` ## Add mangas to mangas.txt diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 1b47ccb..db1dd21 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -13,15 +13,13 @@ services: - ./downloads/:/app/downloads/ # default manga download directory - ./mangas.txt:/app/mangas.txt # default file for manga links to download #- ./crontab:/etc/cron.d/mangadlp # path to default crontab - #- ./schedule:/app/schedules/daily # path to the default schedule which is run daily + #- ./schedule.sh:/app/schedules/daily.sh # path to the default schedule which is run daily environment: - TZ=Europe/Zurich - # - PUID= # custom userid - defaults to 4444 - # - PGID= # custom groupid - defaults to 4444 - + #- PUID= # custom user id - defaults to 4444 + #- PGID= # custom group id - defaults to 4444 networks: appnet: name: mangadlp driver: bridge - diff --git a/docker/rootfs/app/schedules/daily b/docker/rootfs/app/schedules/daily.sh similarity index 100% rename from docker/rootfs/app/schedules/daily rename to docker/rootfs/app/schedules/daily.sh diff --git a/docker/rootfs/etc/cont-init.d/20-setenv b/docker/rootfs/etc/cont-init.d/20-setenv.sh similarity index 60% rename from docker/rootfs/etc/cont-init.d/20-setenv rename to docker/rootfs/etc/cont-init.d/20-setenv.sh index 0062f63..7bc3f29 100644 --- a/docker/rootfs/etc/cont-init.d/20-setenv +++ b/docker/rootfs/etc/cont-init.d/20-setenv.sh @@ -4,8 +4,12 @@ # set all env variables for further use. If variable is unset, it will have the defaults on the right side after ":=" # custom env vars +: "${MDLP_GENERATE_SCHEDULE:=false}" +: "${MDLP_PATH:=/app/downloads}" +: "${MDLP_READ:=/app/mangas.txt}" : "${MDLP_LANGUAGE:=en}" -: "${MDLP_FORCEVOL:=false}" +: "${MDLP_CHAPTERS:=all}" : "${MDLP_FILE_FORMAT:=cbz}" -: "${MDLP_DOWNLOAD_WAIT:=2}" -: "${MDLP_VERBOSE:=false}" +: "${MDLP_WAIT:=0.5}" +: "${MDLP_FORCEVOL:=false}" +: "${MDLP_LOG_LEVEL:=lean}" diff --git a/docker/rootfs/etc/cont-init.d/51-fix-schedule.sh b/docker/rootfs/etc/cont-init.d/51-fix-schedule.sh new file mode 100644 index 0000000..097199d --- /dev/null +++ b/docker/rootfs/etc/cont-init.d/51-fix-schedule.sh @@ -0,0 +1,28 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +# source env variables +source /etc/cont-init.d/20-setenv.sh + +# check schedule +[[ -f "/app/schedules/daily.sh" ]] && DAILYSH=true +[[ -f "/app/schedules/daily" ]] && DAILY=true +# check crontab +if grep -q -e "/app/schedules/daily.sh\s" /etc/cron.d/mangadlp; then + CRONSH=true +elif grep -q -e "/app/schedules/daily\s" /etc/cron.d/mangadlp; then + CRON=true +fi + +# fix new .sh schedule if its not synced with the crontab +if [[ "${CRONSH}" == "true" ]] && [[ "${DAILYSH}" != "true" ]]; then + echo "Fixing new .sh schedule" + if ! ln -s /app/schedule/daily /app/schedule/daily.sh; then + echo "Cant fix schedule. Maybe the file is missing." + fi +elif [[ "${CRON}" == "true" ]] && [[ "${DAILY}" != "true" ]]; then + echo "Fixing new .sh schedule" + if ! ln -s /app/schedule/daily.sh /app/schedule/daily; then + echo "Cant fix schedule. Maybe the file is missing." + fi +fi diff --git a/docker/rootfs/etc/cont-init.d/52-set-schedule.sh b/docker/rootfs/etc/cont-init.d/52-set-schedule.sh new file mode 100644 index 0000000..72a737f --- /dev/null +++ b/docker/rootfs/etc/cont-init.d/52-set-schedule.sh @@ -0,0 +1,62 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +# source env variables +source /etc/cont-init.d/20-setenv.sh + +function prepare_vars() { + # set log level + case "${MDLP_LOG_LEVEL}" in + "lean") + MDLP_LOG_LEVEL_FLAG=" --lean" + ;; + "verbose") + MDLP_LOG_LEVEL_FLAG=" --verbose" + ;; + "debug") + MDLP_LOG_LEVEL_FLAG=" --debug" + ;; + esac + + # check if forcevol should be used + if [[ "${MDLP_FORCEVOL,,}" == "true" ]]; then + # add backslash if log level is also specified + if [[ -n "${MDLP_LOG_LEVEL_FLAG}" ]]; then + MDLP_FORCEVOL_FLAG="\n --forcevol \\" + else + MDLP_FORCEVOL_FLAG="\n --forcevol" + fi + fi +} + +# set schedule with env variables +function set_vars() { + echo -n " +#!/bin/bash + +python3 /app/manga-dlp.py \\ + --path ${MDLP_PATH} \\ + --read ${MDLP_READ} \\ + --language ${MDLP_LANGUAGE} \\ + --chapters ${MDLP_CHAPTERS} \\ + --format ${MDLP_FILE_FORMAT} \\ + --wait ${MDLP_WAIT}" \ + > /app/schedules/daily.sh + + # set forcevol or log level if specified + if [[ -n "${MDLP_FORCEVOL_FLAG}" ]] || [[ -n "${MDLP_LOG_LEVEL_FLAG}" ]]; then + sed -i 's/--wait '"${MDLP_WAIT}"'/--wait '"${MDLP_WAIT}"' \\/g' /app/schedules/daily.sh + echo -e "${MDLP_FORCEVOL_FLAG:-}" >> /app/schedules/daily.sh + echo -e "${MDLP_LOG_LEVEL_FLAG:-}" >> /app/schedules/daily.sh + else + # add final newline of not added before + echo -ne "\n" >> /app/schedules/daily.sh + fi +} + +# check if schedule should be generated +if [[ "${MDLP_GENERATE_SCHEDULE,,}" == "true" ]]; then + echo "Generating schedule" + prepare_vars + set_vars +fi diff --git a/docker/rootfs/etc/cont-init.d/80-fix-perms b/docker/rootfs/etc/cont-init.d/80-fix-perms.sh similarity index 93% rename from docker/rootfs/etc/cont-init.d/80-fix-perms rename to docker/rootfs/etc/cont-init.d/80-fix-perms.sh index bddecbc..e535c70 100644 --- a/docker/rootfs/etc/cont-init.d/80-fix-perms +++ b/docker/rootfs/etc/cont-init.d/80-fix-perms.sh @@ -2,7 +2,7 @@ # shellcheck shell=bash # source env variables -source /etc/cont-init.d/20-setenv +source /etc/cont-init.d/20-setenv.sh # fix permissions find '/app' -type 'd' \( -not -perm 775 -and -not -path '/app/downloads*' \) -exec chmod 775 '{}' \+ diff --git a/docker/rootfs/etc/cron.d/mangadlp b/docker/rootfs/etc/cron.d/mangadlp index 3db1d4d..40bfc62 100644 --- a/docker/rootfs/etc/cron.d/mangadlp +++ b/docker/rootfs/etc/cron.d/mangadlp @@ -7,5 +7,5 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # "/proc/1/fd/1 2>&1" is to show the logs in the container # "s6-setuidgid abc" is used to set the permissions -0 12 * * * root s6-setuidgid abc /app/schedules/daily > /proc/1/fd/1 2>&1 +0 12 * * * root s6-setuidgid abc /app/schedules/daily.sh > /proc/1/fd/1 2>&1