Fix Go compatibility for read-only filesystem

This commit is contained in:
Andreas Düren 2025-03-14 23:00:26 +01:00
parent d775c2fb66
commit 192ebd0b5d

View File

@ -315,25 +315,20 @@ if [ -x "$(command -v go)" ]; then
REQUIRED_VERSION=$(grep -o "go [0-9]\+\.[0-9]\+" "$SERVER_DIR/go.mod" | cut -d " " -f 2)
echo "==> Required Go version: $REQUIRED_VERSION"
# 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
# Don't try to modify the go.mod file since filesystem is read-only
# Instead, use environment variables to override version requirements
echo "==> Setting Go flags to override version requirements"
export GOFLAGS="-mod=mod"
export GO111MODULE=on
export GOTOOLCHAIN=local
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"
# Skip trying to install specific Go versions since we can't modify /app/code
echo "==> Using existing Go toolchain with compatibility flags"
# Check for libsodium
if dpkg -l | grep -q libsodium; then
@ -357,11 +352,13 @@ cd "$SERVER_DIR"
# Set Go module cache to a writable location
export GOPATH=/app/data/go
export GO111MODULE=on
# Allow Go to use the current version
# 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 version compatibility flags
export GOTOOLCHAIN=local
# Create a temporary directory for Go module cache and build in writable area
export GOCACHE=/app/data/go/cache
export GOMODCACHE=/app/data/go/modcache
mkdir -p $GOCACHE $GOMODCACHE
echo "==> Set up Go environment in writable directories"
# Set up more verbose logging for API debugging
export ENTE_LOG_LEVEL=debug
@ -432,21 +429,10 @@ 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
# Instead of modifying go.mod, set environment variables for compatibility
echo "==> Setting Go environment variables for compatibility"
export GOFLAGS="-mod=mod"
export GOTOOLCHAIN=local
# For Wasabi specific settings
if [[ "${S3_ENDPOINT}" == *"wasabi"* ]]; then
@ -521,25 +507,10 @@ else
echo "==> Using main.go file at $MAIN_FILE"
cd "$MAIN_DIR"
# 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
# Instead of trying to modify go.mod, set environment variables for compatibility
echo "==> Setting Go environment variables for compatibility"
export GOFLAGS="-mod=mod"
export GOTOOLCHAIN=local
echo "==> Running main.go with Go"
/usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 \
@ -588,7 +559,6 @@ import (
"log"
"net/http"
"os"
"io/ioutil"
)
func main() {
@ -600,7 +570,7 @@ func main() {
// 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")
logs, err := os.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)
}
@ -622,6 +592,11 @@ func main() {
}
EOT
cd /tmp/mock-server
# Set Go environment variables for compatibility with the mock server
export GOFLAGS="-mod=mod"
export GOTOOLCHAIN=local
go run main.go &
SERVER_PID=$!
echo "==> Mock server started with PID $SERVER_PID to show error message"