1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 #####################################################
4 ## VSERVERS RELATED FUNCTIONS FOR NINJAHELPER
7 ## - easydialog library
10 ## Global variables used and modified here:
11 ## - $vservers_are_available (yes/no)
12 ## - $found_vservers (list)
13 ## - $selected_vservers (list)
14 ## - $host_or_vservers (host/vservers/both)
18 ## Get vservers-related variables.
19 ## Then, if Vservers are enabled, check that:
20 ## - VROOTDIR is valid;
21 ## - at least one vserver can be found.
22 ## If, and only if, the above conditions are all true:
23 ## - set $vservers_are_available to 'yes';
24 ## - set $found_vservers to the list of all vservers found on the system.
25 ## This function has to be run once before a new helper is run.
26 ## If the argument is "nodialog", use the backupninja's message functions
27 ## instead of easydialog.
31 # get global variables from the conffile
34 getconf VSERVERINFO /usr/sbin/vserver-info
35 getconf VSERVER /usr/sbin/vserver
36 getconf VROOTDIR `if [ -x "$VSERVERINFO" ]; then $VSERVERINFO info SYSINFO | grep '^ *vserver-Rootdir' | @AWK@ '{print $2}'; fi`
37 # canonicalize VROOTDIR
38 [ -z "$VROOTDIR" ] || VROOTDIR=`readlink --canonicalize $VROOTDIR`
39 # init this library's global variables
40 vservers_are_available=no
44 # check vservers real availability
45 if [ $vservers = yes ]; then
46 if [ ! -x "$VSERVERINFO" ]; then
47 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
48 "vservers enabled in $conffile, but vserver-info command was not found. Please set the VSERVERINFO configuration variable to its full path."
51 if [ ! -x "$VSERVER" ]; then
52 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
53 "vservers enabled in $conffile, but vserver command was not found. Please set the VSERVER configuration variable to its full path."
56 if [ -z "$VROOTDIR" ]; then
57 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
58 "vservers enabled in $conffile, but VROOTDIR is not set and could not be guessed."
61 if [ ! -d "$VROOTDIR" ]; then
62 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
63 "vservers enabled in $conffile, but VROOTDIR ($VROOTDIR) does not exist.";
66 found_vservers=`ls $VROOTDIR | grep -E -v "lost\+found|ARCHIVES" | tr "\n" " "`
67 if [ -z "$found_vservers" ]; then
68 `if [ "$arg" = nodialog ]; then echo warning; else echo "msgBox warning"; fi` \
69 "vservers enabled in $conffile, but no vserver was found in $VROOTDIR.";
72 vservers_are_available=yes
77 ## If all the arguments are existing vservers names, returns 0.
78 ## Else, returns 1. Also returns 1 if no argument is given.
81 [ $# -ge 1 ] || return 1
84 for vserver in $args ; do
86 for i in $found_vservers ; do
87 if [ $vserver = $i ]; then
92 [ $found = yes ] || return 1
98 ## If the argument is the name of a vserver selected by the current helper,
99 ## echoes 'on' and returns 0.
100 ## Else, echoes 'off' and returns 1.
102 vserver_is_selected() {
104 local vserver_is_selected=1
106 for i in $selected_vservers ; do
107 [ "$vserver" == "$i" ] && vserver_is_selected=0
109 if [ $vserver_is_selected = 0 ]; then
114 return $vserver_is_selected
118 ## Have the user choose one Vserver among the existing ones.
119 ## Set $selected_vservers to the chosen one's name.
120 ## Returns 1 if cancelled or if Vservers are not available.
122 choose_one_vserver() {
123 [ "$vservers_are_available" == "yes" ] || return 1
128 while [ -z "$REPLY" ]; do
129 [ -n "$selected_vservers" ] && setDefault $selected_vservers
130 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
131 for vserver in $found_vservers; do
132 listItem "$vserver" "Backup $vserver vserver"
135 [ $? = 0 ] || return 1
137 selected_vservers=$REPLY
141 ## If Vservers are not enabled, set host_or_vservers='host' and then return
142 ## Else, have the user choose if he/she wants to perform the backup on the host
143 ## system or on one Vserver.
144 ## Set, respectively, $host_or_vservers to 'host' or 'vservers'.
145 ## Returns 1 if cancelled.
147 choose_host_or_one_vserver() {
148 if [ "$vservers_are_available" != "yes" ]
150 host_or_vservers='host'
154 # if there is one, set the previously chosen item as the default
155 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
156 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
157 "host" "Host system" \
158 "vserver" "One Vserver"
159 [ $? = 0 ] || return 1
162 host_or_vservers='host'
165 host_or_vservers='vservers'
171 ## If Vservers are not enabled, set host_or_vservers='host' and then return
172 ## Else, have the user choose the target he/she wants to perform the backup on:
173 ## - host system only;
174 ## - some vservers only;
175 ## - both the host system and some vservers.
176 ## Set, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
177 ## Returns 1 if cancelled.
179 choose_host_or_vservers_or_both() {
180 if [ "$vservers_are_available" != "yes" ]
182 host_or_vservers='host'
186 # if there is one, set the previously chosen item as the default
187 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
188 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
189 "host" "Host system only" \
190 "vservers" "Vservers only" \
191 "both" "Host system and Vservers"
192 [ $? = 0 ] || return 1
195 host_or_vservers='host'
198 host_or_vservers='vservers'
201 host_or_vservers='both'
207 ## Have the user choose among "all vservers" and a not-empty subset of these.
208 ## Set $selected_vservers to 'all' or to a space-separated name list.
209 ## Returns 1 if cancelled or if Vservers are not available.
211 choose_one_or_more_vservers() {
212 [ "$vservers_are_available" == "yes" ] || return 1
216 booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$selected_vservers" -o "$selected_vservers" == "all" ] || echo no`
218 selected_vservers="all"
220 # choose among the existing vservers
222 local vserver_was_selected=
224 while [ -z "$REPLY" ]; do
225 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
226 # list existing vservers, preselecting the previously selected ones
227 for vserver in $found_vservers; do
228 listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
230 listDisplay checklist
231 [ $? = 0 ] || return 1
233 # remove quotes around each vserver name
234 selected_vservers=`echo $REPLY | tr -d '"'`