Blob Blame History Raw
From 2f50d3019580d98d3a7073d352c29c6541a4468e Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Tue, 10 Jan 2012 11:06:47 +0100
Subject: [PATCH 1/3] Extract some init functions into a separate file

---
 Makefile.in                |   1 +
 config/ctdb.init           | 157 +------------------------------------------
 config/init_functions      | 163 +++++++++++++++++++++++++++++++++++++++++++++
 packaging/RPM/ctdb.spec.in |   1 +
 4 Dateien geändert, 166 Zeilen hinzugefügt(+), 156 Zeilen entfernt(-)
 create mode 100644 config/init_functions

diff --git a/Makefile.in b/Makefile.in
index fef2e45..d8835f8 100755
--- a/Makefile.in
+++ b/Makefile.in
@@ -326,6 +326,7 @@ install: all manpages $(PMDA_INSTALL)
 	${INSTALLCMD} -m 644 include/ctdb_private.h $(DESTDIR)$(includedir) # for samba3
 	${INSTALLCMD} -m 644 include/ctdb_typesafe_cb.h $(DESTDIR)$(includedir)
 	${INSTALLCMD} -m 644 config/functions $(DESTDIR)$(etcdir)/ctdb
+	${INSTALLCMD} -m 644 config/init_functions $(DESTDIR)$(etcdir)/ctdb
 	${INSTALLCMD} -m 755 config/statd-callout $(DESTDIR)$(etcdir)/ctdb
 	${INSTALLCMD} -m 644 README $(DESTDIR)$(docdir)/ctdb/README
 	${INSTALLCMD} -m 644 COPYING $(DESTDIR)$(docdir)/ctdb/COPYING
diff --git a/config/ctdb.init b/config/ctdb.init
index 9f9b686..804cb6a 100755
--- a/config/ctdb.init
+++ b/config/ctdb.init
@@ -40,6 +40,7 @@ unset TMPDIR
 }
 
 . $CTDB_BASE/functions
+. $CTDB_BASE/init_functions
 loadconfig network
 loadconfig ctdb
 
@@ -57,166 +58,10 @@ else
     init_style="$CTDB_INIT_STYLE"
 fi
 
