Skip to content

Commit

Permalink
Merge pull request #11 from Moyeothon-NewTeams/feature/develop
Browse files Browse the repository at this point in the history
๐Ÿ”€Style: develop -> main
  • Loading branch information
eunxeum authored Oct 31, 2024
2 parents 55e8db3 + d560397 commit 1ff8abb
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
26 changes: 26 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 0.0 #CodeDeploy ๋ฒ„์ „์„(๋ฌด์กฐ๊ฑด 0.0์œผ๋กœ ๊ณ ์ •)
os: linux
files:
- source: / #destination์œผ๋กœ ์ด๋™์‹œํ‚ฌ ํŒŒ์ผ. ์—ฌ๊ธฐ์„œ๋Š” ์ „์ฒด ํŒŒ์ผ์„ ์˜๋ฏธ.
destination: /home/ec2-user/app/zip/ #source์—์„œ ์ง€์ •๋œ ํŒŒ์ผ์„ ๋ฐ›๋Š” ์œ„์น˜
overwrite: yes #๊ธฐ์กด ํŒŒ์ผ๋“ค์„ ๋ฎ์–ด์“ธ์ง€ ์—ฌ๋ถ€

permissions: #CodeDeploy์—์„œ EC2 ์„œ๋ฒ„๋กœ ๋„˜๊ฒจ์ค€ ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ec2-user ๊ถŒํ•œ์„ ๊ฐ–๋„๋ก ํ•จ
- object: /
pattern: "**"
owner: ec2-user
group: ec2-user

hooks:
AfterInstall:
- location: stop.sh #์—”์ง„์—‘์Šค์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์ข…๋ฃŒ
timeout: 60
runas: ec2-user #stop.sh๋ฅผ ec2-user ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰
ApplicationStart:
- location: start.sh #์—”์ง„์—‘์Šค์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ Port๋กœ ์ƒˆ ๋ฒ„์ „์˜ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‹œ์ž‘
timeout: 60
runas: ec2-user
ValidateService:
- location: health.sh #์ƒˆ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋๋Š”์ง€ ํ™•์ธ
timeout: 60
runas: ec2-user
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

jar{
enabled = false
}
39 changes: 39 additions & 0 deletions scripts/health.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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://127.0.0.1:$IDLE_PORT/profile"
sleep 10

for RETRY_COUNT in {1..10}
do
RESPONSE=$(curl -s http://127.0.0.1:${IDLE_PORT}/profile)
UP_COUNT=$(echo ${RESPONSE} | grep 'real' | wc -l)

if [ ${UP_COUNT} -ge 1 ] # Nginx์™€ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ํฌํŠธ๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ž˜ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€ ์ฒดํฌ
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
36 changes: 36 additions & 0 deletions scripts/profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

# ์‰ฌ๊ณ  ์žˆ๋Š” profile ์ฐพ๊ธฐ
# real1์ด ์‚ฌ์šฉ ์ค‘์ด๋ฉด real2๊ฐ€ ์‰ฌ๊ณ  ์žˆ๊ณ  ๋ฐ˜๋Œ€๋ฉด real1์ด ์‰ฌ๊ณ  ์žˆ์Œ

function find_idle_profile() {
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://newteamsgoody.shop/profile) # ํ˜„์žฌ Nginx๊ฐ€ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰ ์ค‘์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์‘๋‹ต๊ฐ’์œผ๋กœ ์ƒํƒœ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌ๋ฐ›์Œ

if [ ${RESPONSE_CODE} -ge 400 ] # 400๋ฒˆ๋Œ€ ์ด์ƒ์˜ ์˜ค๋ฅ˜์ผ ๊ฒฝ์šฐ real2๋ฅผ ์‚ฌ์šฉ
then
CURRENT_PROFILE=real2
else
CURRENT_PROFILE=$(curl -s https://newteamsgoody.shop/profile)
fi

if [ ${CURRENT_PROFILE} == real1 ]
then
IDLE_PROFILE=real2
else
IDLE_PROFILE=real1
fi

echo "${IDLE_PROFILE}" # bash ์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐ˜ํ™˜ ๊ธฐ๋Šฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— echo๋กœ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์—์„œ ๊ทธ ๊ฐ’์„ ์žก์•„์„œ ์‚ฌ์šฉ
}

# ์‰ฌ๊ณ  ์žˆ๋Š” profile์˜ port ์ฐพ๊ธฐ
function find_idle_port() {
IDLE_PROFILE=$(find_idle_profile)

if [ ${IDLE_PROFILE} == real1 ]
then
echo "8081"
else
echo "8082"
fi
}
32 changes: 32 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

ABSPATH=$(readlink -f $0)
ABSDIR=$(dirname $ABSPATH)
source ${ABSDIR}/profile.sh

REPOSITORY=/home/ec2-user/app

echo "> Build ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค."

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

IDLE_PROFILE=$(find_idle_profile)

echo "> ์ƒˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ $IDLE_PROFILE ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค."

# ์„ค์ • ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๊ณ  active profile์„ ํ†ตํ•ด ๊ตฌ๋™๋  ํฌํŠธ๋ฅผ ์ง€์ •
nohup java -jar \
-Dspring.config.location=$REPOSITORY/config/application.yml,\
$REPOSITORY/config/application-prod.yml,\
$REPOSITORY/config/application-$IDLE_PROFILE.yml \
-Dspring.profiles.active=$IDLE_PROFILE,prod \
$JAR_NAME > $REPOSITORY/nohup.out 2>&1 &
20 changes: 20 additions & 0 deletions scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

ABSPATH=$(readlink -f $0) # ํ˜„์žฌ stop.sh๊ฐ€ ์†ํ•ด ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ์Œ

ABSDIR=$(dirname $ABSPATH)
source ${ABSDIR}/profile.sh # ์ผ์ข…์˜ import ๊ตฌ๋ฌธ์œผ๋กœ stop.sh์—์„œ๋„ profile.sh์˜ function์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

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 -9 $IDLE_PID"
kill -9 ${IDLE_PID}
sleep 5
fi
16 changes: 16 additions & 0 deletions scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/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 # ์—”์ง„์—‘์Šค๊ฐ€ ๋ณ€๊ฒฝํ•  ํ”„๋ก์‹œ ์ฃผ์†Œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ service-url.inc๋กœ ๋ฎ์–ด ์”€

echo "> ์—”์ง„์—‘์Šค Reload"
sudo systemctl reload nginx
}
2 changes: 2 additions & 0 deletions src/main/resources/application-real1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8081
2 changes: 2 additions & 0 deletions src/main/resources/application-real2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8082

0 comments on commit 1ff8abb

Please sign in to comment.