From bd022f046651d287309b26164cd60a3d6d38471c Mon Sep 17 00:00:00 2001 From: Colin Wilk Date: Fri, 26 Jun 2026 22:11:18 +0200 Subject: Update and fix pre-commit hooks --- .ansible-lint | 3 +- .github/workflows/pre-commit.yml | 2 +- .github/workflows/release.yml | 4 +- .pre-commit-config.yaml | 11 ++-- .yamllint | 63 ++++++---------------- meta/argument_specs.yml | 24 ++++----- meta/main.yml | 2 +- molecule/default/converge.yml | 5 +- .../tests/test_systemd_success_exit_status.py | 33 +++++++++--- tasks/client_create_scripts_each.yml | 12 ++--- tasks/client_setup.yml | 40 +++++++------- tasks/installation.yml | 2 +- tasks/server_setup.yml | 10 ++-- 13 files changed, 99 insertions(+), 112 deletions(-) diff --git a/.ansible-lint b/.ansible-lint index 2e09147..ab574e1 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -1,6 +1,7 @@ # https://ansible-lint.readthedocs.io --- -exclude_paths: [] +exclude_paths: + - molecule/default/decryption_keys.yml use_default_rules: true diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index fdc7c70..eb9a2f2 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -1,7 +1,7 @@ --- name: pre-commit -'on': push +"on": push jobs: pre-commit: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cc25e83..7684929 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,10 +1,10 @@ --- name: Release -'on': +"on": push: tags: - - '*' + - "*" defaults: run: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0204c17..e9eb853 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,9 +1,8 @@ --- -default_stages: [commit, push] - +default_stages: [pre-commit, pre-push] repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v5.0.0 hooks: - id: check-added-large-files - id: check-yaml @@ -12,12 +11,12 @@ repos: - id: end-of-file-fixer - repo: https://github.com/ansible-community/ansible-lint.git - rev: v6.15.0 + rev: v25.5.0 hooks: - id: ansible-lint files: \.(yaml|yml)$ - - repo: https://github.com/markdownlint/markdownlint - rev: v0.12.0 + - repo: https://github.com/igorshubovych/markdownlint-cli + rev: v0.43.0 hooks: - id: markdownlint diff --git a/.yamllint b/.yamllint index 8551271..c759b0c 100644 --- a/.yamllint +++ b/.yamllint @@ -1,52 +1,19 @@ -# https://yamllint.readthedocs.io/en/stable/rules.html --- -rules: - quoted-strings: - quote-type: single - required: only-when-needed - - colons: - max-spaces-before: 0 - max-spaces-after: 1 - - commas: - max-spaces-before: 0 - min-spaces-after: 1 - max-spaces-after: 1 +extends: default +rules: comments: - require-starting-space: true - ignore-shebangs: true min-spaces-from-content: 1 - - document-end: - present: false - - document-start: - present: true - - empty-lines: - max: 2 - max-start: 0 - max-end: 0 - - empty-values: - forbid-in-block-mappings: true - forbid-in-flow-mappings: true - - float-values: - require-numeral-before-decimal: true - - hyphens: - max-spaces-after: 1 - - indentation: - spaces: 2 - indent-sequences: true - check-multi-line-strings: true - - trailing-spaces: {} - - truthy: - allowed-values: ['true', 'false'] - check-keys: true + comments-indentation: false + document-start: disable + line-length: + max: 160 + braces: + min-spaces-inside: 0 + max-spaces-inside: 1 + octal-values: + forbid-implicit-octal: true + forbid-explicit-octal: true + quoted-strings: + quote-type: double + required: only-when-needed diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml index 5b55ac9..2331794 100644 --- a/meta/argument_specs.yml +++ b/meta/argument_specs.yml @@ -3,9 +3,9 @@ argument_specs: main: options: -################################################################################ -# Required -################################################################################ + ################################################################################ + # Required + ################################################################################ borg_server_host: type: str required: true @@ -14,13 +14,13 @@ argument_specs: type: str required: true -################################################################################ -# Optional -################################################################################ + ################################################################################ + # Optional + ################################################################################ borg_server_host_url: type: str required: false - default: '{{ borg_server_host }}' + default: "{{ borg_server_host }}" borg_server_user_home: type: str @@ -30,12 +30,12 @@ argument_specs: borg_repo_name: type: str required: false - default: '{{ inventory_hostname }}' + default: "{{ inventory_hostname }}" borg_backup_name_format: type: str required: false - default: '{hostname}-{now:%Y-%m-%dT%H:%M:%S}' + default: "{hostname}-{now:%Y-%m-%dT%H:%M:%S}" borg_mode_append_only: type: bool @@ -52,13 +52,11 @@ argument_specs: elements: str required: false default: [] - borg_excluded_dirs: type: list elements: str required: false default: [] - borg_passphrase: type: str required: false @@ -85,12 +83,12 @@ argument_specs: borg_backup_argument: type: str required: false - default: '{ borg_server_host_url }' + default: "{ borg_server_host_url }" borg_systemd_oncalendar: type: str required: false - default: '*-*-* 02:00:00' + default: "*-*-* 02:00:00" borg_systemd_accuracysec: type: str diff --git a/meta/main.yml b/meta/main.yml index 7c4e402..790bc41 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -6,7 +6,7 @@ galaxy_info: author: kliwniloc description: BorgBackup role for for deploying scheduled backups license: license (MIT) - min_ansible_version: '2.4' + min_ansible_version: "2.4" platforms: - name: Debian versions: diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 995ccd0..4c8aa06 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -29,10 +29,11 @@ - name: Set ssh_key ansible.builtin.set_fact: - borg_server_host_ssh_key: '{{ borg_server_ssh_keyscan.stdout + borg_server_host_ssh_key: >- + {{ borg_server_ssh_keyscan.stdout | split(" ") | reject("search", borg_server_host) - | join(" ") }}' + | join(" ") }} - name: Allow additional successful exit codes on alternate client ansible.builtin.set_fact: diff --git a/molecule/default/tests/test_systemd_success_exit_status.py b/molecule/default/tests/test_systemd_success_exit_status.py index 8f2e242..61c20c4 100644 --- a/molecule/default/tests/test_systemd_success_exit_status.py +++ b/molecule/default/tests/test_systemd_success_exit_status.py @@ -1,12 +1,33 @@ -testinfra_hosts = ["borg-client", "borg-client-success-exit-status"] +"""Tests for systemd success exit status functionality""" +import pytest +testinfra_hosts = ['borg-client', 'borg-client-success-exit-status'] -def test_systemd_service_success_exit_status(host): - service = host.file("/etc/systemd/system/borg_backup@borg-server.service") +def test_systemd_service_has_correct_success_exit_status(host): + service = host.file('/etc/systemd/system/borg_backup@borg-server.service') assert service.exists - if host.backend.get_hostname() == "borg-client-success-exit-status": - assert service.contains(r"^SuccessExitStatus=1 TEMPFAIL$") + if host.backend.get_hostname() == 'borg-client-success-exit-status': + assert service.contains('SuccessExitStatus=1 TEMPFAIL') else: - assert not service.contains(r"^SuccessExitStatus=") + assert not service.contains('SuccessExitStatus=') + + +def test_systemd_timer_is_enabled(host): + timer_name = 'borg_backup@borg-server.timer' + c = host.run(f"systemctl is-enabled {timer_name}") + assert c.rc == 0 + + +def test_systemd_timer_is_active(host): + timer_name = 'borg_backup@borg-server.timer' + c = host.run(f"systemctl is-active {timer_name}") + assert c.rc == 0 + + +@pytest.mark.parametrize('exit_status', [1, 'TEMPFAIL']) +def test_success_exit_status_values(host, exit_status): + if host.backend.get_hostname() == 'borg-client-success-exit-status': + service = host.file('/etc/systemd/system/borg_backup@borg-server.service') + assert service.contains(str(exit_status)) diff --git a/tasks/client_create_scripts_each.yml b/tasks/client_create_scripts_each.yml index f36ba7c..8e01174 100644 --- a/tasks/client_create_scripts_each.yml +++ b/tasks/client_create_scripts_each.yml @@ -1,27 +1,27 @@ --- - name: Create script for automatic borg backup ansible.builtin.file: - dest: '{{ script_location }}' + dest: "{{ script_location }}" state: touch owner: root group: root modification_time: preserve access_time: preserve - mode: '0711' + mode: "0711" become: true - name: Insert shebang into backup script ansible.builtin.lineinfile: - path: '{{ script_location }}' - line: '#!/bin/bash' + path: "{{ script_location }}" + line: "#!/bin/bash" insertbefore: BOF state: present become: true - name: Insert Backup job block into scripts ansible.builtin.blockinfile: - path: '{{ script_location }}' - marker: '## {mark} ANSIBLE MANAGED BLOCK for server: {{ borg_server_host_url }}' + path: "{{ script_location }}" + marker: "## {mark} ANSIBLE MANAGED BLOCK for server: {{ borg_server_host_url }}" block: | export BORG_PASSPHRASE={{ borg_passphrase | quote }} borg create -C {{ borg_compression }} \ diff --git a/tasks/client_setup.yml b/tasks/client_setup.yml index eb6c9a1..b3316c2 100644 --- a/tasks/client_setup.yml +++ b/tasks/client_setup.yml @@ -4,14 +4,14 @@ path: /root/.ssh owner: root group: root - mode: '0640' + mode: "0640" state: directory become: true - name: Add borg server to known_hosts ansible.builtin.known_hosts: - name: '{{ borg_server_host_url }}' - key: '{{ borg_server_host_url }} {{ borg_server_host_ssh_key }}' + name: "{{ borg_server_host_url }}" + key: "{{ borg_server_host_url }} {{ borg_server_host_ssh_key }}" path: /root/.ssh/known_hosts state: present become: true @@ -21,30 +21,30 @@ path: /root/.ssh/id_rsa owner: root group: root - mode: '0600' + mode: "0600" comment: root@{{ inventory_hostname }} become: true register: ssh_key - name: Deploy Keys to Borg server ansible.builtin.lineinfile: - path: '{{ borg_server_user_home }}/.ssh/authorized_keys' + path: "{{ borg_server_user_home }}/.ssh/authorized_keys" line: > restrict,command="borg serve {{ "--append-only" if borg_mode_append_only }} --restrict-to-repository {{ borg_repo_name }}" {{ ssh_key.public_key }} root@{{ inventory_hostname }} - search_string: '{{ ssh_key.public_key }}' + search_string: "{{ ssh_key.public_key }}" state: present become: true - delegate_to: '{{ borg_server_host }}' + delegate_to: "{{ borg_server_host }}" - name: Initialise Borg repository ansible.builtin.command: > borg init --encryption=repokey borg@{{ borg_server_host_url }}:{{ borg_server_user_home }}/{{ borg_repo_name }} environment: - BORG_PASSPHRASE: '{{ borg_passphrase }}' + BORG_PASSPHRASE: "{{ borg_passphrase }}" become: true register: init_borg_output changed_when: init_borg_output.rc != 2 @@ -54,9 +54,9 @@ - name: Make sure key file exists ansible.builtin.file: - path: '{{ borg_decryption_keys_yaml_path }}' + path: "{{ borg_decryption_keys_yaml_path }}" state: touch - mode: '0600' + mode: "0600" access_time: preserve modification_time: preserve delegate_to: localhost @@ -64,7 +64,7 @@ - name: Read Vars file ansible.builtin.include_vars: - file: '{{ borg_decryption_keys_yaml_path }}' + file: "{{ borg_decryption_keys_yaml_path }}" register: local - name: Add repository encryption keys to ansible repo @@ -81,13 +81,13 @@ - name: If host new add encryption keys to vars ansible.builtin.set_fact: - decryption_keys: '{{ local.ansible_facts | combine({inventory_hostname: borg_keys.stdout}) }}' + decryption_keys: "{{ local.ansible_facts | combine({inventory_hostname: borg_keys.stdout}) }}" - name: Update encryption vars ansible.builtin.copy: - content: '{{ decryption_keys | to_nice_yaml(indent=2, width=2048) }}' - dest: '{{ borg_decryption_keys_yaml_path }}' - mode: '0600' + content: "{{ decryption_keys | to_nice_yaml(indent=2, width=2048) }}" + dest: "{{ borg_decryption_keys_yaml_path }}" + mode: "0600" when: decryption_keys is defined delegate_to: localhost become: false @@ -95,8 +95,8 @@ - name: Create backup scripts ansible.builtin.include_tasks: client_create_scripts_each.yml loop: - - '{{ borg_backup_script_location }}' - - '{{ borg_backup_script_location }}{{ "@" if borg_backup_argument != "" }}{{ borg_backup_argument }}' + - "{{ borg_backup_script_location }}" + - "{{ borg_backup_script_location }}{{ '@' if borg_backup_argument != '' else '' }}{{ borg_backup_argument }}" loop_control: loop_var: script_location @@ -104,7 +104,7 @@ ansible.builtin.template: src: borg_backup.service.j2 dest: /etc/systemd/system/{{ borg_backup_timer_name }}{{ "@" if borg_backup_argument != "" }}{{ borg_backup_argument }}.service - mode: '0644' + mode: "0644" owner: root group: root notify: Reload systemd @@ -114,7 +114,7 @@ ansible.builtin.template: src: borg_backup.timer.j2 dest: /etc/systemd/system/{{ borg_backup_timer_name }}{{ "@" if borg_backup_argument != "" }}{{ borg_backup_argument }}.timer - mode: '0644' + mode: "0644" owner: root group: root notify: Reload systemd @@ -125,7 +125,7 @@ - name: Enable borg_backup systemd timer ansible.builtin.systemd: - name: '{{ borg_backup_timer_name }}{{ "@" if borg_backup_argument != "" }}{{ borg_backup_argument }}.timer' + name: "{{ borg_backup_timer_name }}{{ '@' if borg_backup_argument != '' else '' }}{{ borg_backup_argument }}.timer" state: started enabled: true become: true diff --git a/tasks/installation.yml b/tasks/installation.yml index 7e64ff3..5fc7ecb 100644 --- a/tasks/installation.yml +++ b/tasks/installation.yml @@ -7,7 +7,7 @@ update_cache: true become: true when: ansible_facts['os_family'] == "Debian" - delegate_to: '{{ borg_server_host }}' + delegate_to: "{{ borg_server_host }}" - name: Install Debian Client dependencies ansible.builtin.apt: diff --git a/tasks/server_setup.yml b/tasks/server_setup.yml index b0a8251..603dd3d 100644 --- a/tasks/server_setup.yml +++ b/tasks/server_setup.yml @@ -4,19 +4,19 @@ name: borg comment: Borgbackup user create_home: true - home: '{{ borg_server_user_home }}' + home: "{{ borg_server_user_home }}" generate_ssh_key: true become: true - delegate_to: '{{ borg_server_host }}' + delegate_to: "{{ borg_server_host }}" - name: Make sure authorized keys exists ansible.builtin.file: - path: '{{ borg_server_user_home }}/.ssh/authorized_keys' + path: "{{ borg_server_user_home }}/.ssh/authorized_keys" state: touch owner: borg group: borg - mode: '644' + mode: "644" access_time: preserve modification_time: preserve become: true - delegate_to: '{{ borg_server_host }}' + delegate_to: "{{ borg_server_host }}" -- cgit v1.2.3