#!/bin/bash if ! command -v jq >/dev/null 2>&1 ; then wget -O /usr/bin/jq http://download.jsaix.cn/d/linux/jq-1.7-linux64 && chmod +x /usr/bin/jq if ! command -v jq >/dev/null 2>&1 ; then echo "Unsupported Linux distribution" exit 1 fi fi UUID="$1" if [ -z "$UUID" ]; then echo "Error: Missing UUID parameter." exit 1 fi INSTALL_DIR="/usr/local/aix-agent" MAX_RETRIES=3 API_ENDPOINT="https://gateway.jsaix.cn/api/servers/agent_update/$UUID" REMOTE_RESPONSE=$(curl -s "$API_ENDPOINT") CODE=$(echo "$REMOTE_RESPONSE" | jq -r '.code') if [ "$CODE" != "1" ]; then echo "Error: Remote API returned a non-successful code. Exiting." echo "$(echo "$REMOTE_RESPONSE" | jq -r '.message')" exit 1 fi REMOTE_VERSION=$(curl -s "$API_ENDPOINT" | jq -r '.data.version') DOWNLOAD_URL=$(echo "$REMOTE_RESPONSE" | jq -r '.data.url') EXPECTED_MD5=$(echo "$REMOTE_RESPONSE" | jq -r '.data.md5') if [ -z "$DOWNLOAD_URL" ] || [ -z "$EXPECTED_MD5" ] || [ -z "$REMOTE_VERSION" ]; then echo "Error: Failed to fetch valid download information from the remote API." exit 1 fi cd "$INSTALL_DIR" download_and_check_md5() { echo "Downloading archive..." curl -O "$DOWNLOAD_URL" if [ $? -ne 0 ]; then echo "Error downloading archive." return 1 fi ARCHIVE_FILE=$(basename "$DOWNLOAD_URL") CALCULATED_MD5=$(md5sum "$ARCHIVE_FILE" | awk '{print $1}' | tr '[:lower:]' '[:upper:]') if [ "$CALCULATED_MD5" != "$EXPECTED_MD5" ]; then echo "Error: MD5 checksum does not match." return 1 fi return 0 } LOCAL_VERSION=$(/usr/local/aix-agent/aix-agent --version) IFS='.' read -r -a array1 <<< "$REMOTE_VERSION" IFS='.' read -r -a array2 <<< "$LOCAL_VERSION" IS_UPDATE=0 for i in {0..2}; do if ((array1[i] > array2[i])); then IS_UPDATE=1 fi done if [ "$IS_UPDATE" -eq 1 ]; then retries=0 while [ $retries -lt $MAX_RETRIES ]; do ((retries++)) echo "Attempt $retries of $MAX_RETRIES" download_and_check_md5 && break if [ $retries -lt $MAX_RETRIES ]; then echo "Retrying in 5 seconds..." sleep 5 fi done if [ $retries -eq $MAX_RETRIES ]; then echo "Max retries reached. Exiting." exit 1 fi tar -xvzf "$ARCHIVE_FILE" -C "$INSTALL_DIR" rm "$ARCHIVE_FILE" chmod +x /usr/local/aix-agent/aix-agent sudo systemctl restart aix-agent echo "Update successful!" else echo "No update available. Current version: $LOCAL_VERSION" fi