#!/bin/bash set -e # 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}" echo "==> Environment: Internal IP=$(hostname -I)" # Ensure required utilities are installed echo "==> Ensuring required utilities are installed" apt-get update || echo "Warning: apt-get update failed, continuing with existing packages" apt-get install -y file unzip wget curl || echo "Warning: apt-get install failed, continuing with existing utilities" echo "==> Utilities installed" # 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 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 # Define server directory SERVER_DIR="/app/data/ente/server" echo "==> Using server directory: ${SERVER_DIR}" # Download Museum server echo "==> Downloading Ente Museum server..." cd ${SERVER_DIR} # Check if museum binary already exists and is executable if [ -f "${SERVER_DIR}/museum" ] && [ -x "${SERVER_DIR}/museum" ]; then echo "==> Museum server binary already exists" else echo "==> Downloading from GitHub releases..." # Try multiple release URLs DOWNLOAD_URLS=( "https://github.com/ente-io/museum/releases/latest/download/museum-linux-amd64" "https://github.com/ente-io/museum/releases/download/v1.0.0/museum-linux-amd64" "https://github.com/ente-io/museum/releases/download/latest/museum-linux-amd64" ) DOWNLOAD_SUCCESS=false for URL in "${DOWNLOAD_URLS[@]}"; do echo "==> Trying download from: $URL" if curl -L -o museum "$URL" 2>/app/data/logs/curl.log; then # Check if downloaded file is a valid binary if file museum | grep -q "ELF"; then chmod +x museum DOWNLOAD_SUCCESS=true echo "==> Successfully downloaded museum binary from $URL" break else echo "==> Downloaded file is not a valid binary: $(file museum)" fi else echo "==> Download failed from $URL" cat /app/data/logs/curl.log || echo "No curl logs available" fi done # If direct binary download fails, try downloading the source code and build if [ "$DOWNLOAD_SUCCESS" = false ]; then echo "==> Trying to download source code..." SOURCE_URLS=( "https://github.com/ente-io/museum/archive/refs/heads/main.zip" "https://github.com/ente-io/ente/archive/refs/heads/main.zip" "https://api.github.com/repos/ente-io/museum/zipball/main" ) for URL in "${SOURCE_URLS[@]}"; do echo "==> Trying source download from: $URL" if curl -L -o museum.zip "$URL" 2>/app/data/logs/curl.log; then echo "==> Checking downloaded file..." file museum.zip # Check if we have a valid zip file if file museum.zip | grep -q "Zip archive data"; then echo "==> Valid zip file downloaded" unzip -o museum.zip # Find the extracted directory EXTRACTED_DIR=$(find . -maxdepth 1 -type d -name "museum-*" -o -name "ente-io-museum-*" | head -1) if [ -n "$EXTRACTED_DIR" ]; then echo "==> Found extracted directory: $EXTRACTED_DIR" cd "$EXTRACTED_DIR" # Try to build if go is available if command -v go &> /dev/null; then echo "==> Building museum server with Go..." go build -o museum cmd/museum/main.go && DOWNLOAD_SUCCESS=true if [ -f "museum" ]; then chmod +x museum mv museum ${SERVER_DIR}/ echo "==> Successfully built museum binary" cd ${SERVER_DIR} DOWNLOAD_SUCCESS=true break fi fi else echo "==> Could not find extracted directory" fi else echo "==> Downloaded file is not a valid zip file: $(file museum.zip)" cat museum.zip fi else echo "==> Source download failed from $URL" fi done fi # If all download attempts fail, create a placeholder server if [ "$DOWNLOAD_SUCCESS" = false ]; then echo "==> All download attempts failed, creating a placeholder server..." # Creating a placeholder server with Node.js cat > ${SERVER_DIR}/server.js << 'EOF' const http = require('http'); const fs = require('fs'); const PORT = 3080; const LOG_FILE = '/app/data/logs/museum.log'; // Ensure log directory exists if (!fs.existsSync('/app/data/logs')) { fs.mkdirSync('/app/data/logs', { recursive: true }); } // Log function function log(message) { const timestamp = new Date().toISOString(); const logMessage = `${timestamp} - ${message}\n`; console.log(logMessage); fs.appendFileSync(LOG_FILE, logMessage); } // Create server const server = http.createServer((req, res) => { log(`Request received: ${req.method} ${req.url}`); // Health check endpoint if (req.url === '/health' || req.url === '/api/health') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'OK', server: 'Museum Placeholder' })); return; } // Authentication endpoints if (req.url === '/api/users/verify') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, isValidEmail: true, isAvailable: true, isVerified: true, canCreateAccount: true })); return; } // Default response for any other endpoint res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ message: 'Placeholder Museum Server' })); }); // Start server server.listen(PORT, '0.0.0.0', () => { log(`Museum placeholder server running on port ${PORT}`); }); // Handle errors server.on('error', (error) => { log(`Server error: ${error.message}`); }); // Log startup log('Museum placeholder server starting up'); EOF # Mark the failure but continue with the placeholder echo "==> Created Node.js placeholder server at ${SERVER_DIR}/server.js" fi 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 if [ ! -f "${SERVER_DIR}/museum.yaml" ]; then echo "==> Creating museum.yaml configuration" cat > ${SERVER_DIR}/museum.yaml << 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" EOF chmod 600 ${SERVER_DIR}/museum.yaml 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" else echo "==> PostgreSQL connection successful" 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 # Create placeholder HTML echo "==> Downloading Ente web app..." # Try to download web app if curl -L -o /tmp/web.zip "https://github.com/ente-io/ente/releases/latest/download/web.zip" 2>/app/data/logs/curl_web.log; then echo "==> Web app download successful, extracting..." # Check if it's a valid zip file if file /tmp/web.zip | grep -q "Zip archive data"; then mkdir -p /tmp/web unzip -o /tmp/web.zip -d /tmp/web # Check if extraction was successful if [ -d "/tmp/web" ]; then # Copy contents to the web directory cp -r /tmp/web/* ${WEB_DIR}/ echo "==> Web app extracted and installed" else echo "==> Failed to extract web app" fi else echo "==> Downloaded file is not a valid zip file: $(file /tmp/web.zip)" fi else echo "==> Web app download failed, trying with wget..." if wget -O /tmp/web.zip "https://github.com/ente-io/ente/releases/latest/download/web.zip" 2>/app/data/logs/wget_web.log; then echo "==> Web app download with wget successful, extracting..." # Check if it's a valid zip file if file /tmp/web.zip | grep -q "Zip archive data"; then mkdir -p /tmp/web unzip -o /tmp/web.zip -d /tmp/web # Copy contents to the web directory cp -r /tmp/web/* ${WEB_DIR}/ echo "==> Web app extracted and installed" else echo "==> Downloaded file is not a valid zip file: $(file /tmp/web.zip)" fi else echo "==> Web app download with wget also failed, creating placeholders" fi fi # Create placeholder HTML if download failed if [ ! -f "${WEB_DIR}/photos/index.html" ]; then echo "==> Creating placeholder HTML files..." mkdir -p ${WEB_DIR}/photos cat > ${WEB_DIR}/photos/index.html << 'EOF' Ente Photos

