60 lines
1.3 KiB
Bash
60 lines
1.3 KiB
Bash
|
#!/bin/sh
|
||
|
|
||
|
|
||
|
pingSentinel() {
|
||
|
svc=${VALKEY_SERVICE:-"localhost"}
|
||
|
|
||
|
resp=$(timeout -s 15 $1 \
|
||
|
valkey-cli \
|
||
|
-h ${svc} \
|
||
|
-p $VALKEY_SENTINEL_PORT \
|
||
|
ping)
|
||
|
ret=${?}
|
||
|
echo $resp
|
||
|
return ${ret}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
getPrimaryInfo() {
|
||
|
valkey-cli --csv -h ${VALKEY_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}'
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|