Skip to content

Commit

Permalink
Add Init Script (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Shaad7 <abdullah.alshaad@appscode.com>
  • Loading branch information
AbdullahAlShaad authored Feb 9, 2024
1 parent 7d220f5 commit 31794eb
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
8 changes: 6 additions & 2 deletions init_scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/bin/sh

if [ "$REDIS_TYPE" = "Cluster" ]; then
if [ "$REDIS_MODE" = "Cluster" ]; then
if [ "$MAJOR_REDIS_VERSION" = "4" ]; then
cp /tmp/scripts/cluster/redis-trib/* /scripts
else
cp /tmp/scripts/cluster/redis-cli/* /scripts
fi
cp /tmp/scripts/redis-node-finder /scripts/
elif [ "$REDIS_TYPE" = "Sentinel" ]; then
elif [ "$REDIS_MODE" = "Sentinel" ]; then

cp /tmp/scripts/sentinel/* /scripts
cp /tmp/scripts/redis-node-finder /scripts/
elif [ "$REDIS_MODE" = "Standalone" ]; then

cp /tmp/scripts/standalone/* /scripts
fi
23 changes: 23 additions & 0 deletions scripts/cluster/redis-cli/on-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ setUpRedisArgs() {
fi
redis_args="$auth_args $tls_args"
}

loadOldNodesConfIfExist() {
unset old_nodes_conf
if [ -e /data/nodes.conf ]; then
Expand Down Expand Up @@ -496,6 +497,27 @@ processRedisNode() {
fi
}

loadInitData() {
if [ -d "/init" ]; then
log "INIT" "Init Directory Exists"
waitForAllRedisServersToBeReady 120
cd /init || true
for file in /init/*
do
case "$file" in
*.sh)
log "INIT" "Running user provided initialization shell script $file"
sh "$file"
;;
*.lua)
log "INIT" "Running user provided initialization lua script $file"
redis-cli -c $redis_args --eval "$file"
;;
esac
done
fi
}

# --cluster-announce-ip command announces nodes ip to redis cluster and update nodes.conf file with updated ip
# for replica failover
# Solution Taken from : https://github.com/kubernetes/kubernetes/issues/57726#issuecomment-412467053
Expand All @@ -513,6 +535,7 @@ runRedis() {
setupInitialThings
startRedisServerInBackground
processRedisNode
loadInitData

log "REDIS" "Bringing back redis server in foreground. Adios"
wait $redis_server_pid
Expand Down
25 changes: 25 additions & 0 deletions scripts/sentinel/on-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,26 @@ not_exists_dns_entry() {
fi
}

loadInitData() {
if [ -d "/init" ]; then
log "INIT" "Init Directory Exists"
cd /init || true
for file in /init/*
do
case "$file" in
*.sh)
log "INIT" "Running user provided initialization shell script $file"
sh "$file"
;;
*.lua)
log "INIT" "Running user provided initialization lua script $file"
redis-cli ${redis_args[@]} --eval "$file"
;;
esac
done
fi
}

setUpInitialThings

while [[ flag -ne 0 ]]; do
Expand Down Expand Up @@ -321,4 +341,9 @@ else
fi
CheckIfMasterIsNotInSDownState
waitToSyncSentinelConfig

self="$HOSTNAME.$REDIS_GOVERNING_SERVICE"
waitForRedisToBeReady $self

loadInitData
wait $pid
104 changes: 104 additions & 0 deletions scripts/standalone/on-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/bin/sh

script_name=${0##*/}
timestamp() {
date +"%Y/%m/%d %T"
}
log() (
type="$1"
msg="$2"
echo "$(timestamp) [$script_name] [$type] $msg" | tee -a /tmp/log.txt
)
#Checks if auth password and tls certificate files exist on the node
#if yes, add them on the argument string
setUpRedisArgs() {
if [ "${REDISCLI_AUTH:-0}" != 0 ]; then
log "ARGS" "Setting up Auth arguments"
auth_args="-a ${REDISCLI_AUTH} --no-auth-warning"
fi

if [ "${TLS:-0}" = "ON" ]; then
log "ARGS" "Setting up TLS arguments"
ca_crt=/certs/ca.crt
client_cert=/certs/client.crt
client_key=/certs/client.key

if [ ! -f "$ca_crt" ] || [ ! -f "$client_cert" ] || [ ! -f "$client_key" ]; then
log "TLS is on , but $ca_crt or $client_cert or $client_key file does not exist"
exit 1
fi

tls_args="--tls --cert $client_cert --key $client_key --cacert $ca_crt"
fi
redis_args="$auth_args $tls_args"
}

checkIfRedisServerIsReady() {
host="$1"
is_current_redis_server_running=false

RESP=$(redis-cli -h "$host" -p 6379 $redis_args ping 2>/dev/null)
if [ "$RESP" = "PONG" ]; then
is_current_redis_server_running=true
fi
}

waitForAllRedisServersToBeReady() (
log "INFO" "Wait for $1s for redis server to be ready"
maxTimeout=$1
# shellcheck disable=SC2039
self_dns_name="$HOSTNAME.$REDIS_GOVERNING_SERVICE"

endTime=$(($(date +%s) + maxTimeout))
while [ "$(date +%s)" -lt $endTime ]; do
checkIfRedisServerIsReady "$self_dns_name"
if [ "$is_current_redis_server_running" = true ]; then
#log "INFO" "$domain_name is up"
break
fi
sleep 1
done

)

loadInitData() {
if [ -d "/init" ]; then
log "INIT" "Init Directory Exists"
waitForAllRedisServersToBeReady 120
cd /init || true
for file in /init/*
do
case "$file" in
*.sh)
log "INIT" "Running user provided initialization shell script $file"
sh "$file"
;;
*.lua)
log "INIT" "Running user provided initialization lua script $file"
redis-cli $redis_args --eval "$file"
;;
esac
done
fi
}

runRedisServerInBackground() {

log "REDIS" "Started Redis Server In Background"
cp /usr/local/etc/redis/default.conf /data/default.conf
exec redis-server /data/default.conf $args &
redis_server_pid=$!
}

runRedis(){

setUpRedisArgs
runRedisServerInBackground
loadInitData

log "REDIS" "Bringing back redis server in foreground. Adios"
wait $redis_server_pid
}

args=$*
runRedis

0 comments on commit 31794eb

Please sign in to comment.