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 0000000..e7334bc Binary files /dev/null and b/roles/litestream/molecule/default/sample.db differ diff --git a/roles/litestream/molecule/default/tests/test_default.py b/roles/litestream/molecule/default/tests/test_default.py new file mode 100644 index 0000000..a820f50 --- /dev/null +++ b/roles/litestream/molecule/default/tests/test_default.py @@ -0,0 +1,63 @@ +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", + "/var/lib/backup/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_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_protecthome_property(host): + s = host.service("litestream") + p = s.systemd_properties + assert p.get("ProtectHome") == "yes" diff --git a/roles/litestream/molecule/latest/molecule.yml b/roles/litestream/molecule/latest/molecule.yml new file mode 100644 index 0000000..ba3bb54 --- /dev/null +++ b/roles/litestream/molecule/latest/molecule.yml @@ -0,0 +1,11 @@ +--- +provisioner: + inventory: + group_vars: + all: + litestream_version: latest + litestream_config: + dbs: + - path: /var/lib/db/sample.db + replicas: + - path: /var/lib/backup diff --git a/roles/litestream/molecule/latest/prepare.yml b/roles/litestream/molecule/latest/prepare.yml new file mode 100644 index 0000000..52c1883 --- /dev/null +++ b/roles/litestream/molecule/latest/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: ../default/sample.db + dest: /var/lib/db/sample.db + owner: root + group: root + mode: "0755" diff --git a/roles/litestream/molecule/latest/tests/test_latest.py b/roles/litestream/molecule/latest/tests/test_latest.py new file mode 100644 index 0000000..a820f50 --- /dev/null +++ b/roles/litestream/molecule/latest/tests/test_latest.py @@ -0,0 +1,63 @@ +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", + "/var/lib/backup/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_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_protecthome_property(host): + s = host.service("litestream") + p = s.systemd_properties + assert p.get("ProtectHome") == "yes" diff --git a/roles/litestream/tasks/configure.yml b/roles/litestream/tasks/configure.yml new file mode 100644 index 0000000..2fcb8e9 --- /dev/null +++ b/roles/litestream/tasks/configure.yml @@ -0,0 +1,66 @@ +--- +- name: Get DB backup base directories + ansible.builtin.set_fact: + _read_write_paths: |- + {% set ns = namespace(read_write_paths=[]) %} + {% for c in litestream_config['dbs'] %} + {% set _ = ns.read_write_paths.append('/'.join(c['path'].split('/')[:-1])) %} + {% for r in c['replicas'] %} + {% if 'path' in r.keys() %} + {% set _ = ns.read_write_paths.append('/'.join(r['path'].split('/')[:-1])) %} + {% endif %} + {% endfor %} + {% endfor %} + {{ ns.read_write_paths | from_yaml | unique }} + +- name: Get DB backup paths + ansible.builtin.set_fact: + _db_backup_paths: |- + {% set ns = namespace(db_backup_paths=[]) %} + {% for c in litestream_config['dbs'] %} + {% for r in c['replicas'] %} + {% if 'path' in r.keys() %} + {% set _ = ns.db_backup_paths.append(r['path']) %} + {% endif %} + {% endfor %} + {% endfor %} + {{ ns.db_backup_paths | from_yaml | unique }} + +- name: Create DB backup directories + ansible.builtin.file: + path: "{{ item }}" + 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: + label: "Creating DB backup directory {{ item }}" + +- name: Copy the litestream systemd service file + ansible.builtin.template: + src: litestream.service.j2 + dest: /etc/systemd/system/litestream.service + owner: root + group: root + mode: "0640" + notify: restart litestream + +- name: Create litestream config directory + ansible.builtin.file: + path: /etc/litestream + state: directory + owner: root + group: "{{ litestream_system_group }}" + mode: u+rwX,g+rwX,o+rX + +- name: Copy the litestream config file + ansible.builtin.copy: + dest: /etc/litestream/config.yml + owner: "{{ litestream_system_user }}" + group: "{{ litestream_system_group }}" + mode: "0640" + content: | + {{ litestream_config | to_nice_yaml | indent(2, true) }} + notify: restart litestream diff --git a/roles/litestream/tasks/install.yml b/roles/litestream/tasks/install.yml new file mode 100644 index 0000000..08a8555 --- /dev/null +++ b/roles/litestream/tasks/install.yml @@ -0,0 +1,67 @@ +--- +- name: Create the litestream group + ansible.builtin.group: + name: "{{ litestream_system_group }}" + state: present + system: true + when: litestream_system_group != "root" + +- name: Create the litestream user + ansible.builtin.user: + name: "{{ litestream_system_user }}" + groups: "{{ litestream_system_group }}" + append: true + shell: /usr/sbin/nologin + system: true + create_home: false + home: / + when: litestream_system_user != "root" + +- name: Get binary + when: + - litestream_binary_local_dir | length == 0 + - not litestream_skip_install + block: + - name: Download litestream binary to local folder + become: false + ansible.builtin.get_url: + url: "{{ litestream_binary_url }}" + dest: /tmp/litestream-v{{ litestream_version }}-linux-{{ go_arch }}.tar.gz + mode: "0644" + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + delegate_to: localhost + check_mode: false + + - name: Unpack litestream binary + become: false + ansible.builtin.unarchive: + src: /tmp/litestream-v{{ litestream_version }}-linux-{{ go_arch }}.tar.gz + dest: /tmp + creates: /tmp/litestream-v{{ litestream_version }}-linux-{{ go_arch }}/litestream + delegate_to: localhost + check_mode: false + + - name: Propagate litestream binaries + ansible.builtin.copy: + src: /tmp/litestream + dest: "{{ litestream_binary_install_dir }}/litestream" + mode: "0755" + owner: root + group: root + notify: restart litestream + when: not ansible_check_mode + +- name: Propagate locally distributed litestream binary + ansible.builtin.copy: + src: "{{ litestream_binary_local_dir }}/litestream" + dest: "{{ litestream_binary_install_dir }}/litestream" + mode: "0755" + owner: root + group: root + when: + - litestream_binary_local_dir | length > 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 {} }}" 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"