Skip to content

Commit

Permalink
무중단 배포 구현 ..1 #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-creative committed Nov 9, 2023
1 parent 7ef165d commit ca630e6
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 5 deletions.
22 changes: 17 additions & 5 deletions appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ permissions:
group: ubuntu

hooks:
AfterInstall:
- location: scripts/stop.sh
timeout: 60
# AfterInstall:
# - location: scripts/stop.sh
# timeout: 60
ApplicationStart:
- location: scripts/start.sh
timeout: 60
- location: scripts/run_new_was.sh
timeout: 180
# runas: ec2-user

- location: scripts/health_check.sh
timeout: 180
# runas: ec2-user

- location: scripts/switch.sh
timeout: 180
# runas: ec2-user

# - location: scripts/start.sh
# timeout: 60
41 changes: 41 additions & 0 deletions scripts/health_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# health_check.sh

#!/bin/bash

# service_url.inc 에서 현재 서비스를 하고 있는 WAS의 포트 번호 가져오기
CURRENT_PORT=$(cat /home/ec2-user/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

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://127.0.0.1:${TARGET_PORT}' ..."

# 아래 커맨드들을 새로 열린 서버가 정상적으로 작동하는지 확인

# 해당 커맨드들을 10번씩 반복
for RETRY_COUNT in 1 2 3 4 5 6 7 8 9 10
do
echo "> #${RETRY_COUNT} trying..."
# 테스트할 API 주소를 통해 http 상태 코드 가져오기
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${TARGET_PORT}/member/login)

# RESPONSE_CODE의 http 상태가 200번인 경우
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
sleep 15
done
48 changes: 48 additions & 0 deletions scripts/run_new_was.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# run_new_was.sh

#!/bin/bash

PROJECT_ROOT="/home/ubuntu/spring-github-action" # 프로젝트 루트
JAR="$ROOT_PATH/application.jar"

APP_LOG="$ROOT_PATH/application.log"
ERROR_LOG="$ROOT_PATH/error.log"
START_LOG="$ROOT_PATH/start.log"

# service_url.inc 에서 현재 서비스를 하고 있는 WAS의 포트 번호 가져오기
CURRENT_PORT=$(cat /home/ec2-user/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 # 현재포트가 8081이면 8082로 배포
elif [ ${CURRENT_PORT} -eq 8082 ]; then
TARGET_PORT=8081 # 현재포트가 8082라면 8081로 배포
else
echo "> Not connected to nginx" # nginx가 실행되고 있지 않다면 에러 코드
fi

# 현재 포트의 PID를 불러온다
TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+')

# PID를 이용해 해당 포트 서버 Kill
if [ ! -z ${TARGET_PID} ]; then
echo "> Kill ${TARGET_PORT}."
sudo kill ${TARGET_PID}
fi

echo "[$NOW] $JAR 복사" >> $START_LOG
cp $ROOT_PATH/build/libs/*.jar $JAR

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 81 -j REDIRECT --to-port 8081

# 타켓 포트에 jar파일을 이용해 새로운 서버 실행
echo "[$NOW] > $JAR 실행 $TARGET_PORT Port" >> $START_LOG
nohup java -jar -Dserver.port=${TARGET_PORT} ${JAR_FILE} > $APP_LOG 2> $ERROR_LOG &

SERVICE_PID=$(pgrep -f $JAR)
echo "[$NOW] > 서비스 PID: $SERVICE_PID" >> $START_LOG

exit 0
30 changes: 30 additions & 0 deletions scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# switch.sh

#!/bin/bash

# service_url.inc 에서 현재 서비스를 하고 있는 WAS의 포트 번호 가져오기
CURRENT_PORT=$(cat /home/ec2-user/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

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

# 위 커맨드들을 통해 현재 타겟포트 가져오기

# $ service_url.inc 파일을 현재 바뀐 서버의 포트로 변경
echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" | tee /home/ec2-user/service_url.inc

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

# nginx를 reload 해준다.
sudo service nginx reload

echo "> Nginx reloaded."

0 comments on commit ca630e6

Please sign in to comment.