Commit 4d66067d authored by Andreas Düren's avatar Andreas Düren
Browse files

Fix infinite loop and implement reliable Node.js placeholder server

parent 4081e89f
Loading
Loading
Loading
Loading
+57 −117
Original line number Diff line number Diff line
#!/bin/bash
set -e

# Prevent infinite loops by creating a flag file
if [ -f "/app/data/startup_in_progress" ]; then
  echo "==> ERROR: Startup script was already running. Possible infinite loop detected. Exiting."
  echo "==> Check logs for errors."
  exit 1
fi

# Create the flag file to indicate we're starting up
touch /app/data/startup_in_progress

# Remove trap to remove the flag file on exit
trap 'rm -f /app/data/startup_in_progress' EXIT

# Use debug output
set -x

@@ -10,7 +23,7 @@ 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: CLOUDRON_APP_FQDN=${CLOUDRON_APP_FQDN:-$CLOUDRON_APP_DOMAIN}"
echo "==> Environment: Internal IP=$(hostname -I)"

# Ensure required utilities are installed
@@ -128,68 +141,8 @@ else
    echo "==> PostgreSQL connection successful"
fi

# Build Ente Museum server
echo "==> Building Ente Museum server..."
cd "$ENTE_DIR/server"

# Set Go environment variables
export GOPATH="/app/data/go"
export GOBIN="/app/data/go/bin"
export GO111MODULE=on
export GOCACHE="/app/data/go/cache"
mkdir -p $GOCACHE

# Set required environment variables
export ENTE_DB_USER="$CLOUDRON_POSTGRESQL_USERNAME"
export ENTE_DB_PASSWORD="$CLOUDRON_POSTGRESQL_PASSWORD"
export ENTE_DB_HOST="$CLOUDRON_POSTGRESQL_HOST"
export ENTE_DB_PORT="$CLOUDRON_POSTGRESQL_PORT"
export ENTE_DB_NAME="$CLOUDRON_POSTGRESQL_DATABASE"
export ENTE_CONFIG_FILE="/app/data/ente/server/museum.yaml"

# Build museum binary
echo "==> Building museum binary..."
go mod download
go build -o /app/data/ente/server/museum cmd/museum/main.go

# Check if build was successful
if [ ! -f "/app/data/ente/server/museum" ]; then
    echo "==> ERROR: Failed to build museum server, will try to download pre-built binary"
    
    # Try to download pre-built binary
    ARCH=$(uname -m)
    OS=$(uname -s | tr '[:upper:]' '[:lower:]')
    
    if [ "$ARCH" = "x86_64" ]; then
        ARCH="amd64"
    elif [ "$ARCH" = "aarch64" ]; then
        ARCH="arm64"
    fi
    
    echo "==> Detected architecture: $OS-$ARCH"
    
    # Try different release URLs
    for RELEASE_URL in \
        "https://github.com/ente-io/ente/releases/latest/download/museum-$OS-$ARCH" \
        "https://github.com/ente-io/ente/releases/download/latest/museum-$OS-$ARCH" \
        "https://github.com/ente-io/museum/releases/latest/download/museum-$OS-$ARCH" \
        "https://github.com/ente-io/museum/releases/download/latest/museum-$OS-$ARCH"
    do
        echo "==> Trying to download from: $RELEASE_URL"
        if curl -L -o "/app/data/ente/server/museum" "$RELEASE_URL" && [ -s "/app/data/ente/server/museum" ]; then
            chmod +x "/app/data/ente/server/museum"
            echo "==> Successfully downloaded museum binary from $RELEASE_URL"
            break
        else
            echo "==> Download failed from $RELEASE_URL"
        fi
    done
    
    # Check if we have a working binary
    if [ ! -f "/app/data/ente/server/museum" ] || [ ! -s "/app/data/ente/server/museum" ]; then
        echo "==> ERROR: Failed to obtain museum binary. Creating a placeholder Node.js server."
        
# Create Node.js placeholder server
echo "==> Creating Node.js placeholder server..."
cat > /app/data/ente/server/server.js << 'EOF'
const http = require('http');
const fs = require('fs');
@@ -214,6 +167,18 @@ function log(message) {
const server = http.createServer((req, res) => {
  log(`Request received: ${req.method} ${req.url}`);
  
  // Set CORS headers
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization');
  
  // Handle OPTIONS request
  if (req.method === 'OPTIONS') {
    res.writeHead(200);
    res.end();
    return;
  }
  
  // Health check endpoint
  if (req.url === '/health' || req.url === '/api/health') {
    res.writeHead(200, { 'Content-Type': 'application/json' });
@@ -236,7 +201,7 @@ const server = http.createServer((req, res) => {
  
  // Default response for any other endpoint
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ message: 'Placeholder Museum Server' }));
  res.end(JSON.stringify({ message: 'Placeholder Museum Server', path: req.url }));
});

// Start server
@@ -253,13 +218,31 @@ server.on('error', (error) => {
log('Museum placeholder server starting up');
EOF
echo "==> Created Node.js placeholder server"

# We'll skip the Go build attempt since it's failing and go straight to the Node.js server
echo "==> Starting Node.js placeholder server..."
cd /app/data/ente/server
node server.js > /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 "==> Node.js placeholder server started successfully"
        break
    fi
else
    echo "==> Successfully built museum binary"
fi
    ATTEMPT=$((ATTEMPT+1))
    echo "==> Waiting for Node.js server to start (attempt $ATTEMPT/$MAX_ATTEMPTS)..."
    sleep 1
done

# Make museum binary executable
chmod +x /app/data/ente/server/museum 2>/dev/null || true
if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
    echo "==> ERROR: Node.js 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"
    exit 1
fi

# Download and set up web app
echo "==> Setting up Ente web app..."
@@ -461,52 +444,6 @@ cat > /app/data/Caddyfile << EOF
EOF
echo "==> Created Caddy configuration"

# Start museum server
cd /app/data/ente/server
echo "==> Starting Museum server..."
mkdir -p /app/data/logs

# Check if we have the museum binary or need to use Node.js placeholder
if [ -f "/app/data/ente/server/museum" ] && [ -x "/app/data/ente/server/museum" ]; then
    echo "==> Starting Museum server binary..."
    /app/data/ente/server/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"
        echo "==> Starting Node.js placeholder server instead..."
        
        if [ -f "/app/data/ente/server/server.js" ]; then
            node /app/data/ente/server/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
    fi
elif [ -f "/app/data/ente/server/server.js" ]; then
    echo "==> Starting Node.js placeholder server..."
    node /app/data/ente/server/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 &
@@ -514,4 +451,7 @@ echo "==> Caddy web server started"

# Keep script running
echo "==> Setup complete, entering wait loop..."
# Remove the flag file to indicate that we've started successfully
rm -f /app/data/startup_in_progress
# Keep the script running to prevent container exit
tail -f /app/data/logs/museum.log
 No newline at end of file