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) REQUIRED_VERSION=$(grep -o "go [0-9]\+\.[0-9]\+" "$SERVER_DIR/go.mod" | cut -d " " -f 2)
echo "==> Required Go version: $REQUIRED_VERSION" echo "==> Required Go version: $REQUIRED_VERSION"
# Always update go.mod to use a known compatible version # Don't try to modify the go.mod file since filesystem is read-only
echo "==> Setting go.mod to use Go 1.20 for better compatibility" # Instead, use environment variables to override version requirements
sed -i "s/go [0-9]\+\.[0-9]\+/go 1.20/" "$SERVER_DIR/go.mod" echo "==> Setting Go flags to override version requirements"
echo "==> Updated go.mod to use Go 1.20" export GOFLAGS="-mod=mod"
export GO111MODULE=on
# Print the modified go.mod for debugging export GOTOOLCHAIN=local
echo "==> Modified go.mod contents:"
cat "$SERVER_DIR/go.mod" | head -5
fi fi
else else
echo "==> Installing Go" echo "==> Installing Go"
apt-get update && apt-get install -y golang apt-get update && apt-get install -y golang
fi fi
# Install Go binaries for fixed versions # Skip trying to install specific Go versions since we can't modify /app/code
echo "==> Ensuring compatible Go version is available" echo "==> Using existing Go toolchain with compatibility flags"
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 # Check for libsodium
if dpkg -l | grep -q libsodium; then if dpkg -l | grep -q libsodium; then
@ -357,11 +352,13 @@ cd "$SERVER_DIR"
# Set Go module cache to a writable location # Set Go module cache to a writable location
export GOPATH=/app/data/go export GOPATH=/app/data/go
export GO111MODULE=on export GO111MODULE=on
# Allow Go to use the current version # Set version compatibility flags
# Change these settings to fix the toolchain availability issue export GOTOOLCHAIN=local
export GOTOOLCHAIN=local # Don't try to download new toolchains # Create a temporary directory for Go module cache and build in writable area
# export GOPROXY=direct # This can cause issues, so let's use the default export GOCACHE=/app/data/go/cache
echo "==> Using existing Go toolchain, disabled auto-downloading" 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 # Set up more verbose logging for API debugging
export ENTE_LOG_LEVEL=debug 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" echo "==> Found Museum source in cmd/museum, running with go run"
cd "$SERVER_DIR" cd "$SERVER_DIR"
# Check go.mod and explicitly modify it to use a compatible Go version # Instead of modifying go.mod, set environment variables for compatibility
if [ -f "$SERVER_DIR/go.mod" ]; then echo "==> Setting Go environment variables for compatibility"
echo "==> Checking go.mod for version requirements" export GOFLAGS="-mod=mod"
GO_VERSION=$(go version | cut -d " " -f 3 | sed 's/go//') export GOTOOLCHAIN=local
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 # For Wasabi specific settings
if [[ "${S3_ENDPOINT}" == *"wasabi"* ]]; then if [[ "${S3_ENDPOINT}" == *"wasabi"* ]]; then
@ -521,25 +507,10 @@ else
echo "==> Using main.go file at $MAIN_FILE" echo "==> Using main.go file at $MAIN_FILE"
cd "$MAIN_DIR" cd "$MAIN_DIR"
# Check for go.mod in this directory # Instead of trying to modify go.mod, set environment variables for compatibility
GO_MOD_DIR=$(find "$(dirname "$MAIN_DIR")" -name "go.mod" | head -1 || echo "") echo "==> Setting Go environment variables for compatibility"
if [ -n "$GO_MOD_DIR" ]; then export GOFLAGS="-mod=mod"
GO_MOD_DIR=$(dirname "$GO_MOD_DIR") export GOTOOLCHAIN=local
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" echo "==> Running main.go with Go"
/usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 \ /usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 \
@ -588,7 +559,6 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"io/ioutil"
) )
func main() { func main() {
@ -600,7 +570,7 @@ func main() {
// Show log excerpts if available // Show log excerpts if available
if _, err := os.Stat("/app/data/logs/museum-server.log"); err == nil { 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 { if err == nil && len(logs) > 0 {
fmt.Fprintf(w, "<h2>Recent Log Output:</h2><pre>%s</pre>", logs) fmt.Fprintf(w, "<h2>Recent Log Output:</h2><pre>%s</pre>", logs)
} }
@ -622,6 +592,11 @@ func main() {
} }
EOT EOT
cd /tmp/mock-server 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 & go run main.go &
SERVER_PID=$! SERVER_PID=$!
echo "==> Mock server started with PID $SERVER_PID to show error message" echo "==> Mock server started with PID $SERVER_PID to show error message"