From 762eb94ee0576b881fd86dcc8c1d9e83dd2f88a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartal=20L=C3=A6arsson?= Date: Sun, 2 Mar 2025 21:42:39 +0000 Subject: [PATCH 1/2] cli test --- cli/cmd/provision/config/config.go | 38 +++++++++++++++++------ cli/cmd/provision/kubernetes/provision.go | 7 +++-- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cli/cmd/provision/config/config.go b/cli/cmd/provision/config/config.go index 046c401..f973bb2 100644 --- a/cli/cmd/provision/config/config.go +++ b/cli/cmd/provision/config/config.go @@ -22,6 +22,7 @@ func GetRepoRoot() (string, error) { } // findRepoRoot attempts to find the repository root by looking for marker files/directories +// Update this function in cli/cmd/provision/config/config.go func findRepoRoot() (string, error) { // Start from the current working directory cwd, err := os.Getwd() @@ -32,34 +33,53 @@ func findRepoRoot() (string, error) { // Walk up the directory tree until we find a marker of the repository root currentDir := cwd for { - // Check for common repository root markers: + // Check for multiple repository root markers in a specific order - // 1. Check if ansible directory exists + // First check for the most reliable markers ansibleDir := filepath.Join(currentDir, "ansible") - if dirExists(ansibleDir) { + ansibleDefaultsDir := filepath.Join(currentDir, "ansible", "defaults") + scriptsDir := filepath.Join(currentDir, "scripts") + + // Add more specific file checks + kubernetesYml := filepath.Join(currentDir, "ansible", "defaults", "kubernetes.yml") + provisionScript := filepath.Join(currentDir, "scripts", "provision-kubernetes.sh") + + // Check all variations + if dirExists(ansibleDir) && dirExists(ansibleDefaultsDir) { return currentDir, nil } - // 2. Check for scripts directory with our kubernetes scripts - scriptsDir := filepath.Join(currentDir, "scripts", "provision-kubernetes.sh") - if fileExists(scriptsDir) { + if dirExists(scriptsDir) && fileExists(provisionScript) { return currentDir, nil } - // 3. Check for .git directory as a fallback + if fileExists(kubernetesYml) { + return currentDir, nil + } + + // Check for .git directory as a fallback gitDir := filepath.Join(currentDir, ".git") if dirExists(gitDir) { - return currentDir, nil + // Additional validation when using .git as marker + // Make sure this is actually our repository by checking for a known file + if fileExists(filepath.Join(currentDir, "README.md")) || + dirExists(filepath.Join(currentDir, "ansible")) || + dirExists(filepath.Join(currentDir, "scripts")) { + 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") + break } currentDir = parentDir } + + // More helpful error message + return "", fmt.Errorf("repository root not found: the CLI needs to be run within the kubernetes repository that contains ansible/defaults/kubernetes.yml file. Please verify your directory structure or run 'git clone' to ensure you have the complete repository") } // dirExists checks if a directory exists diff --git a/cli/cmd/provision/kubernetes/provision.go b/cli/cmd/provision/kubernetes/provision.go index a0c00b8..3b78f40 100644 --- a/cli/cmd/provision/kubernetes/provision.go +++ b/cli/cmd/provision/kubernetes/provision.go @@ -33,12 +33,14 @@ type Config struct { func LoadDefaultConfig() (*Config, error) { defaultsPath, err := config.GetAnsiblePath("defaults/kubernetes.yml") if err != nil { - return nil, fmt.Errorf("failed to locate defaults file: %w", err) + return nil, fmt.Errorf("failed to locate defaults file: %w\n"+ + "Please make sure you are running the command from within the repository and that the ansible/defaults/kubernetes.yml file exists", err) } data, err := os.ReadFile(defaultsPath) if err != nil { - return nil, fmt.Errorf("failed to read defaults file: %w", err) + return nil, fmt.Errorf("failed to read defaults file at %s: %w\n"+ + "Please check file permissions and that the repository is correctly cloned", defaultsPath, err) } config := &Config{} @@ -256,4 +258,3 @@ func Cleanup() error { return cmd.Run() } - From 128f260a3f8bed36a3b2b6f3ff9e20365c4355bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartal=20L=C3=A6arsson?= Date: Sun, 2 Mar 2025 21:53:03 +0000 Subject: [PATCH 2/2] resilient install.sh fie --- cli/install.sh | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/cli/install.sh b/cli/install.sh index 3758a1b..d82a0ec 100755 --- a/cli/install.sh +++ b/cli/install.sh @@ -28,16 +28,40 @@ esac # Binary paths BINARY_NAME="provision" -BINARY_PATH="bin/${OS}_${GOARCH}/${BINARY_NAME}" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(dirname "$SCRIPT_DIR")" + +# Determine if we're running from the cli directory or from the repo root +if [[ "$(basename "$SCRIPT_DIR")" == "cli" ]]; then + # Running from cli directory + BIN_DIR="$SCRIPT_DIR/bin" +else + # Check if we have a cli subdirectory, which suggests we're in the repo root + if [[ -d "$SCRIPT_DIR/cli" ]]; then + BIN_DIR="$SCRIPT_DIR/cli/bin" + else + echo -e "${RED}✗ Error:${NC} Cannot determine the correct path to the binary." + echo "This script should be run either:" + echo " 1. From the repository root as ./cli/install.sh" + echo " 2. From the cli directory as ./install.sh" + exit 1 + fi +fi + +BINARY_PATH="$BIN_DIR/${OS}_${GOARCH}/${BINARY_NAME}" INSTALL_DIR="$HOME/.local/bin" echo "Installing infrastructure CLI tool..." +echo "Looking for binary at: $BINARY_PATH" # Ensure .local directory exists mkdir -p "$HOME/.local" # Check and fix .local/bin directory ownership if needed -if [ "$(stat -c '%U' "$INSTALL_DIR" 2>/dev/null)" != "$USER" ]; then +if [ ! -d "$INSTALL_DIR" ]; then + mkdir -m 755 -p "$INSTALL_DIR" + echo -e "${GREEN}✓${NC} Created bin directory with secure permissions" +elif [ "$(stat -c '%U' "$INSTALL_DIR" 2>/dev/null)" != "$USER" ]; then echo -e "${YELLOW}⚠${NC} Fixing directory ownership..." if command -v sudo >/dev/null; then sudo chown -R "$USER:$USER" "$INSTALL_DIR" @@ -47,13 +71,21 @@ if [ "$(stat -c '%U' "$INSTALL_DIR" 2>/dev/null)" != "$USER" ]; then fi fi -# Ensure .local/bin directory exists with correct permissions -mkdir -m 755 -p "$INSTALL_DIR" - # Check if the binary exists for this platform if [ ! -f "$BINARY_PATH" ]; then echo -e "${RED}✗ Error:${NC} Binary not found for your platform: ${OS}_${GOARCH}" - echo "Please compile the binary for your platform or contact the repository maintainer." + echo "Path checked: $BINARY_PATH" + echo "" + echo "Possible solutions:" + echo " 1. Make sure you're running this script from the correct directory:" + echo " - From the repository root: ./cli/install.sh" + echo " - From the cli directory: ./install.sh" + echo "" + echo " 2. If the binary doesn't exist, you may need to build it:" + echo " cd cli" + echo " go build -o bin/${OS}_${GOARCH}/${BINARY_NAME} ./cmd/provision" + echo "" + echo " 3. Check if your OS/architecture (${OS}_${GOARCH}) is supported" exit 1 fi