Compare commits
2 Commits
294e6721eb
...
97b0dde75b
| Author | SHA1 | Date | |
|---|---|---|---|
| 97b0dde75b | |||
| 891c73d0af |
126
deployment/ansible/playbooks/check-and-restart-gitea.yml
Normal file
126
deployment/ansible/playbooks/check-and-restart-gitea.yml
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
---
|
||||||
|
- name: Check and Restart Gitea if Unhealthy
|
||||||
|
hosts: production
|
||||||
|
gather_facts: yes
|
||||||
|
become: no
|
||||||
|
|
||||||
|
vars:
|
||||||
|
gitea_stack_path: "{{ stacks_base_path | default('/home/deploy/deployment/stacks') }}/gitea"
|
||||||
|
gitea_url: "https://git.michaelschiemer.de"
|
||||||
|
gitea_container_name: "gitea"
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
- name: Check if Gitea stack directory exists
|
||||||
|
stat:
|
||||||
|
path: "{{ gitea_stack_path }}"
|
||||||
|
register: gitea_stack_exists
|
||||||
|
|
||||||
|
- name: Fail if Gitea stack directory does not exist
|
||||||
|
fail:
|
||||||
|
msg: "Gitea stack directory not found at {{ gitea_stack_path }}"
|
||||||
|
when: not gitea_stack_exists.stat.exists
|
||||||
|
|
||||||
|
- name: Check Gitea container status
|
||||||
|
shell: |
|
||||||
|
cd {{ gitea_stack_path }}
|
||||||
|
docker compose ps {{ gitea_container_name }} --format json
|
||||||
|
register: gitea_container_status
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Display Gitea container status
|
||||||
|
debug:
|
||||||
|
msg: |
|
||||||
|
Gitea Container Status:
|
||||||
|
{{ gitea_container_status.stdout | default('Container not found or error') }}
|
||||||
|
|
||||||
|
- name: Check Gitea health endpoint
|
||||||
|
uri:
|
||||||
|
url: "{{ gitea_url }}/api/healthz"
|
||||||
|
method: GET
|
||||||
|
status_code: [200]
|
||||||
|
validate_certs: false
|
||||||
|
timeout: 10
|
||||||
|
register: gitea_health
|
||||||
|
ignore_errors: yes
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Display Gitea health check result
|
||||||
|
debug:
|
||||||
|
msg: |
|
||||||
|
Gitea Health Check:
|
||||||
|
- Status Code: {{ gitea_health.status | default('UNREACHABLE') }}
|
||||||
|
- Response Time: {{ gitea_health.elapsed | default('N/A') }}s
|
||||||
|
{% if gitea_health.status == 200 %}
|
||||||
|
- Status: ✅ HEALTHY
|
||||||
|
{% else %}
|
||||||
|
- Status: ❌ UNHEALTHY or TIMEOUT
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
- name: Get Gitea container logs (last 50 lines)
|
||||||
|
shell: |
|
||||||
|
cd {{ gitea_stack_path }}
|
||||||
|
docker compose logs --tail=50 {{ gitea_container_name }} 2>&1 || echo "LOGS_NOT_AVAILABLE"
|
||||||
|
register: gitea_logs
|
||||||
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Display Gitea container logs
|
||||||
|
debug:
|
||||||
|
msg: |
|
||||||
|
Gitea Container Logs (last 50 lines):
|
||||||
|
{{ gitea_logs.stdout | default('No logs available') }}
|
||||||
|
|
||||||
|
- name: Check if Gitea container is running
|
||||||
|
set_fact:
|
||||||
|
gitea_is_running: "{{ 'State":"running' in gitea_container_status.stdout | default('') }}"
|
||||||
|
|
||||||
|
- name: Check if Gitea is healthy
|
||||||
|
set_fact:
|
||||||
|
gitea_is_healthy: "{{ gitea_health.status | default(0) == 200 }}"
|
||||||
|
|
||||||
|
- name: Restart Gitea container if unhealthy or not running
|
||||||
|
shell: |
|
||||||
|
cd {{ gitea_stack_path }}
|
||||||
|
docker compose restart {{ gitea_container_name }}
|
||||||
|
when: not gitea_is_healthy or not gitea_is_running
|
||||||
|
register: gitea_restart
|
||||||
|
changed_when: gitea_restart.rc == 0
|
||||||
|
|
||||||
|
- name: Wait for Gitea to be ready after restart
|
||||||
|
uri:
|
||||||
|
url: "{{ gitea_url }}/api/healthz"
|
||||||
|
method: GET
|
||||||
|
status_code: [200]
|
||||||
|
validate_certs: false
|
||||||
|
timeout: 10
|
||||||
|
register: gitea_health_after_restart
|
||||||
|
until: gitea_health_after_restart.status == 200
|
||||||
|
retries: 30
|
||||||
|
delay: 2
|
||||||
|
when: not gitea_is_healthy or not gitea_is_running
|
||||||
|
ignore_errors: yes
|
||||||
|
|
||||||
|
- name: Display final status
|
||||||
|
debug:
|
||||||
|
msg: |
|
||||||
|
========================================
|
||||||
|
Gitea Status Summary
|
||||||
|
========================================
|
||||||
|
Container Running: {{ '✅ YES' if gitea_is_running else '❌ NO' }}
|
||||||
|
Health Check: {{ '✅ HEALTHY' if gitea_is_healthy else '❌ UNHEALTHY' }}
|
||||||
|
{% if not gitea_is_healthy or not gitea_is_running %}
|
||||||
|
Action Taken: 🔄 Container restarted
|
||||||
|
Final Status: {{ '✅ HEALTHY' if gitea_health_after_restart.status | default(0) == 200 else '❌ STILL UNHEALTHY' }}
|
||||||
|
{% else %}
|
||||||
|
Action Taken: ℹ️ No action needed
|
||||||
|
{% endif %}
|
||||||
|
========================================
|
||||||
|
|
||||||
|
{% if gitea_health_after_restart.status | default(0) == 200 %}
|
||||||
|
✅ Gitea is now accessible and healthy!
|
||||||
|
{% elif not gitea_is_healthy and not gitea_is_running %}
|
||||||
|
⚠️ Gitea container was restarted but may still be starting up.
|
||||||
|
Please check logs manually: docker compose -f {{ gitea_stack_path }}/docker-compose.yml logs gitea
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
@@ -225,13 +225,23 @@
|
|||||||
|
|
||||||
- name: Wait for Docker daemon to be ready
|
- name: Wait for Docker daemon to be ready
|
||||||
wait_for:
|
wait_for:
|
||||||
port: 2375
|
path: /var/run/docker.sock
|
||||||
host: localhost
|
|
||||||
timeout: 10
|
timeout: 10
|
||||||
when: docker_daemon_updated.changed | default(false)
|
when: docker_daemon_updated.changed | default(false)
|
||||||
ignore_errors: yes
|
ignore_errors: yes
|
||||||
become: no
|
become: no
|
||||||
|
|
||||||
|
- name: Login to Docker registry before compose up
|
||||||
|
community.docker.docker_login:
|
||||||
|
registry_url: "{{ docker_registry }}"
|
||||||
|
username: "{{ docker_registry_username | default('admin') }}"
|
||||||
|
password: "{{ registry_password }}"
|
||||||
|
when:
|
||||||
|
- registry_password | string | trim != ''
|
||||||
|
- registry_accessible == 'true'
|
||||||
|
no_log: yes
|
||||||
|
ignore_errors: yes
|
||||||
|
|
||||||
- name: Deploy application stack with new image
|
- name: Deploy application stack with new image
|
||||||
shell: |
|
shell: |
|
||||||
cd {{ application_code_dest }}
|
cd {{ application_code_dest }}
|
||||||
|
|||||||
Reference in New Issue
Block a user