From c5b6bfd8a4f56e88d7bedae738b4b06357428dce Mon Sep 17 00:00:00 2001 From: JSoi Date: Wed, 27 Jul 2022 04:24:17 +0900 Subject: [PATCH] =?UTF-8?q?#42=20[Add]=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20?= =?UTF-8?q?=EB=B0=B0=ED=8F=AC=20Script=20=EC=B6=94=EA=B0=80,=20appspec=20y?= =?UTF-8?q?ml=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appspec.yml | 12 +++++++++--- scripts/health_check.sh | 35 +++++++++++++++++++++++++++++++++++ scripts/run_new_was.sh | 27 +++++++++++++++++++++++++++ scripts/switch.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 3 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/appspec.yml b/appspec.yml index 8e2fed6..f771f21 100644 --- a/appspec.yml +++ b/appspec.yml @@ -14,7 +14,13 @@ permissions: hooks: - AfterInstall: - - location: deploy.sh - timeout: 60 + ApplicationStart: + - location: scripts/run_new_was.sh + timeout: 180 + runas: ubuntu + - location: scripts/health_check.sh + timeout: 180 + runas: ubuntu + - location: scripts/switch.sh + timeout: 180 runas: ubuntu \ No newline at end of file diff --git a/scripts/health_check.sh b/scripts/health_check.sh new file mode 100644 index 0000000..74ca010 --- /dev/null +++ b/scripts/health_check.sh @@ -0,0 +1,35 @@ +# health_check.sh + +#!/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 9000 ]; then + TARGET_PORT=9001 +elif [ ${CURRENT_PORT} -eq 9001 ]; then + TARGET_PORT=9000 +else + echo "> No WAS is connected to nginx" + exit 1 +fi + + +echo "> Start health check of WAS at 'http://127.0.0.1:${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://127.0.0.1:${TARGET_PORT}/health) + + 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 0000000..7a403cc --- /dev/null +++ b/scripts/run_new_was.sh @@ -0,0 +1,27 @@ +# run_new_was.sh + +#!/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 9000 ]; then + TARGET_PORT=9001 +elif [ ${CURRENT_PORT} -eq 9001 ]; then + TARGET_PORT=9000 +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/baechelin/build/libs/*SNAPSHOT.jar > /home/ubuntu/logging/nohup.out 2>&1 & +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 0000000..42fda85 --- /dev/null +++ b/scripts/switch.sh @@ -0,0 +1,29 @@ +# switch.sh + +#!/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 9000 ]; then + TARGET_PORT=9001 +elif [ ${CURRENT_PORT} -eq 9001 ]; then + TARGET_PORT=9000 +else + echo "> No WAS is connected to nginx" + exit 1 +fi + +# Change proxying port into target port +echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" | tee /home/ubuntu/service_url.inc + +echo "> Now Nginx proxies to ${TARGET_PORT}." + +# Reload nginx +sudo service nginx reload + +echo "> Nginx reloaded." \ No newline at end of file