From ca12dd9363f7c65321b8c35c9cb6e28d4b201346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartal=20L=C3=A6arsson?= Date: Sun, 2 Mar 2025 01:16:31 +0000 Subject: [PATCH] add relative path within repo for binary --- .gitignore | 1 + cli/cmd/provision/cmd/cleanup.go | 6 +- cli/cmd/provision/config/config.go | 99 +++++++++++++++++++++++ cli/cmd/provision/kubernetes/provision.go | 50 ++++++------ 4 files changed, 131 insertions(+), 25 deletions(-) create mode 100644 cli/cmd/provision/config/config.go diff --git a/.gitignore b/.gitignore index 766a94e..c184c71 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ ansible/inventories/kubernetes.yml # Go build artifacts (except for released binaries) /build/ /bin/ +cli/bin # Uncomment the lines above if you don't want to include binaries in the repo # Go dependency directories diff --git a/cli/cmd/provision/cmd/cleanup.go b/cli/cmd/provision/cmd/cleanup.go index cde8085..35e3bbb 100644 --- a/cli/cmd/provision/cmd/cleanup.go +++ b/cli/cmd/provision/cmd/cleanup.go @@ -40,9 +40,11 @@ func cleanupInteractive() { if err := kubernetes.Cleanup(); err != nil { exitWithError("Failed to clean up Kubernetes cluster", err) } + fmt.Println("Kubernetes cluster cleanup completed successfully") + case "PostgreSQL Cluster": + fmt.Println("PostgreSQL cluster cleanup is not implemented yet") default: fmt.Println("Invalid choice") } - - fmt.Println("Cleanup completed successfully") } + diff --git a/cli/cmd/provision/config/config.go b/cli/cmd/provision/config/config.go new file mode 100644 index 0000000..046c401 --- /dev/null +++ b/cli/cmd/provision/config/config.go @@ -0,0 +1,99 @@ +package config + +import ( + "fmt" + "os" + "path/filepath" + "sync" +) + +var ( + repoRootCache string + repoRootOnce sync.Once + repoRootErr error +) + +// GetRepoRoot returns the repository root path, caching the result after the first call +func GetRepoRoot() (string, error) { + repoRootOnce.Do(func() { + repoRootCache, repoRootErr = findRepoRoot() + }) + return repoRootCache, repoRootErr +} + +// findRepoRoot attempts to find the repository root by looking for marker files/directories +func findRepoRoot() (string, error) { + // Start from the current working directory + cwd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to get current working directory: %w", err) + } + + // Walk up the directory tree until we find a marker of the repository root + currentDir := cwd + for { + // Check for common repository root markers: + + // 1. Check if ansible directory exists + ansibleDir := filepath.Join(currentDir, "ansible") + if dirExists(ansibleDir) { + return currentDir, nil + } + + // 2. Check for scripts directory with our kubernetes scripts + scriptsDir := filepath.Join(currentDir, "scripts", "provision-kubernetes.sh") + if fileExists(scriptsDir) { + return currentDir, nil + } + + // 3. Check for .git directory as a fallback + gitDir := filepath.Join(currentDir, ".git") + if dirExists(gitDir) { + return currentDir, nil + } + + // Move up one directory + parentDir := filepath.Dir(currentDir) + if parentDir == currentDir { + // We've reached the filesystem root without finding the repo root + return "", fmt.Errorf("repository root not found: no repository markers found in parent directories") + } + currentDir = parentDir + } +} + +// dirExists checks if a directory exists +func dirExists(path string) bool { + info, err := os.Stat(path) + if err != nil { + return false + } + return info.IsDir() +} + +// fileExists checks if a file exists +func fileExists(path string) bool { + info, err := os.Stat(path) + if err != nil { + return false + } + return !info.IsDir() +} + +// GetAnsiblePath returns the absolute path to an ansible resource +func GetAnsiblePath(resourcePath string) (string, error) { + repoRoot, err := GetRepoRoot() + if err != nil { + return "", err + } + return filepath.Join(repoRoot, "ansible", resourcePath), nil +} + +// GetScriptsPath returns the absolute path to a script +func GetScriptsPath(scriptName string) (string, error) { + repoRoot, err := GetRepoRoot() + if err != nil { + return "", err + } + return filepath.Join(repoRoot, "scripts", scriptName), nil +} diff --git a/cli/cmd/provision/kubernetes/provision.go b/cli/cmd/provision/kubernetes/provision.go index d651ac4..a0c00b8 100644 --- a/cli/cmd/provision/kubernetes/provision.go +++ b/cli/cmd/provision/kubernetes/provision.go @@ -7,6 +7,7 @@ import ( "path/filepath" "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/ansible" + "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/config" "github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive" "gopkg.in/yaml.v3" ) @@ -30,13 +31,14 @@ type Config struct { // LoadDefaultConfig loads the default Kubernetes configuration from the defaults file func LoadDefaultConfig() (*Config, error) { - defaultsPath := "../../../ansible/defaults/kubernetes.yml" + defaultsPath, err := config.GetAnsiblePath("defaults/kubernetes.yml") + if err != nil { + return nil, fmt.Errorf("failed to locate defaults file: %w", err) + } data, err := os.ReadFile(defaultsPath) if err != nil { - return nil, fmt.Errorf("Could not find configuration file. Please run this command from the 'cli' directory of the project repository. "+ - "Make sure you are in the correct directory and the file exists at: %s", - defaultsPath) + return nil, fmt.Errorf("failed to read defaults file: %w", err) } config := &Config{} @@ -210,35 +212,36 @@ func ProvisionInteractive() error { // runProvisionScript runs the actual Kubernetes provisioning func runProvisionScript(configPath string) error { - // Path to the shell script for Kubernetes provisioning - provisionScriptPath := "../../../scripts/provision-kubernetes.sh" - - // Check if script exists - if _, err := os.Stat(provisionScriptPath); os.IsNotExist(err) { - return fmt.Errorf("provision script does not exist: %s", provisionScriptPath) + // Get repository paths for inventory and playbook + inventory, err := config.GetAnsiblePath("inventories/kubernetes.yml") + if err != nil { + return fmt.Errorf("failed to locate inventory file: %w", err) } - // Run the provision script - cmd := exec.Command(provisionScriptPath) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin - - // Execute the provision script which handles Multipass VM creation - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to run provision script: %w", err) + playbook, err := config.GetAnsiblePath("playbooks/kubernetes.yml") + if err != nil { + return fmt.Errorf("failed to locate playbook file: %w", err) } - // Continue with Ansible playbook - inventory := "../../../ansible/inventories/kubernetes.yml" - playbook := "../../../ansible/playbooks/kubernetes.yml" + // Check if files exist + if _, err := os.Stat(inventory); os.IsNotExist(err) { + return fmt.Errorf("inventory file does not exist: %s", inventory) + } + if _, err := os.Stat(playbook); os.IsNotExist(err) { + return fmt.Errorf("playbook file does not exist: %s", playbook) + } + // Run ansible-playbook return ansible.RunPlaybook(playbook, inventory, []string{"-e", "@" + configPath}) } // Cleanup handles Kubernetes cluster cleanup func Cleanup() error { - scriptPath := "../../../scripts/cleanup-kubernetes.sh" + // Get path to cleanup script + scriptPath, err := config.GetScriptsPath("cleanup-kubernetes.sh") + if err != nil { + return fmt.Errorf("failed to locate cleanup script: %w", err) + } // Check if script exists if _, err := os.Stat(scriptPath); os.IsNotExist(err) { @@ -253,3 +256,4 @@ func Cleanup() error { return cmd.Run() } +