First commit
This commit is contained in:
81
resources/node/files/scripts/common.sh
Normal file
81
resources/node/files/scripts/common.sh
Normal file
@ -0,0 +1,81 @@
|
||||
#!/bin/sh
|
||||
|
||||
|
||||
pingSentinel() {
|
||||
svc=${VALKEY_HEADLESS_SERVICE:-"localhost"}
|
||||
|
||||
resp=$(timeout -s 15 $1 \
|
||||
valkey-cli \
|
||||
-h ${svc} \
|
||||
-p $VALKEY_SENTINEL_PORT \
|
||||
ping)
|
||||
ret=${?}
|
||||
echo $resp
|
||||
return ${ret}
|
||||
}
|
||||
|
||||
getPrimaryInfo() {
|
||||
valkey-cli --csv -h ${VALKEY_HEADLESS_SERVICE} -p ${VALKEY_SENTINEL_PORT} sentinel get-primary-addr-by-name "mymaster"| \
|
||||
awk -F ',' '{ gsub(/"/,"",$0); print $1 " " $2 }'
|
||||
return ${?}
|
||||
}
|
||||
|
||||
getSentinelMasterName() {
|
||||
getPrimaryInfo | awk -F ' ' '{print $1}'
|
||||
}
|
||||
|
||||
getFailOverStatus() {
|
||||
# Check if the failover is finished
|
||||
local failoverStatus
|
||||
primaryInfo=$(getPrimaryInfo)
|
||||
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
|
||||
currentHost=$(hostname -f)
|
||||
if [[ "${primaryHost}" != "${currentHost}" ]]; then
|
||||
echo "Failover finished"
|
||||
return 0
|
||||
else
|
||||
echo "Failover in progress"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
waitForSentinel() {
|
||||
tout=60
|
||||
while true; do
|
||||
response=$(pingSentinel 5)
|
||||
if [ "${response}" = "PONG" ]; then
|
||||
echo "Sentinel is responding"
|
||||
break
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Sentinel is not responding [${response}]"
|
||||
sleep 1
|
||||
tout=$((tout - 1))
|
||||
if [ "${tout}" -le 0 ]; then
|
||||
echo "Sentinel ping timed out"
|
||||
return 124
|
||||
fi
|
||||
done
|
||||
sleep 10
|
||||
}
|
||||
|
||||
sentinelIsMaster() {
|
||||
# Check if the sentinel is master
|
||||
primaryInfo=$(getPrimaryInfo)
|
||||
primaryHost=$(echo ${primaryInfo} | awk -F ' ' '{print $1}')
|
||||
currentHost=$(hostname -f)
|
||||
if [[ "${primaryHost}" != "${currentHost}" ]]; then
|
||||
echo "Sentinel is not master"
|
||||
return 1
|
||||
else
|
||||
echo "Sentinel is master"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
sentinelReset() {
|
||||
# Reset the sentinel
|
||||
valkey-cli -h ${VALKEY_HEADLESS_SERVICE} -p ${VALKEY_SENTINEL_PORT} sentinel reset "${1}"
|
||||
return $?
|
||||
}
|
Reference in New Issue
Block a user