From 4f44e0ac54d0e874d10a1f6883a36f0ea7642e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=95=99=EC=9E=AC?= Date: Tue, 21 Dec 2021 02:57:02 +0900 Subject: [PATCH] =?UTF-8?q?=EB=AC=B4=EC=A4=91=EB=8B=A8=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- scripts/health.sh | 36 ++++++++++++++++++++++++++++++++++++ scripts/profile.sh | 31 +++++++++++++++++++++++++++++++ scripts/start.sh | 32 ++++++++++++++++++++++++++++++++ scripts/stop.sh | 18 ++++++++++++++++++ scripts/switch.sh | 17 +++++++++++++++++ 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 scripts/health.sh create mode 100644 scripts/profile.sh create mode 100644 scripts/start.sh create mode 100644 scripts/stop.sh create mode 100644 scripts/switch.sh diff --git a/build.gradle b/build.gradle index 94b6549..ad87b3d 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group 'com.ririnto.book' -version '1.0.1-SNAPSHOT' +version '1.0.1-SNAPSHOT-' + new Date().format("yyyyMMddHHmmss") sourceCompatibility = 1.8 repositories { diff --git a/scripts/health.sh b/scripts/health.sh new file mode 100644 index 0000000..0424cd3 --- /dev/null +++ b/scripts/health.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh +source ${ABSDIR}/switch.sh + +IDLE_PORT=$(find_idle_port) + +echo "> Health Check Start!" +echo "> IDLE_PORT: $IDLE_PORT" +echo "> curl -s http://localhost:$IDLE_PORT/profile" +sleep 10 + +for RETRY_COUNT in {1..10}; do + RESPONSE=$(curl -s http://localhost:${IDLE_PORT}/profile) + UP_COUNT=$(echo ${RESPONSE} | grep 'real' | wc -l) + + if [ ${UP_COUNT} -ge 1 ]; then # $up_count >= 1 ("real" 문자열이 있는지 검증) + echo "> Health check 성공" + switch_proxy + break + else + echo "> Health check의 응답을 알 수 없거나 혹은 실행 항태가 아닙니다." + echo "> Health check: ${RESPONSE}" + fi + + if [ ${RETRY_COUNT} -eq 10 ]; then + echo "> Health check 실패." + echo "> 엔진엑스에 연결하지 않고 배포를 종료합니다." + exit 1 + fi + + echo "> Health check 연결 실패. 재시도..." + sleep 10 +done diff --git a/scripts/profile.sh b/scripts/profile.sh new file mode 100644 index 0000000..ae43ab3 --- /dev/null +++ b/scripts/profile.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +# 쉬고 있는 profile 찾기: real1이 사용 중이면 real2가 쉬고 있고, 반대면 real1이 쉬고 있음 + +function find_idle_profile() { + RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/profile) + + if [ ${RESPONSE_CODE} -ge 400 ]; then # 400보다 크면(즉, 40x/50x 에러 모두 포함) + CURRENT_PROFILE=real2 + else + CURRENT_PROFILE=$(curl -s http://localhost/profile) + fi + + if [ ${CURRENT_PROFILE} == real1 ]; then + IDLE_PROFILE=real2 + else + IDLE_PROFILE=real1 + fi + + echo "${IDLE_PROFILE}" +} + +function find_idle_port() { + IDLE_PROFILE=$(find_idle_profile) + + if [ ${IDLE_PROFILE} == real1 ]; then + echo "8081" + else + echo "8082" + fi +} diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..e688d10 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh + +REPOSITORY=/home/ec2-user/app/step3 +PROJECT_NAME=freelec-springboot2-webservice + +echo "> Build 파일 복사" +echo "> cp $REPOSITORY/zip/*.jar $REPOSITORY/" + +cp $REPOSITORY/zip/*.jar $REPOSITORY/ + +echo "> 새 애플리케이션 배포" +JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tail -n 1) + +echo "> JAR_NAME: $JAR_NAME" + +echo "> $JAR_NAME 에 실행권한 추가" + +chmod +x $JAR_NAME + +echo "> $JAR_NAME 실행" + +IDLE_PROFILE=$(find_idle_profile) + +echo "> $JAR_NAME 를 profile=$IDLE_PROFILE 로 실행합니다." +nohup java -jar \ + -Dspring.config.location=classpath:/application.properties,classpath:/application-$IDLE_PROFILE.properties,/home/ec2-user/app/application-oauth.properties,/home/ec2-user/app/application-real-db.properties \ + -Dspring.profiles.active=$IDLE_PROFILE \ + $JAR_NAME > $REPOSITORY/nohup.out 2>&1 & diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100644 index 0000000..d9398db --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh + +IDLE_PORT=$(find_idle_port) + +echo "> $IDLE_PORT 에서 구동 중인 애플리케이션 pid 확인" +IDLE_PID=$(lsof -ti tcp:${IDLE_PORT}) + +if [ -z ${IDLE_PID} ]; then + echo "> 현재 구동 중인 애플리케이션이 없으므로 종료하지 않습니다." +else + echo "> kill -15 $IDLE_PID" + kill -15 ${IDLE_PID} + sleep 5 +fi diff --git a/scripts/switch.sh b/scripts/switch.sh new file mode 100644 index 0000000..08753ba --- /dev/null +++ b/scripts/switch.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh + +function switch_proxy() { + IDLE_PORT=$(find_idle_port) + + echo "> 전환할 Port: $IDLE_PORT" + echo "> Port 전환" + echo "set \$service_url http://127.0.0.1:${IDLE_PORT};" | sudo tee /etc/nginx/conf.d/service-url.inc + + echo "> 엔진엑스 Reload" + sudo systemctl stop nginx + sudo systemctl start nginx +} \ No newline at end of file