Skip to content

Commit c9a7e89

Browse files
committed
Add a monitor script in keycloak template to restart the keycloak server if it goes down
1 parent ff9f4aa commit c9a7e89

File tree

1 file changed

+66
-17
lines changed

1 file changed

+66
-17
lines changed

recipes/res/res_demo_env/assets/keycloak.yaml

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,29 @@ Resources:
162162
mkdir -p /root/bootstrap/logs/
163163
exec > /root/bootstrap/logs/userdata.log 2>&1
164164
165+
# Create utils.sh script
166+
echo -e "#!/bin/sh
167+
wait_for_server() {
168+
SERVER_URL=\$1
169+
MAX_ATTEMPTS=\$2
170+
RETRY_INTERVAL=\$3
171+
attempt=0
172+
while [ \$attempt -lt \$MAX_ATTEMPTS ]; do
173+
response=\$(curl -s -o /dev/null -w \"%{http_code}\" \"\$SERVER_URL\")
174+
if [ \"\$response\" == \"200\" ] || [ \"\$response\" == \"302\" ]; then
175+
echo \"Server is up!\"
176+
return 0
177+
else
178+
echo \"Server is not yet up. Retrying in \$RETRY_INTERVAL seconds...\"
179+
sleep \$RETRY_INTERVAL
180+
((attempt++))
181+
fi
182+
done
183+
echo \"Server is not up after \$MAX_ATTEMPTS attempts, exiting...\"
184+
return 1
185+
}
186+
" > /root/bootstrap/utils.sh
187+
165188
#Install java17
166189
MAX_ATTEMPTS=5
167190
RETRY_INTERVAL=5
@@ -195,23 +218,14 @@ Resources:
195218
SERVER_URL="http://0.0.0.0:80"
196219
MAX_ATTEMPTS=15
197220
RETRY_INTERVAL=10
198-
attempt=0
199-
while [ $attempt -lt $MAX_ATTEMPTS ]; do
200-
response=$(curl -s -o /dev/null -w "%{http_code}" "$SERVER_URL")
201-
if [ "$response" == "200" ] || [ "$response" == "302" ]; then
202-
echo "Server is up!"
203-
break
204-
else
205-
echo "Server is not yet up. Retrying in $RETRY_INTERVAL seconds..."
206-
sleep $RETRY_INTERVAL
207-
((attempt++))
208-
if [ $attempt == $MAX_ATTEMPTS ]; then
209-
echo "Server is not up, exiting.."
210-
/opt/aws/bin/cfn-signal -e 1 --stack "${AWS::StackName}" --resource "KeycloakEC2Instance" --region "${AWS::Region}"
211-
sleep 30
212-
fi
213-
fi
214-
done
221+
222+
# Initial setup to wait for the server to be up
223+
. /root/bootstrap/utils.sh
224+
wait_for_server "$SERVER_URL" $MAX_ATTEMPTS $RETRY_INTERVAL
225+
if [ $? -ne 0 ]; then
226+
/opt/aws/bin/cfn-signal -e 1 --stack "${AWS::StackName}" --resource "KeycloakEC2Instance" --region "${AWS::Region}"
227+
sleep 30
228+
fi
215229
216230
echo "Keycloak server is up"
217231
# Login to Keycloak
@@ -299,6 +313,41 @@ Resources:
299313
(crontab -l; echo "*/30 * * * * /root/bootstrap/password_rotation.sh") | crontab -
300314
(crontab -l; echo "*/5 * * * * /root/bootstrap/user_sync.sh") | crontab -
301315
316+
# Monitoring script to restart Keycloak if it goes down
317+
echo -e "#!/bin/sh -x
318+
exec >> /root/bootstrap/logs/userdata.log 2>&1
319+
. /root/bootstrap/utils.sh
320+
SERVER_URL=\"http://0.0.0.0:80\"
321+
MAX_ATTEMPTS=15
322+
RETRY_INTERVAL=10
323+
324+
while true; do
325+
echo \"Start monitoring keycloak server...\"
326+
response=\$(curl -s -o /dev/null -w \"%{http_code}\" \"\$SERVER_URL\")
327+
if [ \"\$response\" == \"200\" ] || [ \"\$response\" == \"302\" ]; then
328+
echo \"Keycloak server is running.\"
329+
else
330+
# Check for running Keycloak processes and kill them if found
331+
if pgrep -f \"keycloak\" > /dev/null; then
332+
pkill -f \"keycloak\"
333+
echo \"Killed existing Keycloak processes.\"
334+
else
335+
echo \"No Keycloak processes found.\"
336+
fi
337+
echo \"Keycloak server is down. Restarting...\"
338+
339+
cd /root/bootstrap/keycloak-$KEYCLOAK_VERSION
340+
sudo -E nohup ./bin/kc.sh start-dev --http-port 80 > keycloak.log &
341+
wait_for_server \"\$SERVER_URL\" \$MAX_ATTEMPTS \$RETRY_INTERVAL
342+
fi
343+
sleep 60
344+
done
345+
" > /root/bootstrap/monitor.sh
346+
chmod +x /root/bootstrap/monitor.sh
347+
348+
# Start the monitoring script in the background
349+
nohup /root/bootstrap/monitor.sh &
350+
302351
# Signal stack to continue based on last command output
303352
/opt/aws/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource "KeycloakEC2Instance" --region "${AWS::Region}"
304353
- KeycloakVersion: !FindInMap [Keycloak, Config, Version]

0 commit comments

Comments
 (0)