- moved getFileValue/getFileArray from 'vserver.functions' into 'functions'
[util-vserver.git] / util-vserver / scripts / functions
1 # $Id$  --*- sh -*--
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
8 #  
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #  
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 _VS_NEWLINE='
19 '
20 declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
21
22 function findObject
23 {
24     local mod=$1
25     local var=$2
26     local file=
27     local i=X
28     shift 2
29     
30     for i; do
31         test "$i"        || continue
32         test ! $mod "$i" || { file=$i; break; }
33     done
34
35     test -z "$i" -o "$file" || {
36         echo "Can not find file for '$var'; aborting"
37         exit 1
38     } >&2
39
40     eval "$var=\"$file\""
41 }
42
43 function findFile
44 {
45     findObject -f "$@"
46 }
47
48 function findDir
49 {
50     findObject -d "$@"
51 }
52
53 function getPhysicalDir
54 {
55     ( set -P && cd "$1" && pwd )
56 }
57
58 function _pkgMountBindDir()
59 {
60     test "$1" != "$2" || return 0
61
62     mount -n --bind "$1" "$2"
63 }
64
65 function _pkgSetVarsBase
66 {
67     case "$vserver" in
68         /*)
69             if test -d "$vserver/vdir"; then
70                 BASEDIR=$vserver
71                 VDIR=$(getPhysicalDir "$vserver/vdir")
72                 
73                 PKGDIR=$BASEDIR/apps/pkgmgmt
74                 test -d "$PKGDIR" || {
75                     echo "Can not find configuration-directory for package-managment tools"
76                     exit 1
77                 }
78                 findDir EXECDIR      $PKGDIR/execdir     /
79             else
80                 VDIR=$(getPhysicalDir "$vserver")
81                 PKGDIR=
82             fi
83             ;;
84         *)
85             BASEDIR=$CONFDIR/$vserver
86             test -d "$BASEDIR" || {
87                 echo "Can not find configuration-directory"
88                 exit 1
89             }
90             
91             VDIR=$BASEDIR/vdir
92             test -d "$VDIR"   || VDIR=/vservers/$vserver
93             VDIR=$(getPhysicalDir "$VDIR")
94             
95             PKGDIR=$BASEDIR/apps/pkgmgmt
96             test -d "$PKGDIR" || {
97                 echo "Can not find configuration-directory for package-managment tools"
98                 exit 1
99             }
100
101             findDir EXECDIR      $PKGDIR/execdir     /
102
103             ;;
104     esac
105
106     if test -z "$WORKAROUND_106057"; then
107         _rpmdb_mntpoint=/dev
108     else
109         _rpmdb_mntpoint=/.rpmdb
110     fi
111 }
112
113 function _pkgSetVarsRPM
114 {
115     if test "$PKGDIR"; then
116         findDir RPMETCDIR    $PKGDIR/rpmetc      $PKGDIR/base/rpm/etc       /etc/rpm
117         findDir RPMSTATEDIR  $PKGDIR/rpmstate    $PKGDIR/base/rpm/state
118
119         findDir RPMLIBDIR    $PKGDIR/rpmlib      /
120
121     else
122         findDir RPMETCDIR    "$VDIR"/etc/rpm     /etc/rpm
123         findDir RPMSTATEDIR  "$VDIR"/var/lib/rpm
124         RPMLIBDIR=/
125     fi
126     
127     RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR")
128     RPMETCDIR=$(getPhysicalDir "$RPMETCDIR")
129 }
130
131 function _pkgSetVarsApt
132 {
133     if test "$PKGDIR"; then
134         findDir APTETCDIR    $PKGDIR/aptetc      $PKGDIR/base/apt/etc       /etc/apt
135         findDir APTSTATEDIR  $PKGDIR/aptstate    $PKGDIR/base/apt/state
136         findDir APTCACHEDIR  $PKGDIR/aptcache    $PKGDIR/base/apt/cache
137         findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives  /var/cache/apt/archives
138     else
139         findDir APTETCDIR    "$VDIR"/etc/apt            /etc/apt
140         findDir APTSTATEDIR  "$VDIR"/var/state/apt
141         findDir APTCACHEDIR  "$VDIR"/var/cache/apt
142         findDir APTARCHIVDIR "$VDIR"/var/cache/apt/archives /var/cache/apt/archives
143     fi
144 }
145
146 function _pkgMountBase
147 {
148     :
149 }
150
151 function _pkgMountApt
152 {
153     :
154 }
155
156 function _pkgMountRPM
157 {
158     _pkgMountBindDir "$RPMETCDIR" /etc/rpm
159     test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
160
161     "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
162     test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
163 }
164
165 function _pkgSetEnvBase
166 {
167     test "$EXECDIR"   = "/" || {
168         PATH=$EXECDIR:$PATH
169         LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
170     }
171
172     export PATH LD_LIBRARY_PATH
173 }
174
175 function _pkgSetEnvApt
176 {
177     :
178 }
179
180 function _pkgSetEnvRPM
181 {
182     CUR_VSERVER=$vserver
183     RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint
184     RPM_BINARY=$_VRPM_PRELOAD
185
186     export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
187 }
188
189 function pkgInit
190 {
191     local i
192     local vserver=$1
193     shift
194     
195     _pkgSetVarsBase
196     for i; do
197         case "$i" in
198             rpm)        _pkgSetVarsRPM;;
199             apt)        _pkgSetVarsApt;;
200             *)          echo "Unknown packaging flavor"; exit 1;;
201         esac
202     done
203
204     _pkgMountBase
205     for i; do
206         case "$i" in
207             rpm)        _pkgMountRPM;;
208             apt)        _pkgMountApt;;
209         esac
210     done
211
212     _pkgSetEnvBase
213     for i; do
214         case "$i" in
215             rpm)        _pkgSetEnvRPM;;
216             apt)        _pkgSetEnvApt;;
217         esac
218     done
219
220     _PKG_FLAVORS="$@"
221     _PKG_VSERVER=$vserver
222 }
223
224 function getAllVservers
225 {
226     local i
227     declare -a tmp=()
228
229     for i in $CONFDIR/*; do
230         test   -d "$i"          || continue
231         test ! -e "$i"/disabled || continue
232         test   -d "$i"/vdir     || continue
233
234         tmp=( "${tmp[@]}" "${i##$CONFDIR/}")
235     done
236
237     eval $1=\$tmp
238 }
239
240 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
241 function getVserverStatus
242 {
243     test -r "$1"/run || return 1
244     eval read "$2" <"$1"/run
245
246     test "$3"        || return 0
247     local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$ctx" | $_WC -l )
248     eval "$3"=\$_tmp
249
250     test "$4" -a $_tmp = 0 || return 0
251     _tmp=$(readlink "$1/run")
252     test "$_tmp"           || return 1
253     rm -f "$_tmp"
254     return 0
255 }
256
257 ## Usage: isVserverRunning <vdir> [<ctx-varname>]
258 function isVserverRunning
259 {
260     local ctx procnum
261
262     getVserverStatus "$1" ctx procnum 1 || return 1
263     test $procnum != 0                  || return 1
264     test -z "$2" || eval "$2"=\$ctx
265     return 0
266 }
267
268 ## Called as 'getFileValue <varname> <filename>'
269 function getFileValue
270 {
271     test -r "$2" || return 0
272     eval read "$1" <"$2"
273 }
274
275 ## Called as 'getFileArray <varname> <filename>'
276 function getFileArray
277 {
278     test -r "$2" || return 0
279     
280     local IFS=$_VS_NEWLINE
281     eval "$1"='( $(< "$2") )'
282 }