add relative path within repo for binary
This commit is contained in:
@@ -6,6 +6,7 @@ ansible/inventories/kubernetes.yml
|
|||||||
# Go build artifacts (except for released binaries)
|
# Go build artifacts (except for released binaries)
|
||||||
/build/
|
/build/
|
||||||
/bin/
|
/bin/
|
||||||
|
cli/bin
|
||||||
# Uncomment the lines above if you don't want to include binaries in the repo
|
# Uncomment the lines above if you don't want to include binaries in the repo
|
||||||
|
|
||||||
# Go dependency directories
|
# Go dependency directories
|
||||||
|
|||||||
@@ -40,9 +40,11 @@ func cleanupInteractive() {
|
|||||||
if err := kubernetes.Cleanup(); err != nil {
|
if err := kubernetes.Cleanup(); err != nil {
|
||||||
exitWithError("Failed to clean up Kubernetes cluster", err)
|
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:
|
default:
|
||||||
fmt.Println("Invalid choice")
|
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"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/ansible"
|
"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"
|
"github.com/bxtal-lsn/kubernetes/cli/cmd/provision/interactive"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@@ -30,13 +31,14 @@ type Config struct {
|
|||||||
|
|
||||||
// LoadDefaultConfig loads the default Kubernetes configuration from the defaults file
|
// LoadDefaultConfig loads the default Kubernetes configuration from the defaults file
|
||||||
func LoadDefaultConfig() (*Config, error) {
|
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)
|
data, err := os.ReadFile(defaultsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Could not find configuration file. Please run this command from the 'cli' directory of the project repository. "+
|
return nil, fmt.Errorf("failed to read defaults file: %w", err)
|
||||||
"Make sure you are in the correct directory and the file exists at: %s",
|
|
||||||
defaultsPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &Config{}
|
config := &Config{}
|
||||||
@@ -210,35 +212,36 @@ func ProvisionInteractive() error {
|
|||||||
|
|
||||||
// runProvisionScript runs the actual Kubernetes provisioning
|
// runProvisionScript runs the actual Kubernetes provisioning
|
||||||
func runProvisionScript(configPath string) error {
|
func runProvisionScript(configPath string) error {
|
||||||
// Path to the shell script for Kubernetes provisioning
|
// Get repository paths for inventory and playbook
|
||||||
provisionScriptPath := "../../../scripts/provision-kubernetes.sh"
|
inventory, err := config.GetAnsiblePath("inventories/kubernetes.yml")
|
||||||
|
if err != nil {
|
||||||
// Check if script exists
|
return fmt.Errorf("failed to locate inventory file: %w", err)
|
||||||
if _, err := os.Stat(provisionScriptPath); os.IsNotExist(err) {
|
|
||||||
return fmt.Errorf("provision script does not exist: %s", provisionScriptPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the provision script
|
playbook, err := config.GetAnsiblePath("playbooks/kubernetes.yml")
|
||||||
cmd := exec.Command(provisionScriptPath)
|
if err != nil {
|
||||||
cmd.Stdout = os.Stdout
|
return fmt.Errorf("failed to locate playbook file: %w", err)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Continue with Ansible playbook
|
// Check if files exist
|
||||||
inventory := "../../../ansible/inventories/kubernetes.yml"
|
if _, err := os.Stat(inventory); os.IsNotExist(err) {
|
||||||
playbook := "../../../ansible/playbooks/kubernetes.yml"
|
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})
|
return ansible.RunPlaybook(playbook, inventory, []string{"-e", "@" + configPath})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup handles Kubernetes cluster cleanup
|
// Cleanup handles Kubernetes cluster cleanup
|
||||||
func Cleanup() error {
|
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
|
// Check if script exists
|
||||||
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
|
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
|
||||||
@@ -253,3 +256,4 @@ func Cleanup() error {
|
|||||||
|
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user