Clean up logging

This commit is contained in:
2025-10-15 10:39:19 +01:00
parent 150f065349
commit 2fed2ded69
5 changed files with 170 additions and 72 deletions

View File

@@ -9,21 +9,66 @@
MYDIR="$(dirname "$(realpath "$0")")"
# Load RSC configuration from rsc.json
# Logging helpers
VERBOSE=${VERBOSE:-0}
log_info() {
if [ "${VERBOSE}" -eq 1 ]; then
echo "INFO: $*"
fi
}
log_warn() {
echo "WARN: $*" >&2
}
log_error() {
echo "ERROR: $*" >&2
}
# Ensure required commands are available
require_cmds() {
local miss=0
for cmd in jq curl; do
if ! command -v "$cmd" >/dev/null 2>&1; then
log_error "Required command '$cmd' not found in PATH"
miss=1
fi
done
if [ "$miss" -ne 0 ]; then
exit 1
fi
}
require_cmds
# Load and validate RSC configuration from rsc.json
if [ -f "$MYDIR/rsc.json" ]; then
RSC_ID=$(jq -r '.client_id' "$MYDIR/rsc.json")
RSC_SECRET=$(jq -r '.client_secret' "$MYDIR/rsc.json")
RSC_HOST=$(jq -r '.access_token_uri' "$MYDIR/rsc.json" | sed 's|https://||' | sed 's|/api/client_token||')
RSC_ID=$(jq -r '.client_id // empty' "$MYDIR/rsc.json")
RSC_SECRET=$(jq -r '.client_secret // empty' "$MYDIR/rsc.json")
ACCESS_TOKEN_URI=$(jq -r '.access_token_uri // empty' "$MYDIR/rsc.json")
if [ -z "${RSC_ID}" ] || [ -z "${RSC_SECRET}" ] || [ -z "${ACCESS_TOKEN_URI}" ]; then
log_error "rsc.json is missing required fields: client_id, client_secret or access_token_uri"
log_error "Please populate $MYDIR/rsc.json (see rsc.json.example)"
exit 1
fi
# Derive host from access token URI and normalize
RSC_HOST=$(echo "$ACCESS_TOKEN_URI" | sed 's|https://||' | sed 's|/api/client_token||')
# Restrict config file permissions
if [ -f "$MYDIR/rsc.json" ]; then
chmod 600 "$MYDIR/rsc.json" 2>/dev/null || true
fi
log_info "Loaded RSC configuration for host: $RSC_HOST"
else
echo "ERROR: rsc.json configuration file not found"
log_error "rsc.json configuration file not found at $MYDIR/rsc.json"
exit 1
fi
# Set DATE command based on OS
if [[ "$OSTYPE" == "darwin"* ]]; then
DATE=gdate
# Set DATE command based on available binaries (prefer gdate if installed)
if command -v gdate >/dev/null 2>&1; then
DATE=gdate
else
DATE=date
DATE=date
fi
# Utility functions
@@ -42,8 +87,11 @@ cleanup () {
check_http_error () {
# All good responses start with a 2
if [ ${http_response:0:1} != "2" ]; then
echo FATAL: HTTP error from API call: $http_response. The server responded with:
cat /tmp/rbkresponse.$$ ; echo ; exit_with_error
log_error "HTTP error from API call: $http_response"
# Show a short excerpt of the response body for debugging
head -c 4096 /tmp/rbkresponse.$$ 2>/dev/null || true
echo
exit_with_error
fi
}
@@ -85,8 +133,11 @@ get_rsc_token () {
RSC_AUTH_TOKEN=$(cat /tmp/rbkresponse.$$ | jq -r '.access_token')
SECONDS=$(cat /tmp/rbkresponse.$$ | jq -r '.expires_in')
EXPIRATION=$($DATE +%s -d "+${SECONDS} seconds")
#cat /tmp/rbkresponse.$$ | jq
echo "$EXPIRATION $RSC_AUTH_TOKEN" > ~/.rbkRscsession.$id_string
# Save session securely
umask 077
printf '%s %s\n' "$EXPIRATION" "$RSC_AUTH_TOKEN" > ~/.rbkRscsession.$id_string
chmod 600 ~/.rbkRscsession.$id_string 2>/dev/null || true
log_info "Cached RSC session to ~/.rbkRscsession.$id_string"
}
@@ -136,11 +187,11 @@ EOF
#cat /tmp/payload.$$ | jq -r
error=$(cat /tmp/rbkresponse.$$ | jq -r '.errors // empty')
if [ "$error" ]; then
echo "ERROR: The last GraphQL API call returned an error"
log_error "The last GraphQL API call returned an error"
echo
echo "PAYLOAD:"
log_error "PAYLOAD:"
cat /tmp/payload.$$ | jq -r
echo "RESPONSE:"
log_error "RESPONSE:"
cat /tmp/rbkresponse.$$ | jq -r '.errors'
exit_with_error
fi
@@ -168,11 +219,11 @@ rsc_get_host_id() {
# Get all matching host IDs (portable, no mapfile)
host_ids=$(cat /tmp/rbkresponse.$$ | jq -r '.data.physicalHosts.nodes[] | .id')
host_count=$(echo "$host_ids" | grep -c .)
if [[ $host_count -ne 1 ]]; then
echo "ERROR: Multiple hosts found for '$1':"
cat /tmp/rbkresponse.$$ | jq -r '.data.physicalHosts.nodes[] | "\(.name) \(.id)"'
exit_with_error
fi
if [[ $host_count -ne 1 ]]; then
log_error "Multiple hosts found for '$1':"
cat /tmp/rbkresponse.$$ | jq -r '.data.physicalHosts.nodes[] | "\(.name) \(.id)"'
exit_with_error
fi
# Set the first match (or empty if none)
targetHostId=$(echo "$host_ids" | head -n 1)
@@ -189,9 +240,9 @@ rsc_find_database () {
#echo cluster UUID not, set, getting it
get_cluster_uuid
#echo Cluster UUID is $cluster_uuid
else
echo Cluster UUID was already $cluster_uuid
fi
else
log_info "Cluster UUID was already $cluster_uuid"
fi
variables="{
\"filter\":[{\"field\":\"REGEX\",\"texts\":[\"$RBK_SID\"]},{\"field\":\"IS_GHOST\",\"texts\":[\"false\"]},{\"field\":\"IS_ACTIVE\",\"texts\":[\"true\"]},{\"field\":\"CLUSTER_ID\",\"texts\":[\"$cluster_uuid\"]}],
@@ -356,8 +407,8 @@ if [ $num -eq 1 ]; then
read name rsc_db_id < <(echo $(cat /tmp/rbkresponse.$$ | jq -r '.data.globalSearchResults.edges[] | select (.node.objectType=="ORACLE_DATA_GUARD_GROUP" and .node.isRelic==false)| .node.name, .node.id'))
database_type="Data Guard"
elif [ $num -gt 1 ]; then
echo "ERROR: There were $num entries returned for Data Guard databases with name $RBK_SID"
exit_with_error
log_error "There were $num entries returned for Data Guard databases with name $RBK_SID"
exit_with_error
fi
if [ -z "$rsc_db_id" ]; then
@@ -370,8 +421,8 @@ if [ -z "$rsc_db_id" ]; then
database_type="Standalone"
#echo Good, There is just one Standalone DB with name $name and RSC ID $rsc_db_id
else
echo "ERROR: There were $num entries returned from JQ for DB with name $RBK_SID on host $RBK_HOST"
exit_with_error
log_error "There were $num entries returned from JQ for DB with name $RBK_SID on host $RBK_HOST"
exit_with_error
fi
else
num=$(cat /tmp/rbkresponse.$$ | jq -r --arg HOST "$RBK_HOST" '[.data.globalSearchResults.edges[] | select (.node.logicalPath[0].name==$HOST and .node.objectType=="OracleDatabase" and .node.isRelic==false)] | length')
@@ -380,8 +431,8 @@ if [ -z "$rsc_db_id" ]; then
database_type="Standalone"
#echo Good, There is just one Standalone DB with name $name on ${RBK_HOST} and RSC ID $rsc_db_id
else
echo "ERROR: There were $num entries returned from for DB with name $RBK_SID on host $RBK_HOST"
exit_with_error
log_error "There were $num entries returned from for DB with name $RBK_SID on host $RBK_HOST"
exit_with_error
fi
fi
fi