From d8a40880d833cf4c1f9af8c9d6faaa7a23140e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20D=C3=BCren?= Date: Fri, 14 Mar 2025 22:47:27 +0100 Subject: [PATCH] Improve start.sh with Cloudron best practices --- start.sh | 80 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/start.sh b/start.sh index bea3915..5e18f67 100644 --- a/start.sh +++ b/start.sh @@ -1,34 +1,46 @@ #!/bin/bash +# Better signal handling - forward signals to child processes +trap 'kill -TERM $SERVER_PID; kill -TERM $NGINX_PID; exit' TERM INT + set -eu +echo "==> Starting Ente Cloudron app..." + # 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" -find /app/code -type d -maxdepth 3 -not -path "*/node_modules/*" -not -path "*/\.*" | sort +# Add comment about Cloudron filesystem limitations +echo "==> NOTE: Running in Cloudron environment with limited write access" +echo "==> Writable directories: /app/data, /tmp, /run" -echo "==> DEBUG: Looking for Go files" -find /app/code -name "*.go" | grep -v test | sort | head -10 +# One-time initialization tracking +if [[ ! -f /app/data/.initialized ]]; then + echo "==> Fresh installation, setting up data directory..." + + echo "==> DEBUG: Full repository structure at /app/code" + find /app/code -type d -maxdepth 3 -not -path "*/node_modules/*" -not -path "*/\.*" | sort -echo "==> DEBUG: Looking for server-related directories" -find /app/code -type d -path "*/server*" -o -path "*/museum*" | sort + echo "==> DEBUG: Looking for Go files" + find /app/code -name "*.go" | grep -v test | sort | head -10 -echo "==> DEBUG: All package.json files in repository" -find /app/code -name "package.json" -not -path "*/node_modules/*" | sort + echo "==> DEBUG: Looking for server-related directories" + find /app/code -type d -path "*/server*" -o -path "*/museum*" | sort -echo "==> DEBUG: Looking for web app directories" -find /app/code -type d -path "*/web*" | sort + echo "==> DEBUG: All package.json files in repository" + find /app/code -name "package.json" -not -path "*/node_modules/*" | 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 + echo "==> DEBUG: Looking for web app directories" + find /app/code -type d -path "*/web*" | sort -# Create config template file on first run -if [[ ! -f /app/data/config/config.yaml ]]; then + 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" # Generate random secrets @@ -97,6 +109,10 @@ S3_SECRET_KEY=aPdHB4fkvQAuJUqPhneoIDcHEHee9cvP2j0nKSly S3_PREFIX=ente/ EOT echo "==> Test S3 configuration created for Wasabi" + + # Mark initialization as complete + touch /app/data/.initialized + echo "==> Initialization complete" fi # Check if s3.env exists @@ -342,6 +358,16 @@ export ENTE_LOG_LEVEL=debug echo "==> Testing API connectivity" 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 export ENTE_DB_USER="${CLOUDRON_POSTGRESQL_USERNAME}" export ENTE_DB_PASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}" @@ -361,10 +387,7 @@ export REMOTE_STORAGE_PREFIX="${S3_PREFIX:-ente/}" # Change ownership to cloudron user chown -R cloudron:cloudron /app/data - -# Add comment about Cloudron filesystem limitations -echo "==> NOTE: Running in Cloudron environment with limited write access" -echo "==> Writable directories: /app/data, /tmp, /run" +chown -R cloudron:cloudron /run/nginx # Start Museum server on port 8000 (different from the NGINX port 8080) echo "==> Starting Museum server" @@ -510,4 +533,13 @@ fi # Serve the static web apps in the foreground using our custom nginx config echo "==> Running NGINX in the foreground with custom configuration" -exec nginx -c /app/data/nginx/ente.conf \ No newline at end of file +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 \ No newline at end of file