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 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 the argument is the name of a vserver selected use by the current helper,
78 ## echoes 'on' and returns 0.
79 ## Else, echoes 'off' and returns 1.
81 vserver_is_selected() {
83 local vserver_is_selected=1
85 for i in $selected_vservers ; do
86 [ "$vserver" == "$i" ] && vserver_is_selected=0
88 if [ $vserver_is_selected = 0 ]; then
93 return $vserver_is_selected
97 ## Have the user choose one Vserver among the existing ones.
98 ## Set $selected_vservers to the chosen one's name.
99 ## Returns 1 if cancelled or if Vservers are not available.
101 choose_one_vserver() {
102 [ "$vservers_are_available" == "yes" ] || return 1
107 while [ -z "$REPLY" ]; do
108 [ -n "$selected_vservers" ] && setDefault $selected_vservers
109 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
110 for vserver in $found_vservers; do
111 listItem "$vserver" "Backup $vserver vserver"
114 [ $? = 0 ] || return 1
116 selected_vservers=$REPLY
120 ## If Vservers are not enabled, set host_or_vservers='host' and then return
121 ## Else, have the user choose if he/she wants to perform the backup on the host
122 ## system or on one Vserver.
123 ## Set, respectively, $host_or_vservers to 'host' or 'vservers'.
124 ## Returns 1 if cancelled.
126 choose_host_or_one_vserver() {
127 if [ "$vservers_are_available" != "yes" ]
129 host_or_vservers='host'
133 # if there is one, set the previously chosen item as the default
134 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
135 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
136 "host" "Host system" \
137 "vserver" "One Vserver"
138 [ $? = 0 ] || return 1
141 host_or_vservers='host'
144 host_or_vservers='vservers'
150 ## If Vservers are not enabled, set host_or_vservers='host' and then return
151 ## Else, have the user choose the target he/she wants to perform the backup on:
152 ## - host system only;
153 ## - some vservers only;
154 ## - both the host system and some vservers.
155 ## Set, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
156 ## Returns 1 if cancelled.
158 choose_host_or_vservers_or_both() {
159 if [ "$vservers_are_available" != "yes" ]
161 host_or_vservers='host'
165 # if there is one, set the previously chosen item as the default
166 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
167 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
168 "host" "Host system only" \
169 "vservers" "Vservers only" \
170 "both" "Host system and Vservers"
171 [ $? = 0 ] || return 1
174 host_or_vservers='host'
177 host_or_vservers='vservers'
180 host_or_vservers='both'
186 ## Have the user choose among "all vservers" and a not-empty subset of these.
187 ## Set $selected_vservers to 'all' or to a space-separated name list.
188 ## Returns 1 if cancelled or if Vservers are not available.
190 choose_one_or_more_vservers() {
191 [ "$vservers_are_available" == "yes" ] || return 1
195 booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$selected_vservers" -o "$selected_vservers" == "all" ] || echo no`
197 selected_vservers="all"
199 # choose among the existing vservers
201 local vserver_was_selected=
203 while [ -z "$REPLY" ]; do
204 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
205 # list existing vservers, preselecting the previously selected ones
206 for vserver in $found_vservers; do
207 listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
209 listDisplay checklist
210 [ $? = 0 ] || return 1
212 # remove quotes around each vserver name
213 selected_vservers=`echo $REPLY | tr -d '"'`