...
This commit is contained in:
		| @@ -5,7 +5,7 @@ import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/hetznerinstall" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/hetznerinstall" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
|   | ||||
| @@ -6,10 +6,10 @@ import ( | ||||
| 	"os/exec" | ||||
| 	"path/filepath" | ||||
|  | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql/dependencies" | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql/gosp" | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql/postgres" | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql/verification" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql/dependencies" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql/gosp" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql/postgres" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql/verification" | ||||
| ) | ||||
|  | ||||
| // Constants for PostgreSQL installation | ||||
|   | ||||
| @@ -4,7 +4,7 @@ import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
|   | ||||
| @@ -6,7 +6,7 @@ import ( | ||||
| 	"os/exec" | ||||
| 	"path/filepath" | ||||
|  | ||||
| 	"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql/postgres" | ||||
| 	"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql/postgres" | ||||
| ) | ||||
|  | ||||
| // Constants for Go stored procedure | ||||
| @@ -46,10 +46,10 @@ func (b *GoSPBuilder) run(cmd string, args ...string) error { | ||||
| 	fmt.Println("Running:", cmd, args) | ||||
| 	c := exec.Command(cmd, args...) | ||||
| 	// Set environment variables | ||||
| 	c.Env = append(os.Environ(),  | ||||
| 	c.Env = append(os.Environ(), | ||||
| 		"GOROOT=/usr/local/go", | ||||
| 		"GOPATH=/root/go",  | ||||
| 		"PATH=/usr/local/go/bin:" + os.Getenv("PATH")) | ||||
| 		"GOPATH=/root/go", | ||||
| 		"PATH=/usr/local/go/bin:"+os.Getenv("PATH")) | ||||
| 	c.Stdout = os.Stdout | ||||
| 	c.Stderr = os.Stderr | ||||
| 	return c.Run() | ||||
| @@ -58,7 +58,7 @@ func (b *GoSPBuilder) run(cmd string, args ...string) error { | ||||
| // Build builds a Go stored procedure | ||||
| func (b *GoSPBuilder) Build() error { | ||||
| 	fmt.Println("Building Go stored procedure...") | ||||
| 	 | ||||
|  | ||||
| 	// Use the explicitly provided Go path if available | ||||
| 	var goExePath string | ||||
| 	if b.GoPath != "" { | ||||
| @@ -74,7 +74,7 @@ func (b *GoSPBuilder) Build() error { | ||||
| 		} | ||||
| 		fmt.Printf("Using detected Go executable from: %s\n", goExePath) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	if err := os.MkdirAll(b.GoSharedLibDir, 0755); err != nil { | ||||
| 		return fmt.Errorf("failed to create directory: %w", err) | ||||
| 	} | ||||
| @@ -98,27 +98,27 @@ func main() {} | ||||
|  | ||||
| 	// Use the full path to Go rather than relying on PATH | ||||
| 	fmt.Println("Running Go build with full path:", goExePath) | ||||
| 	 | ||||
|  | ||||
| 	// Show debug information | ||||
| 	fmt.Println("Environment variables that will be set:") | ||||
| 	fmt.Println("  GOROOT=/usr/local/go") | ||||
| 	fmt.Println("  GOPATH=/root/go") | ||||
| 	fmt.Println("  PATH=/usr/local/go/bin:" + os.Getenv("PATH")) | ||||
| 	 | ||||
|  | ||||
| 	// Verify that the Go executable exists before using it | ||||
| 	if _, err := os.Stat(goExePath); err != nil { | ||||
| 		return fmt.Errorf("Go executable not found at %s: %w", goExePath, err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Create the output directory if it doesn't exist | ||||
| 	outputDir := filepath.Join(b.InstallPrefix, "lib") | ||||
| 	if err := os.MkdirAll(outputDir, 0755); err != nil { | ||||
| 		return fmt.Errorf("failed to create output directory %s: %w", outputDir, err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Prepare output path | ||||
| 	outputPath := filepath.Join(outputDir, "libgosp.so") | ||||
| 	 | ||||
|  | ||||
| 	// Instead of relying on environment variables, create a wrapper shell script | ||||
| 	// that sets all required environment variables and then calls the Go executable | ||||
| 	tempDir, err := os.MkdirTemp("", "go-build-") | ||||
| @@ -126,7 +126,7 @@ func main() {} | ||||
| 		return fmt.Errorf("failed to create temp directory: %w", err) | ||||
| 	} | ||||
| 	defer os.RemoveAll(tempDir) // Clean up when done | ||||
| 	 | ||||
|  | ||||
| 	goRoot := filepath.Dir(filepath.Dir(goExePath)) // /usr/local/go | ||||
| 	wrapperScript := filepath.Join(tempDir, "go-wrapper.sh") | ||||
| 	wrapperContent := fmt.Sprintf(`#!/bin/sh | ||||
| @@ -143,25 +143,25 @@ echo "PATH=$PATH" | ||||
| echo "=== Running Go command ===" | ||||
| echo "%s $@" | ||||
| exec %s "$@" | ||||
| `,  | ||||
| 		goRoot,  | ||||
| `, | ||||
| 		goRoot, | ||||
| 		filepath.Dir(goExePath), | ||||
| 		goExePath, | ||||
| 		goExePath) | ||||
| 	 | ||||
|  | ||||
| 	// Write the wrapper script | ||||
| 	if err := os.WriteFile(wrapperScript, []byte(wrapperContent), 0755); err != nil { | ||||
| 		return fmt.Errorf("failed to write wrapper script: %w", err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	fmt.Printf("Created wrapper script at %s\n", wrapperScript) | ||||
| 	 | ||||
|  | ||||
| 	// Use the wrapper script to build the Go shared library | ||||
| 	cmd := exec.Command(wrapperScript, "build", "-buildmode=c-shared", "-o", outputPath, libPath) | ||||
| 	cmd.Dir = filepath.Dir(libPath) // Set working directory to where the source file is | ||||
| 	cmd.Stdout = os.Stdout | ||||
| 	cmd.Stderr = os.Stderr | ||||
| 	 | ||||
|  | ||||
| 	fmt.Printf("Executing Go build via wrapper script\n") | ||||
| 	if err := cmd.Run(); err != nil { | ||||
| 		return fmt.Errorf("failed to build Go stored procedure: %w", err) | ||||
|   | ||||
| @@ -8,7 +8,7 @@ import ( | ||||
| 	"runtime" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/mholt/archiver/v3" | ||||
| 	"github.com/mholt/archiver/v4" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| @@ -51,14 +51,14 @@ func (g *GoInstaller) GetGoVersion() (string, error) { | ||||
| 	if err != nil { | ||||
| 		return "", fmt.Errorf("failed to get Go version: %w", err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Parse go version output (format: "go version go1.x.x ...") | ||||
| 	version := strings.TrimSpace(string(output)) | ||||
| 	parts := strings.Split(version, " ") | ||||
| 	if len(parts) < 3 { | ||||
| 		return "", fmt.Errorf("unexpected go version output format: %s", version) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Return just the version number without the "go" prefix | ||||
| 	return strings.TrimPrefix(parts[2], "go"), nil | ||||
| } | ||||
| @@ -77,7 +77,7 @@ func (g *GoInstaller) InstallGo() (string, error) { | ||||
| 	// Default Go installation location | ||||
| 	var installDir string = "/usr/local" | ||||
| 	var goExePath string = filepath.Join(installDir, "go", "bin", "go") | ||||
| 	 | ||||
|  | ||||
| 	// Check if Go is already installed by checking the binary directly | ||||
| 	if _, err := os.Stat(goExePath); err == nil { | ||||
| 		version, err := g.GetGoVersion() | ||||
| @@ -86,7 +86,7 @@ func (g *GoInstaller) InstallGo() (string, error) { | ||||
| 			return goExePath, nil | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Also check if Go is available in PATH as a fallback | ||||
| 	if g.IsGoInstalled() { | ||||
| 		path, err := exec.LookPath("go") | ||||
| @@ -98,31 +98,31 @@ func (g *GoInstaller) InstallGo() (string, error) { | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	fmt.Printf("Installing Go version %s...\n", g.Version) | ||||
| 	 | ||||
|  | ||||
| 	// Determine architecture and OS | ||||
| 	goOS := runtime.GOOS | ||||
| 	goArch := runtime.GOARCH | ||||
| 	 | ||||
|  | ||||
| 	// Construct download URL | ||||
| 	downloadURL := fmt.Sprintf("https://golang.org/dl/go%s.%s-%s.tar.gz", g.Version, goOS, goArch) | ||||
| 	 | ||||
|  | ||||
| 	// Create a temporary directory for download | ||||
| 	tempDir, err := os.MkdirTemp("", "go-install-") | ||||
| 	if err != nil { | ||||
| 		return "", fmt.Errorf("failed to create temporary directory: %w", err) | ||||
| 	} | ||||
| 	defer os.RemoveAll(tempDir) | ||||
| 	 | ||||
|  | ||||
| 	// Download Go tarball | ||||
| 	tarballPath := filepath.Join(tempDir, "go.tar.gz") | ||||
| 	if err := downloadFile(downloadURL, tarballPath); err != nil { | ||||
| 		return "", fmt.Errorf("failed to download Go: %w", err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Install directory - typically /usr/local for Linux/macOS | ||||
| 	 | ||||
|  | ||||
| 	// Check if existing Go installation exists and remove it | ||||
| 	existingGoDir := filepath.Join(installDir, "go") | ||||
| 	if _, err := os.Stat(existingGoDir); err == nil { | ||||
| @@ -131,34 +131,34 @@ func (g *GoInstaller) InstallGo() (string, error) { | ||||
| 			return "", fmt.Errorf("failed to remove existing Go installation: %w", err) | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Extract tarball to install directory | ||||
| 	fmt.Printf("Extracting Go to %s\n", installDir) | ||||
| 	err = extractTarGz(tarballPath, installDir) | ||||
| 	if err != nil { | ||||
| 		return "", fmt.Errorf("failed to extract Go tarball: %w", err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Verify installation | ||||
| 	var goExePathVerify = filepath.Join(installDir, "go", "bin", "go") // Use = instead of := to avoid variable shadowing | ||||
| 	 | ||||
|  | ||||
| 	// Check if the Go binary exists | ||||
| 	var statErr error | ||||
| 	_, statErr = os.Stat(goExePathVerify) | ||||
| 	if statErr != nil { | ||||
| 		return "", fmt.Errorf("Go installation failed - go executable not found at %s", goExePathVerify) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Set up environment variables | ||||
| 	fmt.Println("Setting up Go environment variables...") | ||||
| 	 | ||||
|  | ||||
| 	// Update PATH in /etc/profile | ||||
| 	profilePath := "/etc/profile" | ||||
| 	profileContent, err := os.ReadFile(profilePath) | ||||
| 	if err != nil { | ||||
| 		return "", fmt.Errorf("failed to read profile: %w", err) | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	// Add Go bin to PATH if not already there | ||||
| 	goBinPath := filepath.Join(installDir, "go", "bin") | ||||
| 	if !strings.Contains(string(profileContent), goBinPath) { | ||||
| @@ -167,7 +167,7 @@ func (g *GoInstaller) InstallGo() (string, error) { | ||||
| 			return "", fmt.Errorf("failed to update profile: %w", err) | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	fmt.Printf("✅ Go %s installed successfully!\n", g.Version) | ||||
| 	return goExePath, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user