diff --git a/.ansible-lint-ignore b/.ansible-lint-ignore index 2bd0723..e69de29 100644 --- a/.ansible-lint-ignore +++ b/.ansible-lint-ignore @@ -1,6 +0,0 @@ - -# In den hier aufgelisteten Dateien sollen die dahinter stehenden -# Warnungen und Fehler von ansible-lint ignoriert werden. - -tasks/main.yaml no-handler # Der Restart / Recreate Gitea Task ist gut dort, wo er ist -tasks/start.yaml name[template] # Es braucht zwei Templates, um den FQDN zusammenzusetzen. diff --git a/handlers/backup.yaml b/handlers/backup.yaml new file mode 100644 index 0000000..746b357 --- /dev/null +++ b/handlers/backup.yaml @@ -0,0 +1,30 @@ +- name: Get stats of docker-compose.yaml + ansible.builtin.stat: + path: /opt/gitea/docker-compose.yaml + register: docker_compose_stat + listen: + - Backup Gitea + +- name: Stop Gitea for Backup + community.docker.docker_compose: + project_src: /opt/gitea + state: present + stopped: true + register: docker_compose + listen: + - Backup Gitea + notify: + - Ensure Gitea is running + when: docker_compose_stat.stat.exists is defined and docker_compose_stat.stat.exists + +- name: Backup Gitea Directory + community.general.archive: + path: /opt/gitea/ + dest: "/var/backups/gitea-{{ gitea.hostname }}.{{ gitea.domain }}@{{ lookup('pipe', 'date --utc --iso-8601=seconds') }}.tar.gz" + force_archive: true + format: gz + owner: root + mode: "400" + listen: + - Backup Gitea + register: backup diff --git a/handlers/main.yaml b/handlers/main.yaml new file mode 100644 index 0000000..3b5fef5 --- /dev/null +++ b/handlers/main.yaml @@ -0,0 +1,32 @@ +- name: Import Backup Handler + ansible.builtin.import_tasks: + file: backup.yaml + +- name: Ensure Gitea is restarted + community.docker.docker_compose: + project_src: /opt/gitea + restarted: true + register: docker_compose + +- name: Ensure Gitea is running + community.docker.docker_compose: + project_src: /opt/gitea + register: docker_compose + +- name: Get Gitea HTTP Response + ansible.builtin.uri: + url: "https://{{ gitea.hostname }}.{{ gitea.domain }}" + register: gitea_http_response + failed_when: 400 <= gitea_http_response.status < 500 + +- name: Wait until Gitea is up + ansible.builtin.uri: + url: "https://{{ gitea.hostname }}.{{ gitea.domain }}" + register: gitea_http_response + until: gitea_http_response.status == 200 + retries: 30 + delay: 2 + +- name: Output Docker Compose State + ansible.builtin.debug: + var: docker_compose diff --git a/tasks/main.yaml b/tasks/main.yaml index 36f2d4d..fcfc825 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -39,64 +39,48 @@ enabled: true state: started -- name: Backup Files - block: - - name: Stop Gitea for Backup - community.docker.docker_compose: - project_src: /opt/gitea - state: present - stopped: true +- name: Check if environment.env will get changed + ansible.builtin.template: + src: environment.env.j2 + dest: "/opt/gitea/environment.env" + owner: root + mode: "600" + register: env_diff + check_mode: true + notify: + - Backup Gitea - - name: Backup Gitea Directory - ansible.builtin.copy: - src: /opt/gitea/ - dest: "/opt/gitea.backup@{{ lookup('pipe', 'date --utc --iso-8601=seconds') }}" - mode: "700" - remote_src: true - directory_mode: true - register: backup +- name: Check if docker-compose.yaml will get changed + ansible.builtin.template: + src: docker-compose.yaml.j2 + dest: /opt/gitea/docker-compose.yaml + owner: root + mode: "600" + register: docker_compose_diff + check_mode: true + notify: + - Backup Gitea -- name: Apply Changes - block: - - name: Upload Environment File - ansible.builtin.template: - src: environment.env.j2 - dest: "/opt/gitea/environment.env" - owner: root - mode: "600" - register: gitea_env +- name: Execute Handlers before changing configfiles + ansible.builtin.meta: flush_handlers - - name: Upload docker-compose.yaml - ansible.builtin.template: - src: docker-compose.yaml.j2 - dest: /opt/gitea/docker-compose.yaml - owner: root - mode: "600" - validate: /usr/bin/docker compose -f %s config -q +- name: Upload Environment File + ansible.builtin.template: + src: environment.env.j2 + dest: "/opt/gitea/environment.env" + owner: root + mode: "600" + notify: + - Ensure Gitea is restarted + - Wait until Gitea is up - - name: Start Gitea and try reaching it - ansible.builtin.include_tasks: - file: start.yaml - - rescue: - - name: Stop Gitea for Backup - community.docker.docker_compose: - project_src: /opt/gitea - state: present - stopped: true - - - name: Restore Gitea Directory from Backup - ansible.builtin.copy: - src: "{{ backup.dest }}" - dest: "{{ backup.src }}" - remote_src: true - mode: "700" - - - name: Start Gitea and try reaching it - ansible.builtin.include_tasks: - file: start.yaml - - always: - - name: Output Docker Compose State - ansible.builtin.debug: - var: docker_compose +- name: Upload docker-compose.yaml + ansible.builtin.template: + src: docker-compose.yaml.j2 + dest: /opt/gitea/docker-compose.yaml + owner: root + mode: "600" + notify: + - Output Docker Compose State + - Ensure Gitea is restarted + - Wait until Gitea is up diff --git a/tasks/start.yaml b/tasks/start.yaml deleted file mode 100644 index 27e37b9..0000000 --- a/tasks/start.yaml +++ /dev/null @@ -1,12 +0,0 @@ -- name: Start Gitea - community.docker.docker_compose: - project_src: /opt/gitea - register: docker_compose - -- name: Check if {{ gitea.hostname }}.{{ gitea.domain }} is available and returning status 200 - ansible.builtin.uri: - url: "https://{{ gitea.hostname }}.{{ gitea.domain }}" - register: _result - until: _result.status == 200 - retries: 30 - delay: 2 diff --git a/templates/environment.env.j2 b/templates/environment.env.j2 index 90f8197..68454f3 100644 --- a/templates/environment.env.j2 +++ b/templates/environment.env.j2 @@ -93,7 +93,7 @@ GITEA__service__ENABLE_NOTIFY_MAIL={{ gitea_vars.service.enable_notify_mail }} GITEA__service__DEFAULT_KEEP_EMAIL_PRIVATE={{ gitea_vars.service.default_keep_email_private }} {% endif %} {% if 'default_allow_create_organization' in gitea_vars['service'] %} -GITEA__service__DEFAULT_ALLOW_CREATE_ORGANIZATION ={{ gitea_vars.service.default_allow_create_organization }} +GITEA__service__DEFAULT_ALLOW_CREATE_ORGANIZATION={{ gitea_vars.service.default_allow_create_organization }} {% endif %} {% if 'default_enable_timetracking' in gitea_vars['service'] %} GITEA__service__DEFAULT_ENABLE_TIMETRACKING={{ gitea_vars.service.default_enable_timetracking }}