feat(node): adding node component
Cleaning up the repo
This commit is contained in:
103
components/node/files/scripts/common.sh
Normal file
103
components/node/files/scripts/common.sh
Normal file
@ -0,0 +1,103 @@
|
||||
#!/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() {
|
||||
masterName=${VALKEY_MASTER_NAME:-"mymaster"}
|
||||
it="${1:-0}"
|
||||
|
||||
info=$(valkey-cli --csv -h ${VALKEY_HEADLESS_SERVICE} -p ${VALKEY_SENTINEL_PORT} sentinel get-primary-addr-by-name "${masterName}"| \
|
||||
awk -F ',' '{ gsub(/"/,"",$0); print $1 " " $2 }')
|
||||
|
||||
if [ -z "${info}" ]; then
|
||||
echo "Failed to get primary info for master '${masterName}'"
|
||||
it=$((it + 1))
|
||||
getPrimaryInfo ${it}
|
||||
if [ ${it} -ge 10 ]; then
|
||||
echo "Failed to get primary info after 5 attempts"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
if [[ "${info}" =~ /^NULL/ ]]; then
|
||||
it=$((it + 1))
|
||||
getPrimaryInfo ${it}
|
||||
if [ ${it} -ge 10 ]; then
|
||||
echo "Failed to get primary info after 5 attempts"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
echo "${info}"
|
||||
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