README / entrypoint.sh
nexus33rd's picture
Upload 29 files
47e088b verified
Raw History Blame Contribute Delete
4.22 kB
#!/usr/bin/env bash
set -euo pipefail
if [[ -n "${QBT_PROFILE:-}" ]]; then
CONFIG_DIR="$QBT_PROFILE"
elif [[ -d /data ]]; then
CONFIG_DIR="/data/qbittorrent"
else
CONFIG_DIR="/config"
fi
if [[ -n "${QBITTORRENT_SAVE_PATH:-}" ]]; then
DOWNLOAD_DIR="$QBITTORRENT_SAVE_PATH"
elif [[ -d /data ]]; then
DOWNLOAD_DIR="/data/downloads"
else
DOWNLOAD_DIR="/downloads"
fi
DEFAULT_CONFIG="/defaults/qBittorrent.conf"
ACTIVE_CONFIG_DIR="$CONFIG_DIR/qBittorrent/config"
ACTIVE_CONFIG="$ACTIVE_CONFIG_DIR/qBittorrent.conf"
LEGACY_CONFIG="$CONFIG_DIR/qBittorrent.conf"
WEB_PORT="${PORT:-5000}"
TORRENT_PORT="${QBITTORRENT_TORRENT_PORT:-6881}"
TORRENT_SSL_PORT="${QBITTORRENT_TORRENT_SSL_PORT:-14906}"
KEEPALIVE_ENABLED="${KEEPALIVE_ENABLED:-true}"
KEEPALIVE_INTERVAL_SECONDS="${KEEPALIVE_INTERVAL_SECONDS:-900}"
KEEPALIVE_INITIAL_DELAY_SECONDS="${KEEPALIVE_INITIAL_DELAY_SECONDS:-60}"
KEEPALIVE_TIMEOUT_SECONDS="${KEEPALIVE_TIMEOUT_SECONDS:-20}"
mkdir -p "$CONFIG_DIR" "$ACTIVE_CONFIG_DIR" "$DOWNLOAD_DIR" "$DOWNLOAD_DIR/incomplete"
has_webui_config() {
local config_file="$1"
[[ -f "$config_file" ]] \
&& grep -q '^WebUI\\Username=' "$config_file" \
&& grep -q '^WebUI\\Password_PBKDF2=' "$config_file"
}
ensure_config_value() {
local config_file="$1"
local key="$2"
local value="$3"
if grep -q "^${key}=" "$config_file"; then
sed -i "s|^${key}=.*|${key}=${value}|" "$config_file"
else
printf '%s=%s\n' "$key" "$value" >> "$config_file"
fi
}
SOURCE_CONFIG="$DEFAULT_CONFIG"
if has_webui_config "$LEGACY_CONFIG"; then
SOURCE_CONFIG="$LEGACY_CONFIG"
fi
# qBittorrent stores its real profile under <profile>/qBittorrent/config.
# Keep older /config/qBittorrent.conf mounts working by migrating them forward.
if ! has_webui_config "$ACTIVE_CONFIG"; then
cp "$SOURCE_CONFIG" "$ACTIVE_CONFIG"
fi
ensure_config_value "$ACTIVE_CONFIG" 'Session\\Port' "$TORRENT_PORT"
ensure_config_value "$ACTIVE_CONFIG" 'Session\\SSL\\Port' "$TORRENT_SSL_PORT"
ensure_config_value "$ACTIVE_CONFIG" 'Session\\AddTorrentStopped' "false"
ensure_config_value "$ACTIVE_CONFIG" 'Session\\ShareLimitAction' "Stop"
cp "$ACTIVE_CONFIG" "$LEGACY_CONFIG"
shutdown() {
if [[ -n "${KEEPALIVE_PID:-}" ]] && kill -0 "$KEEPALIVE_PID" 2>/dev/null; then
kill "$KEEPALIVE_PID" 2>/dev/null || true
fi
if [[ -n "${BOT_PID:-}" ]] && kill -0 "$BOT_PID" 2>/dev/null; then
kill "$BOT_PID" 2>/dev/null || true
fi
if [[ -n "${WEB_PID:-}" ]] && kill -0 "$WEB_PID" 2>/dev/null; then
kill "$WEB_PID" 2>/dev/null || true
fi
if [[ -n "${QBT_PID:-}" ]] && kill -0 "$QBT_PID" 2>/dev/null; then
kill "$QBT_PID" 2>/dev/null || true
fi
wait || true
}
trap shutdown SIGINT SIGTERM
resolve_keepalive_url() {
if [[ -n "${KEEPALIVE_URL:-}" ]]; then
printf '%s\n' "$KEEPALIVE_URL"
elif [[ -n "${SPACE_HOST:-}" ]]; then
printf 'https://%s/healthz\n' "$SPACE_HOST"
else
printf 'http://127.0.0.1:%s/healthz\n' "$WEB_PORT"
fi
}
start_keepalive() {
case "$KEEPALIVE_ENABLED" in
true|TRUE|1|yes|YES|on|ON) ;;
*)
echo "[keepalive] Disabled"
return
;;
esac
local keepalive_url
keepalive_url="$(resolve_keepalive_url)"
(
sleep "$KEEPALIVE_INITIAL_DELAY_SECONDS" || true
while true; do
if curl -fsS --max-time "$KEEPALIVE_TIMEOUT_SECONDS" -A "torrent-web-keepalive/1.0" "$keepalive_url" >/dev/null; then
echo "[keepalive] Ping OK: $keepalive_url"
else
echo "[keepalive] Ping failed: $keepalive_url"
fi
sleep "$KEEPALIVE_INTERVAL_SECONDS"
done
) &
KEEPALIVE_PID=$!
echo "[entrypoint] Keepalive PID: $KEEPALIVE_PID"
}
qbittorrent-nox --profile="$CONFIG_DIR" &
QBT_PID=$!
echo "[entrypoint] qBittorrent PID: $QBT_PID"
gunicorn \
--bind "0.0.0.0:${WEB_PORT}" \
--workers "${GUNICORN_WORKERS:-2}" \
--threads "${GUNICORN_THREADS:-4}" \
--timeout "${GUNICORN_TIMEOUT:-120}" \
app:app &
WEB_PID=$!
echo "[entrypoint] Web PID: $WEB_PID"
start_keepalive
echo "[entrypoint] Starting Telegram bot..."
python -m bot.bot &
BOT_PID=$!
echo "[entrypoint] Telegram bot PID: $BOT_PID"
WAIT_PIDS=("$QBT_PID" "$WEB_PID" "$BOT_PID")
wait -n "${WAIT_PIDS[@]}"
EXIT_CODE=$?
shutdown
exit "$EXIT_CODE"