kind: ConfigMap
apiVersion: v1
metadata:
  name: pgsql-config
  namespace: edoc2
  labels:
    addonmanager.kubernetes.io/mode: Reconcile
data:
  patroni01.sh: |
    #!/bin/bash
    sed_pgsql() {
        sed -i "s/bip_poll/$BIP_POOL/g" /opt/pg_hba.conf
        sed -i "s/hostname/$MY_POD_IP/g" /opt/patroni.yml
        sed -i "s/etcd_ip01/$ETCD_PodIP01/g" /opt/patroni.yml
        sed -i "s/etcd_ip02/$ETCD_PodIP02/g" /opt/patroni.yml
        sed -i "s/etcd_ip03/$ETCD_PodIP03/g" /opt/patroni.yml
        sed -i "s/pgsql_node/$PG_NODE_NAME/g" /opt/patroni.yml
        \cp /opt/patroni.yml /etc/patroni/
        sed -i "s/pgsql0_ip/$PGSQL_IP01/g" /opt/hosts
        sed -i "s/pgsql1_ip/$PGSQL_IP02/g" /opt/hosts
        sed -i "s/pgsql2_ip/$PGSQL_IP03/g" /opt/hosts
        \cp /opt/hosts /etc/hosts
    }
    case $1 in
    start)
        sed_pgsql
        sleep 3
        pgsql1_status=`curl -s http://pgsql1:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        pgsql2_status=`curl -s http://pgsql2:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        pgsql3_status=`curl -s http://pgsql3:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        sleep 3
        if [ ! -f "/home/postgres/data/pg_hba.conf" ]; then
            if [ ${pgsql2_status} == "master" ]; then
                su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql1 -U replica -D /home/postgres/data/ -P -v"
            elif [ ${pgsql3_status} == "master" ]; then
                su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql2 -U replica -D /home/postgres/data/ -P -v"
            else
                su - postgres -c "/usr/pgsql-13/bin/initdb  -D /home/postgres/data/ -E UTF8"
                sleep 1
                \cp /opt/pg_hba.conf /home/postgres/data/ && \cp /opt/postgresql.conf /home/postgres/data/
                \cp /opt/pgsql.sh /home/postgres/data/
                chown -R postgres:postgres /home/postgres/data
                chmod -R 644 /home/postgres/data/{pg_hba.conf,postgresql.conf}
                su - postgres -c "cd /home/postgres/data && ./pgsql.sh start"
                sleep 3
                su - postgres -c "psql -c \"ALTER USER postgres WITH ENCRYPTED PASSWORD 'edoc2@123';\""
                su - postgres -c "psql -c \"\du\""
                su - postgres -c "psql -c \"\q\""
                su - postgres -c "psql -c \"CREATE USER replica  WITH REPLICATION PASSWORD 'edoc2@123';\""
                su - postgres -c "psql -c \"\du\""
                su - postgres -c "psql -c \"\q\""
                su - postgres -c "psql -c \"SELECT * FROM pg_replication_slots;\""
                su - postgres -c "psql -c \"SELECT * FROM pg_create_physical_replication_slot('pgsql_slot1');\""
                su - postgres -c "psql -c \"SELECT * FROM pg_create_physical_replication_slot('pgsql_slot2');\""
                su - postgres -c "psql -c \"SELECT * FROM pg_create_physical_replication_slot('pgsql_slot3');\""
                su - postgres -c "psql -c \"SELECT * FROM pg_replication_slots;\""
                su - postgres -c "psql -c \"CREATE EXTENSION vector;\""
                su - postgres -c "psql -c \"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));\""
                su - postgres -c "psql -c \"INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');\""
                su - postgres -c "psql -c \"SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;\""
                su - postgres -c "psql -c \"SELECT extversion FROM pg_extension WHERE extname = 'vector';\""
            fi
        else
            sleep 1
        fi
        chown -R postgres:postgres /etc/patroni/ && chmod -R 755 /etc/patroni/patroni.yml
        sleep 1
        /usr/sbin/watchdog
        mkdir -p /dev/watchdog && chown -R postgres:postgres /dev/watchdog
        modprobe softdog &
        chown postgres /dev/watchdog
        su - postgres -c "/usr/local/bin/patroni /etc/patroni/patroni.yml"
        ;;
    stop)
        su - postgres -c "cd /home/postgres/data && ./pgsql.sh stop"
        ;;
    restart)
        sed_pgsql
        sleep 3
        su - postgres -c "cd /home/postgres/data && ./pgsql.sh stop"
        if [ ! -f "/home/postgres/data/pg_hba.conf" ]; then
            sleep 1
        else
            su - postgres -c "cd /home/postgres/data && ./pgsql.sh start"
        fi
        chown -R postgres:postgres /etc/patroni/ && chmod -R 755 /etc/patroni/patroni.yml
        sleep 1
        /usr/sbin/watchdog
        mkdir -p /dev/watchdog && chown -R postgres:postgres /dev/watchdog
        modprobe softdog &
        chown postgres /dev/watchdog
        su - postgres -c "/usr/local/bin/patroni /etc/patroni/patroni.yml"
        ;;
    *)
        echo "useage:$0 [start, stop, restart]";;
    esac
  patroni02.sh: |
    #!/bin/bash
    sleep 3
    sed_pgsql() {
        sed -i "s/bip_poll/$BIP_POOL/g" /opt/pg_hba.conf
        sed -i "s/hostname/$MY_POD_IP/g" /opt/patroni.yml
        sed -i "s/etcd_ip01/$ETCD_PodIP01/g" /opt/patroni.yml
        sed -i "s/etcd_ip02/$ETCD_PodIP02/g" /opt/patroni.yml
        sed -i "s/etcd_ip03/$ETCD_PodIP03/g" /opt/patroni.yml
        sed -i "s/pgsql_node/$PG_NODE_NAME/g" /opt/patroni.yml
        \cp /opt/patroni.yml /etc/patroni/
        sed -i "s/pgsql0_ip/$PGSQL_IP01/g" /opt/hosts
        sed -i "s/pgsql1_ip/$PGSQL_IP02/g" /opt/hosts
        sed -i "s/pgsql2_ip/$PGSQL_IP03/g" /opt/hosts
        \cp /opt/hosts /etc/hosts
    }
    sleep 3
    case $1 in
    start)
        sed_pgsql
        sleep 3
        pgsql1_status=`curl -s http://pgsql1:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        pgsql2_status=`curl -s http://pgsql2:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        pgsql3_status=`curl -s http://pgsql3:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        sleep 3
        chown -R postgres:postgres /home/postgres/ && chmod -R 700 /home/postgres/
        if [ ! -f "/home/postgres/data/pg_hba.conf" ]; then
            sleep 25
            if [ ${pgsql1_status} == "master" ]; then
                su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql0 -U replica -D /home/postgres/data/ -P -v"
            fi
            if [ ${pgsql3_status} == "master" ]; then
                su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql2 -U replica -D /home/postgres/data/ -P -v"
            fi
        else
            sleep 1
        fi
        chown -R postgres:postgres /etc/patroni/ && chmod -R 755 /etc/patroni/patroni.yml
        sleep 1
        /usr/sbin/watchdog
        mkdir -p /dev/watchdog && chown -R postgres:postgres /dev/watchdog
        modprobe softdog &
        chown postgres /dev/watchdog
        #chown -R postgres:postgres /home/postgres/ && chmod -R 700 /home/postgres/
        sleep 1
        su - postgres -c "/usr/local/bin/patroni /etc/patroni/patroni.yml"
        ;;
    stop)
        /usr/pgsql-13/bin/pg_ctl -D /home/postgres/data/ stop
        ;;
    restart)
        sed_pgsql
        sleep 3
        if [ ! -f "/home/postgres/data/pg_hba.conf" ]; then
            su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql0 -U replica -D /home/postgres/data/ -P -v"
        else
            sleep 1
        fi
        chown -R postgres:postgres /etc/patroni/ && chmod -R 755 /etc/patroni/patroni.yml
        sleep 1
        /usr/sbin/watchdog
        mkdir -p /dev/watchdog && chown -R postgres:postgres /dev/watchdog
        modprobe softdog &
        chown postgres /dev/watchdog
        su - postgres -c "/usr/local/bin/patroni /etc/patroni/patroni.yml"
        ;;
    *)
        echo "useage:$0 [start, stop, restart]";;
    esac
  patroni03.sh: |
    #!/bin/bash
    sleep 3
    sed_pgsql() {
        sed -i "s/bip_poll/$BIP_POOL/g" /opt/pg_hba.conf
        sed -i "s/hostname/$MY_POD_IP/g" /opt/patroni.yml
        sed -i "s/etcd_ip01/$ETCD_PodIP01/g" /opt/patroni.yml
        sed -i "s/etcd_ip02/$ETCD_PodIP02/g" /opt/patroni.yml
        sed -i "s/etcd_ip03/$ETCD_PodIP03/g" /opt/patroni.yml
        sed -i "s/pgsql_node/$PG_NODE_NAME/g" /opt/patroni.yml
        \cp /opt/patroni.yml /etc/patroni/
        sed -i "s/pgsql0_ip/$PGSQL_IP01/g" /opt/hosts
        sed -i "s/pgsql1_ip/$PGSQL_IP02/g" /opt/hosts
        sed -i "s/pgsql2_ip/$PGSQL_IP03/g" /opt/hosts
        \cp /opt/hosts /etc/hosts
    }
    sleep 3
    case $1 in
    start)
        sed_pgsql
        sleep 3
        pgsql1_status=`curl -s http://pgsql1:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        pgsql2_status=`curl -s http://pgsql2:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        pgsql3_status=`curl -s http://pgsql3:8008/patroni | jq | grep role | awk -F '"' '{print $4}'`
        sleep 3
        chown -R postgres:postgres /home/postgres/ && chmod -R 700 /home/postgres/
        if [ ! -f "/home/postgres/data/pg_hba.conf" ]; then
            sleep 25
            if [ ${pgsql1_status} == "master" ]; then
                su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql0 -U replica -D /home/postgres/data/ -P -v"
            fi
            if [ ${pgsql2_status} == "master" ]; then
                su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql1 -U replica -D /home/postgres/data/ -P -v"
            fi
        else
            sleep 1
        fi
        chown -R postgres:postgres /etc/patroni/ && chmod -R 755 /etc/patroni/patroni.yml
        sleep 1
        /usr/sbin/watchdog
        mkdir -p /dev/watchdog && chown -R postgres:postgres /dev/watchdog
        modprobe softdog &
        chown postgres /dev/watchdog
        #chown -R postgres:postgres /home/postgres/ && chmod -R 700 /home/postgres/
        sleep 1
        su - postgres -c "/usr/local/bin/patroni /etc/patroni/patroni.yml"
        ;;
    stop)
        /usr/pgsql-13/bin/pg_ctl -D /home/postgres/data/ stop
        ;;
    restart)
        sed_pgsql
        sleep 3
        if [ ! -f "/home/postgres/data/pg_hba.conf" ]; then
            su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql0 -U replica -D /home/postgres/data/ -P -v"
        else
            sleep 1
        fi
        chown -R postgres:postgres /etc/patroni/ && chmod -R 755 /etc/patroni/patroni.yml
        sleep 1
        /usr/sbin/watchdog
        mkdir -p /dev/watchdog && chown -R postgres:postgres /dev/watchdog
        modprobe softdog &
        chown postgres /dev/watchdog
        su - postgres -c "/usr/local/bin/patroni /etc/patroni/patroni.yml"
        ;;
    *)
        echo "useage:$0 [start, stop, restart]";;
    esac
  pgbackup.sh: |
    #!/bin/bash
    sleep 300
    local_hostname=`hostname`
    back_time=`date "+%H"`
    get_status() {
        pgsql_status0=`curl -s http://pgsql0:8008/patroni | jq | grep role | awk -F "\"" '{print $4}'`
        pgsql_status1=`curl -s http://pgsql1:8008/patroni | jq | grep role | awk -F "\"" '{print $4}'`
        pgsql_status2=`curl -s http://pgsql2:8008/patroni | jq | grep role | awk -F "\"" '{print $4}'`
    }
    exec_pgback() {
        mkdir -p /home/postgres/backup/ && chown -R postgres:postgres /home/postgres/backup && chmod -R 700 /home/postgres/backup
        rm -rf /home/postgres/backup/*
        su - postgres -c "/usr/pgsql-13/bin/pg_basebackup -h pgsql0 -U replica -D /home/postgres/backup/ -P -v"
    }
    while true
    do  
        get_status
        echo ${pgsql_status0}
        echo ${pgsql_status1}
        echo ${pgsql_status1}
        if [ "${back_time}" == "02" ];then
            sleep 5
            echo "${local_hostname}"
            if [ "${local_hostname}" == "pgsql0" ] && [ "${pgsql_status0}" == "master" ];then
                exec_pgback 
            elif [ "${local_hostname}" == "pgsql1" ] && [ "${pgsql_status1}" == "master" ];then
                exec_pgback
            elif [ "${local_hostname}" == "pgsql2" ] && [ "${pgsql_status2}" == "master" ];then
                exec_pgback
            fi
            sleep 3000
        fi
        sleep 300
    done
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: proxy-config
  namespace: edoc2
  labels:
    addonmanager.kubernetes.io/mode: Reconcile
data:
  haproxy.sh: |
    #!/bin/bash
    \cp /opt/haproxy.cfg /usr/local/etc/haproxy/
    sleep 3
    HAPROXY_CONFIG='/usr/local/etc/haproxy/haproxy.cfg'
    pgsql0="server pgserver0 pgsql0:5432 check port 5432 inter 2s rise 2 fall 2"
    pgsql1="server pgserver1 pgsql1:5432 check port 5432 inter 2s rise 2 fall 2"
    pgsql2="server pgserver2 pgsql2:5432 check port 5432 inter 2s rise 2 fall 2"
    PGSQL_MEM=($pgsql0 $pgsql1 $pgsql2)
    MD5_FILE=/opt/haproxy_config.md5
    PID_FILE=/var/run/haproxy.pid
    subStr="#"
    check_status() {
        info=`cat ${HAPROXY_CONFIG} | grep "${HOSTNAME}"`
        if [[ ${info} == *${subStr}* ]];then
            str_status="t"
            echo "incloud #"
        else
            str_status="f"
            echo "don't incloud #"
        fi
        rest_info=`cat ${HAPROXY_CONFIG} | grep "${HOSTNAME}" | awk -F " " '{print$1" "$2}'`
        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        echo "${rest_info}"
    }
    change_conf() {
        if [ "${pgsql_status}" == "master" ];then
            if [ $str_status == "t" ];then
                sed -i "s/${rest_info}/${new_conf}/g" "${HAPROXY_CONFIG}"
                echo ${rest_info}
                echo ${new_conf}
            elif [ $str_status == "f" ];then
                echo "nothing to do"
            fi
        else
            if [ $str_status == "t" ];then
                echo "nothing to do"            
            elif [ $str_status == "f" ];then
                sed -i "s/${rest_info}/#${new_conf}/g" "${HAPROXY_CONFIG}"
                echo ${rest_info}
                echo "#"${new_conf}
            fi
        fi
    }
    get_status() {
        pgsql_status0=`curl -s http://pgsql0:8008/patroni | jq | grep role | awk -F "\"" '{print $4}'`
        pgsql_status1=`curl -s http://pgsql1:8008/patroni | jq | grep role | awk -F "\"" '{print $4}'`
        pgsql_status2=`curl -s http://pgsql2:8008/patroni | jq | grep role | awk -F "\"" '{print $4}'`
    }
    while true
    do
        for HOSTNAME in ${PGSQL_MEM[@]};do
            if [ "${HOSTNAME}" == "pgsql0:5432" ];then
                echo "$HOSTNAME"
                get_status
                echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                echo ${pgsql_status0}
                echo ${pgsql_status1}
                echo ${pgsql_status1}
                check_status
                new_conf="server pgserver0"
                pgsql_status="${pgsql_status0}"
                change_conf
            elif [ "${HOSTNAME}" == "pgsql1:5432" ];then
                echo "$HOSTNAME"
                get_status
                echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                echo ${pgsql_status0}
                echo ${pgsql_status1}
                echo ${pgsql_status1}
                check_status
                new_conf="server pgserver1"
                pgsql_status="${pgsql_status1}"
                change_conf
            elif [ "${HOSTNAME}" == "pgsql2:5432" ];then
                echo "$HOSTNAME"
                get_status
                echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                echo ${pgsql_status0}
                echo ${pgsql_status1}
                echo ${pgsql_status1}
                check_status
                new_conf="server pgserver2"
                pgsql_status="${pgsql_status2}"
                change_conf
            fi
        done
        if [ ! -f ${PID_FILE} ];then
            echo "start haproxy"
            /usr/bin/md5sum ${HAPROXY_CONFIG}|cut -f1 -d ' ' > ${MD5_FILE}
            haproxy -W -db -f ${HAPROXY_CONFIG} -p ${PID_FILE} &
        else
            if [ "$(/usr/bin/md5sum ${HAPROXY_CONFIG}|cut -f1 -d ' ')" != "$(cat ${MD5_FILE})" ];then
                /usr/bin/md5sum ${HAPROXY_CONFIG}|cut -f1 -d ' ' > ${MD5_FILE}
                echo "reload haproxy"
                haproxy -W -db -f ${HAPROXY_CONFIG} -st $(cat ${PID_FILE}) -p ${PID_FILE} &
            fi
        fi
        sleep 5
    done
  haproxy.cfg: |
    global
      nbproc 8
      maxconn 8000
      log 127.0.0.1 local0 info
      user root
      group root
      stats socket /run/haproxy/admin.sock mode 660 level admin
    defaults
      mode tcp
      log global
      retries 3
      timeout connect 60s
      timeout client 600s
      timeout server 600s
      timeout check 30s
      default-server init-addr last,libc,none
    listen haproxy_pgsql
        bind 0.0.0.0:5432
        maxconn 8000
        mode tcp
        timeout client 7200s
        timeout server 7200s
        option tcplog
        server pgserver0 pgsql0:5432 check port 5432 inter 2s rise 2 fall 2
        server pgserver1 pgsql1:5432 check port 5432 inter 2s rise 2 fall 2
        server pgserver2 pgsql2:5432 check port 5432 inter 2s rise 2 fall 2
    listen admin_status
        mode http
        bind 0.0.0.0:8998
        stats enable
        stats refresh 5s
        stats hide-version
        stats realm Haproxy\ Statistics
        stats uri /admin-status
        stats auth admin:edoc2
        stats admin if TRUE    
  limits.conf: |
    * soft nofile 655360
    * hard nofile 312720
    * soft nproc 655355
    * hard nproc 655355
    * soft memlock unlimited
    * hard memlock unlimited
