# yaml-language-server: $schema=https://schema.zeabur.app/template.json
apiVersion: zeabur.com/v1
kind: Template
metadata:
    name: Patroni4 (for PostgreSQL HA)
spec:
    description: |
        Add the 4th PostgreSQL node to expand your PostgreSQL HA cluster. Automatically joins as a replica.
    coverImage: https://raw.githubusercontent.com/canyugs/zeabur-template/refs/heads/main/postgresql-ha/cover.png
    icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/postgresql.svg
    variables:
        - key: POSTGRES_SUPERUSER_PASSWORD
          type: PASSWORD
          name: Superuser Password
          description: Password for PostgreSQL superuser (must match existing cluster)
        - key: POSTGRES_REPLICATION_PASSWORD
          type: PASSWORD
          name: Replication Password
          description: Password for PostgreSQL replication user (must match existing cluster)
        - key: POSTGRES_ADMIN_PASSWORD
          type: PASSWORD
          name: Admin Password
          description: Password for PostgreSQL admin user (must match existing cluster)
        - key: ETCD3_HOSTS
          type: STRING
          name: etcd Hosts
          description: etcd cluster endpoints (e.g., etcd1:2379,etcd2:2379,etcd3:2379)
    tags:
        - Database
        - PostgreSQL
        - High Availability
        - Patroni
    readme: |
        # Patroni4 (4th Node for PostgreSQL HA)

        ⚠️ **Use the same passwords as your existing cluster.**

        ## Deploy & Verify

        Deploy this template, then verify (run inside any patroni container):

        ```bash
        patronictl list pg-ha
        ```
    services:
        - name: patroni4
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/postgresql.svg
          template: PREBUILT_V2
          spec:
            source:
                image: ghcr.io/zalando/spilo-15:3.2-p1
                command:
                    - sh
                    - -c
                    - |
                      # Wrapper to fix ownership after basebackup
                      (
                        while sleep 10; do
                          if [ -d /home/postgres/pgdata/pgroot ]; then
                            chown -R postgres:postgres /home/postgres/pgdata/pgroot 2>/dev/null || true
                          fi
                        done
                      ) &
                      exec /launch.sh init
            ports:
                - id: database
                  port: 5432
                  type: TCP
                - id: api
                  port: 8008
                  type: HTTP
            volumes:
                - id: data
                  dir: /home/postgres/pgdata
            instructions:
                - title: Superuser Connection String
                  content: postgresql://postgres:${POSTGRES_SUPERUSER_PASSWORD}@${PORT_FORWARDED_HOSTNAME}:${DATABASE_PORT_FORWARDED_PORT}/postgres
                - title: Admin Connection String
                  content: postgresql://admin:${POSTGRES_ADMIN_PASSWORD}@${PORT_FORWARDED_HOSTNAME}:${DATABASE_PORT_FORWARDED_PORT}/postgres
                - title: Superuser Username
                  content: postgres
                - title: Superuser Password
                  content: ${POSTGRES_SUPERUSER_PASSWORD}
                - title: Admin Username
                  content: admin
                - title: Admin Password
                  content: ${POSTGRES_ADMIN_PASSWORD}
                - title: Cluster Status (run in container)
                  content: patronictl list pg-ha
            env:
                ETCD3_HOSTS:
                    default: ${ETCD3_HOSTS}
                PASSWORD:
                    default: ${POSTGRES_SUPERUSER_PASSWORD}
                PATRONI_NAME:
                    default: patroni4
                PATRONI_POSTGRESQL_CONNECT_ADDRESS:
                    default: ${CONTAINER_HOSTNAME}:5432
                PATRONI_POSTGRESQL_LISTEN:
                    default: 0.0.0.0:5432
                PATRONI_REPLICATION_PASSWORD:
                    default: ${POSTGRES_REPLICATION_PASSWORD}
                PATRONI_REPLICATION_USERNAME:
                    default: replicator
                PATRONI_RESTAPI_CONNECT_ADDRESS:
                    default: ${CONTAINER_HOSTNAME}:8008
                PATRONI_RESTAPI_LISTEN:
                    default: 0.0.0.0:8008
                PATRONI_SUPERUSER_PASSWORD:
                    default: ${POSTGRES_SUPERUSER_PASSWORD}
                PATRONI_SUPERUSER_USERNAME:
                    default: postgres
                PATRONI_admin_PASSWORD:
                    default: ${POSTGRES_ADMIN_PASSWORD}
                PATRONI_admin_USERNAME:
                    default: admin
                PGPASSWORD_ADMIN:
                    default: ${POSTGRES_ADMIN_PASSWORD}
                PGPASSWORD_STANDBY:
                    default: ${POSTGRES_REPLICATION_PASSWORD}
                PGPASSWORD_SUPERUSER:
                    default: ${POSTGRES_SUPERUSER_PASSWORD}
                POSTGRES_DB:
                    default: postgres
                POSTGRES_HOST:
                    default: ${CONTAINER_HOSTNAME}
                POSTGRES_PASSWORD:
                    default: ${POSTGRES_SUPERUSER_PASSWORD}
                POSTGRES_PORT:
                    default: ${DATABASE_PORT}
                POSTGRES_USER:
                    default: postgres
                SCOPE:
                    default: pg-ha
            healthCheck:
                type: HTTP
                port: api
                http:
                    path: /health
