From 70b84b5b22e2eb5f5c954a18f9b6f5c62f339a9d Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Sat, 2 Mar 2024 11:41:21 +0100 Subject: [PATCH 1/4] feat: Add litestream role Signed-off-by: Mahendra Paipuri --- roles/litestream/README.md | 63 +++++++++++++++ roles/litestream/TROUBLESHOOTING.md | 20 +++++ roles/litestream/defaults/main.yml | 13 ++++ roles/litestream/handlers/main.yml | 10 +++ roles/litestream/meta/argument_specs.yml | 57 ++++++++++++++ roles/litestream/meta/main.yml | 33 ++++++++ .../molecule/alternative/molecule.yml | 14 ++++ .../molecule/alternative/prepare.yml | 37 +++++++++ .../alternative/tests/test_alternative.py | 72 ++++++++++++++++++ .../litestream/molecule/default/molecule.yml | 10 +++ roles/litestream/molecule/default/prepare.yml | 34 +++++++++ roles/litestream/molecule/default/sample.db | Bin 0 -> 65536 bytes .../molecule/default/tests/test_default.py | 64 ++++++++++++++++ roles/litestream/molecule/latest/molecule.yml | 11 +++ roles/litestream/molecule/latest/prepare.yml | 34 +++++++++ .../molecule/latest/tests/test_latest.py | 63 +++++++++++++++ roles/litestream/tasks/configure.yml | 66 ++++++++++++++++ roles/litestream/tasks/install.yml | 67 ++++++++++++++++ roles/litestream/tasks/main.yml | 64 ++++++++++++++++ roles/litestream/tasks/preflight.yml | 56 ++++++++++++++ roles/litestream/tasks/selinux.yml | 23 ++++++ .../templates/litestream.service.j2 | 46 +++++++++++ roles/litestream/test-requirements.txt | 1 + roles/litestream/vars/main.yml | 11 +++ 24 files changed, 869 insertions(+) create mode 100644 roles/litestream/README.md create mode 100644 roles/litestream/TROUBLESHOOTING.md create mode 100644 roles/litestream/defaults/main.yml create mode 100644 roles/litestream/handlers/main.yml create mode 100644 roles/litestream/meta/argument_specs.yml create mode 100644 roles/litestream/meta/main.yml create mode 100644 roles/litestream/molecule/alternative/molecule.yml create mode 100644 roles/litestream/molecule/alternative/prepare.yml create mode 100644 roles/litestream/molecule/alternative/tests/test_alternative.py create mode 100644 roles/litestream/molecule/default/molecule.yml create mode 100644 roles/litestream/molecule/default/prepare.yml create mode 100644 roles/litestream/molecule/default/sample.db create mode 100644 roles/litestream/molecule/default/tests/test_default.py create mode 100644 roles/litestream/molecule/latest/molecule.yml create mode 100644 roles/litestream/molecule/latest/prepare.yml create mode 100644 roles/litestream/molecule/latest/tests/test_latest.py create mode 100644 roles/litestream/tasks/configure.yml create mode 100644 roles/litestream/tasks/install.yml create mode 100644 roles/litestream/tasks/main.yml create mode 100644 roles/litestream/tasks/preflight.yml create mode 100644 roles/litestream/tasks/selinux.yml create mode 100644 roles/litestream/templates/litestream.service.j2 create mode 100644 roles/litestream/test-requirements.txt create mode 100644 roles/litestream/vars/main.yml diff --git a/roles/litestream/README.md b/roles/litestream/README.md new file mode 100644 index 0000000..7f8cd0b --- /dev/null +++ b/roles/litestream/README.md @@ -0,0 +1,63 @@ +# Ansible Role: Litestream + +## Description + +Deploy [Litestream](https://github.com/benbjohnson/litestream) using ansible. + +## Requirements + +- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it) +- gnu-tar on Mac deployer host (`brew install gnu-tar`) +- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`) + +## Role Variables + +All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml). +Please refer to the [collection docs](https://mahendrapaipuri.github.io/ansible/branch/main/litestream_role.html) for description and default values of the variables. + +## Example + +### Playbook + +Use it in a playbook as follows: +```yaml +- hosts: all + roles: + - mahendrapaipuri.ansible.litestream +``` + +### TLS config + +Before running litestream role, the user needs to provision their own certificate and key. +```yaml +- hosts: all + roles: + - mahendrapaipuri.ansible.litestream + vars: + litestream_config: + dbs: + - path: /var/lib/db1 + replicas: + - url: s3://mybkt.litestream.io/db1 +``` + + +## Local Testing + +The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`. + +## Continuous Integration + +Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient. + +## Contributing + +See [contributor guideline](../../CONTRIBUTING.md). + +## Troubleshooting + +See [troubleshooting](TROUBLESHOOTING.md). + +## License + +This project is licensed under MIT License. See [LICENSE](../../LICENSE) for more details. diff --git a/roles/litestream/TROUBLESHOOTING.md b/roles/litestream/TROUBLESHOOTING.md new file mode 100644 index 0000000..0bece12 --- /dev/null +++ b/roles/litestream/TROUBLESHOOTING.md @@ -0,0 +1,20 @@ +# Troubleshooting + +## Bad requests (HTTP 400) + +This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur. + +This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers. + +Unless netrc is needed by your playbook and ansible roles, please unset the var like so: + +``` +$ NETRC= ansible-playbook ... +``` + +Or: + +``` +$ export NETRC= +$ ansible-playbook ... +``` diff --git a/roles/litestream/defaults/main.yml b/roles/litestream/defaults/main.yml new file mode 100644 index 0000000..4aca95f --- /dev/null +++ b/roles/litestream/defaults/main.yml @@ -0,0 +1,13 @@ +--- +litestream_version: "0.3.13" +litestream_binary_local_dir: "" +litestream_binary_url: https://github.com/{{ _litestream_repo }}/releases/download/v{{ litestream_version }}/litestream-v{{ litestream_version }}-linux-{{ go_arch }}.tar.gz +litestream_skip_install: false + +litestream_config: {} + +litestream_cli_args: [] +litestream_env_vars: {} +litestream_binary_install_dir: /usr/local/bin +litestream_system_group: root +litestream_system_user: "{{ litestream_system_group }}" diff --git a/roles/litestream/handlers/main.yml b/roles/litestream/handlers/main.yml new file mode 100644 index 0000000..14a969a --- /dev/null +++ b/roles/litestream/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: Restart litestream + listen: restart litestream + become: true + ansible.builtin.systemd: + daemon_reload: true + name: litestream + state: restarted + when: + - not ansible_check_mode diff --git a/roles/litestream/meta/argument_specs.yml b/roles/litestream/meta/argument_specs.yml new file mode 100644 index 0000000..3d283f8 --- /dev/null +++ b/roles/litestream/meta/argument_specs.yml @@ -0,0 +1,57 @@ +--- +# yamllint disable rule:line-length +argument_specs: + main: + short_description: Litestream replicator + description: + - Deploy L(Litestream,https://litestream.io) using ansible + author: + - Mahendra Paipuri + options: + litestream_version: + description: Litestream package version. Also accepts latest as parameter. + default: "0.3.13" + litestream_skip_install: + description: Litestream installation tasks gets skipped when set to true. + type: bool + default: false + litestream_binary_local_dir: + description: + - Enables the use of local packages instead of those distributed on github. + - The parameter may be set to a directory where the C(litestream) binary is stored on the host where ansible is run. + - This overrides the I(litestream_version) parameter + litestream_binary_url: + description: URL of the Litestream binaries .tar.gz file + default: https://github.com/{{ _litestream_repo }}/releases/download/v{{ litestream_version }}/litestream-{{ litestream_version }}.linux-{{ go_arch }}.tar.gz + litestream_config: + description: + - Litstream config. + - More details on Litestream config in L(docs,https://litestream.io/reference/config/) + type: dict + required: true + litestream_binary_install_dir: + description: + - I(Advanced) + - Directory to install litestream binary + default: /usr/local/bin + litestream_system_group: + description: + - I(Advanced) + - System group for litestream + default: root + litestream_system_user: + description: + - I(Advanced) + - Litestream user + default: root + litestream_cli_args: + description: + - I(Advanced) + - Additional CLI arguments that will be passed to Litestream + type: list + elements: str + litestream_env_vars: + description: + - I(Advanced) + - Environment variables will be passed to Litestream + type: dict diff --git a/roles/litestream/meta/main.yml b/roles/litestream/meta/main.yml new file mode 100644 index 0000000..229f5bf --- /dev/null +++ b/roles/litestream/meta/main.yml @@ -0,0 +1,33 @@ +--- +galaxy_info: + author: Mahendra Paipuri + description: Litestream + license: Apache + min_ansible_version: "2.9" + platforms: + - name: Ubuntu + versions: + - focal + - jammy + - name: Debian + versions: + - bullseye + - buster + - name: EL + versions: + - "7" + - "8" + - "9" + - name: Fedora + versions: + - "37" + - "38" + galaxy_tags: + - database + - replication + - sqlite + - litestream + - wal + - object + - s3 + - cloud diff --git a/roles/litestream/molecule/alternative/molecule.yml b/roles/litestream/molecule/alternative/molecule.yml new file mode 100644 index 0000000..f104ae8 --- /dev/null +++ b/roles/litestream/molecule/alternative/molecule.yml @@ -0,0 +1,14 @@ +--- +provisioner: + inventory: + group_vars: + all: + litestream_system_group: litestream + litestream_config: + dbs: + - path: /var/lib/db/sample1.db + replicas: + - path: /var/lib/backup_sample + - path: /var/lib/db/sample2.db + replicas: + - path: /nfs/db/backups/backup_sample diff --git a/roles/litestream/molecule/alternative/prepare.yml b/roles/litestream/molecule/alternative/prepare.yml new file mode 100644 index 0000000..ed3df1e --- /dev/null +++ b/roles/litestream/molecule/alternative/prepare.yml @@ -0,0 +1,37 @@ +--- +- name: Run target preparation + hosts: all + strategy: free + any_errors_fatal: true + tasks: + - name: Install RedHat sqlite package + ansible.builtin.yum: + name: sqlite + state: present + when: ansible_os_family == 'RedHat' + + - name: Install Debian sqlite package + ansible.builtin.apt: + name: sqlite3 + state: present + update_cache: true + when: ansible_os_family == 'Debian' + + - name: Create directory for sqlite sample DB + ansible.builtin.file: + path: /var/lib/db + owner: root + group: root + mode: "0777" + state: directory + + - name: Copy sqlite sample DB + ansible.builtin.copy: + src: ../default/sample.db + dest: "/var/lib/db/sample{{ item }}.db" + owner: root + group: root + mode: "0777" + with_items: + - "1" + - "2" diff --git a/roles/litestream/molecule/alternative/tests/test_alternative.py b/roles/litestream/molecule/alternative/tests/test_alternative.py new file mode 100644 index 0000000..afe39ac --- /dev/null +++ b/roles/litestream/molecule/alternative/tests/test_alternative.py @@ -0,0 +1,72 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +@pytest.mark.parametrize("dir", [ + "/etc/litestream", + "/var/lib/backup_sample", + "/var/lib/backup_sample/generations", + "/nfs/db/backups/backup_sample", + "/nfs/db/backups/backup_sample/generations", +]) +def test_directories(host, dir): + d = host.file(dir) + assert d.is_directory + assert d.exists + + +@pytest.mark.parametrize("file", [ + "/etc/systemd/system/litestream.service", + "/etc/litestream/config.yml", +]) +def test_files(host, file): + f = host.file(file) + assert f.exists + assert f.is_file + + +@pytest.mark.parametrize("file", [ + "/etc", + "/root", + "/usr", + "/var" +]) +def test_permissions_didnt_change(host, file): + f = host.file(file) + assert f.exists + assert f.is_directory + assert f.user == "root" + assert f.group == "root" + + +def test_user(host): + assert host.group("litestream").exists + assert "litestream" in host.user("litestream").groups + assert host.user("litestream").shell == "/usr/sbin/nologin" + assert host.user("litestream").home == "/" + + +def test_service(host): + s = host.service("litestream") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u litestream --since "1 hour ago"') + print("\n==== journalctl -u litestream Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_systemd_properties(host): + s = host.service("litestream") + p = s.systemd_properties + assert p.get("ProtectHome") == "yes" diff --git a/roles/litestream/molecule/default/molecule.yml b/roles/litestream/molecule/default/molecule.yml new file mode 100644 index 0000000..e1452ba --- /dev/null +++ b/roles/litestream/molecule/default/molecule.yml @@ -0,0 +1,10 @@ +--- +provisioner: + inventory: + group_vars: + all: + litestream_config: + dbs: + - path: /var/lib/db/sample.db + replicas: + - path: /var/lib/backup diff --git a/roles/litestream/molecule/default/prepare.yml b/roles/litestream/molecule/default/prepare.yml new file mode 100644 index 0000000..cd73af4 --- /dev/null +++ b/roles/litestream/molecule/default/prepare.yml @@ -0,0 +1,34 @@ +--- +- name: Run target preparation + hosts: all + strategy: free + any_errors_fatal: true + tasks: + - name: Install RedHat sqlite package + ansible.builtin.yum: + name: sqlite + state: present + when: ansible_os_family == 'RedHat' + + - name: Install Debian sqlite package + ansible.builtin.apt: + name: sqlite3 + state: present + update_cache: true + when: ansible_os_family == 'Debian' + + - name: Create directory for sqlite sample DB + ansible.builtin.file: + path: /var/lib/db + owner: root + group: root + mode: "0755" + state: directory + + - name: Copy sqlite sample DB + ansible.builtin.copy: + src: sample.db + dest: /var/lib/db/sample.db + owner: root + group: root + mode: "0755" diff --git a/roles/litestream/molecule/default/sample.db b/roles/litestream/molecule/default/sample.db new file mode 100644 index 0000000000000000000000000000000000000000..e7334bc813dfaf940b0b0803326f4a536d7ce988 GIT binary patch literal 65536 zcmeI54{RLSS-|J-{vv!==YqPt#T&f<+cz0qC zH@oA^>?UU~AA$eg?>Ov8-T`d+!2V&oBizh?KPNL~@-3!wgnX;-mBeuTrP#+~?~J@EzgzB? zRB=!k5aNd=f$}$mKx8mt=X3j+qtTJ8W3k=Aj;U>Ks^(2a*P2R4t*b3X)9V{5v=z0k zwJp``)Eix^W3*JW=x(iIrBtYuXsxomv{b6l2Z~G+3#G-iN{KF)tEEbfu9c~u9t15d zmlKp$ODm;fjTQ^l5*%HFTU)6fLjS=*sft5}lf?pRd;^XW{=8t$`BgRtn42 z63rAA)+)8>spYn96Llgg&8DeZ)>IbsO63c0PQaf#YXi}d$3|m!S6u>Y+a1kl*Q;u$ zzG^g8y?`!4`UjNLO~EZ@Zy_^a*k0k+W?QxaTGJLUR@PQ2Gs4Ny zqKZX}YjzL-0-*LTwLuw9AuK3_)}-e^Z#FDa+D@X5x|ffm=^bTRiVUj*HM3Y)UYVv7 zz*J|gggSOmAT<#rdi$Xx(e%g&e`lj)?1i42C|H)-+R%3Ov0i()bH-`WHdcC#@`cdyzIoWbN6>5A)LOLVcZ5YADkRh4uNA0 zEYuM1TP>_R^IB|B(dw{-S!YZyuwK^D({)BCsHZ}s^tEiVES{-06kYevr3xd$;?eZFtPlhzH!HIj$|@xk(aRzf&n~W&@xxfGU>CTT;I!WrK|3$T zBk8O8!%%vc*7_QpN2axd$D@s#6yUC!x`v?mrkHp z57NA;wfz z6(~Wdvb^$InV?2bMMD9@(OeH8!vcD=eY-+&dfU_fh7mAfnzED=+1t0G_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rqo%6>i-xo*G_2xs4oa^j+M)&%FA$xpR z@w*t}_l#{qEJ8-z&%3;PV=$UNae{wm)~`U<5e>vjh5f;RvkER$C||!0?&}kJczkW| z?u^>rgwH?+Z>a_Sba+nIwpyBHvD?Rov 0 + - not litestream_skip_install + notify: restart litestream diff --git a/roles/litestream/tasks/main.yml b/roles/litestream/tasks/main.yml new file mode 100644 index 0000000..ce4a46b --- /dev/null +++ b/roles/litestream/tasks/main.yml @@ -0,0 +1,64 @@ +--- +- name: Preflight + ansible.builtin.include_tasks: + file: preflight.yml + apply: + tags: + - litestream_install + - litestream_configure + - litestream_run + tags: + - litestream_install + - litestream_configure + - litestream_run + +- name: Install + ansible.builtin.include_tasks: + file: install.yml + apply: + become: true + tags: + - litestream_install + when: + ( not __litestream_is_installed.stat.exists ) or + ( (__litestream_current_version_output.stderr_lines | length > 0) and + (__litestream_current_version_output.stderr_lines[0].split("v")[1] != litestream_version) ) or + ( (__litestream_current_version_output.stdout_lines | length > 0) and + (__litestream_current_version_output.stdout_lines[0].split("v")[1] != litestream_version) ) or + ( litestream_binary_local_dir | length> 0 + ) + tags: + - litestream_install + +- name: SELinux + ansible.builtin.include_tasks: + file: selinux.yml + apply: + become: true + tags: + - litestream_configure + when: ansible_selinux.status == "enabled" + tags: + - litestream_configure + +- name: Configure + ansible.builtin.include_tasks: + file: configure.yml + apply: + become: true + tags: + - litestream_configure + tags: + - litestream_configure + +- name: Ensure Litestream is enabled on boot + become: true + ansible.builtin.systemd: + daemon_reload: true + name: litestream + enabled: true + state: started + when: + - not ansible_check_mode + tags: + - litestream_run diff --git a/roles/litestream/tasks/preflight.yml b/roles/litestream/tasks/preflight.yml new file mode 100644 index 0000000..a888982 --- /dev/null +++ b/roles/litestream/tasks/preflight.yml @@ -0,0 +1,56 @@ +--- +- name: Assert usage of systemd as an init system + ansible.builtin.assert: + that: ansible_service_mgr == 'systemd' + msg: This role only works with systemd + +- name: Assert litestream config is not empty + ansible.builtin.assert: + that: litestream_config | length > 0 + msg: Litestream config should not be empty + +- name: Install package fact dependencies + become: true + ansible.builtin.package: + name: "{{ _pkg_fact_req }}" + state: present + when: (_pkg_fact_req) + vars: + _pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\ + {{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }} + {% else %}\ + {% endif %}" + +- name: Gather package facts + ansible.builtin.package_facts: + when: not 'packages' in ansible_facts + +- name: Check if litestream is installed + ansible.builtin.stat: + path: "{{ litestream_binary_install_dir }}/litestream" + register: __litestream_is_installed + check_mode: false + tags: + - litestream_install + +- name: Gather currently installed litestream version (if any) + ansible.builtin.command: "{{ litestream_binary_install_dir }}/litestream version" + changed_when: false + register: __litestream_current_version_output + check_mode: false + when: __litestream_is_installed.stat.exists + tags: + - litestream_install + +- name: Discover latest version + ansible.builtin.set_fact: + litestream_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _litestream_repo }}/releases/latest', + headers=_github_api_headers, split_lines=False) | from_json).get('tag_name') | replace('v', + '') }}" + run_once: true + until: litestream_version is version('0.0.0', '>=') + retries: 10 + when: + - litestream_version == "latest" + - litestream_binary_local_dir | length == 0 + - not litestream_skip_install diff --git a/roles/litestream/tasks/selinux.yml b/roles/litestream/tasks/selinux.yml new file mode 100644 index 0000000..e3ddb6a --- /dev/null +++ b/roles/litestream/tasks/selinux.yml @@ -0,0 +1,23 @@ +--- +- name: Install selinux python packages [RedHat] + ansible.builtin.package: + name: "{{ ['libselinux-python', 'policycoreutils-python'] + if ansible_python_version is version('3', '<') else + ['python3-libselinux', 'python3-policycoreutils'] }}" + state: present + register: _install_selinux_packages + until: _install_selinux_packages is success + retries: 5 + delay: 2 + when: ansible_os_family | lower == "redhat" + +- name: Install selinux python packages [clearlinux] + ansible.builtin.package: + name: sysadmin-basic + state: present + register: _install_selinux_packages + until: _install_selinux_packages is success + retries: 5 + delay: 2 + when: + - ansible_distribution | lower == "clearlinux" diff --git a/roles/litestream/templates/litestream.service.j2 b/roles/litestream/templates/litestream.service.j2 new file mode 100644 index 0000000..4cf8a2a --- /dev/null +++ b/roles/litestream/templates/litestream.service.j2 @@ -0,0 +1,46 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Litestream +After=network-online.target + +[Service] +Type=simple +User={{ litestream_system_user }} +Group={{ litestream_system_group }} +ExecStart={{ litestream_binary_install_dir }}/litestream \ + replicate \ + -config=/etc/litestream/config.yml + +SyslogIdentifier=litestream +Restart=always +RestartSec=1 +StartLimitInterval=0 + +{% set ns = namespace(protect_home = 'yes') %} +{% for m in ansible_mounts if m.mount.startswith('/home') %} +{% set ns.protect_home = 'read-only' %} +{% endfor %} + +ProtectHome={{ ns.protect_home }} +ReadWritePaths={{ _read_write_paths | join(' ') }} + +NoNewPrivileges=yes + +{% if litestream_env_vars | length > 0 %} +{% for k, v in litestream_env_vars.items() %} +Environment={{ k }}={{ v }} +{% endfor %} +{% endif %} + +{% if (ansible_facts.packages.systemd | first).version is version('232', '>=') %} +ProtectSystem=strict +ProtectControlGroups=true +ProtectKernelModules=true +ProtectKernelTunables=yes +{% else %} +ProtectSystem=full +{% endif %} + +[Install] +WantedBy=multi-user.target diff --git a/roles/litestream/test-requirements.txt b/roles/litestream/test-requirements.txt new file mode 100644 index 0000000..7f0b6e7 --- /dev/null +++ b/roles/litestream/test-requirements.txt @@ -0,0 +1 @@ +bcrypt diff --git a/roles/litestream/vars/main.yml b/roles/litestream/vars/main.yml new file mode 100644 index 0000000..ee68533 --- /dev/null +++ b/roles/litestream/vars/main.yml @@ -0,0 +1,11 @@ +--- +go_arch_map: + i386: "386" + x86_64: amd64 + aarch64: arm64 + armv7l: armv7 + armv6l: armv6 + +go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}" +_litestream_repo: benbjohnson/litestream +_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" From 8cd235494c6bf50f0b1069c1c4c66002e7c8d509 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Sat, 2 Mar 2024 11:53:31 +0100 Subject: [PATCH 2/4] test: Fix sanity check errors Signed-off-by: Mahendra Paipuri --- roles/litestream/molecule/default/tests/test_default.py | 1 - 1 file changed, 1 deletion(-) diff --git a/roles/litestream/molecule/default/tests/test_default.py b/roles/litestream/molecule/default/tests/test_default.py index 26075c8..a820f50 100644 --- a/roles/litestream/molecule/default/tests/test_default.py +++ b/roles/litestream/molecule/default/tests/test_default.py @@ -61,4 +61,3 @@ def test_protecthome_property(host): s = host.service("litestream") p = s.systemd_properties assert p.get("ProtectHome") == "yes" - From e2a5e52f1870f058c75e05acf05ede4f4270e5bf Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Sat, 2 Mar 2024 12:55:01 +0100 Subject: [PATCH 3/4] chore: Cosmetic changes Signed-off-by: Mahendra Paipuri --- roles/litestream/tasks/configure.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/litestream/tasks/configure.yml b/roles/litestream/tasks/configure.yml index 480d86b..2fcb8e9 100644 --- a/roles/litestream/tasks/configure.yml +++ b/roles/litestream/tasks/configure.yml @@ -26,13 +26,13 @@ {% endfor %} {{ ns.db_backup_paths | from_yaml | unique }} -- name: Create litestream DB backup directories +- name: Create DB backup directories ansible.builtin.file: path: "{{ item }}" - state: directory owner: "{{ litestream_system_user }}" group: "{{ litestream_system_group }}" mode: u+rwX,g+rwX,o-rwX + state: directory when: litestream_system_user != "root" loop: "{{ _db_backup_paths }}" loop_control: From 3bce232983a3129f04af3899a9c04d157940c9fa Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Sat, 2 Mar 2024 13:12:57 +0100 Subject: [PATCH 4/4] test: Add integration tests for litestream role Signed-off-by: Mahendra Paipuri --- .../targets/molecule-litestream-alternative/runme.sh | 4 ++++ .../integration/targets/molecule-litestream-default/runme.sh | 4 ++++ tests/integration/targets/molecule-litestream-latest/runme.sh | 4 ++++ 3 files changed, 12 insertions(+) create mode 100755 tests/integration/targets/molecule-litestream-alternative/runme.sh create mode 100755 tests/integration/targets/molecule-litestream-default/runme.sh create mode 100755 tests/integration/targets/molecule-litestream-latest/runme.sh diff --git a/tests/integration/targets/molecule-litestream-alternative/runme.sh b/tests/integration/targets/molecule-litestream-alternative/runme.sh new file mode 100755 index 0000000..d094c3e --- /dev/null +++ b/tests/integration/targets/molecule-litestream-alternative/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-litestream-default/runme.sh b/tests/integration/targets/molecule-litestream-default/runme.sh new file mode 100755 index 0000000..d094c3e --- /dev/null +++ b/tests/integration/targets/molecule-litestream-default/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-litestream-latest/runme.sh b/tests/integration/targets/molecule-litestream-latest/runme.sh new file mode 100755 index 0000000..d094c3e --- /dev/null +++ b/tests/integration/targets/molecule-litestream-latest/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh"