Commit 1d30b3d9 authored by Andreas Düren's avatar Andreas Düren
Browse files

Update start.sh to handle Museum Go server component

parent fb0d4fd3
Loading
Loading
Loading
Loading
+101 −59
Original line number Diff line number Diff line
@@ -89,79 +89,121 @@ sed -i \
    -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
# Looking for Museum (Go server component)
echo "==> Looking for Museum (Go server component)"

# Find the server directory
if [ -d "/app/code/server" ]; then
    echo "==> Found server directory at /app/code/server"
    SERVER_DIR="/app/code/server"
else
    echo "==> ERROR: Could not find server directory at /app/code/server"
    echo "==> Looking for Museum executable or source in alternate locations"
    
    # Search for Museum executable or Go source files
    GO_FILES=$(find /app/code -name "*.go" -not -path "*/\.*" | sort)
    if [ -n "$GO_FILES" ]; then
        echo "==> Found Go files in repository:"
        echo "$GO_FILES"
        
        # Check for museum in Go files path
        if find /app/code -path "*/museum/*" -name "*.go" | grep -q .; then
            MUSEUM_DIR=$(find /app/code -path "*/museum/*" -name "*.go" | head -1 | xargs dirname)
            echo "==> Found Museum directory at $MUSEUM_DIR"
        else
            echo "==> No clear Museum directory found"
            echo "==> Using first Go file directory as fallback"
            FIRST_GO_FILE=$(echo "$GO_FILES" | head -1)
            MUSEUM_DIR=$(dirname "$FIRST_GO_FILE")
        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"
        # Check for cmd/museum pattern which is mentioned in docs
        if [ -d "/app/code/server/cmd/museum" ]; then
            MUSEUM_DIR="/app/code/server/cmd/museum"
            echo "==> Found Museum main directory at $MUSEUM_DIR"
        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."
        SERVER_DIR=$(dirname $(dirname "$MUSEUM_DIR"))
    else
        echo "==> FATAL ERROR: Could not find Go files in repository"
        echo "==> Repository structure:"
        find /app/code -type d -maxdepth 3 | sort
        exit 1
    fi
fi

echo "==> Found server directory at $SERVER_DIR"
echo "==> Selected server directory: $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"
# Check for server dependencies
echo "==> Installing Go dependencies"
if [ -x "$(command -v go)" ]; then
    echo "==> Go is installed, version: $(go version)"
else
    echo "==> Installing Go"
    apt-get update && apt-get install -y golang
fi

# 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"
    }
# Check for libsodium
if dpkg -l | grep -q libsodium; then
    echo "==> libsodium is installed"
else
    echo "==> Skipping database migrations (script not found)"
    echo "==> Installing libsodium"
    apt-get update && apt-get install -y libsodium23 libsodium-dev
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
# Check for pkg-config
if [ -x "$(command -v pkg-config)" ]; then
    echo "==> pkg-config is installed"
else
    echo "==> Installing pkg-config"
    apt-get update && apt-get install -y pkg-config
fi
done

# Change to server directory
cd "$SERVER_DIR"

# Set up database 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 CONFIG_PATH="/app/data/config/config.yaml"

# Additional environment variables
export REMOTE_STORAGE_ENDPOINT="${S3_ENDPOINT}"
export REMOTE_STORAGE_REGION="${S3_REGION}"
export REMOTE_STORAGE_BUCKET="${S3_BUCKET}"
export REMOTE_STORAGE_ACCESS_KEY="${S3_ACCESS_KEY}"
export REMOTE_STORAGE_SECRET_KEY="${S3_SECRET_KEY}"
export REMOTE_STORAGE_PREFIX="${S3_PREFIX:-ente/}"

# 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"
# Look for Museum binary first
if [ -x "$SERVER_DIR/museum" ]; then
    echo "==> Found Museum binary, running it directly"
    exec /usr/local/bin/gosu cloudron:cloudron "$SERVER_DIR/museum"
elif [ -d "$SERVER_DIR/cmd/museum" ]; then
    echo "==> Found Museum source in cmd/museum, running with go run"
    cd "$SERVER_DIR"
    exec /usr/local/bin/gosu cloudron:cloudron go run cmd/museum/main.go
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."
    # Fallback approach - find main.go files
    MAIN_FILES=$(find "$SERVER_DIR" -name "main.go" | grep -v "test")
    if [ -n "$MAIN_FILES" ]; then
        MAIN_FILE=$(echo "$MAIN_FILES" | head -1)
        MAIN_DIR=$(dirname "$MAIN_FILE")
        echo "==> Using main.go file at $MAIN_FILE"
        cd "$MAIN_DIR"
        exec /usr/local/bin/gosu cloudron:cloudron go run main.go
    else
        echo "==> FATAL ERROR: Could not find Museum binary or main.go source file"
        echo "==> Available Go files:"
        find "$SERVER_DIR" -name "*.go" | sort
        exit 1
    fi
fi 
 No newline at end of file