Here is a quick reference on how to upgrade F5 BIG-IP (tmos) using API instead of the traditional tmsh commands.
Step 1: Authenticate and Get the Token
F5 BIG-IP API uses a session-based authentication. First, you’ll authenticate using your credentials.
# Set the necessary parameters
BIGIP_HOST="https://<bigip_host>"
BIGIP_USER="<username>"
BIGIP_PASS="<password>"
LOG_FILE="f5_upgrade_error_log.txt"
# Function to log errors with detailed information
log_error() {
local status_code=$1
local response=$2
local api_url=$3
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$timestamp] ERROR: API call to $api_url failed with status code $status_code" >> $LOG_FILE
echo "Response: $response" >> $LOG_FILE
echo "-----------------------------" >> $LOG_FILE
}
# Get authentication token
echo "Authenticating to $BIGIP_HOST"
auth_response=$(curl -s -k -w "%{http_code}" -X POST "$BIGIP_HOST/mgmt/shared/authn/login" -H "Content-Type: application/json" -d '{"username": "'"$BIGIP_USER"'", "password": "'"$BIGIP_PASS"'", "loginProviderName": "local"}')
# Extract HTTP status code and response body
http_code=$(echo "$auth_response" | tail -n1) # Extract status code (last line)
auth_response_body=$(echo "$auth_response" | sed '$ d') # Remove status code
if [ "$http_code" -ne 200 ]; then
log_error "http_code" "$auth_response_body" "BIGIP_HOST/mgmt/shared/authn/login"
echo "Authentication failed. Check error log for details."
exit 1
fi
# Extract the token from the response
token=$(echo $auth_response_body | jq -r '.token.token')
echo "Authentication token: $token"
# GET current system version
api_url="BIGIP_HOST/mgmt/tm/sys/version"
echo "Making GET request to $api_url..."
response=$(curl -s -k -w "%{http_code}" -X GET "$api_url" -H "X-F5-Auth-Token: $token" -H "Content-Type: application/json")
# Extract HTTP status code and response body
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$ d')
if [ "$http_code" -ne 200 ]; then
log_error "$http_code" "$response_body" "$api_url"
echo "Failed to get system version. Check error log for details."
exit 1
fi
# Parse and display the version info if the request was successful
version=$(echo "$response_body" | jq -r '.version')
echo "BIG-IP version: $version"
# Save the successful request detail (response) to the log
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$timestamp] SUCCESS: API call to $api_url succeeded." >> $LOG_FILE
echo "Response: $response_body" >> $LOG_FILE
echo "-------------------------------" >> $LOG_FILEStep 2: Upload the Software Image
Next, upload the software image (<upgrade_image>.tgz) to the F5 BIG-IP system.
# Path to the upgrade image
image_path="/path/to/your/upgrade_image.tgz"
# Upload the software image
response=$(curl -s -k -w "%{http_code}" -X POST "$BIGIP_HOST/mgmt/tm/sys/backup" -H "Content-Type: application/json" -H "X-F5-Auth-Token: $token" -F "file=@$image_path")
# Extract HTTP status code and response body
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$ d')
if [ "$http_code" -ne 200 ]; then
log_error "$http_code" "$response_body" "$api_url"
echo "Failed to Upload the software. Check error log for details."
exit 1
fi
# Check the response
echo "Upload response: $response"Step 3: Initiate the Upgrade
Once the image is uploaded, you can trigger the upgrade by initiating the installation of the new software.
# Define the software image location (after upload)
image_location="bigip-image-2024.1.0.tgz" # replace with actual image file name
# Install the upgrade
response=$(curl -s -k -w "%{http_code}" -X POST "$BIGIP_HOST/mgmt/tm/sys/software/installation" -H "X-F5-Auth-Token: $token" -d '{"file": "'"$image_location"'", "reboot": true}')
# Extract HTTP status code and response body
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$ d')
if [ "$http_code" -ne 200 ]; then
log_error "$http_code" "$response_body" "$api_url"
echo "Failed to Upgrade the software. Check error log for details."
exit 1
fi
# Check the response
echo "Upgrade response: $response"Step 4: Reboot (if necessary)
The system will likely need to reboot to complete the upgrade. The reboot: true flag can trigger the reboot automatically, or you can do it manually using the following API call:
# Reboot the system (if not done automatically)
response=$(curl -s -k -w "%{http_code}" -X POST "$BIGIP_HOST/mgmt/tm/sys/reboot" -H "X-F5-Auth-Token: $token")
# Check the reboot response
echo "Reboot response: $response"Step 5: Verify the Upgrade
After rebooting, you can check the system version to verify that the upgrade was successful.
# Check the current version
response=$(curl -s -k -X GET "$BIGIP_HOST/mgmt/tm/sys/version" -H "X-F5-Auth-Toke: $token")
# Output the version details
echo "Current version:" $response
# Function to log status details
log_status () {
local status_message=$1
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$timestamp] $status_message" >> $LOG_FILE
}
# Check system health (CPU, memory, disk usage)
echo "Checking system health..."
health_response=$(curl -s -k -w "%{http_code}" -X GET "$BIGIP_HOST/mgmt/tm/sys/health" -H "X-F5-Auth-Token: $token" -H "Content-Type: application/json")
http_code=$(echo "$health_response" | tail -n1)
health_response_body=$(echo "$health_response" | sed '$ d')
if [ "$http_code" -ne 200 ]; then
log_status "Failed to retrieve system health. Status Code: $http_code. Response: $health_response_body"
echo "Failed to retrieve system health. Check the error log for details."
exit 1
fi
cpu_usage=$(echo "$health_response_body" | jq -r '.cpu')
memory_usage=$(echo "$health_response_body" | jw -r '.memory')
disk_usage=$(echo "$healeth_response_body" | jq -r '.disk')
log_status "System Health - CPU Usage: $cpu_usage, Memory Usage: $memory_usage, Disk Usage: $disk_usage"
# Check the status of services to ensure everything is running
echo "Checking service status..."
services_response=$(curl -s -k -w "%{http_code}" -X GET "$BIGIP_HOST/mgmt/tm/sys/service" -H "X-F5-Auth-Token: $token" -H "Content-Type: application/json")
http_code=$(echo "$services_reponse" | tail -n1)
services_response_body=$(echo "$services_response" | sed '$ d')
if [ "http_code" -ne 200 ]; then
log_status "Failed to retrieve service status. Status Code: $http_code. Response: $services_response_body"
echo "Failed to retrieve service status. Check the error log for details."
exit 1
fi
# Parse and log service status
echo "$services_resposne_body" | jq -r '.items[] | "\(.name): \(.status)"' >> $LOG_FILE
# Check Logs for any critical errors related to the upgrade
echo "Checking system logs for errors..."
log_check_response=$(curl -s -k -w "%{http_code}" -X GET "$BIGIP_HOST/mgmt/tm/sys/logs" -H "X-F5-Auth-Token: $token" -H "Content-Type: application/json")
http_code=$(echo "$log_check_response" | tail -n1)
log_check_response_body=$(echo "$log_check_response" | sed '$ d')
if [ "$http_code" -ne 200 ]; then
log_status "Failed to retrieve system logs. Status Code: $http_code. Response: $log_check_response_body"
echo "Failed to retrieve system logs. Check the error log. fordetails."
exit 1
fi
log_status "System logs checked successfully."
# Check status of network interfaces
echo "Checking network interfaceses status..."
network_interfaces_response=$(curl -s -k -w "%{http_code}" -X "$BIGIP_HOST/mgmt/tm/net/interface" -H "X-F5-Auth-Token: $token" -H "Content-Type: application/json")
http_code=$(echo "$network_interfaces_response" | tail -n1)
network_interfaces_response_body=$(echo "$network_interfaces_response" | sed '$ d')
if [ "http_code" -ne 200 ]; then
log_status "Failed to retrieve network interfaces status. Status Code: $http_code. Response: $network_interfaces_response_body"
echo "Failed to retrieve network interfaces status. Check the error log for details."
exit 1
fi
log_status "Network interfaces status checked successfully."
echo "Status check complete. See $LOG_FILE for detailed results."
Additional Notes:
- Token Expiry: The token may expire after some time, so ensure that your script handles token renewal if needed.
- Error Handling: The above scripts don’t include detailed error handling, so you should include error checking (E.G., checking HTTP status codes) for production scripts
- Software Image Location: The path to the image (image_location) must point to the correct directory or location where the upgrade image is uploaded.