From 391105f8ecdd264a42749cda02ad29396396b72c Mon Sep 17 00:00:00 2001 From: Youngmyung Kim <83266154+ymkim97@users.noreply.github.com> Date: Fri, 3 Nov 2023 17:49:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EB=B0=A9=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=EC=97=90=20=ED=95=84=EC=9A=94=ED=95=9C=20=ED=95=84=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#36)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/moabam/api/dto/ModifyRoomRequest.java | 9 ++++++++- .../api/presentation/RoomControllerTest.java | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java b/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java index 4e24341a..ec44c1b5 100644 --- a/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java +++ b/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java @@ -1,13 +1,20 @@ package com.moabam.api.dto; +import java.util.List; + +import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Range; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; public record ModifyRoomRequest( @NotBlank String title, - @Pattern(regexp = "^(|[0-9]{4,8})$") String password, + @Length(max = 255, message = "방 공지의 길이가 너무 깁니다.") String announcement, + @NotNull @Size(min = 1, max = 4) List routines, + @Pattern(regexp = "^(|\\d{4,8})$") String password, @Range(min = 0, max = 23) int certifyTime, @Range(min = 0, max = 10) int maxUserCount ) { diff --git a/src/test/java/com/moabam/api/presentation/RoomControllerTest.java b/src/test/java/com/moabam/api/presentation/RoomControllerTest.java index 36bce475..31ce6517 100644 --- a/src/test/java/com/moabam/api/presentation/RoomControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/RoomControllerTest.java @@ -245,14 +245,20 @@ void modify_room_success() throws Exception { .certifyTime(9) .maxUserCount(5) .build(); + Participant participant = Participant.builder() .room(room) .memberId(1L) .build(); participant.enableManager(); + + List routines = new ArrayList<>(); + routines.add("물 마시기"); + routines.add("코테 풀기"); + roomRepository.save(room); participantRepository.save(participant); - ModifyRoomRequest modifyRoomRequest = new ModifyRoomRequest("수정할 방임!", "1234", 10, 7); + ModifyRoomRequest modifyRoomRequest = new ModifyRoomRequest("수정할 방임!", null, routines, "1234", 10, 7); String json = objectMapper.writeValueAsString(modifyRoomRequest); // expected @@ -274,13 +280,19 @@ void unauthorized_modify_room_fail() throws Exception { .certifyTime(9) .maxUserCount(5) .build(); + Participant participant = Participant.builder() .room(room) .memberId(1L) .build(); + + List routines = new ArrayList<>(); + routines.add("물 마시기"); + routines.add("코테 풀기"); + roomRepository.save(room); participantRepository.save(participant); - ModifyRoomRequest modifyRoomRequest = new ModifyRoomRequest("수정할 방임!", "1234", 10, 7); + ModifyRoomRequest modifyRoomRequest = new ModifyRoomRequest("수정할 방임!", "방 공지", routines, "1234", 9, 7); String json = objectMapper.writeValueAsString(modifyRoomRequest); String message = "{\"message\":\"방장이 아닌 사용자는 방을 수정할 수 없습니다.\"}"; From 2c5291bbe2cf6f970431dcdb106029000f39d222 Mon Sep 17 00:00:00 2001 From: Dev Uni Date: Sat, 4 Nov 2023 02:34:26 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20ec2=20dev=20=EC=84=9C=EB=B2=84=20?= =?UTF-8?q?=EB=B0=B0=ED=8F=AC=20=EA=B5=AC=ED=98=84=20(#40)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: submodule 업데이트 * feat: docker-compose 파일 세팅 * feat: nginx 템플릿 설정 * feat: Dockerfile 설정 * feat: 쉘 스크립트 파일 작성 * feat: HealthCheckController 구현 * chore: build.gradle 커버리지 항목 제외 추가 * feat: github actions ci, cd 작성 * style: ci 파일 오타 수정 --- .github/workflows/ci.yml | 12 ++ .github/workflows/develop-cd.yml | 187 ++++++++++++++++++ Dockerfile | 8 + build.gradle | 5 +- docker-compose-dev.yml | 73 +++++++ nginx/nginx.template | 60 ++++++ scripts/deploy-dev.sh | 118 +++++++++++ scripts/init-letsencrypt.sh | 86 ++++++++ scripts/init-nginx-converter.sh | 13 ++ .../presentation/HealthCheckController.java | 16 ++ src/main/resources/config | 2 +- 11 files changed, 577 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/develop-cd.yml create mode 100644 Dockerfile create mode 100644 docker-compose-dev.yml create mode 100644 nginx/nginx.template create mode 100644 scripts/deploy-dev.sh create mode 100644 scripts/init-letsencrypt.sh create mode 100644 scripts/init-nginx-converter.sh create mode 100644 src/main/java/com/moabam/api/presentation/HealthCheckController.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54d5168f..45bfe330 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,10 @@ jobs: java-version: '17' distribution: 'corretto' + - name: environment 세팅 + run: | + echo "${{secrets.DEV_ENV_FILE }}" > ./.env + - name: Gradle 캐싱 uses: actions/cache@v3 with: @@ -34,6 +38,14 @@ jobs: - name: Gradle Grant 권한 부여 run: chmod +x gradlew + - name: 테스트용 MySQL 도커 컨테이너 실행 + run: | + sudo docker run -d -p 3306:3306 --env MYSQL_DATABASE=test --env MYSQL_ROOT_PASSWORD=test mysql:8.0.33 + + - name: 테스트용 Redis 도커 컨테이너 실행 + run: | + sudo docker run --name redis-test -p 6379:6379 -d redis + - name: SonarCloud 캐싱 uses: actions/cache@v3 with: diff --git a/.github/workflows/develop-cd.yml b/.github/workflows/develop-cd.yml new file mode 100644 index 00000000..6b8814bd --- /dev/null +++ b/.github/workflows/develop-cd.yml @@ -0,0 +1,187 @@ +name: develop-CD + +on: + push: + branches: [ "develop" ] + +permissions: + contents: write + +jobs: + move-files: + name: move-files + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: true + token: ${{ secrets.MOABAM_SUBMODULE_KEY }} + + - name: Github Actions IP 획득 + id: ip + uses: haythem/public-ip@v1.3 + + - name: AWS Credentials 설정 + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Github Actions IP 보안그룹 추가 + run: | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 + + - name: 디렉토리 생성 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_INSTANCE_HOST }} + port: 22 + username: ubuntu + key: ${{ secrets.EC2_INSTANCE_PRIVATE_KEY }} + script: | + mkdir -p /home/ubuntu/moabam/nginx + + - name: Docker env 파일 생성 + run: + echo "${{secrets.DEV_ENV_FILE }}" > ./.env + + - name: 서버로 전송 기본 파일들 전송 + uses: appleboy/scp-action@master + with: + host: ${{ secrets.EC2_INSTANCE_HOST }} + port: 22 + username: ${{ secrets.EC2_INSTANCE_USERNAME }} + key: ${{ secrets.EC2_INSTANCE_PRIVATE_KEY }} + source: "./.env, ./docker-compose-dev.yml, init-letsencrypt.sh, ./scripts/*" + target: "/home/ubuntu/moabam" + + - name: 서버로 전송 "nginx conf 파일들" + uses: appleboy/scp-action@master + with: + host: ${{ secrets.EC2_INSTANCE_HOST }} + port: 22 + username: ${{ secrets.EC2_INSTANCE_USERNAME }} + key: ${{ secrets.EC2_INSTANCE_PRIVATE_KEY }} + source: "./nginx/*" + target: "/home/ubuntu/moabam" + + - name: 파일 세팅 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_INSTANCE_HOST }} + port: 22 + username: ubuntu + key: ${{ secrets.EC2_INSTANCE_PRIVATE_KEY }} + script: | + cd /home/ubuntu/moabam + mv docker-compose-dev.yml docker-compose.yml + chmod +x ./scripts/deploy-dev.sh + chmod +x ./scripts/init-letsencrypt.sh + chmod +x ./scripts/init-nginx-converter.sh + + - name: Github Actions IP 보안그룹에서 삭제 + if: always() + run: | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 + + deploy: + name: deploy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: true + token: ${{ secrets.MOABAM_SUBMODULE_KEY }} + + - name: JDK 17 셋업 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'corretto' + + - name: Gradle 캐싱 + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Gradle Grant 권한 부여 + run: chmod +x gradlew + + - name: 테스트용 MySQL 도커 컨테이너 실행 + run: | + sudo docker run -d -p 3306:3306 --env MYSQL_DATABASE=test --env MYSQL_ROOT_PASSWORD=test mysql:8.0.33 + + - name: 테스트용 Redis 도커 컨테이너 실행 + run: | + sudo docker run --name redis-test -p 6379:6379 -d redis + + - name: Gradle 빌드 + uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0 + with: + arguments: build + + - name: 멀티플랫폼 위한 Docker Buildx 설정 + uses: docker/setup-buildx-action@v2 + + - name: Docker Hub 로그인 + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_TOKEN }} + + - name: Docker Hub 빌드하고 푸시 + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPOSITORY }}:${{ secrets.DOCKER_HUB_DEV_TAG }} + build-args: | + "SPRING_ACTIVE_PROFILES=dev" + platforms: | + linux/amd64 + linux/arm64 + + - name: Github Actions IP 획득 + id: ip + uses: haythem/public-ip@v1.3 + + - name: AWS Credentials 설정 + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Github Actions IP 보안그룹 추가 + run: | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 + + - name: EC2 서버에 배포 + uses: appleboy/ssh-action@master + id: deploy-dev + if: contains(github.ref, 'dev') + with: + host: ${{ secrets.EC2_INSTANCE_HOST }} + port: 22 + username: ubuntu + key: ${{ secrets.EC2_INSTANCE_PRIVATE_KEY }} + source: "docker-compose-dev.yml" + script: | + cd /home/ubuntu/moabam + echo ${{ secrets.DOCKER_HUB_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin + sudo docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPOSITORY }}:${{ secrets.DOCKER_HUB_DEV_TAG }} + ./scripts/deploy-dev.sh + docker rm `docker ps -a -q` + docker rmi $(docker images -aq) + echo "### 배포 완료 ###" + + - name: Github Actions IP 보안그룹에서 삭제 + if: always() + run: | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..6e564b38 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM amazoncorretto:17 + +ARG SPRING_ACTIVE_PROFILES +ENV SPRING_ACTIVE_PROFILES ${SPRING_ACTIVE_PROFILES} + +COPY build/libs/moabam-server-0.0.1-SNAPSHOT.jar moabam.jar + +ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=${SPRING_ACTIVE_PROFILES}", "/moabam.jar"] diff --git a/build.gradle b/build.gradle index 8de7b745..d50f157c 100644 --- a/build.gradle +++ b/build.gradle @@ -55,7 +55,7 @@ dependencies { // Configuration Binding annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" - + // Apache Commons Lang 3 implementation 'org.apache.commons:commons-lang3:3.13.0' @@ -96,6 +96,7 @@ jacocoTestReport { "**/*ErrorMessage*", "**/*DynamicQuery*", "**/*BaseTimeEntity*", + "**/*HealthCheckController*", ] + Qdomains) }) ) @@ -127,7 +128,7 @@ sonar { property 'sonar.coverage.jacoco.xmlReportPaths', 'build/reports/jacoco/test/jacocoTestReport.xml' property 'sonar.coverage.exclusions', '**/test/**, **/Q*.java, **/*Doc*.java, **/resources/** ' + ',**/*Application*.java , **/*Config*.java, **/*Request*.java, **/*Response*.java ,**/*Exception*.java ' + - ',**/*ErrorMessage*.java, **/*Mapper*.java' + ',**/*ErrorMessage*.java, **/*Mapper*.java, **/*DynamicQuery*, **/*BaseTimeEntity*, **/*HealthCheckController*' property 'sonar.java.checkstyle.reportPaths', 'build/reports/checkstyle/main.xml' } } diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 00000000..4e92e97b --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,73 @@ +version: '3.7' + +services: + nginx: + image: nginx:latest + container_name: nginx + platform: linux/arm64/v8 + restart: always + ports: + - "80:80" + - "443:443" + volumes: + - /home/ubuntu/moabam/nginx/certbot/conf:/etc/letsencrypt + - /home/ubuntu/moabam/nginx/certbot/www:/var/www/certbot + - /home/ubuntu/moabam/nginx/nginx.conf:/etc/nginx/nginx.conf + command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'" + certbot: + image: certbot/certbot:latest + container_name: certbot + platform: linux/arm64 + restart: unless-stopped + volumes: + - /home/ubuntu/moabam/nginx/certbot/conf:/etc/letsencrypt + - /home/ubuntu/moabam/nginx/certbot/www:/var/www/certbot + entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" + moabam-blue: + image: ${DOCKER_HUB_USERNAME}/${DOCKER_HUB_REPOSITORY}:${DOCKER_HUB_TAG} + container_name: ${BLUE_CONTAINER} + restart: always + expose: + - ${SERVER_PORT} + depends_on: + - redis + - mysql + environment: + SPRING_ACTIVE_PROFILES: ${SPRING_ACTIVE_PROFILES} + moabam-green: + image: ${DOCKER_HUB_USERNAME}/${DOCKER_HUB_REPOSITORY}:${DOCKER_HUB_TAG} + container_name: ${GREEN_CONTAINER} + expose: + - ${SERVER_PORT} + depends_on: + - redis + - mysql + environment: + SPRING_ACTIVE_PROFILES: ${SPRING_ACTIVE_PROFILES} + redis: + image: redis:alpine + container_name: redis + platform: linux/arm64 + restart: always + command: redis-server + ports: + - "6379:6379" + volumes: + - /home/ubuntu/moabam/data/redis:/data + mysql: + image: mysql:8.0.33 + container_name: mysql + platform: linux/arm64/v8 + restart: always + ports: + - "3306:3306" + environment: + MYSQL_DATABASE: ${DEV_MYSQL_DATABASE} + MYSQL_USERNAME: ${DEV_MYSQL_USERNAME} + MYSQL_ROOT_PASSWORD: ${DEV_MYSQL_PASSWORD} + TZ: Asia/Seoul + command: + - --character-set-server=utf8mb4 + - --collation-server=utf8mb4_unicode_ci + volumes: + - /home/ubuntu/moabam/data/mysql:/var/lib/mysql diff --git a/nginx/nginx.template b/nginx/nginx.template new file mode 100644 index 00000000..d62eef8a --- /dev/null +++ b/nginx/nginx.template @@ -0,0 +1,60 @@ +worker_processes auto; + +events { + use epoll; + worker_connections 1024; +} + +http { + + include mime.types; + sendfile on; + + map $http_upgrade $connection_upgrade { + default "upgrade"; + } + + upstream backend { + server ${BLUE_CONTAINER}:${SERVER_PORT}; + keepalive 1024; + } + + server { + listen 80; + server_name ${SERVER_DOMAIN}; + server_tokens off; + + location / { + return 301 https://$host$request_uri; + } + + location /.well-known/acme-challenge/ { + allow all; + root /var/www/certbot; + } + } + + server { + listen 443 ssl; + server_name ${SERVER_DOMAIN}; + server_tokens off; + + ssl_certificate /etc/letsencrypt/live/${SERVER_DOMAIN}/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/${SERVER_DOMAIN}/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + + location / { + resolver ${RESOLVER_IP} valid=10s; + proxy_pass http://backend; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Upgrade $http_upgrade; + + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + } +} diff --git a/scripts/deploy-dev.sh b/scripts/deploy-dev.sh new file mode 100644 index 00000000..4786c149 --- /dev/null +++ b/scripts/deploy-dev.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +# .env 파일 로드 +if [ -f /home/ubuntu/moabam/.env ]; then + source /home/ubuntu/moabam/.env +fi + +if [ $(docker ps | grep -c "nginx") -eq 0 ]; then + echo "### nginx 시작 ###" + docker-compose up -d nginx +else + echo "-------------------------------------------" + echo "nginx 이미 실행 중 입니다." + echo "-------------------------------------------" +fi + +echo +echo + +if [ $(docker ps | grep -c "redis") -eq 0 ]; then + echo "### redis 시작 ###" + docker-compose up -d redis +else + echo "-------------------------------------------" + echo "redis 이미 실행 중 입니다." + echo "-------------------------------------------" +fi + +echo +echo + +if [ $(docker ps | grep -c "mysql") -eq 0 ]; then + echo "### mysql 시작 ###" + docker-compose up -d mysql +else + echo "-------------------------------------------" + echo "mysql 이미 실행 중 입니다." + echo "-------------------------------------------" +fi + +echo +echo + +echo +echo "### springboot blue-green 무중단 배포 시작 ###" +echo + +IS_BLUE=$(docker ps | grep ${BLUE_CONTAINER}) +NGINX_CONF="/home/ubuntu/moabam/nginx/nginx.conf" + +if [ -n "$IS_BLUE" ]; then + echo "### BLUE => GREEN ###" + echo "1. ${GREEN_CONTAINER} 이미지 가져오고 실행" + docker-compose pull moabam-green + docker-compose up -d moabam-green + + attempt=1 + while [ $attempt -le 24 ]; do + echo "2. ${GREEN_CONTAINER} health check (Attempt: $attempt)" + sleep 5 + REQUEST=$(docker exec nginx curl http://${GREEN_CONTAINER}:${SERVER_PORT}) + + if [ -n "$REQUEST" ]; then + echo "${GREEN_CONTAINER} health check 성공" + sed -i "s/${BLUE_CONTAINER}/${GREEN_CONTAINER}/g" $NGINX_CONF + echo "3. nginx 설정파일 reload" + docker exec nginx service nginx reload + echo "4. ${BLUE_CONTAINER} 컨테이너 종료" + docker-compose stop moabam-blue + + echo "5. ${GREEN_CONTAINER} 배포 성공" + break; + fi + + if [ $attempt -eq 24 ]; then + echo "${GREEN_CONTAINER} 배포 실패 !!" + + docker-compose stop moabam-green + + exit 1; + fi + + attempt=$((attempt+1)) + done; +else + echo "### GREEN => BLUE ###" + echo "1. ${BLUE_CONTAINER} 이미지 가져오고 실행" + docker-compose pull moabam-blue + docker-compose up -d moabam-blue + + attempt=1 + while [ $attempt -le 24 ]; do + echo "2. ${BLUE_CONTAINER} health check (Attempt: $attempt)" + sleep 5 + REQUEST=$(docker exec nginx curl http://${BLUE_CONTAINER}:${SERVER_PORT}) + + if [ -n "$REQUEST" ]; then + echo "${BLUE_CONTAINER} health check 성공" + sed -i "s/${GREEN_CONTAINER}/${BLUE_CONTAINER}/g" $NGINX_CONF + echo "3. nginx 설정파일 reload" + docker exec nginx service nginx reload + echo "4. ${GREEN_CONTAINER} 컨테이너 종료" + docker-compose stop moabam-green + + echo "5. ${BLUE_CONTAINER} 배포 성공" + break; + fi + + if [ $attempt -eq 24 ]; then + echo "${BLUE_CONTAINER} 배포 실패 !!" + + docker-compose stop moabam-blue + exit 1; + fi + + attempt=$((attempt+1)) + done; +fi diff --git a/scripts/init-letsencrypt.sh b/scripts/init-letsencrypt.sh new file mode 100644 index 00000000..b9040e36 --- /dev/null +++ b/scripts/init-letsencrypt.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +# .env 파일 로드 +if [ -f /home/ubuntu/moabam/.env ]; then + source /home/ubuntu/moabam/.env +fi + +if ! [ -x "$(command -v docker-compose)" ]; then + echo 'Error: docker-compose is not installed.' >&2 + exit 1 +fi + +domains="${SERVER_DOMAIN}" +rsa_key_size=4096 +data_path="/home/ubuntu/moabam/nginx/certbot" +email="${MY_EMAIL}" # Adding a valid address is strongly recommended +staging=1 # Set to 1 if you're testing your setup to avoid hitting request limits + +if [ -d "$data_path" ]; then + read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision + if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then + exit + fi +fi + + +if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then + echo "### Downloading recommended TLS parameters ..." + mkdir -p "$data_path/conf" + sudo chmod 777 "$data_path/conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem" + echo +fi + +echo "### Creating dummy certificate for $domains ..." +path="/etc/letsencrypt/live/$domains" +sudo mkdir -p "$data_path/conf/live/$domains" +docker-compose run --rm --entrypoint "\ + openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\ + -keyout '$path/privkey.pem' \ + -out '$path/fullchain.pem' \ + -subj '/CN=localhost'" certbot +echo + + +echo "### Starting nginx ..." +docker-compose up --force-recreate -d nginx +echo + +echo "### Deleting dummy certificate for $domains ..." +docker-compose run --rm --entrypoint "\ + rm -Rf /etc/letsencrypt/live/$domains && \ + rm -Rf /etc/letsencrypt/archive/$domains && \ + rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot +echo + + +echo "### Requesting Let's Encrypt certificate for $domains ..." +#Join $domains to -d args +domain_args="" +for domain in "${domains[@]}"; do + domain_args="$domain_args -d $domain" +done + +# Select appropriate email arg +case "$email" in + "") email_arg="--register-unsafely-without-email" ;; + *) email_arg="--email $email" ;; +esac + +# Enable staging mode if needed +if [ $staging != "0" ]; then staging_arg="--staging"; fi + +docker-compose run --rm --entrypoint "\ + certbot certonly --webroot -w /var/www/certbot \ + $staging_arg \ + $email_arg \ + $domain_args \ + --rsa-key-size $rsa_key_size \ + --agree-tos \ + --force-renewal" certbot +echo + +echo "### Reloading nginx ..." +docker-compose exec nginx nginx -s reload diff --git a/scripts/init-nginx-converter.sh b/scripts/init-nginx-converter.sh new file mode 100644 index 00000000..ad2b88e6 --- /dev/null +++ b/scripts/init-nginx-converter.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# .env 파일 로드 +if [ -f /home/ubuntu/moabam/.env ]; then + source /home/ubuntu/moabam/.env +fi + +export SERVER_DOMAIN=${SERVER_DOMAIN} +export SERVER_PORT=${SERVER_PORT} +export RESOLVER_IP=${RESOLVER_IP} +export BLUE_CONTAINER=${BLUE_CONTAINER} + +envsubst '$SERVER_DOMAIN $SERVER_PORT $RESOLVER_IP $BLUE_CONTAINER' < /home/ubuntu/moabam/nginx/nginx.template > /home/ubuntu/moabam/nginx/nginx.conf diff --git a/src/main/java/com/moabam/api/presentation/HealthCheckController.java b/src/main/java/com/moabam/api/presentation/HealthCheckController.java new file mode 100644 index 00000000..4f67e4c2 --- /dev/null +++ b/src/main/java/com/moabam/api/presentation/HealthCheckController.java @@ -0,0 +1,16 @@ +package com.moabam.api.presentation; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HealthCheckController { + + @GetMapping + @ResponseStatus(HttpStatus.OK) + public String healthCheck() { + return "Health Check Success"; + } +} diff --git a/src/main/resources/config b/src/main/resources/config index ab594df9..e5689e37 160000 --- a/src/main/resources/config +++ b/src/main/resources/config @@ -1 +1 @@ -Subproject commit ab594df9fcbf13159da3bb2fbb57d792b3f9f0f9 +Subproject commit e5689e37766a6e98213c74df4336b46b79e30f4f From 7ad26294ecc05108732bde4b570e38110fd5736d Mon Sep 17 00:00:00 2001 From: Dev Uni Date: Sat, 4 Nov 2023 03:02:46 +0900 Subject: [PATCH 3/3] =?UTF-8?q?hotfix:=20submodule=20mysql=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/config b/src/main/resources/config index e5689e37..7026a658 160000 --- a/src/main/resources/config +++ b/src/main/resources/config @@ -1 +1 @@ -Subproject commit e5689e37766a6e98213c74df4336b46b79e30f4f +Subproject commit 7026a65853d700a4f25a700fd327e926b562eabf