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

Fix Go version compatibility issues in start.sh

parent d8a40880
Loading
Loading
Loading
Loading
+119 −23
Original line number Diff line number Diff line
@@ -314,17 +314,27 @@ if [ -x "$(command -v go)" ]; then
    if [ -f "$SERVER_DIR/go.mod" ]; then
        REQUIRED_VERSION=$(grep -o "go [0-9]\+\.[0-9]\+" "$SERVER_DIR/go.mod" | cut -d " " -f 2)
        echo "==> Required Go version: $REQUIRED_VERSION"
        if [ "$(printf '%s\n' "$REQUIRED_VERSION" "$GO_VERSION" | sort -V | head -n1)" != "$GO_VERSION" ]; then
            echo "==> Modifying go.mod to accept current Go version"
            sed -i "s/go [0-9]\+\.[0-9]\+/go $GO_VERSION/" "$SERVER_DIR/go.mod"
            echo "==> Updated go.mod to use Go $GO_VERSION"
        fi
        
        # Always update go.mod to use a known compatible version
        echo "==> Setting go.mod to use Go 1.20 for better compatibility"
        sed -i "s/go [0-9]\+\.[0-9]\+/go 1.20/" "$SERVER_DIR/go.mod"
        echo "==> Updated go.mod to use Go 1.20"
        
        # Print the modified go.mod for debugging
        echo "==> Modified go.mod contents:"
        cat "$SERVER_DIR/go.mod" | head -5
    fi
else
    echo "==> Installing Go"
    apt-get update && apt-get install -y golang
fi

# Install Go binaries for fixed versions
echo "==> Ensuring compatible Go version is available"
go install golang.org/dl/go1.20@latest
echo "==> Installed go1.20 download tool"
$GOPATH/bin/go1.20 download || echo "==> Note: Could not download go1.20 - will use system version"

# Check for libsodium
if dpkg -l | grep -q libsodium; then
    echo "==> libsodium is installed"
@@ -348,8 +358,10 @@ cd "$SERVER_DIR"
export GOPATH=/app/data/go
export GO111MODULE=on
# Allow Go to use the current version
export GOTOOLCHAIN=auto
export GOPROXY=direct
# Change these settings to fix the toolchain availability issue
export GOTOOLCHAIN=local  # Don't try to download new toolchains
# export GOPROXY=direct    # This can cause issues, so let's use the default
echo "==> Using existing Go toolchain, disabled auto-downloading"

# Set up more verbose logging for API debugging
export ENTE_LOG_LEVEL=debug
@@ -419,6 +431,23 @@ if find "$SERVER_DIR" -name "museum" -type f -executable | grep -q .; then
elif [ -d "$SERVER_DIR/cmd/museum" ]; then
    echo "==> Found Museum source in cmd/museum, running with go run"
    cd "$SERVER_DIR"
    
    # Check go.mod and explicitly modify it to use a compatible Go version
    if [ -f "$SERVER_DIR/go.mod" ]; then
        echo "==> Checking go.mod for version requirements"
        GO_VERSION=$(go version | cut -d " " -f 3 | sed 's/go//')
        echo "==> System Go version: $GO_VERSION"
        
        # Force go.mod to use a compatible version
        echo "==> Setting go.mod to use Go 1.20"
        sed -i "s/go 1\.[0-9]\+/go 1.20/" "$SERVER_DIR/go.mod"
        echo "==> Updated go.mod to use Go 1.20"
        
        # Print the modified go.mod for debugging
        echo "==> Modified go.mod contents:"
        cat "$SERVER_DIR/go.mod" | head -5
    fi
    
    # For Wasabi specific settings
    if [[ "${S3_ENDPOINT}" == *"wasabi"* ]]; then
        echo "==> Detected Wasabi S3 endpoint, adjusting settings"
