|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Configuration |
| 5 | +BASE_URL="https://linux.mellanox.com/public/repo/doca/" |
| 6 | +JSON_FILE="version.json" |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +BLUE='\033[0;34m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +log() { |
| 16 | + echo -e "${BLUE}[INFO]${NC} $*" >&2 |
| 17 | +} |
| 18 | + |
| 19 | +warn() { |
| 20 | + echo -e "${YELLOW}[WARN]${NC} $*" >&2 |
| 21 | +} |
| 22 | + |
| 23 | +error() { |
| 24 | + echo -e "${RED}[ERROR]${NC} $*" >&2 |
| 25 | +} |
| 26 | + |
| 27 | +success() { |
| 28 | + echo -e "${GREEN}[SUCCESS]${NC} $*" >&2 |
| 29 | +} |
| 30 | + |
| 31 | +# Check if a version folder is valid (not latest/lts/etc) |
| 32 | +is_valid_version() { |
| 33 | + local version="$1" |
| 34 | + # Skip folders containing these patterns |
| 35 | + if [[ "$version" =~ (latest|lts|tmp|DGX_|bclinux) ]]; then |
| 36 | + return 1 |
| 37 | + fi |
| 38 | + # Only consider folders that look like version numbers with a dash suffix (x.y.z-something) |
| 39 | + # This ensures we get deterministic versions and not dynamic pointers like "3.0.0" |
| 40 | + if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+-.*$ ]]; then |
| 41 | + return 0 |
| 42 | + fi |
| 43 | + return 1 |
| 44 | +} |
| 45 | + |
| 46 | +# Get current version from JSON |
| 47 | +get_current_version() { |
| 48 | + if [[ -f "$JSON_FILE" ]]; then |
| 49 | + jq -r '.version' "$JSON_FILE" |
| 50 | + else |
| 51 | + error "JSON file $JSON_FILE not found" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +# Find all valid versions with MLNX-OFED sources |
| 57 | +find_latest_version() { |
| 58 | + log "Fetching directory listing from $BASE_URL" |
| 59 | + local html |
| 60 | + html=$(curl -s "$BASE_URL") |
| 61 | + |
| 62 | + local directories |
| 63 | + # Sort in descending order for faster search |
| 64 | + directories=$(echo "$html" | grep -oP 'href="[^"]*/"' | sed 's/href="//;s/\/"//' | grep -v "^[.]" | sort -V -r) |
| 65 | + |
| 66 | + local latest_doca_version="" |
| 67 | + local latest_mlnx_version="" |
| 68 | + local latest_url="" |
| 69 | + |
| 70 | + while IFS= read -r dir; do |
| 71 | + [[ -z "$dir" ]] && continue |
| 72 | + |
| 73 | + if ! is_valid_version "$dir"; then |
| 74 | + continue |
| 75 | + fi |
| 76 | + |
| 77 | + log "Checking version folder: $dir" |
| 78 | + |
| 79 | + # Check if SOURCES/MLNX_OFED/ exists |
| 80 | + local sources_url="${BASE_URL}${dir}/SOURCES/MLNX_OFED/" |
| 81 | + local sources_html |
| 82 | + |
| 83 | + if ! sources_html=$(curl -s "$sources_url" 2>/dev/null); then |
| 84 | + warn " Failed to fetch $sources_url" |
| 85 | + continue |
| 86 | + fi |
| 87 | + |
| 88 | + # Check if it's a 404 or valid page |
| 89 | + if [[ "$sources_html" =~ "404 Not Found" ]]; then |
| 90 | + warn " No SOURCES/MLNX_OFED/ found in $dir" |
| 91 | + continue |
| 92 | + fi |
| 93 | + |
| 94 | + # Look for debian .tgz file |
| 95 | + local files |
| 96 | + files=$(echo "$sources_html" | grep -oP 'href="[^"]*[^/]"' | sed 's/href="//;s/"//' | grep -v "^[.]") |
| 97 | + |
| 98 | + local debian_file |
| 99 | + debian_file=$(echo "$files" | grep "^MLNX_OFED_SRC-debian-.*\.tgz$" | head -n1) |
| 100 | + |
| 101 | + if [[ -z "$debian_file" ]]; then |
| 102 | + warn " No debian .tgz file found in $dir" |
| 103 | + continue |
| 104 | + fi |
| 105 | + |
| 106 | + # Extract MLNX version from filename |
| 107 | + local mlnx_version |
| 108 | + if [[ "$debian_file" =~ MLNX_OFED_SRC-debian-(.+)\.tgz ]]; then |
| 109 | + mlnx_version="${BASH_REMATCH[1]}" |
| 110 | + else |
| 111 | + warn " Could not extract version from $debian_file" |
| 112 | + continue |
| 113 | + fi |
| 114 | + |
| 115 | + local download_url="${sources_url}${debian_file}" |
| 116 | + |
| 117 | + log " Found: $debian_file (MLNX version: $mlnx_version)" |
| 118 | + |
| 119 | + # Since we're iterating in descending order, the first valid version we find is the latest |
| 120 | + if [[ -z "$latest_mlnx_version" ]]; then |
| 121 | + latest_doca_version="$dir" |
| 122 | + latest_mlnx_version="$mlnx_version" |
| 123 | + latest_url="$download_url" |
| 124 | + |
| 125 | + # Early termination: we found the latest version, no need to continue |
| 126 | + log " This is the latest version, stopping search" |
| 127 | + break |
| 128 | + fi |
| 129 | + |
| 130 | + done <<< "$directories" |
| 131 | + |
| 132 | + if [[ -z "$latest_mlnx_version" ]]; then |
| 133 | + error "No valid MLNX-OFED versions found" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | + |
| 137 | + echo "$latest_doca_version|$latest_mlnx_version|$latest_url" |
| 138 | +} |
| 139 | + |
| 140 | +# Update JSON file with new version info |
| 141 | +update_json() { |
| 142 | + local version="$1" |
| 143 | + local url="$2" |
| 144 | + local sha256="$3" |
| 145 | + |
| 146 | + local temp_file |
| 147 | + temp_file=$(mktemp) |
| 148 | + |
| 149 | + jq -n \ |
| 150 | + --arg version "$version" \ |
| 151 | + --arg url "$url" \ |
| 152 | + --arg sha256 "$sha256" \ |
| 153 | + '{ |
| 154 | + version: $version, |
| 155 | + url: $url, |
| 156 | + sha256: $sha256 |
| 157 | + }' > "$temp_file" |
| 158 | + |
| 159 | + mv "$temp_file" "$JSON_FILE" |
| 160 | + success "Updated $JSON_FILE" |
| 161 | +} |
| 162 | + |
| 163 | +# Main function |
| 164 | +main() { |
| 165 | + local check_only=false |
| 166 | + |
| 167 | + # Parse arguments |
| 168 | + while [[ $# -gt 0 ]]; do |
| 169 | + case $1 in |
| 170 | + --check) |
| 171 | + check_only=true |
| 172 | + shift |
| 173 | + ;; |
| 174 | + -h|--help) |
| 175 | + echo "Usage: $0 [--check] [--help]" |
| 176 | + echo " --check Only check for updates, don't modify files" |
| 177 | + echo " --help Show this help message" |
| 178 | + exit 0 |
| 179 | + ;; |
| 180 | + *) |
| 181 | + error "Unknown argument: $1" |
| 182 | + exit 1 |
| 183 | + ;; |
| 184 | + esac |
| 185 | + done |
| 186 | + |
| 187 | + # Check dependencies |
| 188 | + if ! command -v jq >/dev/null 2>&1; then |
| 189 | + error "jq is required but not installed" |
| 190 | + exit 1 |
| 191 | + fi |
| 192 | + |
| 193 | + if ! command -v curl >/dev/null 2>&1; then |
| 194 | + error "curl is required but not installed" |
| 195 | + exit 1 |
| 196 | + fi |
| 197 | + |
| 198 | + # Get current version |
| 199 | + local current_version |
| 200 | + current_version=$(get_current_version) |
| 201 | + log "Current version: $current_version" |
| 202 | + |
| 203 | + # Find latest version |
| 204 | + log "Scanning for available MLNX-OFED versions..." |
| 205 | + local latest_info |
| 206 | + latest_info=$(find_latest_version) |
| 207 | + |
| 208 | + IFS='|' read -r doca_version mlnx_version url <<< "$latest_info" |
| 209 | + |
| 210 | + log "Latest version found:" |
| 211 | + log " DOCA version: $doca_version" |
| 212 | + log " MLNX version: $mlnx_version" |
| 213 | + log " URL: $url" |
| 214 | + |
| 215 | + # Check if update is needed |
| 216 | + if [[ "$current_version" == "$mlnx_version" ]]; then |
| 217 | + success "Already up to date!" |
| 218 | + exit 0 |
| 219 | + fi |
| 220 | + |
| 221 | + log "Update available: $current_version -> $mlnx_version" |
| 222 | + |
| 223 | + if [[ "$check_only" == true ]]; then |
| 224 | + log "Check-only mode, not updating files" |
| 225 | + exit 0 |
| 226 | + fi |
| 227 | + |
| 228 | + # Calculate new SHA256 |
| 229 | + log "Calculating SHA256 for $url" |
| 230 | + local new_sha256 |
| 231 | + if command -v nix-prefetch-url >/dev/null 2>&1; then |
| 232 | + new_sha256=$(nix-prefetch-url --type sha256 "$url" 2>/dev/null | tail -n1) |
| 233 | + else |
| 234 | + error "nix-prefetch-url not found" |
| 235 | + exit 1 |
| 236 | + fi |
| 237 | + |
| 238 | + log "New SHA256: $new_sha256" |
| 239 | + |
| 240 | + # Update JSON file |
| 241 | + update_json "$mlnx_version" "$url" "$new_sha256" |
| 242 | + |
| 243 | + success "Update completed successfully!" |
| 244 | + success "Updated from $current_version to $mlnx_version" |
| 245 | +} |
| 246 | + |
| 247 | +# Run main function |
| 248 | +main "$@" |
0 commit comments