Dont run net serverid wipe in the background
[sahlberg/ctdb.git] / config / events.d / 50.samba
index e0e2ad7bb5b307d68844d43228b9798980a7b42c..a1f99fbb248cf7460ccd72d04a4721267e2def6f 100755 (executable)
@@ -1,11 +1,7 @@
 #!/bin/sh
 # ctdb event script for Samba
 
-PATH=/bin:/usr/bin:$PATH
-
 . $CTDB_BASE/functions
-loadconfig ctdb
-loadconfig samba
 
 detect_init_style
 
@@ -15,16 +11,11 @@ case $CTDB_INIT_STYLE in
                CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
                CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
                ;;
-       ubuntu)
+       debian)
                CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
                CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
                CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
                ;;
-       redhat)
-               CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
-               CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
-               CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
-               ;;
        *)
                # should not happen, but for now use redhat style as default:
                CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
@@ -33,10 +24,77 @@ case $CTDB_INIT_STYLE in
                ;;
 esac
 
-cmd="$1"
-shift
+service_name="samba"
+service_start="start_samba"
+service_stop="stop_samba"
+
+loadconfig
+
+start_samba() {
+       # create the state directory for samba
+       /bin/mkdir -p $CTDB_VARDIR/state/samba
+
+       # make sure samba is not already started
+       [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
+               service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
+               service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
+               killall -0 -q smbd && {
+                   sleep 1
+                   # make absolutely sure samba is dead
+                   killall -q -9 smbd
+               }
+
+               killall -0 -q nmbd && {
+                   sleep 1
+                   # make absolutely sure samba is dead
+                   killall -q -9 nmbd
+               }
+       }
+
+       # make sure winbind is not already started
+       check_ctdb_manages_winbind
+       [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
+               service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
+               killall -0 -q winbindd && {
+                   sleep 1
+                   # make absolutely sure winbindd is dead
+                   killall -q -9 winbindd
+               }
+
+       }
+
+       # start the winbind service
+       [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
+               service "$CTDB_SERVICE_WINBIND" start
+       }
+
+       # start Samba service. Start it reniced, as under very heavy load 
+       # the number of smbd processes will mean that it leaves few cycles for
+       # anything else
+       [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
+               /usr/bin/net serverid wipe
 
-[ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
+               nice_service "$CTDB_SERVICE_NMB" start
+               nice_service "$CTDB_SERVICE_SMB" start
+       }
+       return 0
+}
+
+stop_samba() {
+       # shutdown Samba when ctdb goes down
+       [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
+               service "$CTDB_SERVICE_SMB" stop
+               service "$CTDB_SERVICE_NMB" stop
+       }
+
+       # stop the winbind service
+       check_ctdb_manages_winbind
+       [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
+               service "$CTDB_SERVICE_WINBIND" stop
+       }
+
+       return 0
+}
 
 # set default samba cleanup period - in minutes
 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
@@ -44,13 +102,13 @@ shift
 }
 
 # we keep a cached copy of smb.conf here
-smbconf_cache="$CTDB_BASE/state/samba/smb.conf.cache"
+smbconf_cache="$CTDB_VARDIR/state/samba/smb.conf.cache"
 
 
 #############################################
 # update the smb.conf cache in the foreground
 testparm_foreground_update() {
-    mkdir -p "$CTDB_BASE/state/samba" || exit 1
+    mkdir -p "$CTDB_VARDIR/state/samba" || exit 1
     testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
 }
 