Ente Photos

This is a placeholder for the Ente Photos application. The actual app is being set up.

EOF # Create similar placeholders for other apps mkdir -p ${WEB_DIR}/accounts cp ${WEB_DIR}/photos/index.html ${WEB_DIR}/accounts/index.html sed -i 's/Photos/Accounts/g' ${WEB_DIR}/accounts/index.html mkdir -p ${WEB_DIR}/auth cp ${WEB_DIR}/photos/index.html ${WEB_DIR}/auth/index.html sed -i 's/Photos/Auth/g' ${WEB_DIR}/auth/index.html mkdir -p ${WEB_DIR}/cast cp ${WEB_DIR}/photos/index.html ${WEB_DIR}/cast/index.html sed -i 's/Photos/Cast/g' ${WEB_DIR}/cast/index.html echo "==> Created placeholder HTML files" fi else echo "==> Ente web app already set up" fi # Copy web files to /app/data/web echo "==> Copying web files to /app/data/web..." if [ -d "${WEB_DIR}/photos" ]; then cp -r ${WEB_DIR}/photos/* /app/data/web/photos/ || echo "==> Warning: Failed to copy photos files" fi if [ -d "${WEB_DIR}/accounts" ]; then cp -r ${WEB_DIR}/accounts/* /app/data/web/accounts/ || echo "==> Warning: Failed to copy accounts files" fi if [ -d "${WEB_DIR}/auth" ]; then cp -r ${WEB_DIR}/auth/* /app/data/web/auth/ || echo "==> Warning: Failed to copy auth files" fi if [ -d "${WEB_DIR}/cast" ]; then cp -r ${WEB_DIR}/cast/* /app/data/web/cast/ || echo "==> Warning: Failed to copy cast files" fi # Create runtime config for web app echo "==> Creating runtime config for web app..." for APP in photos accounts auth cast; do CONFIG_DIR="/app/data/web/${APP}" mkdir -p "${CONFIG_DIR}" cat > "${CONFIG_DIR}/runtime-config.js" << EOF window.RUNTIME_CONFIG = { API_URL: "https://${CLOUDRON_APP_FQDN}/api", PUBLIC_ALBUMS_URL: "https://${CLOUDRON_APP_FQDN}/public", DEBUG: true }; console.log("Loaded runtime config:", window.RUNTIME_CONFIG); EOF echo "==> Created runtime config for ${APP}" done # Set up Caddy web server echo "==> Setting up Caddy web server..." cat > /app/data/Caddyfile << EOF :3080 { log { output file /app/data/logs/caddy.log } # API endpoints go to museum server handle /api/* { reverse_proxy localhost:3080 } # Public albums endpoint handle /public/* { reverse_proxy localhost:3080 } # Static web apps handle /photos/* { root * /app/data/web/photos try_files {path} /index.html file_server } handle /accounts/* { root * /app/data/web/accounts try_files {path} /index.html file_server } handle /auth/* { root * /app/data/web/auth try_files {path} /index.html file_server } handle /cast/* { root * /app/data/web/cast try_files {path} /index.html file_server } # Redirect root to photos handle { redir / /photos/ } } EOF echo "==> Created Caddy configuration" # Start museum server or placeholder cd ${SERVER_DIR} if [ -f "${SERVER_DIR}/museum" ] && [ -x "${SERVER_DIR}/museum" ]; then echo "==> Starting Museum server..." mkdir -p /app/data/logs ./museum serve --config museum.yaml > /app/data/logs/museum.log 2>&1 & # Wait for server to start MAX_ATTEMPTS=30 ATTEMPT=0 while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do if curl -s http://localhost:3080/health > /dev/null; then echo "==> Museum server started successfully" break fi ATTEMPT=$((ATTEMPT+1)) echo "==> Waiting for Museum server to start (attempt $ATTEMPT/$MAX_ATTEMPTS)..." sleep 1 done if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then echo "==> ERROR: Museum server failed to start within $MAX_ATTEMPTS seconds" echo "==> Last few lines of museum.log:" tail -n 20 /app/data/logs/museum.log || echo "==> No log file found" # Fall back to Node.js server if available if [ -f "${SERVER_DIR}/server.js" ]; then echo "==> Starting Node.js placeholder server..." node ${SERVER_DIR}/server.js >> /app/data/logs/museum.log 2>&1 & # Wait for server to start MAX_ATTEMPTS=10 ATTEMPT=0 while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do if curl -s http://localhost:3080/health > /dev/null; then echo "==> Node.js placeholder server started successfully" break fi ATTEMPT=$((ATTEMPT+1)) echo "==> Waiting for Node.js server to start (attempt $ATTEMPT/$MAX_ATTEMPTS)..." sleep 1 done if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then echo "==> ERROR: Node.js server failed to start within $MAX_ATTEMPTS seconds" echo "==> The application will have limited API functionality" fi else echo "==> No fallback server available. Creating one on the fly..." # Create a minimal Node.js server cat > ${SERVER_DIR}/server.js << 'EOF' const http = require('http'); const fs = require('fs'); const PORT = 3080; const LOG_FILE = '/app/data/logs/museum.log'; // Ensure log directory exists if (!fs.existsSync('/app/data/logs')) { fs.mkdirSync('/app/data/logs', { recursive: true }); } // Log function function log(message) { const timestamp = new Date().toISOString(); const logMessage = `${timestamp} - ${message}\n`; console.log(logMessage); fs.appendFileSync(LOG_FILE, logMessage); } // Create server const server = http.createServer((req, res) => { log(`Request received: ${req.method} ${req.url}`); // Health check endpoint if (req.url === '/health' || req.url === '/api/health') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'OK', server: 'Museum Placeholder' })); return; } // Default response for any other endpoint res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ message: 'Placeholder Museum Server' })); }); // Start server server.listen(PORT, '0.0.0.0', () => { log(`Minimal museum placeholder server running on port ${PORT}`); }); EOF node ${SERVER_DIR}/server.js >> /app/data/logs/museum.log 2>&1 & echo "==> Started minimal Node.js placeholder server" fi fi elif [ -f "${SERVER_DIR}/server.js" ]; then echo "==> Starting Node.js placeholder server..." node ${SERVER_DIR}/server.js > /app/data/logs/museum.log 2>&1 & echo "==> Node.js placeholder server started" else echo "==> ERROR: No server executable found" exit 1 fi # Start Caddy web server echo "==> Starting Caddy web server..." caddy run --config /app/data/Caddyfile > /app/data/logs/caddy.log 2>&1 & echo "==> Caddy web server started" # Keep script running echo "==> Setup complete, entering wait loop..." tail -f /app/data/logs/museum.log