#!/bin/bash set -eu # Create necessary directories mkdir -p /app/data/config /app/data/storage # 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="" for dir in "/app/code/server" "/app/code/packages/server"; do if [ -f "$dir/package.json" ]; then SERVER_DIR="$dir" break fi done 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" # 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 # Change ownership to cloudron user chown -R cloudron:cloudron /app/data # Start the server echo "==> Starting Ente server" if grep -q "\"serve\"" package.json; then exec /usr/local/bin/gosu cloudron:cloudron NODE_ENV=production CONFIG_PATH=/app/data/config/config.yaml yarn run serve else echo "==> ERROR: Could not find 'serve' script in package.json" echo " Please check the ente repository structure." exit 1 fi