#!/bin/bash # Hecho por Ariel S. Weher # ariel [at] weher [dot] net set -euo pipefail echo "[DEBUG] Starting installation script..." # Check if we are root if [ "$(id -u)" -ne 0 ]; then echo "This script must be run as root. Please run with sudo." exit 1 fi echo "[DEBUG] Running as root: OK" # Check system architecture ARCH=$(uname -m) echo "[DEBUG] Detected architecture: $ARCH" case $ARCH in x86_64) MC_ARCH="amd64" ;; i386|i686) MC_ARCH="386" ;; armv6l|armv7l) MC_ARCH="arm" ;; aarch64|arm64) MC_ARCH="arm64" ;; *) echo "Not supported architecture: $ARCH" exit 1 ;; esac echo "[DEBUG] Selected MC architecture: $MC_ARCH" # Check required command dependencies echo "[DEBUG] Checking required dependencies..." REQUIRED_CMDS=(curl file) OPTIONAL_CMDS=(jq) MISSING_CMDS=() MISSING_OPTIONAL=() for cmd in "${REQUIRED_CMDS[@]}"; do if ! command -v "$cmd" >/dev/null 2>&1; then MISSING_CMDS+=("$cmd") else echo "[DEBUG] Dependency $cmd: OK" fi done for cmd in "${OPTIONAL_CMDS[@]}"; do if ! command -v "$cmd" >/dev/null 2>&1; then MISSING_OPTIONAL+=("$cmd") else echo "[DEBUG] Optional dependency $cmd: OK" fi done if [ ${#MISSING_CMDS[@]} -gt 0 ] || [ ${#MISSING_OPTIONAL[@]} -gt 0 ]; then echo "" if [ ${#MISSING_CMDS[@]} -gt 0 ]; then echo "Faltan las siguientes dependencias obligatorias: ${MISSING_CMDS[*]}" fi if [ ${#MISSING_OPTIONAL[@]} -gt 0 ]; then echo "Faltan las siguientes dependencias opcionales: ${MISSING_OPTIONAL[*]} (mejoran la experiencia, pero no son obligatorias)" fi if command -v apt-get >/dev/null 2>&1; then read -p "¿Desea instalar las dependencias faltantes automáticamente? [S/n]: " RESP RESP=${RESP:-S} if [[ "$RESP" =~ ^[Ss]$ ]]; then echo "Instalando dependencias..." apt-get update if [ ${#MISSING_CMDS[@]} -gt 0 ]; then apt-get install -y "${MISSING_CMDS[@]}" fi if [ ${#MISSING_OPTIONAL[@]} -gt 0 ]; then apt-get install -y "${MISSING_OPTIONAL[@]}" fi else echo "Por favor, instale manualmente: ${MISSING_CMDS[*]} ${MISSING_OPTIONAL[*]}" exit 1 fi else echo "No se detectó apt-get. Por favor, instale manualmente: ${MISSING_CMDS[*]} ${MISSING_OPTIONAL[*]}" exit 1 fi fi # Create temporary directory TEMP_DIR=$(mktemp -d) echo "[DEBUG] Created temporary directory: $TEMP_DIR" trap 'rm -rf "$TEMP_DIR"' EXIT # Get latest version echo "[DEBUG] Fetching latest version information from GitHub..." GITHUB_API_RESPONSE=$(curl -sS https://api.github.com/repos/minio/mc/releases/latest) if [ -z "$GITHUB_API_RESPONSE" ]; then echo "Error: Unable to get GitHub API response" exit 1 fi echo "[DEBUG] GitHub API response received" # Extract version using jq if available, otherwise use grep if command -v jq >/dev/null 2>&1; then MC_TAG_LATEST=$(echo "$GITHUB_API_RESPONSE" | jq -r '.tag_name') else MC_TAG_LATEST=$(echo "$GITHUB_API_RESPONSE" | grep -o '"tag_name": *"[^"]*"' | cut -d'"' -f4) fi if [ -z "$MC_TAG_LATEST" ]; then echo "Error: Unable to get latest version from GitHub" echo "GitHub API response:" echo "$GITHUB_API_RESPONSE" exit 1 fi echo "[DEBUG] Latest version detected: $MC_TAG_LATEST" # Extract the version number without the initial 'v' for the filename VERSION_NUMBER=${MC_TAG_LATEST#v} echo "[DEBUG] Version number for download: $VERSION_NUMBER" # Build URL using the correct format URL="https://dl.min.io/client/mc/release/linux-${MC_ARCH}/mc" TEMP_FILE="$TEMP_DIR/mc" echo "[DEBUG] Download URL: $URL" echo "[DEBUG] Temporary file path: $TEMP_FILE" echo "Downloading MinIO Client ${MC_TAG_LATEST} for ${MC_ARCH} architecture..." echo "URL: $URL" # Download with HTTP status code check and progress bar echo "[DEBUG] Starting download..." HTTP_RESPONSE=$(curl -L --write-out "%{http_code}" --progress-bar --output "$TEMP_FILE" "$URL") echo "[DEBUG] Download completed with HTTP status: $HTTP_RESPONSE" if [ "$HTTP_RESPONSE" != "200" ]; then echo "Error: Download failed with HTTP status code $HTTP_RESPONSE" echo "Please check the URL: $URL" exit 1 fi # Check if the file exists and is not empty if [ ! -s "$TEMP_FILE" ]; then echo "Error: Downloaded file is empty" exit 1 fi echo "[DEBUG] Downloaded file size: $(stat -c%s "$TEMP_FILE") bytes" # Check if the file is an executable echo "[DEBUG] Checking file type..." FILE_TYPE=$(file "$TEMP_FILE") echo "[DEBUG] File type: $FILE_TYPE" if ! file "$TEMP_FILE" | grep -q "executable"; then echo "Error: Downloaded file is not a valid executable" echo "File type: $(file "$TEMP_FILE")" exit 1 fi # Install echo "[DEBUG] Installing to /usr/local/bin/m..." if ! mv "$TEMP_FILE" /usr/local/bin/m; then echo "Error: Could not move file to /usr/local/bin/" exit 1 fi echo "[DEBUG] Setting executable permissions..." chmod 0755 /usr/local/bin/m # Verify installation echo "[DEBUG] Verifying installation..." if command -v m >/dev/null 2>&1; then echo -e "\nInstallation completed successfully!" echo "[DEBUG] Running version check..." m --version else echo "Error: Installation failed" exit 1 fi echo "[DEBUG] Installation process completed"