-build_ctdb_options () {
-
-    maybe_set () {
-	# If the 2nd arg is null then return - don't set anything.
-	# Else if the 3rd arg is set and it doesn't match the 2nd arg
-	# then return
-	[ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return
-
-	val="'$2'"
-	case "$1" in
-	    --*) sep="=" ;;
-	    -*)  sep=" " ;;
-	esac
-	# For these options we're only passing a value-less flag.
-	[ -n "$3" ] && {
-	    val=""
-	    sep=""
-	}
-
-	CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
-    }
-
-    [ -z "$CTDB_RECOVERY_LOCK" ] && {
-        echo "No recovery lock specified. Starting CTDB without split brain prevention"
-    }
-    maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
-
-    # build up CTDB_OPTIONS variable from optional parameters
-    maybe_set "--logfile"                "$CTDB_LOGFILE"
-    maybe_set "--nlist"                  "$CTDB_NODES"
-    maybe_set "--socket"                 "$CTDB_SOCKET"
-    maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
-    maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
-    maybe_set "--dbdir"                  "$CTDB_DBDIR"
-    maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
-    maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
-    maybe_set "--transport"              "$CTDB_TRANSPORT"
-    maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
-    maybe_set "--debug-hung-script"      "$CTDB_DEBUG_HUNG_SCRIPT"
-    maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
-    maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
-    maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
-    maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
-    maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
-    maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
-    maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
-    maybe_set "--log-ringbuf-size"       "$CTDB_LOG_RINGBUF_SIZE"
-    maybe_set "--syslog"                 "$CTDB_SYSLOG"               "yes"
-    maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
-}
-
-# Log given message or stdin to either syslog or a CTDB log file
-do_log ()
-{
-    script_log "ctdb.init" "$@"
-}
-
-select_tdb_checker ()
-{
-    # Find the best TDB consistency check available.
-    use_tdb_tool_check=false
-    if [ -x /usr/bin/tdbtool ] && \
-	echo "help" | /usr/bin/tdbtool | grep -q check ; then
-
-	use_tdb_tool_check=true
-    elif [ -x /usr/bin/tdbtool -a -x /usr/bin/tdbdump ] ; then
-	    do_log <<EOF
-WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
- Using 'tdbdump' for database checks.
- Consider updating 'tdbtool' for better checks!
-EOF
-    elif [ -x /usr/bin/tdbdump ] ; then
-	do_log <<EOF
-WARNING: 'tdbtool' is not available.
- Using 'tdbdump' to check the databases.
- Consider installing a recent 'tdbtool' for better checks!
-EOF
-    else
-	do_log <<EOF
-WARNING: Cannot check databases since neither
- 'tdbdump' nor 'tdbtool check' is available.
- Consider installing tdbtool or at least tdbdump!
-EOF
-        return 1
-    fi
-}
-
-check_tdb ()
-{
-    _db="$1"
-
-    if $use_tdb_tool_check ; then
-	# tdbtool always exits with 0  :-(
-	if tdbtool "$_db" check 2>/dev/null |
-	    grep -q "Database integrity is OK" ; then
-	    return 0
-	else
-	    return 1
-	fi
-    else
-	tdbdump "$_db" >/dev/null 2>/dev/null
-	return $?
-    fi
-}
-
-check_persistent_databases ()
-{
-    _dir="${CTDB_DBDIR_PERSISTENT:-${CTDB_DBDIR:-/var/ctdb}/persistent}"
-    mkdir -p "$_dir" 2>/dev/null
-
-    [ "${CTDB_MAX_PERSISTENT_CHECK_ERRORS:-0}" = "0" ] || return 0
-
-    for _db in $(ls "$_dir/"*.tdb.*[0-9] 2>/dev/null) ; do
-	check_tdb $_db || {
-	    do_log "Persistent database $_db is corrupted! CTDB will not start."
-	    return 1
-	}
-    done
-}
-
-check_non_persistent_databases ()
-{
-    _dir="${CTDB_DBDIR:-/var/ctdb}"
-    mkdir -p "$_dir" 2>/dev/null
-
-    for _db in $(ls "${_dir}/"*.tdb.*[0-9] 2>/dev/null) ; do
-	check_tdb $_db || {
-	    _backup="${_db}.$(date +'%Y%m%d.%H%M%S.%N').corrupt"
-	    do_log <<EOF
-WARNING: database ${_db} is corrupted.
- Moving to backup ${_backup} for later analysis.
-EOF
-	    mv "$_db" "$_backup"
-
-	    # Now remove excess backups
-	    ls -td "${_db}."*".corrupt" |
-	    tail -n +$((${CTDB_MAX_CORRUPT_DB_BACKUPS:-10} + 1)) |
-	    xargs rm -f
-	    
-	}
-    done
-}
-
 set_retval() {
     return $1
 }
 