@@ -491,16 +520,65 @@ else
        MAIN_DIR=$(dirname "$MAIN_FILE")
        echo "==> Using main.go file at $MAIN_FILE"
        cd "$MAIN_DIR"
        /usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 &
        
        # Check for go.mod in this directory
        GO_MOD_DIR=$(find "$(dirname "$MAIN_DIR")" -name "go.mod" | head -1 || echo "")
        if [ -n "$GO_MOD_DIR" ]; then
            GO_MOD_DIR=$(dirname "$GO_MOD_DIR")
            echo "==> Found go.mod in $GO_MOD_DIR"
            cd "$GO_MOD_DIR"
            
            # Modify go.mod to use a compatible Go version
            echo "==> Setting go.mod to use Go 1.20"
            sed -i "s/go 1\.[0-9]\+/go 1.20/" "go.mod"
            echo "==> Updated go.mod to use Go 1.20"
            
            # Print the modified go.mod for debugging
            echo "==> Modified go.mod contents:"
            cat "go.mod" | head -5
            
            # Change back to main directory
            cd "$MAIN_DIR"
        fi
        
        echo "==> Running main.go with Go"
        /usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 \
            --storage.s3.endpoint="${S3_ENDPOINT}" \
            --storage.s3.region="${S3_REGION}" \
            --storage.s3.bucket="${S3_BUCKET}" \
            --storage.s3.accessKey="${S3_ACCESS_KEY}" \
            --storage.s3.secretKey="${S3_SECRET_KEY}" \
            --storage.s3.prefix="${S3_PREFIX:-ente/}" \
            --storage.s3.forcePathStyle=true \
            --storage.s3.areLocalBuckets=false \
            --storage.type="s3" \
            --config.path="/app/data/config/museum.yaml" \
            --log.level=debug > /app/data/logs/museum-server.log 2>&1 &
        SERVER_PID=$!
        echo "==> Museum server started with PID $SERVER_PID"
        
        # Check if the server process is actually running after a brief pause
        sleep 2
        if ! ps -p $SERVER_PID > /dev/null; then
            echo "==> WARNING: Server process exited immediately"
            echo "==> Recent server log output:"
            tail -n 50 /app/data/logs/museum-server.log || echo "==> No logs available yet"
            echo "==> Falling back to mock server"
            SERVER_PID=""
        fi
    else
        echo "==> ERROR: Could not find Museum binary or main.go source file"
        echo "==> Available Go files:"
        find "$SERVER_DIR" -name "*.go" | sort
        
        SERVER_PID=""
    fi
fi

# If server didn't start successfully, use mock server
if [ -z "$SERVER_PID" ] || ! ps -p $SERVER_PID > /dev/null; then
    echo "==> Starting mock server as fallback"
    # Last resort - create a temporary Go HTTP server that returns a meaningful error
        echo "==> Creating a temporary HTTP server to show the error to users"
    mkdir -p /tmp/mock-server
    cat > /tmp/mock-server/main.go <<EOT
package main
@@ -509,19 +587,38 @@ import (
    "fmt"
    "log"
    "net/http"
    "os"
    "io/ioutil"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/html")
        fmt.Fprintf(w, "<html><body><h1>Ente Museum Server Error</h1>")
        fmt.Fprintf(w, "<p>The Ente Museum server could not be started because no suitable Go source files were found.</p>")
        fmt.Fprintf(w, "<p>The Ente Museum server could not be started due to compatibility issues.</p>")
        fmt.Fprintf(w, "<p>Please check the logs for more information.</p>")
        
        // Show log excerpts if available
        if _, err := os.Stat("/app/data/logs/museum-server.log"); err == nil {
            logs, err := ioutil.ReadFile("/app/data/logs/museum-server.log")
            if err == nil && len(logs) > 0 {
                fmt.Fprintf(w, "<h2>Recent Log Output:</h2><pre>%s</pre>", logs)
            }
        }
        
        fmt.Fprintf(w, "</body></html>")
    })
    
    // Add a health endpoint
    http.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.Write([]byte(`{"status": "error", "message": "Running in fallback mode"}`))
    })
    
    log.Println("Starting mock server on port 8000")
    go func() {
        log.Fatal(http.ListenAndServe(":8000", nil))
    }()
}
EOT
    cd /tmp/mock-server
@@ -529,7 +626,6 @@ EOT
    SERVER_PID=$!
    echo "==> Mock server started with PID $SERVER_PID to show error message"
fi
fi

# Serve the static web apps in the foreground using our custom nginx config
echo "==> Running NGINX in the foreground with custom configuration"