Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions roles/litestream/README.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions roles/litestream/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -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 ...
```
13 changes: 13 additions & 0 deletions roles/litestream/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
10 changes: 10 additions & 0 deletions roles/litestream/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions roles/litestream/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions roles/litestream/meta/main.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions roles/litestream/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions roles/litestream/molecule/alternative/prepare.yml
Original file line number Diff line number Diff line change
@@ -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"
72 changes: 72 additions & 0 deletions roles/litestream/molecule/alternative/tests/test_alternative.py
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions roles/litestream/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
provisioner:
inventory:
group_vars:
all:
litestream_config:
dbs:
- path: /var/lib/db/sample.db
replicas:
- path: /var/lib/backup
34 changes: 34 additions & 0 deletions roles/litestream/molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -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"
Binary file added roles/litestream/molecule/default/sample.db
Binary file not shown.
Loading