# HG changeset patch
# User Marian Marinov <[email protected]>
# Date 1267196151 -7200
# Branch mysql-ms
# Node ID dc0dad7b935739987478e876d431e068f7ca1373
# Parent  2da5a4ff4b7c17899376fc69a68dfc6b1d28c140
Medium: RA: mysql: removed all bashimisms

All regular expressions are now used with grep
All arrays are replaced by while|read
Removed two pointles checks of $? for the mysql cli

diff -r 2da5a4ff4b7c -r dc0dad7b9357 heartbeat/mysql
--- a/heartbeat/mysql   Fri Feb 26 16:04:03 2010 +0200
+++ b/heartbeat/mysql   Fri Feb 26 16:55:51 2010 +0200
@@ -327,38 +327,36 @@
 }
 
 is_slave() {
-       slave_info=($(mysql \
-               --user=$OCF_RESKEY_replication_user \
-               --password=$OCF_RESKEY_replication_passwd \
-               --socket=$OCF_RESKEY_socket -O connect_timeout=1 \
-               -e 'SHOW SLAVE STATUS\G'|awk '/Running/ || /Master_[UHP]/{print 
$2}'))
+    master_host=''
+    master_user=''
+    master_port=''
+    slave_sql=''
+    slave_io=''
+    mysql \
+        --socket=$OCF_RESKEY_socket -O connect_timeout=1 \
+        -e 'SHOW SLAVE STATUS\G'|awk '/Running/ || /Master_[UHP]/{print 
$2}'|while read k v; do
+        if [ "$a" == 'Master_Host:' ]; then master_host="$v"; fi
+        if [ "$a" == 'Master_User:' ]; then master_user="$v"; fi
+        if [ "$a" == 'Master_Port:' ]; then master_port="$v"; fi
+        if [ "$a" == 'Slave_IO_Running:' ]; then slave_io="$v"; fi
+        if [ "$a" == 'Slave_SQL_Running:' ]; then slave_sql="$v"; fi
+    done
 
-       if [ "$?" != 0 ]; then
-               ocf_log err "Unable to get local slave status"
-               return 1
-       fi
-
-       if [ -z "${slave_info[*]}" ]; then
-               # no slave configuration, can not be slave
-               return 1;
-       fi
-
-       if [ -z "${slave_info[3]}" ] || [ -z "${slave_info[4]}" ] || [ -z 
"${slave_info[0]}" ] || [ -z "${slave_info[0]}" ]; then
-               ocf_log err "Unable to get slave status"
-               return 1
-       fi
+    if [ -z "$master_host" ] || [ -z "$master_user" ] || [ -z "$master_port" ] 
|| [ -z "$slave_io" ] || [ -z "$slave_sql" ]; then
+        ocf_log err "unable to get slave status value"
+        return 1
+    fi
 
        if [ $# == 1 ]; then
-               if [ "${slave_info[3]}" == 'Yes' ] &&
-                       [ "${slave_info[4]}" == 'Yes' ] &&
-                       [ "${slave_info[1]}" == "$OCF_RESKEY_replication_user" 
] &&
-                       ( grep "${slave_info[0]}" /etc/hosts > /dev/null ); then
+               if [ "$slave_io" == 'Yes' ] &&
+                       [ "$slave_sql" == 'Yes' ] &&
+                       [ "$master_user" == "$OCF_RESKEY_replication_user" ]; 
then
                        # machine is slave
                        return 0;
                fi
        else
-               if [ "${slave_info[3]}" == 'Yes' ] || [ "${slave_info[4]}" == 
'Yes' ]; then
-                       if [ "${slave_info[1]}" == 
"$OCF_RESKEY_replication_user" ] && ( grep "${slave_info[0]}" /etc/hosts > 
/dev/null ); then
+               if [ "$slave_io" == 'Yes' ] || [ "$slave_sql" == 'Yes' ]; then
+                       if [ "$slave_user" == "$OCF_RESKEY_replication_user" ]; 
then
                                # machine is slave
                                return 0;
                        fi
@@ -569,63 +567,62 @@
 }
 
 mysql_demote() {
-       search_uname=''
-       if [[ "$OCF_RESKEY_CRM_meta_notify_master_uname" =~ "^\s*$" ]] &&
-               [[ "$OCF_RESKEY_CRM_meta_notify_promote_uname" =~ "^\s*$" ]]; 
then
-               echo "No master or promote uname found" >> /tmp/replica.log
+       master_host=''
+       if ( echo "$OCF_RESKEY_CRM_meta_notify_master_uname"|grep '^\s*$' > 
/dev/null) 
+               ( echo "$OCF_RESKEY_CRM_meta_notify_promote_uname"|grep '^\s*$' 
> /dev/null); then 
+               ocf_log err "unable to demote (No master or promote uname 
found)"
                return $OCF_ERR_GENERIC
        fi
        if [ "$OCF_RESKEY_CRM_meta_notify_promote_uname" == "$HOSTNAME " ]; then
-               echo "Should not connect to my self(promote)" >> 
/tmp/replica.log
+               ocf_log err "should not try to connect to my self(promote)"
                return $OCF_ERR_GENERIC
        else
-               search_uname=$OCF_RESKEY_CRM_meta_notify_promote_uname
+               master_host=$OCF_RESKEY_CRM_meta_notify_promote_uname
        fi
        if [ "$OCF_RESKEY_CRM_meta_notify_master_uname" == "$HOSTNAME " ]; then
-               echo "Should not connect to my self(master)" >> /tmp/replica.log
+               ocf_log err "should not try to connect to my self(master)"
                return $OCF_ERR_GENERIC
        else
-               search_uname=$OCF_RESKEY_CRM_meta_notify_master_uname
+               master_host=$OCF_RESKEY_CRM_meta_notify_master_uname
        fi
 
        if ( ! mysql_status ); then
                return $OCF_NOT_RUNNING
-fi
+       fi
        if [ $# != 1 ] && [ "$OCF_RESKEY_CRM_meta_notify_demote_uname" != 
"$HOSTNAME " ]; then
                return $OCF_ERR_GENERIC
        fi
        if is_slave 1; then
                return $OCF_SUCCESS
        else
-               if [ -z "$search_uname" ]; then return $OCF_ERR_GENERIC; fi
-               master_host=$(grep $OCF_RESKEY_CRM_meta_notify_master_uname 
/etc/hosts|awk '{print $1;exit}')
-               if [ -z "$master_host" ]; then
-                       ocf_log err "Unable to get IP address of host 
$OCF_RESKEY_CRM_meta_notify_master_uname"
-               return $OCF_ERR_GENERIC;
-               fi
-               master_info=($(mysql \
-                       --password=$OCF_RESKEY_replication_passwd \
-                       --user=$OCF_RESKEY_replication_user \
-                       -h $master_host \
-                       -O connect_timeout=1 \
-               -e 'SHOW MASTER STATUS\G'|awk '/File/||/Position/{print $2}'))
-               if [ -z "${master_info[0]}" ] || [ -z "${master_info[1]}" ]; 
then
-                       ocf_log err "Empty master file or master position"
-               return $OCF_ERR_GENERIC;
-               fi
+               if [ -z "$master_host" ]; then  return $OCF_ERR_GENERIC; fi
+
+               master_file=''
+               master_pos=''
+        mysql --password=$OCF_RESKEY_replication_passwd \
+            --user=$OCF_RESKEY_replication_user \
+            -h $master_host \
+            -O connect_timeout=1 \
+            -e 'SHOW MASTER STATUS\G'|while read k v; do
+            if [ "$k" == 'File:' ]; then master_file="$v"; fi
+            if [ "$k" == 'Position:' ]; then master_pos="$v"; fi
+        done
+
+        if [ -z "$master_file" ] || [ -z "$master_pos" ]; then
+            ocf_log err "unable to find master file or master position"
+            return $OCF_ERR_GENERIC;
+        fi
+
                mysql --socket=$OCF_RESKEY_socket -O connect_timeout=1 -e 'STOP 
SLAVE';
                mysql --socket=$OCF_RESKEY_socket -O connect_timeout=1 \
                        -e "CHANGE MASTER TO MASTER_HOST='$master_host', \
                                MASTER_USER='$OCF_RESKEY_replication_user', \
                                
MASTER_PASSWORD='$OCF_RESKEY_replication_passwd', \
                                MASTER_PORT=$OCF_RESKEY_replication_port, \
-                               MASTER_LOG_FILE='${master_info[0]}', \
-                               MASTER_LOG_POS=${master_info[1]}, \
+                               MASTER_LOG_FILE='$master_file', \
+                               MASTER_LOG_POS=$master_pos, \
                                MASTER_CONNECT_RETRY=4"
                mysql --socket=$OCF_RESKEY_socket -O connect_timeout=1 -e 
'START SLAVE';
-               if [ $? == $OCF_ERR_GENERIC ]; then
-                       return $OCF_ERR_GENERIC
-               fi
        fi
        if is_slave 1; then
                return $OCF_SUCCESS
_______________________________________________________
Linux-HA-Dev: [email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/

Reply via email to