lib/vserver: added vservers_running function, use it in sys handler (to ease future...
[matthijs/upstream/backupninja.git] / lib / vserver.in
index 4c62291d4aca4b8ddd6f375aa0a9f28b28706695..153a9b0241929c661888a4c20fc3b2654ddf1755 100644 (file)
@@ -1,3 +1,5 @@
+# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
+
 #####################################################
 ## VSERVERS RELATED FUNCTIONS FOR NINJAHELPER
 ##
 ##   - set $vservers_are_available to 'yes';
 ##   - set $found_vservers to the list of all vservers found on the system.
 ## This function has to be run once before a new helper is run.
+## If the argument is "nodialog", use the backupninja's message functions
+## instead of easydialog.
 ##
 init_vservers() {
+   local arg=$1
    # get global variables from the conffile
    setfile $conffile
    getconf vservers no
    getconf VSERVERINFO /usr/sbin/vserver-info
    getconf VSERVER /usr/sbin/vserver
-   getconf VROOTDIR `if [ -f "$VSERVERINFO" ]; then $VSERVERINFO info SYSINFO |grep vserver-Rootdir | awk '{print $2}'; fi`
+   getconf VROOTDIR `if [ -x "$VSERVERINFO" ]; then $VSERVERINFO info SYSINFO | grep '^ *vserver-Rootdir' | @AWK@ '{print $2}'; fi`
+   # canonicalize VROOTDIR
+   [ -z "$VROOTDIR" ] || VROOTDIR=`readlink --canonicalize $VROOTDIR`
    # init this library's global variables
    vservers_are_available=no
    found_vservers=
    selected_vservers=
    host_or_vservers=host
    # check vservers real availability
-   if [ $vservers == yes ]; then
-      [ -n "$VROOTDIR" ] || (msgBox "warning" "VROOTDIR is not set in $conffile and could not be guessed."; return)
-      [ -d "$VROOTDIR" ] || (msgBox "warning" "VROOTDIR ($VROOTDIR) does not exist."; return)
-      found_vservers=`ls $VROOTDIR | grep -E -v "lost+found|ARCHIVES" | tr "\n" " "`
-      [ -n "$found_vservers" ] || return
+   if [ $vservers = yes ]; then
+      if [ ! -x "$VSERVERINFO" ]; then
+         `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
+            "vservers enabled in $conffile, but vserver-info command was not found. Please set the VSERVERINFO configuration variable to its full path."
+            return
+      fi
+      if [ ! -x "$VSERVER" ]; then
+         `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
+            "vservers enabled in $conffile, but vserver command was not found. Please set the VSERVER configuration variable to its full path."
+            return
+      fi
+      if [ -z "$VROOTDIR" ]; then
+         `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
+            "vservers enabled in $conffile, but VROOTDIR is not set and could not be guessed."
+            return
+      fi
+      if [ ! -d "$VROOTDIR" ]; then
+         `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
+            "vservers enabled in $conffile, but VROOTDIR ($VROOTDIR) does not exist.";
+            return
+      fi
+      found_vservers=`ls $VROOTDIR | grep -E -v "lost\+found|ARCHIVES" | tr "\n" " "`
+      if [ -z "$found_vservers" ]; then
+         `if [ "$arg" = nodialog ]; then echo warning; else echo "msgBox warning"; fi` \
+            "vservers enabled in $conffile, but no vserver was found in $VROOTDIR.";      
+            return
+      fi
       vservers_are_available=yes
    fi
 }
 
 ##
-## If the argument is the name of a vserver selected use by the current helper,
+## If all the arguments are existing vservers names, returns 0.
+## Else, returns 1. Also returns 1 if no argument is given.
+##
+vservers_exist() {
+   [ $# -ge 1 ] || return 1
+   local args="$1"
+   local vserver i found
+   for vserver in $args ; do
+      found=no
+      for i in $found_vservers ; do
+         if [ $vserver = $i ]; then
+            found=yes
+            break
+         fi
+      done
+      [ $found = yes ] || return 1
+   done
+   return 0
+}
+
+##
+## If all the arguments are running vservers names, returns 0.
+## Else, returns 1. Also returns 1 if no argument is given.
+##
+vservers_running() {
+   [ $# -ge 1 ] || return 1
+   local args="$1"
+   local vserver
+   for vserver in $args ; do
+      $VSERVERINFO -q $vserver RUNNING || return 1
+   done
+   return 0
+}
+
+##
+## If the argument is the name of a vserver selected by the current helper,
 ## echoes 'on' and returns 0.
 ## Else, echoes 'off' and returns 1.
 ##
@@ -88,14 +152,18 @@ choose_one_vserver() {
 }
 
 ##
-## If Vservers are not enabled, exit silently.
+## If Vservers are not enabled, set host_or_vservers='host' and then return 
 ## Else, have the user choose if he/she wants to perform the backup on the host
 ## system or on one Vserver.
 ## Set, respectively, $host_or_vservers to 'host' or 'vservers'.
 ## Returns 1 if cancelled.
 ##
 choose_host_or_one_vserver() {
-   [ "$vservers_are_available" == "yes" ] || return
+   if [ "$vservers_are_available" != "yes" ] 
+   then
+       host_or_vservers='host'
+       return
+   fi
    local title=$1
    # if there is one, set the previously chosen item as the default
    [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
@@ -114,7 +182,7 @@ choose_host_or_one_vserver() {
 }
 
 ##
-## If Vservers are not enabled, exit silently.
+## If Vservers are not enabled, set host_or_vservers='host' and then return
 ## Else, have the user choose the target he/she wants to perform the backup on:
 ##   - host system only;
 ##   - some vservers only;
@@ -123,7 +191,11 @@ choose_host_or_one_vserver() {
 ## Returns 1 if cancelled.
 ##
 choose_host_or_vservers_or_both() {
-   [ "$vservers_are_available" == "yes" ] || return
+   if [ "$vservers_are_available" != "yes" ] 
+   then
+       host_or_vservers='host'
+       return
+   fi
    local title=$1
    # if there is one, set the previously chosen item as the default
    [ -n "$host_or_vservers" ] && setDefault $host_or_vservers