# HAProxy auto-update bundle

This bundle adds automatic HAProxy config update and reload when you add or remove PostgreSQL replicas.

## Files

- `scripts/cluster-node-haproxy.sh`

## What it does

- Adds a replica:
  - starts the DB stack on the new host
  - adds a `server <name> <name>:5432 check port 8008` line to HAProxy backend
  - validates the HAProxy config
  - restarts HAProxy

- Removes a replica:
  - stops Patroni on the old host
  - removes that backend `server` line from HAProxy
  - validates the HAProxy config
  - restarts HAProxy

## Assumptions

- `db1`, `db2`, `db3`, etc. and `proxy1` resolve via `/etc/hosts` or DNS
- proxy stack lives in `/opt/proxy-stack`
- DB stack lives in `/opt/db-stack`
- HAProxy config is `/opt/proxy-stack/haproxy.cfg`
- SSH access works from your admin machine

## Examples

Add a replica:

```bash
chmod +x scripts/cluster-node-haproxy.sh
./scripts/cluster-node-haproxy.sh add-replica db4 10.20.0.14 root@db4.example.com root@proxy1.example.com
```

Remove a replica:

```bash
./scripts/cluster-node-haproxy.sh remove-replica db4 root@db4.example.com root@proxy1.example.com
```

Show current backend block:

```bash
./scripts/cluster-node-haproxy.sh list-haproxy root@proxy1.example.com
```

## Notes

- This script only updates HAProxy for PostgreSQL backend membership.
- It does not modify etcd membership.
- It assumes your HAProxy backend uses hostnames like `db1`, `db2`, `db3`.
