diff --git a/.github/workflows/ec2-reboot.yml b/.github/workflows/ec2-reboot.yml new file mode 100644 index 0000000..ce0067b --- /dev/null +++ b/.github/workflows/ec2-reboot.yml @@ -0,0 +1,99 @@ +name: EC2-RECOVERY + +on: + workflow_dispatch: + +jobs: + recover: + name: Recover Server + runs-on: ubuntu-latest + + steps: + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_PROD_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_PROD_SECRET_KEY }} + aws-region: ap-northeast-2 + + - name: Check IAM identity + run: | + echo "=== IAM 정보 확인 ===" + aws sts get-caller-identity || echo "STS 호출 실패" + + - name: Check CodeDeploy deployments + run: | + echo "=== 최근 배포 상태 확인 ===" + aws deploy list-deployments \ + --application-name runnect-prod-codedeploy \ + --deployment-group-name runnect-prod-codedeploy-group \ + --include-only-statuses "Succeeded,Failed,InProgress" \ + --query "deployments[:3]" \ + --output text || echo "배포 목록 조회 실패" + + LATEST=$(aws deploy list-deployments \ + --application-name runnect-prod-codedeploy \ + --deployment-group-name runnect-prod-codedeploy-group \ + --query "deployments[0]" \ + --output text 2>/dev/null) + + if [ -n "$LATEST" ] && [ "$LATEST" != "None" ]; then + echo "" + echo "=== 최신 배포 상세 ===" + aws deploy get-deployment --deployment-id "$LATEST" \ + --query "deploymentInfo.{status:status, createTime:createTime, completeTime:completeTime, errorInfo:errorInformation}" \ + --output json + fi + + - name: Trigger new CodeDeploy deployment + run: | + echo "=== 새 CodeDeploy 배포 트리거 ===" + DEPLOYMENT_ID=$(aws deploy create-deployment \ + --application-name runnect-prod-codedeploy \ + --deployment-group-name runnect-prod-codedeploy-group \ + --file-exists-behavior OVERWRITE \ + --s3-location bucket=runnect-prod-bucket,bundleType=zip,key=runnect_prod_server.zip \ + --region ap-northeast-2 \ + --query "deploymentId" \ + --output text) + + echo "Deployment ID: $DEPLOYMENT_ID" + + echo "배포 완료 대기 (최대 5분)..." + for i in $(seq 1 30); do + STATUS=$(aws deploy get-deployment --deployment-id "$DEPLOYMENT_ID" \ + --query "deploymentInfo.status" --output text 2>/dev/null) + echo "[$i/30] Status: $STATUS" + + if [ "$STATUS" = "Succeeded" ]; then + echo "배포 성공!" + break + elif [ "$STATUS" = "Failed" ] || [ "$STATUS" = "Stopped" ]; then + echo "배포 실패! 상세 정보:" + aws deploy get-deployment --deployment-id "$DEPLOYMENT_ID" \ + --query "deploymentInfo.errorInformation" --output json + break + fi + sleep 10 + done + + - name: Health check + run: | + echo "서버 헬스 체크 (최대 3분 대기)..." + for i in $(seq 1 18); do + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 --max-time 10 http://3.35.195.11/actuator/health 2>/dev/null || echo "000") + echo "[$i/18] HTTP: $HTTP_CODE" + if [ "$HTTP_CODE" = "200" ]; then + echo "서버 복구 완료!" + exit 0 + fi + sleep 10 + done + + echo "" + echo "=== 포트별 체크 ===" + for PORT in 80 8081 8082; do + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 --max-time 10 http://3.35.195.11:$PORT/actuator/health 2>/dev/null || echo "000") + echo "Port $PORT: HTTP $HTTP_CODE" + done + echo "WARNING: 서버가 아직 응답하지 않습니다." diff --git a/scripts/deploy.sh b/scripts/deploy.sh index cec838a..c53ea7a 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -77,6 +77,28 @@ do sleep 10 done +echo "> Nginx 상태 확인" +if ! sudo systemctl is-active --quiet nginx; then + echo "> Nginx가 중지되어 있습니다. 재시작합니다." + sudo systemctl start nginx + sleep 2 + if sudo systemctl is-active --quiet nginx; then + echo "> Nginx 재시작 성공" + else + echo "> Nginx 재시작 실패. 상태:" + sudo systemctl status nginx + fi +else + echo "> Nginx 정상 구동 중" +fi + echo "> 스위칭" sleep 10 /home/ubuntu/app/nonstop/switch.sh + +echo "> 배포 완료. 최종 상태 확인" +echo "> Nginx: $(sudo systemctl is-active nginx)" +echo "> Java 프로세스:" +pgrep -a java || echo "> Java 프로세스 없음" +echo "> 포트 리스닝:" +sudo ss -tlnp | grep -E ':(80|8081|8082) ' || echo "> 해당 포트 리스닝 없음"