diff --git a/appspec.yml b/appspec.yml index bf5efbac..43218db7 100644 --- a/appspec.yml +++ b/appspec.yml @@ -12,8 +12,8 @@ permissions: group: ubuntu hooks: - ApplicationStart: - - location: scripts/deploy.sh - timeout: 60 + AfterInstall: # 기존 실행되던 애플리케이션 종료 + - location : scripts/execute-deploy.sh + timeout: 180 runas: ubuntu file_exists_behavior: OVERWRITE diff --git a/scripts/deploy-blue-green.sh b/scripts/deploy-blue-green.sh new file mode 100644 index 00000000..b4c102c1 --- /dev/null +++ b/scripts/deploy-blue-green.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +# jasypt 암호키 설정 +source /home/ubuntu/action/scripts/properties.sh + +PROJECT_NAME=space-club-backend +REPOSITORY=/home/ubuntu +PACKAGE=$REPOSITORY/action/build/libs/ +JAR_NAME=$(ls -tr $PACKAGE | grep 'SNAPSHOT.jar' | tail -n 1) +JAR_PATH=$PACKAGE$JAR_NAME +echo "> build 파일명: $JAR_NAME" + +echo "> build 파일 복사" +DEPLOY_PATH=$REPOSITORY/jar/ +cp $JAR_PATH $DEPLOY_PATH + +echo "> 현재 구동중인 SET 확인" +CURRENT_PROFILE=$(curl -s http://localhost/api/profile) +echo "> $CURRENT_PROFILE" + +# 쉬고 있는 set 찾기: set1이 사용중이면 set2가 쉬고 있고, 반대면 set1이 쉬고 있음 +if [ $CURRENT_PROFILE == blue ] +then + IDLE_PROFILE=green + IDLE_PORT=8082 +elif [ $CURRENT_PROFILE == green ] +then + IDLE_PROFILE=blue + IDLE_PORT=8081 +else + echo "> 일치하는 Profile이 없습니다. Profile: $CURRENT_PROFILE" + echo "> BLUE를 할당합니다. IDLE_PROFILE: blue" + IDLE_PROFILE=blue + IDLE_PORT=8081 +fi + +echo "> application.jar 교체" +IDLE_APPLICATION=$IDLE_PROFILE-SpaceClub.jar +IDLE_APPLICATION_PATH=$DEPLOY_PATH$IDLE_APPLICATION + +ln -Tfs $DEPLOY_PATH$JAR_NAME $IDLE_APPLICATION_PATH + +echo "> $IDLE_PROFILE 에서 구동중인 애플리케이션 pid 확인" +IDLE_PID=$(pgrep -f $IDLE_APPLICATION) + +if [ -z $IDLE_PID ] +then + echo "> 현재 구동중인 애플리케이션이 없으므로 종료하지 않습니다." +else + echo "> kill -15 $IDLE_PID" + kill -15 $IDLE_PID + sleep 5 +fi + +echo "> $IDLE_PROFILE 배포" +sudo nohup java -jar $JAR_PATH --spring.profiles.active=develop,$IDLE_PROFILE --jasypt.encryptor.password=${encrypt} > /home/ubuntu/log/nohup_log.out 2> /home/ubuntu/log/nohup_error.out & + +echo "> $IDLE_PROFILE 10초 후 Health check 시작" +echo "> curl -s http://localhost:$IDLE_PORT/actuator/health " +sleep 10 + +for retry_count in {1..10} +do + response=$(curl -s http://localhost:$IDLE_PORT/actuator/health) + up_count=$(echo $response | grep 'UP' | wc -l) + + if [ $up_count -ge 1 ] + then # $up_count >= 1 ("UP" 문자열이 있는지 검증) + echo "> Health check 성공" + break + else + echo "> Health check의 응답을 알 수 없거나 혹은 status가 UP이 아닙니다." + echo "> Health check: ${response}" + fi + + if [ $retry_count -eq 10 ] + then + echo "> Health check 실패. " + echo "> Nginx에 연결하지 않고 배포를 종료합니다." + exit 1 + fi + + echo "> Health check 연결 실패. 재시도..." + sleep 10 +done + +echo "> 스위칭" +sleep 10 +$REPOSITORY/scripts/switch.sh diff --git a/scripts/deploy-execute.sh b/scripts/deploy-execute.sh new file mode 100644 index 00000000..c0f2388c --- /dev/null +++ b/scripts/deploy-execute.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/home/ubuntu/action/scripts/deploy.sh > /dev/null 2> /dev/null < /dev/null & diff --git a/src/main/java/com/spaceclub/global/profile/ProfileController.java b/src/main/java/com/spaceclub/global/profile/ProfileController.java new file mode 100644 index 00000000..7c332bd1 --- /dev/null +++ b/src/main/java/com/spaceclub/global/profile/ProfileController.java @@ -0,0 +1,25 @@ +package com.spaceclub.global.profile; + +import lombok.RequiredArgsConstructor; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api") +public class ProfileController { + + private final Environment env; + + @GetMapping("/profile") + public String profile() { + return Arrays.stream(env.getActiveProfiles()) + .findAny() + .orElseGet(() -> " "); + } + +} diff --git a/src/main/resources/application-blue.yml b/src/main/resources/application-blue.yml new file mode 100644 index 00000000..11e54c54 --- /dev/null +++ b/src/main/resources/application-blue.yml @@ -0,0 +1,2 @@ +server: + port: 8081 diff --git a/src/main/resources/application-green.yml b/src/main/resources/application-green.yml new file mode 100644 index 00000000..4a2daf55 --- /dev/null +++ b/src/main/resources/application-green.yml @@ -0,0 +1,2 @@ +server: + port: 8082