Improve start.sh with Cloudron best practices

This commit is contained in:
Andreas Düren 2025-03-14 22:47:27 +01:00
parent 789d7028b9
commit d8a40880d8

View File

@ -1,34 +1,46 @@
#!/bin/bash #!/bin/bash
# Better signal handling - forward signals to child processes
trap 'kill -TERM $SERVER_PID; kill -TERM $NGINX_PID; exit' TERM INT
set -eu set -eu
echo "==> Starting Ente Cloudron app..."
# Create necessary directories # Create necessary directories
mkdir -p /app/data/config /app/data/storage /app/data/nginx/tmp /app/data/go /app/data/logs mkdir -p /app/data/config /app/data/storage /app/data/nginx/tmp /app/data/go /app/data/logs /run/nginx
echo "==> DEBUG: Full repository structure at /app/code" # Add comment about Cloudron filesystem limitations
find /app/code -type d -maxdepth 3 -not -path "*/node_modules/*" -not -path "*/\.*" | sort echo "==> NOTE: Running in Cloudron environment with limited write access"
echo "==> Writable directories: /app/data, /tmp, /run"
echo "==> DEBUG: Looking for Go files" # One-time initialization tracking
find /app/code -name "*.go" | grep -v test | sort | head -10 if [[ ! -f /app/data/.initialized ]]; then
echo "==> Fresh installation, setting up data directory..."
echo "==> DEBUG: Looking for server-related directories" echo "==> DEBUG: Full repository structure at /app/code"
find /app/code -type d -path "*/server*" -o -path "*/museum*" | sort find /app/code -type d -maxdepth 3 -not -path "*/node_modules/*" -not -path "*/\.*" | sort
echo "==> DEBUG: All package.json files in repository" echo "==> DEBUG: Looking for Go files"
find /app/code -name "package.json" -not -path "*/node_modules/*" | sort find /app/code -name "*.go" | grep -v test | sort | head -10
echo "==> DEBUG: Looking for web app directories" echo "==> DEBUG: Looking for server-related directories"
find /app/code -type d -path "*/web*" | sort find /app/code -type d -path "*/server*" -o -path "*/museum*" | sort
echo "==> DEBUG: Web app directories in /app/web (if they exist)" echo "==> DEBUG: All package.json files in repository"
if [ -d "/app/web" ]; then find /app/code -name "package.json" -not -path "*/node_modules/*" | sort
ls -la /app/web
else
echo "Web app directory not yet copied to /app/web"
fi
# Create config template file on first run echo "==> DEBUG: Looking for web app directories"
if [[ ! -f /app/data/config/config.yaml ]]; then find /app/code -type d -path "*/web*" | sort
echo "==> DEBUG: Web app directories in /app/web (if they exist)"
if [ -d "/app/web" ]; then
ls -la /app/web
else
echo "Web app directory not yet copied to /app/web"
fi
# Create config template file on first run
echo "==> First run - creating configuration template" echo "==> First run - creating configuration template"
# Generate random secrets # Generate random secrets
@ -97,6 +109,10 @@ S3_SECRET_KEY=aPdHB4fkvQAuJUqPhneoIDcHEHee9cvP2j0nKSly
S3_PREFIX=ente/ S3_PREFIX=ente/
EOT EOT
echo "==> Test S3 configuration created for Wasabi" echo "==> Test S3 configuration created for Wasabi"
# Mark initialization as complete
touch /app/data/.initialized
echo "==> Initialization complete"
fi fi
# Check if s3.env exists # Check if s3.env exists
@ -342,6 +358,16 @@ export ENTE_LOG_LEVEL=debug
echo "==> Testing API connectivity" echo "==> Testing API connectivity"
curl -v http://localhost:8000/api/health || echo "API not yet available, this is normal during startup" curl -v http://localhost:8000/api/health || echo "API not yet available, this is normal during startup"
# Determine available memory and set limits accordingly
if [[ -f /sys/fs/cgroup/cgroup.controllers ]]; then # cgroup v2
memory_limit=$(cat /sys/fs/cgroup/memory.max)
[[ "${memory_limit}" == "max" ]] && memory_limit=$(( 2 * 1024 * 1024 * 1024 )) # "max" really means unlimited
else
memory_limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) # this is the RAM. we have equal amount of swap
fi
memory_mb=$((memory_limit/1024/1024))
echo "==> Available memory: ${memory_mb}MB"
# Set up database environment variables and ensure proper SSL config # Set up database environment variables and ensure proper SSL config
export ENTE_DB_USER="${CLOUDRON_POSTGRESQL_USERNAME}" export ENTE_DB_USER="${CLOUDRON_POSTGRESQL_USERNAME}"
export ENTE_DB_PASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}" export ENTE_DB_PASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
@ -361,10 +387,7 @@ export REMOTE_STORAGE_PREFIX="${S3_PREFIX:-ente/}"
# Change ownership to cloudron user # Change ownership to cloudron user
chown -R cloudron:cloudron /app/data chown -R cloudron:cloudron /app/data
chown -R cloudron:cloudron /run/nginx
# Add comment about Cloudron filesystem limitations
echo "==> NOTE: Running in Cloudron environment with limited write access"
echo "==> Writable directories: /app/data, /tmp, /run"
# Start Museum server on port 8000 (different from the NGINX port 8080) # Start Museum server on port 8000 (different from the NGINX port 8080)
echo "==> Starting Museum server" echo "==> Starting Museum server"
@ -510,4 +533,13 @@ fi
# Serve the static web apps in the foreground using our custom nginx config # Serve the static web apps in the foreground using our custom nginx config
echo "==> Running NGINX in the foreground with custom configuration" echo "==> Running NGINX in the foreground with custom configuration"
exec nginx -c /app/data/nginx/ente.conf nginx -c /app/data/nginx/ente.conf &
NGINX_PID=$!
echo "==> NGINX started with PID $NGINX_PID"
echo "==> Ente is now running!"
echo "==> Museum server: PID $SERVER_PID"
echo "==> NGINX: PID $NGINX_PID"
# Wait for the processes to finish (or be terminated)
wait