aboutsummaryrefslogtreecommitdiff
path: root/lib/vserver.in
blob: cc96a71050215b063d897800875e6f813298c099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-

#####################################################
## VSERVERS RELATED FUNCTIONS FOR NINJAHELPER
##
## Depends on:
##    - easydialog library
##    - $conffile
##
## Global variables used and modified here:
##    - $vservers_are_available (yes/no)
##    - $found_vservers (list)
##    - $selected_vservers (list)
##    - $host_or_vservers (host/vservers/both)
##

##
## Get vservers-related variables.
## Then, if Vservers are enabled, check that:
##   - VROOTDIR is valid;
##   - at least one vserver can be found.
## If, and only if, the above conditions are all true:
##   - 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 [ -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
      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 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.
##
vserver_is_selected() {
   local vserver=$1
   local vserver_is_selected=1
   local i
   for i in $selected_vservers ; do
      [ "$vserver" == "$i" ] && vserver_is_selected=0
   done
   if [ $vserver_is_selected = 0 ]; then
      echo on
   else
      echo off
   fi
   return $vserver_is_selected
}

##
## Have the user choose one Vserver among the existing ones.
## Set $selected_vservers to the chosen one's name.
## Returns 1 if cancelled or if Vservers are not available.
##
choose_one_vserver() {
   [ "$vservers_are_available" == "yes" ] || return 1
   local title=$1
   local i=
   local vserver=
   REPLY=
   while [ -z "$REPLY" ]; do
      [ -n "$selected_vservers" ] && setDefault $selected_vservers
      listBegin "$title" "Choose at least one Linux-Vserver to backup:"
         for vserver in $found_vservers; do
            listItem "$vserver" "Backup $vserver vserver"
         done
      listDisplay menu
      [ $? = 0 ] || return 1
   done
   selected_vservers=$REPLY
}

##
## 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() {
   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
   menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
      "host" "Host system" \
      "vserver" "One Vserver"
   [ $? = 0 ] || return 1
   case $REPLY in
      "host")
         host_or_vservers='host'
         ;;
      "vserver")
         host_or_vservers='vservers'
         ;;
   esac
}

##
## 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;
##   - both the host system and some vservers.
## Set, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
## Returns 1 if cancelled.
##
choose_host_or_vservers_or_both() {
   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
   menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
      "host" "Host system only" \
      "vservers" "Vservers only" \
      "both" "Host system and Vservers"
   [ $? = 0 ] || return 1
   case $REPLY in
      "host")
         host_or_vservers='host'
         ;;
      "vservers")
         host_or_vservers='vservers'
         ;;
      "both")
         host_or_vservers='both'
         ;;
   esac
}

##
## Have the user choose among "all vservers" and a not-empty subset of these.
## Set $selected_vservers to 'all' or to a space-separated name list.
## Returns 1 if cancelled or if Vservers are not available.
##
choose_one_or_more_vservers() {
   [ "$vservers_are_available" == "yes" ] || return 1
   local title=$1
   local i=
   # UI
   booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$selected_vservers" -o "$selected_vservers" == "all" ] || echo no`
   if [ $? = 0 ]; then
      selected_vservers="all"
   else
      # choose among the existing vservers
      local vserver=
      local vserver_was_selected=
      REPLY=
      while [ -z "$REPLY" ]; do
         listBegin "$title" "Choose at least one Linux-Vserver to backup:"
            # list existing vservers, preselecting the previously selected ones
            for vserver in $found_vservers; do
               listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
            done
         listDisplay checklist
         [ $? = 0 ] || return 1
      done
      # remove quotes around each vserver name
      selected_vservers=`echo $REPLY | tr -d '"'`
   fi
}