diff --git a/.platform/nginx.conf b/.platform/nginx.conf deleted file mode 100644 index 612092e..0000000 --- a/.platform/nginx.conf +++ /dev/null @@ -1,63 +0,0 @@ -user nginx; -error_log /var/log/nginx/error.log warn; -pid /var/run/nginx.pid; -worker_processes auto; -worker_rlimit_nofile 33282; - -events { - use epoll; - worker_connections 1024; - multi_accept on; -} - -http { - include /etc/nginx/mime.types; - default_type application/octet-stream; - - - log_format main '$remote_addr - $remote_user [$time_local] "$request" ' - '$status $body_bytes_sent "$http_referer" ' - '"$http_user_agent" "$http_x_forwarded_for"'; - - include conf.d/*.conf; - - map $http_upgrade $connection_upgrade { - default "upgrade"; - } - - upstream springboot { - server 127.0.0.1:8080; - keepalive 1024; - } - - server { - listen 80 default_server; - listen [::]:80 default_server; - - location / { - proxy_pass http://springboot; - # CORS 관련 헤더 추가 - add_header 'Access-Control-Allow-Origin' '*'; - add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; - add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type'; - proxy_http_version 1.1; - proxy_set_header Connection $connection_upgrade; - proxy_set_header Upgrade $http_upgrade; - - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - } - - access_log /var/log/nginx/access.log main; - - client_header_timeout 60; - client_body_timeout 60; - keepalive_timeout 60; - gzip off; - gzip_comp_level 4; - - # Include the Elastic Beanstalk generated locations - include conf.d/elasticbeanstalk/healthd.conf; - } -} \ No newline at end of file diff --git a/appspec.yml b/appspec.yml index 2959735..aea80b9 100644 --- a/appspec.yml +++ b/appspec.yml @@ -11,4 +11,16 @@ permissions: - object: / pattern: "**" owner: ec2-user - group: ec2-user \ No newline at end of file + group: ec2-user + +hooks: + ApplicationStart: + - location: scripts/run_new_was.sh + timeout: 180 + runas: ec2-user + - location: scripts/health_check.sh + timeout: 180 + runas: ec2-user + - location: scripts/switch.sh + timeout: 180 + runas: ec2-user \ No newline at end of file diff --git a/scripts/health_check.sh b/scripts/health_check.sh new file mode 100644 index 0000000..ecb8b3a --- /dev/null +++ b/scripts/health_check.sh @@ -0,0 +1,35 @@ +# health_check.sh + +#!/bin/bash + +# Crawl current connected port of WAS +CURRENT_PORT=$(cat /home/ec2-user/service_url.inc | grep -Po '[0-9]+' | tail -1) +TARGET_PORT=0 + +# Toggle port Number +if [ ${CURRENT_PORT} -eq 8081 ]; then + TARGET_PORT=8082 +elif [ ${CURRENT_PORT} -eq 8082 ]; then + TARGET_PORT=8081 +else + echo "> No WAS is connected to nginx" + exit 1 +fi + + +echo "> Start health check of WAS at 'http://127.0.0.1:${TARGET_PORT}' ..." + +for RETRY_COUNT in 1 2 3 4 5 6 7 8 9 10 +do + echo "> #${RETRY_COUNT} trying..." + RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${TARGET_PORT}/health) + + if [ ${RESPONSE_CODE} -eq 200 ]; then + echo "> New WAS successfully running" + exit 0 + elif [ ${RETRY_COUNT} -eq 10 ]; then + echo "> Health check failed." + exit 1 + fi + sleep 10 +done \ No newline at end of file diff --git a/scripts/run_new_was.sh b/scripts/run_new_was.sh new file mode 100644 index 0000000..5eed8e3 --- /dev/null +++ b/scripts/run_new_was.sh @@ -0,0 +1,27 @@ +# run_new_was.sh + +#!/bin/bash + +CURRENT_PORT=$(cat /home/ec2-user/service_url.inc | grep -Po '[0-9]+' | tail -1) +TARGET_PORT=0 + +echo "> Current port of running WAS is ${CURRENT_PORT}." + +if [ ${CURRENT_PORT} -eq 8081 ]; then + TARGET_PORT=8082 +elif [ ${CURRENT_PORT} -eq 8082 ]; then + TARGET_PORT=8081 +else + echo "> No WAS is connected to nginx" +fi + +TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') + +if [ ! -z ${TARGET_PID} ]; then + echo "> Kill WAS running at ${TARGET_PORT}." + sudo kill ${TARGET_PID} +fi + +nohup java -jar -Dserver.port=${TARGET_PORT} /home/ec2-user/ssu-plector/build/libs/* > /home/ec2-user/nohup.out 2>&1 & +echo "> Now new WAS runs at ${TARGET_PORT}." +exit 0 \ No newline at end of file diff --git a/scripts/switch.sh b/scripts/switch.sh new file mode 100644 index 0000000..a7171a5 --- /dev/null +++ b/scripts/switch.sh @@ -0,0 +1,29 @@ +# switch.sh + +#!/bin/bash + +# Crawl current connected port of WAS +CURRENT_PORT=$(cat /home/ec2-user/service_url.inc | grep -Po '[0-9]+' | tail -1) +TARGET_PORT=0 + +echo "> Nginx currently proxies to ${CURRENT_PORT}." + +# Toggle port number +if [ ${CURRENT_PORT} -eq 8081 ]; then + TARGET_PORT=8082 +elif [ ${CURRENT_PORT} -eq 8082 ]; then + TARGET_PORT=8081 +else + echo "> No WAS is connected to nginx" + exit 1 +fi + +# Change proxying port into target port +echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" | tee /home/ec2-user/service_url.inc + +echo "> Now Nginx proxies to ${TARGET_PORT}." + +# Reload nginx +sudo service nginx reload + +echo "> Nginx reloaded." \ No newline at end of file