1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 #####################################################
5 ## VSERVERS RELATED FUNCTIONS FOR NINJAHELPER
8 ## - easydialog library
11 ## Global variables used and modified here:
12 ## - $vservers_are_available (yes/no)
13 ## - $found_vservers (list)
14 ## - $selected_vservers (list)
15 ## - $host_or_vservers (host/vservers/both)
19 ## Get vservers-related variables.
20 ## Then, if Vservers are enabled, check that:
21 ## - VROOTDIR is valid;
22 ## - at least one vserver can be found.
23 ## If, and only if, the above conditions are all true:
24 ## - set $vservers_are_available to 'yes';
25 ## - set $found_vservers to the list of all vservers found on the system.
26 ## This function has to be run once before a new helper is run.
27 ## If the argument is "nodialog", use the backupninja's message functions
28 ## instead of easydialog.
32 # get global variables from the conffile
35 getconf VSERVERINFO /usr/sbin/vserver-info
36 getconf VSERVER /usr/sbin/vserver
37 getconf VROOTDIR `if [ -x "$VSERVERINFO" ]; then $VSERVERINFO info SYSINFO | grep '^ *vserver-Rootdir' | @AWK@ '{print $2}'; fi`
38 # canonicalize VROOTDIR
39 [ -z "$VROOTDIR" ] || VROOTDIR=`readlink --canonicalize $VROOTDIR`
40 # init this library's global variables
41 vservers_are_available=no
45 # check vservers real availability
46 if [ $vservers = yes ]; then
47 if [ ! -x "$VSERVERINFO" ]; then
48 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
49 "vservers enabled in $conffile, but vserver-info command was not found. Please set the VSERVERINFO configuration variable to its full path."
52 if [ ! -x "$VSERVER" ]; then
53 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
54 "vservers enabled in $conffile, but vserver command was not found. Please set the VSERVER configuration variable to its full path."
57 if [ -z "$VROOTDIR" ]; then
58 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
59 "vservers enabled in $conffile, but VROOTDIR is not set and could not be guessed."
62 if [ ! -d "$VROOTDIR" ]; then
63 `if [ "$arg" = nodialog ]; then echo fatal; else echo "msgBox warning"; fi` \
64 "vservers enabled in $conffile, but VROOTDIR ($VROOTDIR) does not exist.";
67 found_vservers=`ls $VROOTDIR | grep -E -v "lost\+found|ARCHIVES" | tr "\n" " "`
68 if [ -z "$found_vservers" ]; then
69 `if [ "$arg" = nodialog ]; then echo warning; else echo "msgBox warning"; fi` \
70 "vservers enabled in $conffile, but no vserver was found in $VROOTDIR.";
73 vservers_are_available=yes
78 ## If all the arguments are existing vservers names, returns 0.
79 ## Else, returns 1. Also returns 1 if no argument is given.
82 [ $# -ge 1 ] || return 1
85 for vserver in $args ; do
87 for i in $found_vservers ; do
88 if [ $vserver = $i ]; then
93 [ $found = yes ] || return 1
99 ## If all the arguments are running vservers names, returns 0.
100 ## Else, returns 1. Also returns 1 if no argument is given.
103 [ $# -ge 1 ] || return 1
106 for vserver in $args ; do
107 $VSERVERINFO -q $vserver RUNNING || return 1
113 ## If the argument is the name of a vserver selected by the current helper,
114 ## echoes 'on' and returns 0.
115 ## Else, echoes 'off' and returns 1.
117 vserver_is_selected() {
119 local vserver_is_selected=1
121 for i in $selected_vservers ; do
122 [ "$vserver" == "$i" ] && vserver_is_selected=0
124 if [ $vserver_is_selected = 0 ]; then
129 return $vserver_is_selected
133 ## Have the user choose one Vserver among the existing ones.
134 ## Set $selected_vservers to the chosen one's name.
135 ## Returns 1 if cancelled or if Vservers are not available.
137 choose_one_vserver() {
138 [ "$vservers_are_available" == "yes" ] || return 1
143 while [ -z "$REPLY" ]; do
144 [ -n "$selected_vservers" ] && setDefault $selected_vservers
145 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
146 for vserver in $found_vservers; do
147 listItem "$vserver" "Backup $vserver vserver"
150 [ $? = 0 ] || return 1
152 selected_vservers=$REPLY
156 ## If Vservers are not enabled, set host_or_vservers='host' and then return
157 ## Else, have the user choose if he/she wants to perform the backup on the host
158 ## system or on one Vserver.
159 ## Set, respectively, $host_or_vservers to 'host' or 'vservers'.
160 ## Returns 1 if cancelled.
162 choose_host_or_one_vserver() {
163 if [ "$vservers_are_available" != "yes" ]
165 host_or_vservers='host'
169 # if there is one, set the previously chosen item as the default
170 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
171 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
172 "host" "Host system" \
173 "vserver" "One Vserver"
174 [ $? = 0 ] || return 1
177 host_or_vservers='host'
180 host_or_vservers='vservers'
186 ## If Vservers are not enabled, set host_or_vservers='host' and then return
187 ## Else, have the user choose the target he/she wants to perform the backup on:
188 ## - host system only;
189 ## - some vservers only;
190 ## - both the host system and some vservers.
191 ## Set, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
192 ## Returns 1 if cancelled.
194 choose_host_or_vservers_or_both() {
195 if [ "$vservers_are_available" != "yes" ]
197 host_or_vservers='host'
201 # if there is one, set the previously chosen item as the default
202 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
203 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
204 "host" "Host system only" \
205 "vservers" "Vservers only" \
206 "both" "Host system and Vservers"
207 [ $? = 0 ] || return 1
210 host_or_vservers='host'
213 host_or_vservers='vservers'
216 host_or_vservers='both'
222 ## Have the user choose among "all vservers" and a not-empty subset of these.
223 ## Set $selected_vservers to 'all' or to a space-separated name list.
224 ## Returns 1 if cancelled or if Vservers are not available.
226 choose_one_or_more_vservers() {
227 [ "$vservers_are_available" == "yes" ] || return 1
231 booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$selected_vservers" -o "$selected_vservers" == "all" ] || echo no`
233 selected_vservers="all"
235 # choose among the existing vservers
237 local vserver_was_selected=
239 while [ -z "$REPLY" ]; do
240 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
241 # list existing vservers, preselecting the previously selected ones
242 for vserver in $found_vservers; do
243 listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
245 listDisplay checklist
246 [ $? = 0 ] || return 1
248 # remove quotes around each vserver name
249 selected_vservers=`echo $REPLY | tr -d '"'`