-wait_until_ready () {
-    _timeout="${1:-10}" # default is 10 seconds
-
-    _count=0
-    while ! ctdb ping >/dev/null 2>&1 ; do
-	if [ $_count -ge $_timeout ] ; then
-	    return 1
-	fi
-	sleep 1
-	_count=$(($_count + 1))
-    done
-}
-
 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
 
 start() {
diff --git a/config/init_functions b/config/init_functions
new file mode 100644
index 0000000..6d3e789
--- /dev/null
+++ b/config/init_functions
@@ -0,0 +1,163 @@
+# Shared init functions used by SysV and systemd init systems
+
+build_ctdb_options () {
+
+    maybe_set () {
+        # If the 2nd arg is null then return - don't set anything.
+        # Else if the 3rd arg is set and it doesn't match the 2nd arg
+        # then return
+        [ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return
+
+        val="'$2'"
+        case "$1" in
+            --*) sep="=" ;;
+            -*)  sep=" " ;;
+        esac
+        # For these options we're only passing a value-less flag.
+        [ -n "$3" ] && {
+            val=""
+            sep=""
+        }
+
+        CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
+    }
+
+    [ -z "$CTDB_RECOVERY_LOCK" ] && {
+        echo "No recovery lock specified. Starting CTDB without split brain prevention"
+    }
+    maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
+
+    # build up CTDB_OPTIONS variable from optional parameters
+    maybe_set "--logfile"                "$CTDB_LOGFILE"
+    maybe_set "--nlist"                  "$CTDB_NODES"
+    maybe_set "--socket"                 "$CTDB_SOCKET"
+    maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
+    maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
+    maybe_set "--dbdir"                  "$CTDB_DBDIR"
+    maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
+    maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
+    maybe_set "--transport"              "$CTDB_TRANSPORT"
+    maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
+    maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
+    maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
+    maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
+    maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
+    maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
+    maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
+    maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
+    maybe_set "--log-ringbuf-size"       "$CTDB_LOG_RINGBUF_SIZE"
+    maybe_set "--syslog"                 "$CTDB_SYSLOG"               "yes"
+    maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
+}
+
+check_tdb () {
+    local PDBASE=$1
+
+    test x"$TDBTOOL_HAS_CHECK" = x"1" && {
+        #
+        # Note tdbtool always exits with 0
+        #
+        local OK=`/usr/bin/tdbtool $PDBASE check | grep "Database integrity is OK" | wc -l`
+        test x"$OK" = x"1" || {
+            return 1;
+        }
+
+        return 0;
+    }
+
+    /usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
+        return $?;
+    }
+
+    return 0;
+}
+
+check_persistent_databases () {
+    PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
+    mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
+    local ERRCOUNT=$CTDB_MAX_PERSISTENT_CHECK_ERRORS
+
+    test -z "$ERRCOUNT" && {
+        ERRCOUNT="0"
+    }
+    test x"$ERRCOUNT" != x"0" && {
+        return 0;
+    }
+
+    if test -x /usr/bin/tdbtool ; then
+        HAVE_TDBTOOL=1
+    else
+        HAVE_TDBTOOL=0
+    fi
+
+    if test x"$HAVE_TDBTOOL" = x"1" ; then
+        TDBTOOL_HAS_CHECK=`echo "help" | /usr/bin/tdbtool | grep check | wc -l`
+    else
+        TDBTOOL_HAS_CHECK=0
+    fi
+
+    if test -x /usr/bin/tdbdump ; then
+        HAVE_TDBDUMP=1
+    else
+        HAVE_TDBDUMP=0
+    fi
+
+    if test x"$HAVE_TDBDUMP" = x"0" -a x"$TDBTOOL_HAS_CHECK" = x"0" ; then
+        echo "WARNING: Cannot check persistent databases since"
+        echo "neither 'tdbdump' nor 'tdbtool check' is available."
+        echo "Consider installing tdbtool or at least tdbdump!"
+        return 0
+    fi
+
+    if test x"$HAVE_TDBDUMP" = x"1" -a x"$TDBTOOL_HAS_CHECK" = x"0" ; then
+        if test x"$HAVE_TDBTOOL" = x"0"; then
+            echo "WARNING: 'tdbtool' is not available. Using 'tdbdump' to"
+            echo "check the persistent databases."
+            echo "Consider installing a recent 'tdbtool' for better checks!"
+        else
+            echo "WARNING: The installed 'tdbtool' does not offer the 'check'"
+            echo "subcommand. Using 'tdbdump' for persistent database checks."
+            echo "Consider updating 'tdbtool' for better checks!"
+        fi
+    fi
+
+    for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
+        check_tdb $PDBASE || {
+            echo "Persistent database $PDBASE is corrupted! CTDB will not start."
+            return 1
+        }
+    done
+}
+
+set_ctdb_variables () {
+    # set any tunables from the config file
+    set | grep ^CTDB_SET_ | cut -d_ -f3- |
+    while read v; do
+        varname=`echo $v | cut -d= -f1`
+        value=`echo $v | cut -d= -f2`
+        ctdb setvar $varname $value || RETVAL=1
+    done || exit 1
+}
+
+wait_until_ready () {
+    _timeout="${1:-10}" # default is 10 seconds
+
+    _count=0
+    while ! ctdb ping >/dev/null 2>&1 ; do
+        if [ $_count -ge $_timeout ] ; then
+            return 1
+        fi
+        sleep 1
+        _count=$(($_count + 1))
+    done
+}
+
+drop_all_public_ips() {
+    [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
+        return
+    }
+
+    cat $CTDB_PUBLIC_ADDRESSES | while read IP IFACE REST; do
+        ip addr del $IP dev $IFACE >/dev/null 2>/dev/null
+    done
+}
diff --git a/packaging/RPM/ctdb.spec.in b/packaging/RPM/ctdb.spec.in
index 50287ab..5bcf005 100644
--- a/packaging/RPM/ctdb.spec.in
+++ b/packaging/RPM/ctdb.spec.in
@@ -155,6 +155,7 @@ rm -rf $RPM_BUILD_ROOT
 %{_sysconfdir}/ctdb/events.d/70.iscsi
 %{_sysconfdir}/ctdb/events.d/91.lvs
 %{_sysconfdir}/ctdb/statd-callout
+%{_sysconfdir}/ctdb/init_functions
 %{_sbindir}/ctdbd
 %{_bindir}/ctdb
 %{_bindir}/smnotify
-- 
1.7.11.7