2798e29d / scripts /start /aria2-start.sh
autoface's picture
Add aria2 and webdav configuration template files and update the associated startup scripts to generate configurations from the templates. Modify restic startup scripts to use project configuration templates to simplify configuration management.
a225f5b
#!/bin/bash
set -e
# Function for logging
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ARIA2] $*"
}
# Function to check if port is available
check_port() {
local port=$1
if nc -z localhost "$port" 2>/dev/null; then
return 1 # Port is in use
else
return 0 # Port is available
fi
}
# Function to wait for aria2 to start
wait_for_aria2() {
local port=$1
local max_attempts=30
local attempt=1
log "Waiting for aria2 to start on port $port..."
while [ $attempt -le $max_attempts ]; do
if nc -z localhost "$port" 2>/dev/null; then
log "aria2 is running on port $port"
return 0
fi
sleep 1
attempt=$((attempt + 1))
done
log "ERROR: aria2 failed to start after $max_attempts seconds"
return 1
}
log "Starting aria2 service..."
# Set default values
export ARIA2_PORT="${ARIA2_PORT:-6800}"
export ARIA2_WEBUI_PORT="${ARIA2_WEBUI_PORT:-6880}"
export ARIA2_SECRET="${ARIA2_SECRET:-$(openssl rand -base64 32)}"
export ARIA2_DOWNLOAD_DIR="${ARIA2_DOWNLOAD_DIR:-/home/user/download}"
export ARIA2_SESSION_FILE="${ARIA2_SESSION_FILE:-/home/user/config/aria2.session}"
export ARIA2_LOG_FILE="${ARIA2_LOG_FILE:-/home/user/log/aria2.log}"
# Advanced configuration options with defaults
export ARIA2_MAX_CONCURRENT="${ARIA2_MAX_CONCURRENT:-5}"
export ARIA2_MAX_CONN_PER_SERVER="${ARIA2_MAX_CONN_PER_SERVER:-16}"
export ARIA2_SPLIT="${ARIA2_SPLIT:-16}"
export ARIA2_DISK_CACHE="${ARIA2_DISK_CACHE:-64M}"
export ARIA2_BT_MAX_PEERS="${ARIA2_BT_MAX_PEERS:-55}"
export ARIA2_MAX_OVERALL_DOWNLOAD_LIMIT="${ARIA2_MAX_OVERALL_DOWNLOAD_LIMIT:-0}"
export ARIA2_MAX_DOWNLOAD_LIMIT="${ARIA2_MAX_DOWNLOAD_LIMIT:-0}"
export ARIA2_MAX_OVERALL_UPLOAD_LIMIT="${ARIA2_MAX_OVERALL_UPLOAD_LIMIT:-0}"
export ARIA2_MAX_UPLOAD_LIMIT="${ARIA2_MAX_UPLOAD_LIMIT:-0}"
# Create required directories
mkdir -p "$(dirname "$ARIA2_SESSION_FILE")" \
"$(dirname "$ARIA2_LOG_FILE")" \
"$ARIA2_DOWNLOAD_DIR"
# Create session file if it doesn't exist
touch "$ARIA2_SESSION_FILE"
# Check if aria2 port is available
if ! check_port "$ARIA2_PORT"; then
log "ERROR: Port $ARIA2_PORT is already in use"
exit 1
fi
# Determine template path
TEMPLATE_PATH="/etc/hf_docker_template/configs/templates/aria2.conf.template"
if [ ! -f "$TEMPLATE_PATH" ]; then
TEMPLATE_PATH="$(dirname "$(dirname "$(dirname "$0")")")/configs/templates/aria2.conf.template"
fi
# Generate aria2 configuration
log "Generating aria2 configuration from template..."
if [ -f "$TEMPLATE_PATH" ]; then
envsubst < "$TEMPLATE_PATH" > /home/user/config/aria2.conf
log "Using template from: $TEMPLATE_PATH"
else
log "ERROR: Template file not found at $TEMPLATE_PATH"
exit 1
fi
log "Starting aria2 daemon..."
aria2c --conf-path=/home/user/config/aria2.conf --daemon=true
# Wait for aria2 to start
if wait_for_aria2 "$ARIA2_PORT"; then
log "aria2 daemon started successfully"
log "RPC endpoint: http://localhost:$ARIA2_PORT/jsonrpc"
log "RPC secret: $ARIA2_SECRET"
log "Download directory: $ARIA2_DOWNLOAD_DIR"
# Start web UI if available
if [ -d "/usr/local/share/aria2" ]; then
if check_port "$ARIA2_WEBUI_PORT"; then
log "Starting aria2 web UI on port $ARIA2_WEBUI_PORT..."
cd /usr/local/share/aria2
python3 -m http.server "$ARIA2_WEBUI_PORT" --bind 0.0.0.0 &
WEBUI_PID=$!
log "Web UI started with PID: $WEBUI_PID"
log "Web UI URL: http://localhost:$ARIA2_WEBUI_PORT"
else
log "WARNING: Port $ARIA2_WEBUI_PORT is already in use, skipping web UI startup"
fi
else
log "WARNING: AriaNg web UI not found in /usr/local/share/aria2"
fi
# Keep the script running
while true; do
if ! nc -z localhost "$ARIA2_PORT" 2>/dev/null; then
log "ERROR: aria2 daemon stopped unexpectedly"
exit 1
fi
sleep 30
done
else
log "ERROR: Failed to start aria2 daemon"
exit 1
fi