Skip to content

postgres: add support for restoring backups #125

postgres: add support for restoring backups

postgres: add support for restoring backups #125

Workflow file for this run

name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]
defaults:
run:
shell: bash
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install pre-commit
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pre-commit
ansible-galaxy collection install --force ansible.posix
ansible-galaxy collection install --force community.postgresql
- name: Run pre-commit
run: |
python -m pre_commit run --all-files --show-diff-on-failure
python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install pytest / Ruff
run: |
python -m pip install pytest ruff
- name: Run Python linter
run: |
ruff format --check
ruff check
- name: Run Python tests
run: |
pushd roles/postgres/files
PATH=$PWD:$PATH pytest -v .
popd
ansible:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Ansible
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade ansible
ansible-galaxy collection install --force ansible.posix
ansible-galaxy collection install --force community.postgresql
- name: Remove pre-baked PostgreSQL
run:
# Existing pre-baked PostgreSQL installation may conflict with the
# PostgreSQL installed by our playbook. We better remove it for a
# greater good.
sudo apt purge 'postgresql-*'
- name: Create block storage device (volume)
run: |
VOLUME_DEVICE="$(losetup -f)"
VOLUME_IMAGE="${{ runner.temp }}/diskimage"
dd if=/dev/zero of=$VOLUME_IMAGE bs=1M count=1024
sudo losetup $VOLUME_DEVICE $VOLUME_IMAGE
echo "uri=$VOLUME_DEVICE" >> $GITHUB_OUTPUT
id: volume-device
- name: Add server names to /etc/hosts
run: |
echo "127.0.0.1 xsnippet.local" | sudo tee -a /etc/hosts
echo "127.0.0.1 api.xsnippet.local" | sudo tee -a /etc/hosts
- name: Run the playbook
run: |
ansible-playbook \
-vvv \
-e volume_device="${{ steps.volume-device.outputs.uri }}" \
--inventory inventories/ci \
site.yml
ansible-restore:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Ansible
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade ansible
ansible-galaxy collection install --force ansible.posix
ansible-galaxy collection install --force community.postgresql
- name: Remove pre-baked PostgreSQL
run:
# Existing pre-baked PostgreSQL installation may conflict with the
# PostgreSQL installed by our playbook. We better remove it for a
# greater good.
sudo apt purge 'postgresql-*'
- name: Create block storage device (volume)
run: |
VOLUME_DEVICE="$(losetup -f)"
VOLUME_IMAGE="${{ runner.temp }}/diskimage"
dd if=/dev/zero of=$VOLUME_IMAGE bs=1M count=1024
sudo losetup $VOLUME_DEVICE $VOLUME_IMAGE
echo "uri=$VOLUME_DEVICE" >> $GITHUB_OUTPUT
id: volume-device
- name: Add server names to /etc/hosts
run: |
echo "127.0.0.1 xsnippet.local" | sudo tee -a /etc/hosts
echo "127.0.0.1 api.xsnippet.local" | sudo tee -a /etc/hosts
- name: Run the playbook
run: |
read -r -d '' extra_vars << 'EOF' || true
{
"volume_device": "${{ steps.volume-device.outputs.uri }}",
"postgres_users": [
{
"database": "{{ xsnippet_api_user }}",
"username": "{{ xsnippet_api_user }}",
"backup_schedule": "*-*-* 3:00:00",
"backup_restore": "testdata/xsnippet-api_20241003-030004.pgc"
}
]
}
EOF
ansible-playbook \
-vvv \
-e "${extra_vars}" \
--inventory inventories/ci \
site.yml
- name: Verify that the database backup has been restored correctly
run: |
test "$(curl http://127.0.0.1:8080/v1/snippets | jq length)" "20"