Making log output friendlier

This commit is contained in:
Mike Heier 2022-08-21 08:53:57 -05:00
parent e2f0d97823
commit 8eab9df256

View File

@ -15,7 +15,22 @@ else
echo "MISSING .env FILE!" echo "MISSING .env FILE!"
exit -1 exit -1
fi fi
#source .env
#
# Logging
#
write_log() {
logLevel='DEBUG'
if [[ ! -z $2 ]]; then
echo "$1: $2"
else
echo "logLevel: $1"
fi
}
write_message() {
write_log 'INFO' "$1"
}
# #
# CURL to get the bearer token for the following calls # CURL to get the bearer token for the following calls
@ -62,12 +77,11 @@ put_recordset() {
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$resource_group/providers/Microsoft.Network/dnsZones/$zone_name/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01 https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$resource_group/providers/Microsoft.Network/dnsZones/$zone_name/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01
} }
# Get an Accesss Token
#access_token=$(get_token)
# echo $access_token
resource_group_list=($RESOURCE_GROUP_NAMES) resource_group_list=($RESOURCE_GROUP_NAMES)
zone_list=($ZONE_NAMES) zone_list=($ZONE_NAMES)
write_message "======================================="
write_message "======================================="
for i in "${!resource_group_list[@]}"; do for i in "${!resource_group_list[@]}"; do
resource_group="${resource_group_list[i]}" resource_group="${resource_group_list[i]}"
zone_name="${zone_list[i]}" zone_name="${zone_list[i]}"
@ -75,31 +89,38 @@ for i in "${!resource_group_list[@]}"; do
# Get the Record Set # Get the Record Set
azure_recordset=$(get_recordset) azure_recordset=$(get_recordset)
echo "Azure Record Set: $azure_recordset" write_message "Azure Record Set: $zone_name"
# Parse the Record Set # Parse the Record Set
azure_ip_address=$(get_recordset_address $azure_recordset) azure_ip_address=$(get_recordset_address $azure_recordset)
echo "Azure IP: $azure_ip_address" write_message "Azure IP: $azure_ip_address"
# Get the currently assigned Public IP # Get the currently assigned Public IP
isp_ip_address=$(dig +short myip.opendns.com @resolver1.opendns.com) isp_ip_address=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "ISP IP: $isp_ip_address" write_message "ISP IP: $isp_ip_address"
# If the currently assigned public IP doesn't match the configured IP address in Azure... # If the currently assigned public IP doesn't match the configured IP address in Azure...
# update the Azure record set. # update the Azure record set.
if [ $azure_ip_address != $isp_ip_address ]; then if [ $azure_ip_address != $isp_ip_address ]; then
echo "IP ADDRESSES DON'T MATCH. UPDATING..." write_message "IP ADDRESSES DON'T MATCH. UPDATING..."
# Prepare the request content # Prepare the request content
azure_recordset_UPDATE=$(update_recordset $isp_ip_address) azure_recordset_UPDATE=$(update_recordset $isp_ip_address)
echo $azure_recordset_UPDATE write_message $azure_recordset_UPDATE
# Perform the Update # Perform the Update
AZURE_PUT_RESPONSE=$(put_recordset $azure_recordset_UPDATE) AZURE_PUT_RESPONSE=$(put_recordset $azure_recordset_UPDATE)
echo "Azure PUT Response: $AZURE_PUT_RESPONSE" write_message "Azure PUT Response: $AZURE_PUT_RESPONSE"
echo "Azure IP Address updated successfully" write_message "======================================="
write_message $'Azure IP Address updated successfully\n'
write_message "======================================="
else else
echo "IP Address is up to date." write_message "======================================="
write_message $'IP Address is up to date.\n'
write_message "======================================="
fi fi
done done
write_message "======================================="
write_message "======================================="