---
#--------------------------------------------------------------#
# Setup pgpass & pg_service                        [env_patroni]
#--------------------------------------------------------------#
# write patroni ctl credential, only admin user can access
# if you want someone else to access patronictl, add them to the "admin" group
# e.g. sudo usermod -aG admin someuser vagrant
- name: write admin patronictl conf
  become: true
  tags: env_patroni
  template:
    src: env/patronictl.yml.j2
    dest: /infra/conf/patronictl.yml
    owner: root
    group: admin
    mode: '0640'


#--------------------------------------------------------------#
# Setup pgpass & pg_service                             [env_pg]
#--------------------------------------------------------------#
- name: setup postgres admin environment
  become: false  # write to admin user's home rather than root's
  tags: env_pg
  ignore_errors: true
  block:

    # generate pg pass
    - name: write postgres user credential to pgpass
      tags: env_pgpass
      lineinfile: dest=~/.pgpass state=present create=yes mode=0600 line={{ item }}
      with_items:
        - "*:*:*:{{ pg_replication_username }}:{{ pg_replication_password }}"
        - "*:*:*:{{ pg_monitor_username }}:{{ pg_monitor_password }}"
        - "*:*:*:{{ pg_admin_username }}:{{ pg_admin_password }}"
      no_log: true

    # generate pg_service list
    - name: write pg_service conf
      tags: env_pgscv
      template:
        src: env/pg_service.conf.j2
        dest: ~/.pg_service.conf
        mode: '0600'


#--------------------------------------------------------------#
# Setup pgadmin server list and pgpass             [env_pgadmin]
#--------------------------------------------------------------#
- name: setup pgadmin server list
  become: false  # write to admin user's home rather than root's
  tags: env_pgadmin
  ignore_errors: true
  block:

    # generate pg server list for pgadmin
    - name: write pg list servers.json
      become: true
      template:
        src: env/servers.j2
        dest: /infra/pgadmin/servers.json
        mode: '0600'
        owner: 5050
        group: 5050
      vars:
        servers_query: '[@.*][0][?pg_cluster && pg_cluster!=""].{pg_cluster: pg_cluster,pg_seq: pg_seq,pg_port: pg_port,pg_host: inventory_hostname,pg_admin_username:pg_admin_username,pg_admin_password: pg_admin_password}'

    # generate pg pass for pgadmin
    - name: write pgadmin pgpass file
      become: true
      copy:
        dest: /infra/pgadmin/pgpass
        mode: '0600'
        owner: 5050
        group: 5050
        content: |
          - "*:*:*:{{ pg_admin_username }}:{{ pg_admin_password }}"


#--------------------------------------------------------------#
# Setup environment variables                         [env_etcd]
#--------------------------------------------------------------#
- name: write etcd environment profile
  tags: env_etcd
  copy:
    dest: /etc/profile.d/etcdctl.sh
    mode: 0644
    content: |
      #!/bin/bash
      alias e="etcdctl"
      alias em="etcdctl member"
      {% if 'etcd' in groups %}
      export ETCDCTL_ENDPOINTS="{% for ip in groups['etcd']|sort %}{% if not loop.first %},{% endif %}https://{{ ip }}:{{ etcd_port }}{% endfor %}"
      {% endif %}
      export ETCDCTL_CACERT=/etc/pki/ca.crt
      export ETCDCTL_CERT=/etc/pki/infra.crt
      export ETCDCTL_KEY=/etc/pki/infra.key


#--------------------------------------------------------------#
# Setup environment variables                        [env_pglog]
#--------------------------------------------------------------#
- name: write pglog environment profile
  tags: env_pglog
  copy:
    dest: /etc/profile.d/pglog.sh
    mode: 0644
    content: |
      #!/bin/bash
      # pglog analysis shortcuts
      alias pglog="psql   service=meta -AXtwc 'TRUNCATE pglog.sample CASCADE; COPY pglog.sample14 FROM STDIN CSV;'"
      alias pglog12="psql service=meta -AXtwc 'TRUNCATE pglog.sample CASCADE; COPY pglog.sample12 FROM STDIN CSV;'"
      alias pglog13="psql service=meta -AXtwc 'TRUNCATE pglog.sample CASCADE; COPY pglog.sample13 FROM STDIN CSV;'"
      alias pglog14="psql service=meta -AXtwc 'TRUNCATE pglog.sample CASCADE; COPY pglog.sample14 FROM STDIN CSV;'"
      
      ### default: get pgsql csvlog (localhost)
      function catlog(){ # getlog <ip|host> <date:YYYY-MM-DD>
          local node=${1-'127.0.0.1'}
          ssh -t "${node}" "sudo cat /pg/log/postgres/postgresql-*.csv"
      }


#--------------------------------------------------------------#
# Setup environment variables                          [env_var]
#--------------------------------------------------------------#
- name: setup environment variables
  become: false
  tags: env_var
  ignore_errors: true
  block:

    # create ~/.pigsty bash profile
    - name: write environment file
      template:
        src: env/pigsty.j2
        dest: ~/.pigsty
        mode: 0700

    # load ~/.pigsty in ~/.bashrc
    - name: load environment vars
      lineinfile:
        dest: ~/.bashrc
        state: present
        create: yes
        mode: 0644
        line: '[ -f ~/.pigsty ] && . ~/.pigsty'

...