lib/vserver.in [init_vservers] : canonicalize VROOTDIR (since duplicity et al. don...
[matthijs/upstream/backupninja.git] / lib / vserver.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2
3 #####################################################
4 ## VSERVERS RELATED FUNCTIONS FOR NINJAHELPER
5 ##
6 ## Depends on:
7 ##    - easydialog library
8 ##    - $conffile
9 ##
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)
15 ##
16
17 ##
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.
28 ##
29 init_vservers() {
30    local arg=$1
31    # get global variables from the conffile
32    setfile $conffile
33    getconf vservers no
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
41    found_vservers=
42    selected_vservers=
43    host_or_vservers=host
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."
49             return
50       fi
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."
54             return
55       fi
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."
59             return
60       fi
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.";
64             return
65       fi
66       found_vservers=`ls $VROOTDIR | grep -E -v "lost+found|ARCHIVES" | tr "\n" " "`
67       [ -n "$found_vservers" ] || return
68       vservers_are_available=yes
69    fi
70 }
71
72 ##
73 ## If the argument is the name of a vserver selected use by the current helper,
74 ## echoes 'on' and returns 0.
75 ## Else, echoes 'off' and returns 1.
76 ##
77 vserver_is_selected() {
78    local vserver=$1
79    local vserver_is_selected=1
80    local i
81    for i in $selected_vservers ; do
82       [ "$vserver" == "$i" ] && vserver_is_selected=0
83    done
84    if [ $vserver_is_selected = 0 ]; then
85       echo on
86    else
87       echo off
88    fi
89    return $vserver_is_selected
90 }
91
92 ##
93 ## Have the user choose one Vserver among the existing ones.
94 ## Set $selected_vservers to the chosen one's name.
95 ## Returns 1 if cancelled or if Vservers are not available.
96 ##
97 choose_one_vserver() {
98    [ "$vservers_are_available" == "yes" ] || return 1
99    local title=$1
100    local i=
101    local vserver=
102    REPLY=
103    while [ -z "$REPLY" ]; do
104       [ -n "$selected_vservers" ] && setDefault $selected_vservers
105       listBegin "$title" "Choose at least one Linux-Vserver to backup:"
106             for vserver in $found_vservers; do
107                listItem "$vserver" "Backup $vserver vserver"
108             done
109       listDisplay menu
110       [ $? = 0 ] || return 1
111    done
112    selected_vservers=$REPLY
113 }
114
115 ##
116 ## If Vservers are not enabled, set host_or_vservers='host' and then return 
117 ## Else, have the user choose if he/she wants to perform the backup on the host
118 ## system or on one Vserver.
119 ## Set, respectively, $host_or_vservers to 'host' or 'vservers'.
120 ## Returns 1 if cancelled.
121 ##
122 choose_host_or_one_vserver() {
123    if [ "$vservers_are_available" != "yes" ] 
124    then
125        host_or_vservers='host'
126        return
127    fi
128    local title=$1
129    # if there is one, set the previously chosen item as the default
130    [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
131    menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
132       "host" "Host system" \
133       "vserver" "One Vserver"
134    [ $? = 0 ] || return 1
135    case $REPLY in
136       "host")
137          host_or_vservers='host'
138          ;;
139       "vserver")
140          host_or_vservers='vservers'
141          ;;
142    esac
143 }
144
145 ##
146 ## If Vservers are not enabled, set host_or_vservers='host' and then return
147 ## Else, have the user choose the target he/she wants to perform the backup on:
148 ##   - host system only;
149 ##   - some vservers only;
150 ##   - both the host system and some vservers.
151 ## Set, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
152 ## Returns 1 if cancelled.
153 ##
154 choose_host_or_vservers_or_both() {
155    if [ "$vservers_are_available" != "yes" ] 
156    then
157        host_or_vservers='host'
158        return
159    fi
160    local title=$1
161    # if there is one, set the previously chosen item as the default
162    [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
163    menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
164       "host" "Host system only" \
165       "vservers" "Vservers only" \
166       "both" "Host system and Vservers"
167    [ $? = 0 ] || return 1
168    case $REPLY in
169       "host")
170          host_or_vservers='host'
171          ;;
172       "vservers")
173          host_or_vservers='vservers'
174          ;;
175       "both")
176          host_or_vservers='both'
177          ;;
178    esac
179 }
180
181 ##
182 ## Have the user choose among "all vservers" and a not-empty subset of these.
183 ## Set $selected_vservers to 'all' or to a space-separated name list.
184 ## Returns 1 if cancelled or if Vservers are not available.
185 ##
186 choose_one_or_more_vservers() {
187    [ "$vservers_are_available" == "yes" ] || return 1
188    local title=$1
189    local i=
190    # UI
191    booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$selected_vservers" -o "$selected_vservers" == "all" ] || echo no`
192    if [ $? = 0 ]; then
193       selected_vservers="all"
194    else
195       # choose among the existing vservers
196       local vserver=
197       local vserver_was_selected=
198       REPLY=
199       while [ -z "$REPLY" ]; do
200          listBegin "$title" "Choose at least one Linux-Vserver to backup:"
201             # list existing vservers, preselecting the previously selected ones
202             for vserver in $found_vservers; do
203                listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
204             done
205          listDisplay checklist
206          [ $? = 0 ] || return 1
207       done
208       # remove quotes around each vserver name
209       selected_vservers=`echo $REPLY | tr -d '"'`
210    fi
211 }