add relative path within repo for binary
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user