@@ -72,7 +130,7 @@ testparm_background_update() {
        while [ $timeleft -gt 0 ]; do
            timeleft=$(($timeleft - 1))
            # see if the process still exists
-           kill -0 $pid > /dev/null 2>&1 || {
+           /bin/kill -0 $pid > /dev/null 2>&1 || {
                # it doesn't exist, grab its exit status
                wait $pid
                [ $? = 0 ] || {
@@ -98,7 +156,7 @@ testparm_background_update() {
        done
        # it took more than 10 seconds - kill it off
        rm -f "${tmpfile}"
-       kill -9 "$pid" > /dev/null 2>&1
+       /bin/kill -9 "$pid" > /dev/null 2>&1
        echo "50.samba: timed out updating smbconf cache in background"
        exit 1
     ) &
@@ -129,116 +187,82 @@ check_ctdb_manages_winbind() {
   }
 }
 
+list_samba_shares ()
+{
+    testparm_cat |
+    sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
+    sed -e 's/"//g'
+}
+
+
 ###########################
 # periodic cleanup function
 periodic_cleanup() {
     # running smbstatus scrubs any dead entries from the connections
     # and sessionid database
     # echo "Running periodic cleanup of samba databases"
-    smbstatus -n > /dev/null 2>&1 &
+    smbstatus -np > /dev/null 2>&1 &
 }
 
-case $cmd in 
-     startup)
-       # create the state directory for samba
-       /bin/mkdir -p $CTDB_BASE/state/samba
+###########################
 
-       # make sure samba is not already started
-       service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
-       service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
-       killall -0 -q smbd && {
-           sleep 1
-           # make absolutely sure samba is dead
-           killall -q -9 smbd
-       }
+[ "$1" = "monitor" ] && {
+ctdb_start_stop_service
+ctdb_start_stop_service "winbind"
+}
 
-       killall -0 -q nmbd && {
-           sleep 1
-           # make absolutely sure samba is dead
-           killall -q -9 nmbd
-       }
+is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
 
-       # restart the winbind service
-       check_ctdb_manages_winbind
-       [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
-               service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
-               killall -0 -q winbindd && {
-                   sleep 1
-                   # make absolutely sure winbindd is dead
-                   killall -q -9 winbindd
-               }
-               service "$CTDB_SERVICE_WINBIND" start
-       }
+###########################
 
-       # start Samba service. Start it reniced, as under very heavy load 
-       # the number of smbd processes will mean that it leaves few cycles for
-       # anything else
-       nice_service "$CTDB_SERVICE_NMB" start
-       nice_service "$CTDB_SERVICE_SMB" start
+case "$1" in 
+     startup)
+       ctdb_service_start
        ;;
        
-     takeip)
-       # nothing special for Samba
-       ;;
-
-     releaseip)
-       # nothing special for Samba
-       ;;
-
-     recovered)
-       # nothing special for Samba
-       exit 0
-       ;;
-
      shutdown)
-       # shutdown Samba when ctdb goes down
-       service "$CTDB_SERVICE_SMB" stop
-       service "$CTDB_SERVICE_NMB" stop
-
-       # stop the winbind service
-       check_ctdb_manages_winbind
-       [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
-               service "$CTDB_SERVICE_WINBIND" stop
-       }
+       ctdb_service_stop
        ;;
 
      monitor)
        # Create a dummy file to track when we need to do periodic cleanup
        # of samba databases
-       [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
-               touch $CTDB_BASE/state/samba/periodic_cleanup
+       [ -f $CTDB_VARDIR/state/samba/periodic_cleanup ] || {
+               touch $CTDB_VARDIR/state/samba/periodic_cleanup
        }
-       [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
+       [ `/usr/bin/find $CTDB_VARDIR/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
                # Cleanup the databases
                periodic_cleanup
-               touch $CTDB_BASE/state/samba/periodic_cleanup
+               touch $CTDB_VARDIR/state/samba/periodic_cleanup
        }
 
-       [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
-               testparm_background_update
+       [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
+               [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
+                       testparm_background_update
 
-               testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
-                   testparm_foreground_update
-                   testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
-                       echo "ERROR: testparm shows smb.conf is not clean"
-                       exit 1
-                   }
+                       testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
+                           testparm_foreground_update
+                           testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
+                               echo "ERROR: testparm shows smb.conf is not clean"
+                               exit 1
+                           }
+                       }
+                       
+                       list_samba_shares |
+                       ctdb_check_directories_probe || {
+                           testparm_foreground_update
+                           list_samba_shares |
+                           ctdb_check_directories
+                       } || exit $?
                }
 
-               smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
-               ctdb_check_directories_probe "Samba" $smb_dirs || {
-                   testparm_foreground_update
-                   smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
-                   ctdb_check_directories "Samba" $smb_dirs
+               smb_ports="$CTDB_SAMBA_CHECK_PORTS"
+               [ -z "$smb_ports" ] && {
+                       smb_ports=`testparm_cat --parameter-name="smb ports"`
                }
+               ctdb_check_tcp_ports $smb_ports || exit $?
        }
 
-       smb_ports="$CTDB_SAMBA_CHECK_PORTS"
-       [ -z "$smb_ports" ] && {
-               smb_ports=`testparm_cat --parameter-name="smb ports"`
-       }
-       ctdb_check_tcp_ports "Samba" $smb_ports
-
        # check winbind is OK
        check_ctdb_manages_winbind
        [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
@@ -246,7 +270,9 @@ case $cmd in
        }
        ;;
 
+    *)
+       ctdb_standard_event_handler "$@"
+       ;;
 esac
 
-# ignore unknown commands
 exit 0