diff --git a/appspec.yml b/appspec.yml index e89bc3e..560bf40 100644 --- a/appspec.yml +++ b/appspec.yml @@ -2,7 +2,7 @@ version: 0.0 os: linux files: - source: / - destination: /home/ec2-user/app/step2/zip/ + destination: /home/ec2-user/app/step3/zip/ overwrite: yes permissions: @@ -13,6 +13,17 @@ permissions: hooks: AfterInstall: - - location: ./deploy.sh + - location: stop.sh timeout: 600 - runas: root \ No newline at end of file + runas: root + + ApplicationStart: + - location: start.sh + timeout: 600 + runas: root + + ValidateService: + - location: health.sh + timeout: 600 + runas: root + diff --git a/build.gradle b/build.gradle index 98f8af6..bcdf3a7 100644 --- a/build.gradle +++ b/build.gradle @@ -7,13 +7,12 @@ plugins { } - jar { enabled = false } group = 'myblog' -version = '0.0.1-SNAPSHOT' +version = '0.0.1-SNAPSHOT-'+new Date().format("yyyyMMddHHmmss") sourceCompatibility = '11' configurations { diff --git a/scripts/health.sh b/scripts/health.sh new file mode 100644 index 0000000..e30126b --- /dev/null +++ b/scripts/health.sh @@ -0,0 +1,38 @@ +#!/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_C0UNT=$(echo ${RESPONSE} | grep 'real' | wc -l) + +if [ ${UP_C0UNT} -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 \ No newline at end of file diff --git a/scripts/profile.sh b/scripts/profile.sh new file mode 100644 index 0000000..675fd85 --- /dev/null +++ b/scripts/profile.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# 쉬고있는 profile 찾기 + +function find_idle_profile() { + RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/profile) + + if [ ${RESPONSE_CODE} -ge 400 ] + + then + 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}" +} + +# 쉬고있는 PROFILE PORT 찾기 + +function find_idle_port() { + IDLE_PROFILE=$(find_idle_profile) + + if [ ${IDLE_PROFILE} == real1 ]; + then + echo "8081" + else + echo "8082" + fi +} \ No newline at end of file diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..70ca5bb --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +ABSPATH=$(readlink -f $0) +ABSDIR=$(dirname $ABSPATH) +source ${ABSDIR}/profile.sh + +REPOSITORY=/home/ec2-user/app/step3 +PROJECT_NAME=springboot-blog + +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-$IDLE_PROFILE.yml,/home/ec2-user/app/application-dev.yml \ + $JAR_NAME > $REPOSITORY/nohup.out 2>&1 & \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100644 index 0000000..c3ef020 --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,21 @@ +#!/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..41effeb --- /dev/null +++ b/scripts/switch.sh @@ -0,0 +1,17 @@ +#! /usn/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 " > 엔진엑스 리로드" +sudo service nginx reload +} \ No newline at end of file