93 lines
2.5 KiB
Bash
93 lines
2.5 KiB
Bash
#!/bin/bash
|
|
print_error() {
|
|
RED='\033[0;31m'
|
|
NC='\033[0m'
|
|
echo -e "${RED}[ERROR] $1${NC}" >&2
|
|
}
|
|
print_success() {
|
|
GREEN='\033[0;32m'
|
|
NC='\033[0m'
|
|
echo -e "${GREEN}[SUCCESS] $1${NC}" >&2
|
|
}
|
|
if ! command -v jq >/dev/null 2>&1 ; then
|
|
wget -O /usr/bin/jq http://download.jsaix.cn/d/linux/jq-1.6-linux64 && chmod +x /usr/bin/jq
|
|
if ! command -v jq >/dev/null 2>&1 ; then
|
|
print_error "Unsupported Linux distribution"
|
|
exit 1
|
|
fi
|
|
fi
|
|
while getopts "u:" opt; do
|
|
case $opt in
|
|
u)
|
|
UUID="$OPTARG"
|
|
;;
|
|
\?)
|
|
print_error "Invalid option: -$OPTARG"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
if [ -z "$UUID" ]; then
|
|
print_error "Error: Missing UUID parameter. Usage: $0 -u <UUID>"
|
|
exit 1
|
|
fi
|
|
API_ENDPOINT="https://gateway.jsaix.cn/api/servers/agent_install/$UUID"
|
|
REMOTE_RESPONSE=$(curl -s "$API_ENDPOINT")
|
|
CODE=$(echo "$REMOTE_RESPONSE" | jq -r '.code')
|
|
if [ "$CODE" != "1" ]; then
|
|
print_error "Error: Remote API returned a non-successful code. Exiting."
|
|
print_error "$(echo "$REMOTE_RESPONSE" | jq -r '.message')"
|
|
exit 1
|
|
fi
|
|
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" ]; then
|
|
print_error "Error: Failed to fetch valid download information from the remote API."
|
|
exit 1
|
|
fi
|
|
INSTALL_DIR="/usr/local/aix-agent"
|
|
MAX_RETRIES=3
|
|
if [ ! -d "$INSTALL_DIR" ]; then
|
|
mkdir -p "$INSTALL_DIR"
|
|
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
|
|
print_error "Error: MD5 checksum does not match."
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
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
|
|
print_error "Retrying in 5 seconds..."
|
|
sleep 5
|
|
fi
|
|
done
|
|
if [ $retries -eq $MAX_RETRIES ]; then
|
|
print_error "Max retries reached. Exiting."
|
|
exit 1
|
|
fi
|
|
echo "AIX_UUID=$UUID" > .env
|
|
echo "Extracting archive..."
|
|
tar -xvzf "$ARCHIVE_FILE" -C "$INSTALL_DIR"
|
|
rm "$ARCHIVE_FILE"
|
|
cp aix-agent.service /etc/systemd/system
|
|
systemctl enable aix-agent
|
|
service aix-agent start
|
|
print_success "Installation completed successfully."
|