Skip to content

Commit

Permalink
release: v0.0.7
Browse files Browse the repository at this point in the history
release: v0.0.7
  • Loading branch information
pushedrumex authored Jun 3, 2024
2 parents 2e4b3ae + 95aaab4 commit 09d56db
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 12 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'io.rest-assured:rest-assured:5.4.0'
implementation 'org.springframework.boot:spring-boot-starter-mail:3.2.2'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
Expand Down Expand Up @@ -105,6 +106,8 @@ def jacocoExcludePatterns = [
"**/exception/**",
"**/config/**",
"**/aspect/**",
"**/common/**",
"**/global/profile/**"
]

def excludedClassFilesForReport(classDirectories, jacocoExcludePatterns) {
Expand Down
68 changes: 58 additions & 10 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PROJECT_ROOT=/home/ubuntu/app
PROJECT_NAME=indp-server
APPLICATION_NAME=indp

DEPLOY_PATH=$PROJECT_ROOT/deploy

Expand All @@ -15,21 +14,70 @@ echo "--------------------------------------------------------------------------
echo "[ $(date +%c) ] Build ํŒŒ์ผ ๋ณต์‚ฌ" >> $DEPLOY_LOG
cp $PROJECT_ROOT/$PROJECT_NAME/build/libs/*.jar $DEPLOY_PATH/

echo "[ $(date +%c) ] ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ํ™•์ธ" >> $DEPLOY_LOG
CURRENT_PID=$(pgrep -f $APPLICATION_NAME)
CURRENT_PROFILE=$(curl -s http://localhost/api/profile)
echo "[ $(date +%c) ] ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ Profile: $CURRENT_PROFILE" >> $DEPLOY_LOG
if [ "$CURRENT_PROFILE" == prod1 ]
then
IDLE_PROFILE=prod2
IDLE_PORT=8082
elif [ "$CURRENT_PROFILE" == prod2 ]
then
IDLE_PROFILE=prod1
IDLE_PORT=8081
else
echo "> ์ผ์น˜ํ•˜๋Š” Profile์ด ์—†์Šต๋‹ˆ๋‹ค. Profile: $CURRENT_PROFILE"
echo "> prod1์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. IDLE_PROFILE: prod1"
IDLE_PROFILE=prod1
IDLE_PORT=8081
fi

echo "[ $(date +%c) ] $IDLE_PORT ํฌํŠธ์—์„œ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ํ™•์ธ" >> $DEPLOY_LOG
IDLE_PID=$(lsof -ti tcp:${IDLE_PORT})

echo "[ $(date +%c) ] ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid: $CURRENT_PID" >> $DEPLOY_LOG
if [ -z "$CURRENT_PID" ]; then
echo "[ $(date +%c) ] ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค." >> $DEPLOY_LOG
if [ -z "$IDLE_PID" ]
then
echo "[ $(date +%c) ] $IDLE_PORT ํฌํŠธ์—์„œ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค." >> $DEPLOY_LOG
else
echo "[ $(date +%c) ] kill -15 $CURRENT_PID" >> $DEPLOY_LOG
kill -15 "$CURRENT_PID"
sleep 5
echo "[ $(date +%c) ] kill -15 $IDLE_PID" >> $DEPLOY_LOG
kill -15 "$IDLE_PID"
sleep 5
fi

echo "[ $(date +%c) ] ์ƒˆ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ" >> $DEPLOY_LOG

JAR_NAME=$(ls -tr "$DEPLOY_PATH" | grep jar | tail -n 1)

echo "[ $(date +%c) ] JAR Name: $JAR_NAME" >> $DEPLOY_LOG
nohup java -jar $DEPLOY_PATH/"$JAR_NAME" --spring.profiles.active=prod > $APP_LOG 2> $ERROR_LOG &
nohup java -jar $DEPLOY_PATH/"$JAR_NAME" --spring.profiles.active=$IDLE_PROFILE > $APP_LOG 2> $ERROR_LOG &

echo "[ $(date +%c) ] $IDLE_PROFILE 10์ดˆ ํ›„ Health check ์‹œ์ž‘"
sleep 10

for retry_count in {1..10}
do
response=$(curl -s http://localhost:$IDLE_PORT/health)
up_count=$(echo $response | grep 'UP' | wc -l)

if [ $up_count -ge 1 ]
then
echo "[ $(date +%c) ] Health check ์„ฑ๊ณต"
break
else
echo "[ $(date +%c) ] Health check์˜ ์‘๋‹ต์„ ์•Œ ์ˆ˜ ์—†๊ฑฐ๋‚˜ ํ˜น์€ status๊ฐ€ UP์ด ์•„๋‹™๋‹ˆ๋‹ค."
echo "[ $(date +%c) ] Health check: ${response}"
fi

if [ $retry_count -eq 10 ]
then
echo "[ $(date +%c) ] Health check ์‹คํŒจ. "
echo "[ $(date +%c) ] Nginx์— ์—ฐ๊ฒฐํ•˜์ง€ ์•Š๊ณ  ๋ฐฐํฌ๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."
exit 1
fi

echo "[ $(date +%c) ] Health check ์—ฐ๊ฒฐ ์‹คํŒจ. ์žฌ์‹œ๋„..."
sleep 10
done

echo "[ $(date +%c) ] Port Switching" >> $DEPLOY_LOG
sleep 10
$PROJECT_ROOT/indp-server/scripts/swith.sh
32 changes: 32 additions & 0 deletions scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
PROJECT_ROOT=/home/ubuntu/app
DEPLOY_PATH=$PROJECT_ROOT/deploy
DEPLOY_LOG=$DEPLOY_PATH/deploy.log

CURRENT_PROFILE=$(curl -s http://localhost/api/profile)
echo "[ $(date +%c) ] ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ Profile: $CURRENT_PROFILE" >> $DEPLOY_LOG
if [ "$CURRENT_PROFILE" == prod1 ]
then
CURRENT_PORT=8081
IDLE_PROFILE=prod2
IDLE_PORT=8082
elif [ "$CURRENT_PROFILE" == prod2 ]
then
CURRENT_PORT=8082
IDLE_PROFILE=prod1
IDLE_PORT=8081
else
echo "> ์ผ์น˜ํ•˜๋Š” Profile์ด ์—†์Šต๋‹ˆ๋‹ค. Profile: $CURRENT_PROFILE"
echo "> prod1์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค."
IDLE_PROFILE=prod1
IDLE_PORT=8081
fi

echo "[ $(date +%c) ] Nginx Current Profile / Port: $CURRENT_PROFILE / $CURRENT_PORT"
echo "[ $(date +%c) ] ์ „ํ™˜ํ•  Profile / Port: $IDLE_PROFILE / $IDLE_PORT"
echo "[ $(date +%c) ] set \$service_url http://localhost:${IDLE_PORT};" | sudo tee /etc/nginx/conf.d/service-url.inc

echo "[ $(date +%c) ] Nginx Reload"
sudo service nginx reload

CURRENT_PROFILE=$(curl -s http://localhost/api/profile)
echo "[ $(date +%c) ] Nginx Current Profile: $CURRENT_PROFILE"
21 changes: 21 additions & 0 deletions src/main/java/com/verby/indp/global/profile/ProfileController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.verby.indp.global.profile;

import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class ProfileController {

private final Environment environment;

@GetMapping("/profile")
public String profile() {
return Arrays.stream(environment.getActiveProfiles())
.findFirst()
.orElse("");
}
}
15 changes: 13 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ spring:
spring:
config:
activate:
on-profile: prod
on-profile: prod1
import:
- classpath:server-secrets/application-prod.yml
- classpath:server-secrets/application-prod-default.yml
- classpath:server-secrets/application-prod1.yml

---

spring:
config:
activate:
on-profile: prod2
import:
- classpath:server-secrets/application-prod-default.yml
- classpath:server-secrets/application-prod2.yml

0 comments on commit 09d56db

Please sign in to comment.