apiVersion: v1
data:
  config.json: |
    {
        "command": "/usr/bin/mysqld_safe",
        "config_files": [
            {
                "source": "/var/lib/kolla/config_files/galera.cnf",
                "dest": "/etc//my.cnf",
                "owner": "mysql",
                "perm": "0600"
            },
            {
                "source": "/var/lib/kolla/config_files/wsrep-notify.sh",
                "dest": "/usr/local/bin/wsrep-notify.sh",
                "owner": "mysql",
                "perm": "0700"
            }
        ],
        "permissions": [
            {
                "path": "/var/log/kolla/mariadb",
                "owner": "mysql:mysql",
                "recurse": true
            },
            {
                "path": "/var/lib/mysql",
                "owner": "mysql:mysql",
                "recurse": true
            }
        ]
    }
  galera.cnf: |+
    [client]
    default-character-set = utf8

    [mysql]
    default-character-set = utf8

    [mysqld]
    bind-address = 0.0.0.0
    port = 3306
    log-error = /var/log/kolla/mariadb/mariadb.log
    binlog_format = ROW
    default-storage-engine = innodb
    innodb_autoinc_lock_mode = 2
    collation-server = utf8_unicode_ci
    init-connect = SET NAMES utf8
    character-set-server = utf8
    datadir = /var/lib/mysql/
    wsrep_cluster_address = gcomm://
    wsrep_provider_options = gmcast.listen_addr=tcp://0.0.0.0:4567;ist.recv_addr=0.0.0.0:4568
    wsrep_node_address = 0.0.0.0:4567
    wsrep_sst_receive_address = 0.0.0.0:4444
    wsrep_provider = none
    wsrep_cluster_name = openstack
    wsrep_node_name = borg
    wsrep_sst_method = xtrabackup-v2
    wsrep_sst_auth = root:ezKTank6WwMlosLvFGUHkJnFKdnrcXXG1LVFHdua
    wsrep_slave_threads = 4
    wsrep_notify_cmd = /usr/local/bin/wsrep-notify.sh
    max_connections = 10000
    key_buffer_size = 64M
    max_heap_table_size = 64M
    tmp_table_size = 64M
    innodb_buffer_pool_size = 8192M

    [server]
    pid-file = /var/lib/mysql/mariadb.pid

  wsrep-notify.sh: |
    #!/bin/bash -e

    # Edit parameters below to specify the address and login to server.
    USER=root
    PSWD=ezKTank6WwMlosLvFGUHkJnFKdnrcXXG1LVFHdua
    HOST=0.0.0.0
    PORT=3306
    LB_USER=haproxy

    ENABLE_LB="UPDATE mysql.user SET User='${LB_USER}' WHERE User='${LB_USER}_blocked';"
    DISABLE_LB="UPDATE mysql.user SET User='${LB_USER}_blocked' WHERE User='${LB_USER}';"
    MYSQL_CMD="`type -p mysql` -B -u$USER -p$PSWD -h$HOST -P$PORT"

    status_update()
    {
        echo "SET SESSION wsrep_on=off;"
        echo "$@"
        echo "FLUSH PRIVILEGES;"
    }

    get_sst_method()
    {
        $MYSQL_CMD -s -N -e "SHOW VARIABLES LIKE 'wsrep_sst_method';" | awk '{ print $2 }'
    }

    while [ $# -gt 0 ]
    do
        case $1 in
        --status)
            STATUS=$2
            shift
            ;;
        --uuid)
            CLUSTER_UUID=$2
            shift
            ;;
        --primary)
            [ "$2" = "yes" ] && PRIMARY="1" || PRIMARY="0"
            shift
            ;;
        --index)
            INDEX=$2
            shift
            ;;
        --members)
            MEMBERS=$2
            shift
            ;;
        esac
        shift
    done

    case $STATUS in
    Synced)
        CMD=$ENABLE_LB
        ;;
    Donor)
        # enabling donor only if xtrabackup configured
        SST_METHOD=`get_sst_method`
        [[ $SST_METHOD =~ 'xtrabackup' ]] && CMD=$ENABLE_LB || CMD=$DISABLE_LB
        ;;
    Undefined)
        # shutting down database: do nothing
        ;;
    *)
        CMD=$DISABLE_LB
        ;;
    esac

    if [ -n "$CMD" ]
    then
        status_update "$CMD" | $MYSQL_CMD
    fi

    exit 0
kind: ConfigMap
metadata:
  name: mariadb
