Skip to content

Commit 3a8edf5

Browse files
authored
Merge pull request #49 from dimagi/nh/refactor
Refactor Ansible for CommCare Data Pipeline
2 parents 8cee7ed + 8fd05f4 commit 3a8edf5

73 files changed

Lines changed: 1288 additions & 619 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.ansible-lint

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
profile: production
3+
4+
exclude_paths:
5+
- .venv/
6+
7+
warn_list:
8+
- experimental
9+
10+
# skip_list entries require a justification comment
11+
skip_list:
12+
# These variables are intentionally shared across multiple roles as project-level
13+
# variables — renaming them to per-role prefixes would be a backwards-incompatible
14+
# change affecting all inventories.
15+
- var-naming[no-role-prefix]

.github/workflows/lint.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
name: Lint
3+
4+
on:
5+
push:
6+
branches: ["**"]
7+
pull_request:
8+
branches: ["**"]
9+
jobs:
10+
ansible-lint:
11+
name: ansible-lint
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: "3.11"
22+
23+
- name: Install dependencies
24+
run: |
25+
pip install ansible-dev-tools
26+
ansible-galaxy collection install -r requirements.yml
27+
28+
- name: Run ansible-lint
29+
run: ansible-lint
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
name: Molecule Test
3+
4+
on:
5+
push:
6+
branches: [master, nh/refactor]
7+
pull_request:
8+
branches: [master, nh/refactor]
9+
jobs:
10+
test:
11+
name: Molecule Test
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
role: [postgres, redis, nginx]
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Python
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: "3.11"
24+
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install ansible molecule molecule-docker docker
29+
ansible-galaxy collection install -r requirements.yml
30+
31+
- name: Run Molecule test
32+
run: |
33+
cd roles/${{ matrix.role }}
34+
molecule test
35+
env:
36+
PY_COLORS: "1"
37+
ANSIBLE_FORCE_COLOR: "1"

.readthedocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
23

34
version: 2

CLAUDE.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CommCare Sync Ansible
2+
3+
Ansible playbooks and roles for deploying [CommCare Data Pipeline](https://github.com/dimagi/commcare-sync).
4+
5+
## Setup
6+
7+
```bash
8+
source .venv/bin/activate
9+
```
10+
11+
## Commands
12+
13+
```bash
14+
# Lint
15+
ansible-lint
16+
17+
# Run Molecule tests for a role (requires Docker)
18+
cd roles/<role>
19+
molecule test
20+
21+
# Run playbook
22+
ansible-playbook -i inventories/<env> commcare_sync.yml
23+
```
24+
25+
## Structure
26+
27+
- `roles/` — Ansible roles (`commcare_sync`, `django`, `nginx`, `postgres`, `redis`, `superset`)
28+
- `inventories/` — Per-environment inventory files
29+
- `requirements.yml` — Ansible Galaxy role/collection dependencies
30+
- `requirements-dev.txt` — Python dev tools (`ansible-dev-tools`, `molecule-docker`)
31+
32+
## Molecule tests
33+
34+
Each role has tests in `roles/<role>/molecule/default/`. Tests use Docker (Ubuntu 24.04). Run from the role directory.

Vagrantfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
1515
config.vm.define "commcare_sync", primary: true do |obj|
1616
# Every Vagrant development environment requires a box. You can search for
1717
# boxes at https://atlas.hashicorp.com/search.
18-
obj.vm.box = "bento/ubuntu-18.04"
18+
obj.vm.box = "bento/ubuntu-24.04"
1919
obj.vm.hostname = "dev-commcare-sync"
2020
# obj.vm.boot_timeout = 600
2121
obj.vm.network "private_network", ip: "192.168.11.10"

commcare_sync.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
---
22
# Issue: https://github.com/ansible/ansible/issues/57529#issuecomment-513143430
3-
- hosts: all
3+
- name: Gather facts
4+
hosts: all
45

56
- name: Create CommCare Data Pipeline server.
67
hosts: all
7-
become: yes
8+
become: true
89
become_user: root
910

10-
gather_facts: no
11-
11+
gather_facts: false
1212

1313
roles:
14+
- postgres
15+
- redis
16+
- nginx
17+
- role: superset
18+
when: superset_enabled
19+
- django
1420
- commcare_sync
15-
16-
...

docs/development.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,44 @@ This should download and deploy the latest CommCare Data Pipeline code to a loca
3131

3232
If everything is successful you can load [http://192.168.11.10/](http://192.168.11.10/) in a browser
3333
and get going!
34+
35+
## Running tests with Molecule
36+
37+
Individual roles can be tested in isolation using
38+
[Molecule](https://ansible.readthedocs.io/projects/molecule/), which
39+
provisions a Docker container and runs the role against it. Tests
40+
currently exist for the `postgres`, `redis`, and `nginx` roles.
41+
42+
### Install Molecule and dependencies
43+
44+
```bash
45+
uv venv
46+
source .venv/bin/activate
47+
uv pip install -r requirements-dev.txt
48+
```
49+
50+
You will also need [Docker](https://docs.docker.com/get-docker/)
51+
installed and running.
52+
53+
### Run tests
54+
55+
To test a single role:
56+
57+
```bash
58+
cd roles/postgres
59+
molecule test
60+
```
61+
62+
This will create an Ubuntu 24.04 Docker container, run the role, execute
63+
the verification checks, and tear down the container.
64+
65+
To test all roles that have Molecule scenarios:
66+
67+
```bash
68+
for role in postgres redis nginx; do
69+
(cd roles/$role && molecule test)
70+
done
71+
```
72+
73+
Tests also run automatically on push and pull request via the GitHub
74+
Actions workflow in `.github/workflows/molecule-test.yml`.

docs/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ createdb -U commcare_sync -h localhost -p 5432 superset
4747
```
4848

4949
If you get errors about active connections when dropping databases try stopping all processes
50-
with `supervisorctl stop all` and killing any other active connections in a `psql` shell with something like the below:
50+
by stopping all services with `sudo systemctl stop gunicorn celery superset` and killing any other active connections in a `psql` shell with something like the below:
5151

5252
```
5353
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'superset' AND pid <> pg_backend_pid();

docs/system-administration.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Services
1515
--------
1616

1717
The complete list of services is available in the
18-
[roles/commcare_sync/tasks/main.yml](https://github.com/dimagi/commcare-sync-ansible/blob/master/roles/commcare_sync/tasks/main.yml)
19-
file.
18+
[commcare_sync.yml](https://github.com/dimagi/commcare-sync-ansible/blob/master/commcare_sync.yml)
19+
playbook and its individual roles under `roles/`.
2020

2121
The most important ones are summarized on the
2222
[What's Installed](whats-installed.md) page.
@@ -115,12 +115,12 @@ environment is in `~ansible/www/.virtualenvs/superset`.
115115

116116
### Other Useful Commands
117117

118-
| Task | Command |
119-
|----------------------------------|---------------------------------------------|
120-
| See running Supervisor processes | `sudo supervisorctl status` |
121-
| Restart a Supervisor process | `sudo supervisorctl restart [process name]` |
122-
| Connect to the local database | `sudo -u postgres psql` |
123-
| Restart nginx | `sudo service nginx restart` |
118+
| Task | Command |
119+
|--------------------------------|-----------------------------------------------|
120+
| See running services | `sudo systemctl status gunicorn celery superset` |
121+
| Restart a service | `sudo systemctl restart [service name]` |
122+
| Connect to the local database | `sudo -u postgres psql` |
123+
| Restart nginx | `sudo systemctl restart nginx` |
124124

125125

126126
### Checking for Stuck Exports

0 commit comments

Comments
 (0)