|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Get options from environment (feature options are uppercase) |
| 6 | +VERSION="${VERSION:-"v1.0.116"}" |
| 7 | +SETUP_GLOBAL_HOOKS="${SETUPGLOBALHOOKS:-"true"}" |
| 8 | + |
| 9 | +# Normalize version format (ensure it starts with 'v') |
| 10 | +if [[ ! "$VERSION" =~ ^v ]]; then |
| 11 | + VERSION="v${VERSION}" |
| 12 | +fi |
| 13 | + |
| 14 | +echo "Installing Aikido pre-commit scanner version ${VERSION}..." |
| 15 | + |
| 16 | +# Ensure required tools are installed |
| 17 | +export DEBIAN_FRONTEND=noninteractive |
| 18 | + |
| 19 | +install_if_missing() { |
| 20 | + if ! command -v "$1" >/dev/null 2>&1; then |
| 21 | + echo "Installing $1..." |
| 22 | + apt-get update -y |
| 23 | + apt-get install -y "$2" |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +install_if_missing curl curl |
| 28 | +install_if_missing unzip unzip |
| 29 | +install_if_missing git git |
| 30 | + |
| 31 | +# Detect platform and architecture |
| 32 | +OS=$(uname -s) |
| 33 | +ARCH=$(uname -m) |
| 34 | + |
| 35 | +case "$OS" in |
| 36 | + Linux) |
| 37 | + PLATFORM="linux" |
| 38 | + ;; |
| 39 | + Darwin) |
| 40 | + PLATFORM="darwin" |
| 41 | + ;; |
| 42 | + MINGW*|MSYS*|CYGWIN*) |
| 43 | + PLATFORM="windows" |
| 44 | + ;; |
| 45 | + *) |
| 46 | + echo "Error: Unsupported operating system: $OS" |
| 47 | + exit 1 |
| 48 | + ;; |
| 49 | +esac |
| 50 | + |
| 51 | +case "$ARCH" in |
| 52 | + x86_64) |
| 53 | + ARCH_NAME="X86_64" |
| 54 | + ;; |
| 55 | + aarch64|arm64) |
| 56 | + ARCH_NAME="ARM64" |
| 57 | + ;; |
| 58 | + *) |
| 59 | + echo "Error: Unsupported architecture: $ARCH" |
| 60 | + exit 1 |
| 61 | + ;; |
| 62 | +esac |
| 63 | + |
| 64 | +# Construct download URL |
| 65 | +BASE_URL="https://aikido-local-scanner.s3.eu-west-1.amazonaws.com/${VERSION}" |
| 66 | +BINARY_NAME="aikido-local-scanner" |
| 67 | +DOWNLOAD_FILE="${BINARY_NAME}.zip" |
| 68 | +DOWNLOAD_URL="${BASE_URL}/${PLATFORM}_${ARCH_NAME}/${DOWNLOAD_FILE}" |
| 69 | + |
| 70 | +echo "Downloading from: $DOWNLOAD_URL" |
| 71 | + |
| 72 | +# Create temp directory with cleanup trap |
| 73 | +TEMP_DIR=$(mktemp -d) |
| 74 | +trap 'rm -rf "$TEMP_DIR"' EXIT |
| 75 | + |
| 76 | +# Download the archive |
| 77 | +if ! curl -fsSL -o "${TEMP_DIR}/${DOWNLOAD_FILE}" "$DOWNLOAD_URL"; then |
| 78 | + echo "Error: Failed to download aikido-local-scanner from $DOWNLOAD_URL" |
| 79 | + exit 1 |
| 80 | +fi |
| 81 | + |
| 82 | +# Extract and install |
| 83 | +echo "Extracting aikido-local-scanner..." |
| 84 | +unzip -q "${TEMP_DIR}/${DOWNLOAD_FILE}" -d "${TEMP_DIR}" |
| 85 | + |
| 86 | +# Install to /usr/local/bin (system-wide for container) |
| 87 | +INSTALL_DIR="/usr/local/bin" |
| 88 | +install -m 755 "${TEMP_DIR}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}" |
| 89 | + |
| 90 | +echo "Installed ${BINARY_NAME} to ${INSTALL_DIR}/${BINARY_NAME}" |
| 91 | + |
| 92 | +# Setup global git hooks if requested |
| 93 | +if [ "$SETUP_GLOBAL_HOOKS" = "true" ]; then |
| 94 | + echo "Configuring global git hooks..." |
| 95 | + |
| 96 | + # Determine hooks directory |
| 97 | + GLOBAL_HOOKS_DIR="/etc/git-hooks" |
| 98 | + |
| 99 | + # Check if core.hooksPath is already set |
| 100 | + EXISTING_HOOKS_PATH=$(git config --global core.hooksPath 2>/dev/null || echo "") |
| 101 | + |
| 102 | + if [ -n "$EXISTING_HOOKS_PATH" ]; then |
| 103 | + echo "Using existing hooks path: $EXISTING_HOOKS_PATH" |
| 104 | + ACTUAL_HOOKS_DIR="$EXISTING_HOOKS_PATH" |
| 105 | + else |
| 106 | + echo "Setting global hooks path to: $GLOBAL_HOOKS_DIR" |
| 107 | + git config --global core.hooksPath "$GLOBAL_HOOKS_DIR" |
| 108 | + ACTUAL_HOOKS_DIR="$GLOBAL_HOOKS_DIR" |
| 109 | + fi |
| 110 | + |
| 111 | + # Create hooks directory if it doesn't exist |
| 112 | + mkdir -p "$ACTUAL_HOOKS_DIR" |
| 113 | + |
| 114 | + # Create/update pre-commit hook |
| 115 | + PRECOMMIT_HOOK="${ACTUAL_HOOKS_DIR}/pre-commit" |
| 116 | + |
| 117 | + # Define the Aikido hook snippet |
| 118 | + AIKIDO_HOOK_START="# --- Aikido local scanner ---" |
| 119 | + AIKIDO_HOOK_END="# --- End Aikido local scanner ---" |
| 120 | + AIKIDO_HOOK_SNIPPET="${AIKIDO_HOOK_START} |
| 121 | +[ -x \"${INSTALL_DIR}/${BINARY_NAME}\" ] || { echo \"Aikido local scanner not found at ${INSTALL_DIR}/${BINARY_NAME}\"; exit 1; } |
| 122 | +REPO_ROOT=\"\$(git rev-parse --show-toplevel)\" |
| 123 | +\"${INSTALL_DIR}/${BINARY_NAME}\" pre-commit-scan \"\$REPO_ROOT\" |
| 124 | +${AIKIDO_HOOK_END}" |
| 125 | + |
| 126 | + # Check if hook file exists and if Aikido snippet is already present |
| 127 | + if [ -f "$PRECOMMIT_HOOK" ]; then |
| 128 | + if grep -q "$AIKIDO_HOOK_START" "$PRECOMMIT_HOOK"; then |
| 129 | + echo "Aikido hook already present in pre-commit, skipping..." |
| 130 | + else |
| 131 | + echo "Appending Aikido hook to existing pre-commit..." |
| 132 | + echo "" >> "$PRECOMMIT_HOOK" |
| 133 | + echo "$AIKIDO_HOOK_SNIPPET" >> "$PRECOMMIT_HOOK" |
| 134 | + fi |
| 135 | + else |
| 136 | + echo "Creating new pre-commit hook..." |
| 137 | + echo "#!/bin/sh" > "$PRECOMMIT_HOOK" |
| 138 | + echo "" >> "$PRECOMMIT_HOOK" |
| 139 | + echo "$AIKIDO_HOOK_SNIPPET" >> "$PRECOMMIT_HOOK" |
| 140 | + fi |
| 141 | + |
| 142 | + # Make hook executable |
| 143 | + chmod +x "$PRECOMMIT_HOOK" |
| 144 | + |
| 145 | + echo "Global pre-commit hook configured successfully!" |
| 146 | +fi |
| 147 | + |
| 148 | +# Verify installation |
| 149 | +if command -v aikido-local-scanner >/dev/null 2>&1; then |
| 150 | + echo "" |
| 151 | + echo "✅ aikido-local-scanner installed successfully!" |
| 152 | + echo " Location: $(which aikido-local-scanner)" |
| 153 | + if [ "$SETUP_GLOBAL_HOOKS" = "true" ]; then |
| 154 | + echo " Global hooks: $(git config --global core.hooksPath)" |
| 155 | + fi |
| 156 | +else |
| 157 | + echo "❌ Error: aikido-local-scanner installation failed" |
| 158 | + exit 1 |
| 159 | +fi |
0 commit comments