#!/bin/sh set -e # Ensure data directory exists mkdir -p /app/data # Generate a secure JWT secret if one isn't provided or is using the default value JWT_SECRET_FILE="/app/data/.jwt_secret" if [ "$JWT_SECRET" = "your-secret-key-change-this-in-production" ] || [ -z "$JWT_SECRET" ]; then # Check if we have a previously generated secret if [ -f "$JWT_SECRET_FILE" ]; then echo "Using previously generated JWT secret" export JWT_SECRET=$(cat "$JWT_SECRET_FILE") else echo "Generating a secure random JWT secret" # Try to generate a secure random string using OpenSSL if command -v openssl >/dev/null 2>&1; then GENERATED_SECRET=$(openssl rand -hex 32) else # Fallback to using /dev/urandom if openssl is not available echo "OpenSSL not found, using fallback method for random generation" GENERATED_SECRET=$(head -c 32 /dev/urandom | sha256sum | cut -d' ' -f1) fi export JWT_SECRET="$GENERATED_SECRET" # Save the secret to a file for persistence across container restarts echo "$GENERATED_SECRET" > "$JWT_SECRET_FILE" chmod 600 "$JWT_SECRET_FILE" fi echo "JWT_SECRET has been set to a secure random value" fi # Set up automatic database cleanup cron job # Default to 7 days retention for events and mirror jobs unless specified by environment variables EVENTS_RETENTION_DAYS=${EVENTS_RETENTION_DAYS:-7} JOBS_RETENTION_DAYS=${JOBS_RETENTION_DAYS:-7} # Create cron directory if it doesn't exist mkdir -p /app/data/cron # Create the cron job file cat > /app/data/cron/cleanup-cron <> /app/data/cleanup-events.log 2>&1 # Run mirror jobs cleanup daily at 3 AM 0 3 * * * cd /app && bun dist/scripts/cleanup-mirror-jobs.js ${JOBS_RETENTION_DAYS} >> /app/data/cleanup-mirror-jobs.log 2>&1 # Empty line at the end is required for cron to work properly EOF # Skip dependency installation entirely for pre-built images # Dependencies are already installed during the Docker build process # Initialize the database if it doesn't exist if [ ! -f "/app/data/gitea-mirror.db" ]; then echo "Initializing database..." if [ -f "dist/scripts/init-db.js" ]; then bun dist/scripts/init-db.js elif [ -f "dist/scripts/manage-db.js" ]; then bun dist/scripts/manage-db.js init elif [ -f "scripts/manage-db.ts" ]; then bun scripts/manage-db.ts init else echo "Warning: Could not find database initialization scripts in dist/scripts." echo "Creating and initializing database manually..." # Create the database file touch /app/data/gitea-mirror.db # Initialize the database with required tables sqlite3 /app/data/gitea-mirror.db </dev/null 2>&1; then echo "Setting up automatic database cleanup cron jobs..." # Install cron if not already installed if ! command -v cron >/dev/null 2>&1; then echo "Installing cron..." apt-get update && apt-get install -y cron fi # Install the cron job crontab /app/data/cron/cleanup-cron # Start cron service if command -v service >/dev/null 2>&1; then service cron start echo "Cron service started" elif command -v cron >/dev/null 2>&1; then cron echo "Cron daemon started" else echo "Warning: Could not start cron service. Automatic database cleanup will not run." fi else echo "Warning: crontab command not found. Automatic database cleanup will not be set up." echo "Consider setting up external scheduled tasks to run cleanup scripts." fi # Start the application echo "Starting Gitea Mirror..." exec bun ./dist/server/entry.mjs