#!/bin/bash # Disable 'exit on error' to handle errors more gracefully set +e # Prevent infinite loops by checking and creating a flag file if [ -f "/app/data/startup_in_progress" ]; then # Check if flag file is older than 60 seconds if [ "$(find /app/data/startup_in_progress -mmin +1)" ]; then echo "==> WARNING: Found old startup flag, removing and continuing" rm -f /app/data/startup_in_progress else echo "==> ERROR: Startup script was already running (started less than 60 seconds ago). Possible infinite loop detected. Exiting." echo "==> Check logs for errors." exit 1 fi fi # Create the flag file to indicate we're starting up echo "$(date): Starting up" > /app/data/startup_in_progress # Remove the flag file on exit trap 'rm -f /app/data/startup_in_progress' EXIT # Use debug output set -x # Declare that we're running in a Cloudron environment echo "==> Starting Ente Cloudron app..." echo "==> NOTE: Running in Cloudron environment with limited write access" echo "==> Writable directories: /app/data, /tmp, /run" echo "==> Current directory: $(pwd)" echo "==> Environment: CLOUDRON_APP_DOMAIN=${CLOUDRON_APP_DOMAIN:-localhost}" echo "==> Environment: CLOUDRON_APP_FQDN=${CLOUDRON_APP_FQDN:-$CLOUDRON_APP_DOMAIN}" echo "==> Environment: Internal IP=$(hostname -I)" # Create necessary directories mkdir -p /app/data/ente/server mkdir -p /app/data/ente/web mkdir -p /app/data/logs mkdir -p /app/data/web/photos mkdir -p /app/data/web/accounts mkdir -p /app/data/web/auth mkdir -p /app/data/web/cast # Create Go directories mkdir -p /app/data/go/pkg mkdir -p /app/data/go/bin mkdir -p /app/data/go/src echo "==> Created all necessary directories" # Directory listings for debugging echo "==> Directory listing of /app/data:" ls -la /app/data echo "==> Directory listing of /app/data/web:" ls -la /app/data/web echo "==> Directory listing of /app/data/ente:" ls -la /app/data/ente # Clone Ente repository if it doesn't exist ENTE_DIR="/app/data/ente/repository" if [ ! -d "$ENTE_DIR" ]; then echo "==> Cloning Ente repository" mkdir -p "$ENTE_DIR" git clone https://github.com/ente-io/ente "$ENTE_DIR" echo "==> Ente repository cloned successfully" else echo "==> Ente repository already exists, pulling latest changes" cd "$ENTE_DIR" git pull fi # Set up S3 config if [ ! -f "/app/data/s3.env" ]; then echo "==> Creating default S3 environment file" cat > /app/data/s3.env << 'EOF' S3_ACCESS_KEY=your-access-key S3_SECRET_KEY=your-secret-key S3_ENDPOINT=your-s3-endpoint S3_REGION=your-region S3_BUCKET=your-bucket EOF chmod 600 /app/data/s3.env echo "==> Created S3 environment file at /app/data/s3.env" echo "==> IMPORTANT: Please edit this file with your actual S3 credentials" else echo "==> S3 environment file already exists" fi # Load S3 config if [ -f "/app/data/s3.env" ]; then source /app/data/s3.env echo "==> Loaded S3 configuration" fi # Create museum.yaml config MUSEUM_CONFIG="/app/data/ente/server/museum.yaml" if [ ! -f "$MUSEUM_CONFIG" ]; then echo "==> Creating museum.yaml configuration" cat > "$MUSEUM_CONFIG" << EOF port: 3080 host: 0.0.0.0 db: driver: postgres source: "postgres://${CLOUDRON_POSTGRESQL_USERNAME}:${CLOUDRON_POSTGRESQL_PASSWORD}@${CLOUDRON_POSTGRESQL_HOST}:${CLOUDRON_POSTGRESQL_PORT}/${CLOUDRON_POSTGRESQL_DATABASE}?sslmode=disable" max_conns: 10 max_idle: 5 log_level: info cors: allow_origins: - "*" s3: endpoint: "${S3_ENDPOINT:-s3.amazonaws.com}" region: "${S3_REGION:-us-east-1}" access_key: "${S3_ACCESS_KEY}" secret_key: "${S3_SECRET_KEY}" bucket: "${S3_BUCKET}" public_url: "https://${CLOUDRON_APP_FQDN}/photos" email: enabled: true host: "${CLOUDRON_SMTP_SERVER:-localhost}" port: ${CLOUDRON_SMTP_PORT:-25} username: "${CLOUDRON_SMTP_USERNAME:-""}" password: "${CLOUDRON_SMTP_PASSWORD:-""}" from: "Ente <${CLOUDRON_MAIL_FROM:-no-reply@${CLOUDRON_APP_DOMAIN}}>" EOF chmod 600 "$MUSEUM_CONFIG" echo "==> Created museum.yaml configuration" else echo "==> museum.yaml configuration already exists" fi # Test PostgreSQL connectivity echo "==> Testing PostgreSQL connectivity..." if ! PGPASSWORD="$CLOUDRON_POSTGRESQL_PASSWORD" psql -h "$CLOUDRON_POSTGRESQL_HOST" -p "$CLOUDRON_POSTGRESQL_PORT" -U "$CLOUDRON_POSTGRESQL_USERNAME" -d "$CLOUDRON_POSTGRESQL_DATABASE" -c "SELECT 1;" > /dev/null 2>&1; then echo "==> ERROR: Failed to connect to PostgreSQL" echo "==> Connection details:" echo "==> Host: $CLOUDRON_POSTGRESQL_HOST" echo "==> Port: $CLOUDRON_POSTGRESQL_PORT" echo "==> User: $CLOUDRON_POSTGRESQL_USERNAME" echo "==> Database: $CLOUDRON_POSTGRESQL_DATABASE" exit 1 else echo "==> PostgreSQL connection successful" fi # Build or download Museum server echo "==> Setting up Museum server..." MUSEUM_BIN="/app/data/ente/server/museum" # Check if the Museum binary already exists and is executable if [ -f "$MUSEUM_BIN" ] && [ -x "$MUSEUM_BIN" ]; then echo "==> Museum server binary already exists, skipping build/download" else # Try to build Museum server from source echo "==> Attempting to build Museum server from source..." # Check if Go is installed if command -v go >/dev/null 2>&1; then echo "==> Go is installed, building Museum server..." # Navigate to the server directory and build cd "$ENTE_DIR/server" export GOPATH="/app/data/go" export PATH="$GOPATH/bin:$PATH" # Install dependencies if needed if command -v apt-get >/dev/null 2>&1; then apt-get update && apt-get install -y gcc libsodium-dev pkg-config fi # Build the server go build -o "$MUSEUM_BIN" ./cmd/museum if [ $? -eq 0 ] && [ -f "$MUSEUM_BIN" ] && [ -x "$MUSEUM_BIN" ]; then echo "==> Successfully built Museum server" else echo "==> Failed to build Museum server, will try to download pre-built binary" # Determine architecture for downloading ARCH=$(uname -m) OS=$(uname -s | tr '[:upper:]' '[:lower:]') if [ "$ARCH" == "x86_64" ]; then ARCH="amd64"; fi if [ "$ARCH" == "aarch64" ] || [ "$ARCH" == "arm64" ]; then ARCH="arm64"; fi # Try to download from GitHub releases echo "==> Downloading Museum server binary for ${OS}-${ARCH}..." if ! curl -L -o "$MUSEUM_BIN" "https://github.com/ente-io/ente/releases/latest/download/museum-${OS}-${ARCH}"; then echo "==> Download failed, trying alternative URL..." if ! curl -L -o "$MUSEUM_BIN" "https://github.com/ente-io/ente/releases/download/latest/museum-${OS}-${ARCH}"; then echo "==> All download attempts failed" # Return error and let the next section handle it false fi fi if [ -f "$MUSEUM_BIN" ]; then chmod +x "$MUSEUM_BIN" echo "==> Successfully downloaded Museum server binary" fi fi else echo "==> Go is not installed, downloading pre-built Museum server binary..." # Determine architecture for downloading ARCH=$(uname -m) OS=$(uname -s | tr '[:upper:]' '[:lower:]') if [ "$ARCH" == "x86_64" ]; then ARCH="amd64"; fi if [ "$ARCH" == "aarch64" ] || [ "$ARCH" == "arm64" ]; then ARCH="arm64"; fi # Try to download from GitHub releases echo "==> Downloading Museum server binary for ${OS}-${ARCH}..." if ! curl -L -o "$MUSEUM_BIN" "https://github.com/ente-io/ente/releases/latest/download/museum-${OS}-${ARCH}"; then echo "==> Download failed, trying alternative URL..." if ! curl -L -o "$MUSEUM_BIN" "https://github.com/ente-io/ente/releases/download/latest/museum-${OS}-${ARCH}"; then echo "==> All download attempts failed" # Create guide on setting up the server manually cat > /app/data/MANUAL-SETUP-REQUIRED.md << 'EOF' # Manual Setup Required The automatic setup of the Museum server failed. Please follow these steps to set up the server manually: 1. Connect to your Cloudron server via SSH 2. Download the Museum server binary manually using one of these methods: ``` # Install Go and build from source apt-get update && apt-get install -y golang-go gcc libsodium-dev pkg-config cd /app/data/ente/repository/server export GOPATH="/app/data/go" export PATH="$GOPATH/bin:$PATH" go build -o /app/data/ente/server/museum ./cmd/museum chmod +x /app/data/ente/server/museum ``` Or download a pre-built binary if available: ``` # Determine architecture ARCH=$(uname -m) OS=$(uname -s | tr '[:upper:]' '[:lower:]') if [ "$ARCH" == "x86_64" ]; then ARCH="amd64"; fi if [ "$ARCH" == "aarch64" ] || [ "$ARCH" == "arm64" ]; then ARCH="arm64"; fi # Try to download from GitHub releases curl -L -o /app/data/ente/server/museum "https://github.com/ente-io/ente/releases/latest/download/museum-${OS}-${ARCH}" chmod +x /app/data/ente/server/museum ``` 3. Restart the Ente app in your Cloudron dashboard EOF echo "==> Created guide for manual setup" # Continue and rely on Caddy to serve static files echo "==> The application will run with limited functionality without the Museum server" fi fi if [ -f "$MUSEUM_BIN" ]; then chmod +x "$MUSEUM_BIN" echo "==> Successfully downloaded Museum server binary" fi fi fi # Download and set up web app echo "==> Setting up Ente web app..." WEB_DIR="/app/data/ente/web" if [ ! -d "${WEB_DIR}/photos" ] || [ ! -f "${WEB_DIR}/photos/index.html" ]; then echo "==> Creating placeholder HTML files..." mkdir -p ${WEB_DIR}/photos cat > ${WEB_DIR}/photos/index.html << 'EOF'
This is the Ente Photos application running on Cloudron. To complete the setup, you need to:
/app/data/s3.env
For more information, visit Ente Self-Hosting Documentation