apiVersion: v1
kind: ConfigMap
metadata:
  name: middle-haproxy-cnf
  namespace: edoc2
data:
  haproxy.cfg_bak: |
    global
      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 5s
      timeout client 50s
      timeout server 50s
      timeout check 5s
    listen haproxy_database
        bind 0.0.0.0:3306
        maxconn 8000
        mode tcp
        timeout client 3600s
        timeout server 3600s
        option tcplog
        server mysqlserver0 mysql-mgra-0.mysql-mgra.edoc2.svc.cluster.local:3306 check port 3306 inter 5s rise 2 fall 2
        server mysqlserver1 mysql-mgrb-0.mysql-mgrb.edoc2.svc.cluster.local:3306 check port 3306 inter 5s rise 2 fall 2
        server mysqlserver2 mysql-mgrc-0.mysql-mgrc.edoc2.svc.cluster.local:3306 check port 3306 inter 5s rise 2 fall 2
    listen haproxy_es
        bind 0.0.0.0:9200
        maxconn 20000
        mode tcp
        timeout client 3600s
        timeout server 3600s
        option tcplog
        server es0 es-0.es.edoc2.svc.cluster.local:9200 check port 9200 inter 5s rise 2 fall 2
        server es1 es-0.es.edoc2.svc.cluster.local:9200 check port 9200 inter 5s rise 2 fall 2
        server es2 es-0.es.edoc2.svc.cluster.local:9200 check port 9200 inter 5s rise 2 fall 2
    listen haproxy_minio9000
        bind 0.0.0.0:9000
        maxconn 8000
        mode tcp
        timeout client 3600s
        timeout server 3600s
        option tcplog
        server minioc0 minio-0.minio.edoc2.svc.cluster.local:5000 check port 5000 inter 10s rise 2 fall 2
        server minioc1 minio-1.minio.edoc2.svc.cluster.local:5000 check port 5000 inter 10s rise 2 fall 2
        server minioc2 minio-2.minio.edoc2.svc.cluster.local:5000 check port 5000 inter 10s rise 2 fall 2
        server minioc3 minio-3.minio.edoc2.svc.cluster.local:5000 check port 5000 inter 10s rise 2 fall 2
    listen haproxy_minio5000
        bind 0.0.0.0:5000
        maxconn 8000
        mode tcp
        timeout client 3600s
        timeout server 3600s
        option tcplog
        server minio0 minio-0.minio.edoc2.svc.cluster.local:9000 check port 9000 inter 5s rise 2 fall 2
        server minio1 minio-1.minio.edoc2.svc.cluster.local:9000 check port 9000 inter 5s rise 2 fall 2
        server minio2 minio-2.minio.edoc2.svc.cluster.local:9000 check port 9000 inter 5s rise 2 fall 2
        server minio3 minio-3.minio.edoc2.svc.cluster.local:9000 check port 9000 inter 5s rise 2 fall 2
    listen haproxy_mq5672
        bind 0.0.0.0:5672
        maxconn 8000
        mode tcp
        timeout client 3600s
        timeout server 3600s
        option tcplog
        server rabbitmq0 rabbitmq-0.rabbitmq.edoc2.svc.cluster.local:5672 check port 5672 inter 15s rise 2 fall 2
        server rabbitmq1 rabbitmq-1.rabbitmq.edoc2.svc.cluster.local:5672 check port 5672 inter 15s rise 2 fall 2
        server rabbitmq2 rabbitmq-2.rabbitmq.edoc2.svc.cluster.local:5672 check port 5672 inter 15s rise 2 fall 2
    listen haproxy_mq15672
        bind 0.0.0.0:15672
        maxconn 8000
        mode tcp
        timeout client 3600s
        timeout server 3600s
        option tcplog
        server rabbitmqw0 rabbitmq-0.rabbitmq.edoc2.svc.cluster.local:15672 check port 15672 inter 15s rise 2 fall 2
        server rabbitmqw1 rabbitmq-1.rabbitmq.edoc2.svc.cluster.local:15672 check port 15672 inter 15s rise 2 fall 2
        server rabbitmqw2 rabbitmq-2.rabbitmq.edoc2.svc.cluster.local:15672 check port 15672 inter 15s 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
  backend_check.sh: |
    # haproxy_config.sh
    HAPROXY_CONFIG='/usr/local/etc/haproxy/haproxy.cfg'
    mysql0="mysql-mgra-0.mysql-mgra.edoc2.svc.cluster.local:3306"
    mysql1="mysql-mgrb-0.mysql-mgrb.edoc2.svc.cluster.local:3306"
    mysql2="mysql-mgrc-0.mysql-mgrc.edoc2.svc.cluster.local:3306"
    MYSQL_MEM=($mysql0 $mysql1 $mysql2)
    MD5_FILE=/opt/haproxy_config.md5
    PID_FILE=/var/run/haproxy.pid
    exec_sql="select * from performance_schema.replication_group_members;"
    MYSQL_REPL_USER=`echo $HAPROXY_REPL_USER`
    MYSQL_REPL_PASSWORD=`echo ${HAPROXY_REPL_PASSWORD}`
    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 "${rest_info}"
    }
    change_conf() {
        if [ "${mysql_status1}" == "ONLINE" ] && [ "${mysql_status2}" == "PRIMARY" ] && [ ${mysql_slaves} -ge 2 ];then
            if [ $str_status == "t" ];then
                sed -i "s/${rest_info}/${new_conf}/g" "${HAPROXY_CONFIG}"
            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}"
            fi
        fi
    }
    get_status() {
        mysql_status1=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${HOSTNAME%:*}" | awk -F " " '{print $5}'`
        mysql_status2=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}" | grep "${HOSTNAME%:*}" | awk -F " " '{print $6}'`
        mysql_slaves=`mysql -u$MYSQL_REPL_USER -p$MYSQL_REPL_PASSWORD -h${HOSTNAME%:*} -P${HOSTNAME#*:} -e "${exec_sql}"  | awk '{print $5}'|grep -E "ONLINE|RECOVERING" | wc -l`
    }
    while true
    do
        for HOSTNAME in ${MYSQL_MEM[@]};do
            echo "$HOSTNAME"
            echo ${HOSTNAME%:*}
            echo ${HOSTNAME#*:}
            if [ "${HOSTNAME}" == "mysql-mgra-0.mysql-mgra.edoc2.svc.cluster.local:3306" ];then
                get_status
                check_status
                new_conf="server mysqlserver0"
                change_conf
            elif [ "${HOSTNAME}" == "mysql-mgrb-0.mysql-mgrb.edoc2.svc.cluster.local:3306" ];then
                get_status
                check_status
                new_conf="server mysqlserver1"
                change_conf
            elif [ "${HOSTNAME}" == "mysql-mgrc-0.mysql-mgrc.edoc2.svc.cluster.local:3306" ];then
                get_status
                check_status
                new_conf="server mysqlserver2"
                change_conf
            fi
            echo ${mysql_status1}
            echo ${mysql_status2}
        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
