Skip to content

Commit

Permalink
[CHORE] Deploy Script Modify
Browse files Browse the repository at this point in the history
- run_new_was.sh 에서 변경된 포트가 service-url.inc에 반영이 안된 상태로 health_check.sh가 실행되는 문제 해결
  • Loading branch information
yummygyudon committed Oct 3, 2024
1 parent 058422b commit eedf3a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
5 changes: 3 additions & 2 deletions appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ permissions:
group: ubuntu

hooks:
ApplicationStart:
- location: scripts/run_new_was.sh
AfterInstall:
- location: script/run_new_was.sh
timeout: 180
runas: ubuntu
ApplicationStart:
- location: scripts/health_check.sh
timeout: 500
runas: ubuntu
Expand Down
15 changes: 5 additions & 10 deletions scripts/health_check.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#!/bin/bash

CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0
CURRENT_RUNNING_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1)

if [ ${CURRENT_PORT} -eq 8081 ]; then
TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
TARGET_PORT=8081
else
if [ ${CURRENT_RUNNING_PORT} -ne 8081 -o ${CURRENT_RUNNING_PORT} -ne 8082 ]; then
# run_new_was 에서 정상적으로 처리되지 않았다는 의미 - 더 이상 진행되면 안된다.
echo "> No WAS is connected to nginx"
exit 1
fi


echo "> Start health check of WAS at 'http://127.0.0.1:${TARGET_PORT}/api/v1/test' ..."
echo "> Start health check of WAS at 'http://127.0.0.1:${CURRENT_RUNNING_PORT}/api/v1/test' ..."

for RETRY_COUNT in {1..10}
do
echo "> #${RETRY_COUNT} trying..."
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${TARGET_PORT}/api/v1/test)
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${CURRENT_RUNNING_PORT}/api/v1/test)

if [ ${RESPONSE_CODE} -eq 200 ]; then
echo "> New WAS successfully running"
Expand Down
35 changes: 20 additions & 15 deletions scripts/run_new_was.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
#!/bin/bash

CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0
# 현재 proxy 중인 실구동 port
CURRENT_RUNNING_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1)

echo "> Current port of running WAS is ${CURRENT_PORT}."
echo "> Current port of running WAS is ${CURRENT_RUNNING_PORT}."

if [ ${CURRENT_PORT} -eq 8081 ]; then
TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
TARGET_PORT=8081
if [ ${CURRENT_RUNNING_PORT} -eq 8081 ]; then
NEXT_RUNNING_PORT=8082
elif [ ${CURRENT_RUNNING_PORT} -eq 8082 ]; then
NEXT_RUNNING_PORT=8081
else
echo "> No WAS is connected to nginx"
NEXT_RUNNING_PORT=8081
fi

TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+')
# 혹시나 다른 프로세스가 실행되고 있을 경우를 대비하여 PID Kill
CHECK_PID=$(lsof -Fp -i TCP:${NEXT_RUNNING_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}
if [ ! -z ${CHECK_PID} ]; then
echo "> Kill WAS running at ${NEXT_RUNNING_PORT}."
sudo kill ${CHECK_PID}
fi


if [ "$DEPLOYMENT_GROUP_NAME" == "prod" ]
then
nohup java -jar -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=prod /home/ubuntu/operation/operation-api/build/libs/operation-api-0.0.1-SNAPSHOT.jar > /dev/null 2> /dev/null < /dev/null &
echo "> Now new WAS runs at ${TARGET_PORT}."
nohup java -jar -Dserver.port=${NEXT_RUNNING_PORT} -Dspring.profiles.active=prod /home/ubuntu/operation/operation-api/build/libs/operation-api-0.0.1-SNAPSHOT.jar > /dev/null 2> /dev/null < /dev/null &
echo "> Now new WAS runs at ${NEXT_RUNNING_PORT}."
fi

if [ "$DEPLOYMENT_GROUP_NAME" == "dev" ]
then
nohup java -jar -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=dev /home/ubuntu/operation/operation-api/build/libs/operation-api-0.0.1-SNAPSHOT.jar > /dev/null 2> /dev/null < /dev/null &
echo "> Now new WAS runs at ${TARGET_PORT}."
nohup java -jar -Dserver.port=${NEXT_RUNNING_PORT} -Dspring.profiles.active=dev /home/ubuntu/operation/operation-api/build/libs/operation-api-0.0.1-SNAPSHOT.jar > /dev/null 2> /dev/null < /dev/null &
echo "> Now new WAS runs at ${NEXT_RUNNING_PORT}."
fi
sleep 10

echo "set \$service_url http://127.0.0.1:${NEXT_RUNNING_PORT};" |sudo tee /etc/nginx/conf.d/service-url.inc

exit 0
23 changes: 4 additions & 19 deletions scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
#!/bin/bash

CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0
CURRENT_RUNNING_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1)

echo "> Nginx currently proxies to ${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"
exit 1
fi

echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" |sudo tee /etc/nginx/conf.d/service-url.inc

echo "> Now Nginx proxies to ${TARGET_PORT}."
echo "> Nginx currently proxies to ${CURRENT_RUNNING_PORT}."

sudo service nginx reload

echo "> Nginx reloaded."

CURRENT_PID=$(lsof -Fp -i TCP:${CURRENT_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+')

sudo kill ${CURRENT_PID}
echo "> curl -s http://127.0.0.1:$CURRENT_RUNNING_PORT/api/v1/test"
echo "> Now Nginx proxies to ${CURRENT_RUNNING_PORT}."

0 comments on commit eedf3a5

Please sign in to comment.