#!/bin/bash set -eu # Create necessary directories mkdir -p /app/data/config /app/data/storage # Print debug information about repository structure echo "==> DEBUG: Repository structure at /app/code" find /app/code -type d -not -path "*/node_modules/*" -not -path "*/\.*" | sort echo "==> DEBUG: All package.json files in repository" find /app/code -name "package.json" -not -path "*/node_modules/*" | sort # Create config template file on first run if [[ ! -f /app/data/config/config.yaml ]]; then echo "==> First run - creating configuration template" # Replace variables in template for things we know sed \ -e "s|%%POSTGRESQL_HOST%%|${CLOUDRON_POSTGRESQL_HOST}|g" \ -e "s|%%POSTGRESQL_PORT%%|${CLOUDRON_POSTGRESQL_PORT}|g" \ -e "s|%%POSTGRESQL_USERNAME%%|${CLOUDRON_POSTGRESQL_USERNAME}|g" \ -e "s|%%POSTGRESQL_PASSWORD%%|${CLOUDRON_POSTGRESQL_PASSWORD}|g" \ -e "s|%%POSTGRESQL_DATABASE%%|${CLOUDRON_POSTGRESQL_DATABASE}|g" \ -e "s|%%APP_ORIGIN%%|${CLOUDRON_APP_ORIGIN}|g" \ -e "s|%%MAIL_SMTP_SERVER%%|${CLOUDRON_MAIL_SMTP_SERVER}|g" \ -e "s|%%MAIL_SMTP_PORT%%|${CLOUDRON_MAIL_SMTP_PORT}|g" \ -e "s|%%MAIL_SMTP_USERNAME%%|${CLOUDRON_MAIL_SMTP_USERNAME}|g" \ -e "s|%%MAIL_SMTP_PASSWORD%%|${CLOUDRON_MAIL_SMTP_PASSWORD}|g" \ -e "s|%%MAIL_FROM%%|${CLOUDRON_MAIL_FROM}|g" \ -e "s|%%MAIL_FROM_DISPLAY_NAME%%|${CLOUDRON_MAIL_FROM_DISPLAY_NAME}|g" \ /app/pkg/config.template.yaml > /app/data/config/config.yaml # Create an S3 configuration file template cat > /app/data/config/s3.env.template < IMPORTANT: S3 storage configuration required" echo " 1. Edit the file at /app/data/config/s3.env.template" echo " 2. Fill in your S3 credentials" echo " 3. Rename the file to s3.env" echo " 4. Restart the app" # Exit with a message if this is the first run if [[ ! -f /app/data/config/s3.env ]]; then echo "==> Exiting. Please configure S3 storage and restart the app." exit 1 fi fi # Check if s3.env exists if [[ ! -f /app/data/config/s3.env ]]; then echo "==> ERROR: S3 configuration not found" echo " Please configure S3 storage by editing /app/data/config/s3.env.template" echo " and renaming it to s3.env, then restart the app." exit 1 fi # Load S3 environment variables source /app/data/config/s3.env # Update the config file with S3 credentials sed -i \ -e "s|%%S3_ENDPOINT%%|${S3_ENDPOINT}|g" \ -e "s|%%S3_REGION%%|${S3_REGION}|g" \ -e "s|%%S3_BUCKET%%|${S3_BUCKET}|g" \ -e "s|%%S3_ACCESS_KEY%%|${S3_ACCESS_KEY}|g" \ -e "s|%%S3_SECRET_KEY%%|${S3_SECRET_KEY}|g" \ -e "s|%%S3_PREFIX%%|${S3_PREFIX:-ente/}|g" \ /app/data/config/config.yaml # Find the correct server directory SERVER_DIR="" # More thorough search for server directory # Try specific paths first for dir in "/app/code/server" "/app/code/packages/server"; do if [ -f "$dir/package.json" ]; then SERVER_DIR="$dir" break fi done # If not found, search for any directory containing package.json with "server" in its path if [ -z "$SERVER_DIR" ]; then SERVER_CANDIDATES=$(find /app/code -name "package.json" -not -path "*/node_modules/*" -path "*/server*" | sort) if [ -n "$SERVER_CANDIDATES" ]; then FIRST_CANDIDATE=$(echo "$SERVER_CANDIDATES" | head -n 1) SERVER_DIR=$(dirname "$FIRST_CANDIDATE") fi fi # Last resort - look for package.json in the monorepo root if [ -z "$SERVER_DIR" ] && [ -f "/app/code/package.json" ]; then echo "==> Using root package.json as server directory" SERVER_DIR="/app/code" fi if [ -z "$SERVER_DIR" ]; then echo "==> ERROR: Could not find server directory with package.json" echo " Server directory structure may have changed." echo " Please check the ente repository structure." exit 1 fi echo "==> Found server directory at $SERVER_DIR" echo "==> Contents of $SERVER_DIR:" ls -la "$SERVER_DIR" echo "==> Contents of package.json:" cat "$SERVER_DIR/package.json" # Change to server directory cd "$SERVER_DIR" # Run database migrations if the script exists if grep -q "\"db-migrate\"" package.json; then echo "==> Running database migrations" NODE_ENV=production yarn run db-migrate || { echo "==> WARNING: Database migration failed, but continuing anyway" } else echo "==> Skipping database migrations (script not found)" fi # Check for alternative start scripts if "serve" doesn't exist SERVE_SCRIPT="" for script in "serve" "start" "dev" "server"; do if grep -q "\"$script\"" package.json; then SERVE_SCRIPT="$script" break fi done # Change ownership to cloudron user chown -R cloudron:cloudron /app/data # Start the server if [ -n "$SERVE_SCRIPT" ]; then echo "==> Starting Ente server with script: $SERVE_SCRIPT" exec /usr/local/bin/gosu cloudron:cloudron NODE_ENV=production CONFIG_PATH=/app/data/config/config.yaml yarn run "$SERVE_SCRIPT" else echo "==> ERROR: Could not find any server script in package.json" echo " Available scripts:" grep -o '"[^"]*"\s*:\s*"[^"]*"' package.json | grep -o '"[^"]*"' | head -n 1 | tr -d '"' echo " Please check the ente repository structure." exit 1 fi