From 10adcded2947b539a344f3db62f29ce2e8cff440 Mon Sep 17 00:00:00 2001 From: dldmsql Date: Mon, 6 Nov 2023 21:16:55 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=ED=8C=8C=EC=9D=BC=20=ED=8A=B8?= =?UTF-8?q?=EB=9E=99=ED=82=B9=20=EC=A0=9C=EC=99=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 -- scripts/health-check.sh | 33 +++++++++++++++++++++++++++++++++ scripts/run_new_was.sh | 25 +++++++++++++++++++++++++ scripts/switch.sh | 28 ++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 scripts/health-check.sh create mode 100644 scripts/run_new_was.sh create mode 100644 scripts/switch.sh diff --git a/.gitignore b/.gitignore index 17d7f141..c3724bfc 100644 --- a/.gitignore +++ b/.gitignore @@ -40,5 +40,3 @@ out/ src/main/resources/application-secret.yml src/test/resources/application-test.yml src/main/resources/logback-spring.xml - -/scripts \ No newline at end of file diff --git a/scripts/health-check.sh b/scripts/health-check.sh new file mode 100644 index 00000000..55863484 --- /dev/null +++ b/scripts/health-check.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Crawl current connected port of WAS +CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1) +TARGET_PORT=0 + +# Toggle port Number +if [ ${CURRENT_PORT} -eq 8081 ]; then + TARGET_PORT=8082 +elif [ ${CURRENT_PORT} -eq 8082 ]; then + TARGET_PORT=8081 +else + echo "> No WAS is connected to nginx" + exit 1 +fi + + +echo "> Start health check of WAS at 'http://13.209.202.74:${TARGET_PORT}' ..." + +for RETRY_COUNT in 1 2 3 4 5 6 7 8 9 10 +do + echo "> #${RETRY_COUNT} trying..." + RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://13.209.202.74:${TARGET_PORT}/api) + + if [ ${RESPONSE_CODE} -eq 200 ]; then + echo "> New WAS successfully running" + exit 0 + elif [ ${RETRY_COUNT} -eq 10 ]; then + echo "> Health check failed." + exit 1 + fi + sleep 10 +done \ No newline at end of file diff --git a/scripts/run_new_was.sh b/scripts/run_new_was.sh new file mode 100644 index 00000000..9f9b3b48 --- /dev/null +++ b/scripts/run_new_was.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1) +TARGET_PORT=0 + +echo "> Current port of running WAS is ${CURRENT_PORT}." + +if [ ${CURRENT_PORT} -eq 8081 ]; then + TARGET_PORT=8082 +elif [ ${CURRENT_PORT} -eq 8082 ]; then + TARGET_PORT=8081 +else + echo "> No WAS is connected to nginx" +fi + +TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') + +if [ ! -z ${TARGET_PID} ]; then + echo "> Kill WAS running at ${TARGET_PORT}." + sudo kill ${TARGET_PID} +fi + +nohup java -jar -Dserver.port=${TARGET_PORT} /home/ubuntu/duaily-server/build/libs/* > /home/ubuntu/nohup.out 2>&1 --spring.profiles.active=prod & +echo "> Now new WAS runs at ${TARGET_PORT}." +exit 0 \ No newline at end of file diff --git a/scripts/switch.sh b/scripts/switch.sh new file mode 100644 index 00000000..5f255e3e --- /dev/null +++ b/scripts/switch.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Crawl current connected port of WAS +CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1) +TARGET_PORT=0 + +echo "> Nginx currently proxies to ${CURRENT_PORT}." + +# Toggle port number +if [ ${CURRENT_PORT} -eq 8081 ]; then + TARGET_PORT=8082 +elif [ ${CURRENT_PORT} -eq 8082 ]; then + TARGET_PORT=8081 +else + echo "> No WAS is connected to nginx" + exit 1 +fi + +# Change proxying port into target port +echo "set \$service_url http://13.209.202.74:${TARGET_PORT};" | tee /home/ubuntu/service_url.inc +echo "set \$service_admin_url http://13.209.202.74:${TARGET_PORT}/admin;" | tee /home/ubuntu/service_admin_url.inc + +echo "> Now Nginx proxies to ${TARGET_PORT}." + +# Reload nginx +sudo service nginx reload + +echo "> Nginx reloaded." \ No newline at end of file