initial checkin
[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 function findObject
19 {
20     local mod=$1
21     local var=$2
22     local file=
23     local i=X
24     shift 2
25     
26     for i; do
27         test "$i"        || continue
28         test ! $mod "$i" || { file=$i; break; }
29     done
30
31     test -z "$i" -o "$file" || {
32         echo "Can not find file for '$var'; aborting"
33         exit 1
34     } >&2
35
36     eval "$var=\"$file\""
37 }
38
39 function findFile
40 {
41     findObject -f "$@"
42 }
43
44 function findDir
45 {
46     findObject -d "$@"
47 }
48
49 function getPhysicalDir
50 {
51     ( set -P && cd "$1" && pwd )
52 }
53
54 function _pkgMountBindDir()
55 {
56     test "$1" != "$2" || return 0
57
58     mount -n --bind "$1" "$2"
59 }
60
61 function _pkgSetVarsBase
62 {
63     case "$vserver" in
64         /*)
65             echo "not supported yet"
66             exit 1
67             ;;
68         *)
69             BASEDIR=$CONFDIR/$vserver
70             test -d "$BASEDIR" || {
71                 echo "Can not find configuration-directory"
72                 exit 1
73             }
74             
75             VDIR=$BASEDIR/vdir
76             test -d "$VDIR"   || VDIR=/vservers/$vserver
77             VDIR=$(getPhysicalDir "$VDIR")
78             
79             PKGDIR=$BASEDIR/apps/pkgmgmt
80             test -d "$PKGDIR" || {
81                 echo "Can not find configuration-directory for package-managment tools"
82                 exit 1
83             }
84
85             findDir EXECDIR      $PKGDIR/execdir     /
86
87             ;;
88     esac
89 }
90
91 function _pkgSetVarsRPM
92 {
93     case "$vserver" in
94         /*)
95             echo "not supported yet"
96             exit 1
97             ;;
98             
99         *)
100             findDir RPMETCDIR    $PKGDIR/rpmetc      $PKGDIR/base/rpm/etc       /etc/rpm
101             findDir RPMSTATEDIR  $PKGDIR/rpmstate    $PKGDIR/base/rpm/state
102
103             findDir RPMLIBDIR    $PKGDIR/rpmlib  /
104
105             RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR")
106             RPMETCDIR=$(getPhysicalDir "$RPMETCDIR")
107             ;;
108     esac
109 }
110
111 function _pkgSetVarsApt
112 {
113     case "$vserver" in
114         /*)
115             echo "not supported yet"
116             exit 1
117             ;;
118             
119         *)
120             findDir APTETCDIR    $PKGDIR/aptetc      $PKGDIR/base/apt/etc       /etc/apt
121             findDir APTSTATEDIR  $PKGDIR/aptstate    $PKGDIR/base/apt/state
122             findDir APTCACHEDIR  $PKGDIR/aptcache    $PKGDIR/base/apt/cache
123             findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives  /var/cache/apt/archives
124             ;;
125     esac
126 }
127
128 function _pkgMountBase
129 {
130     :
131 }
132
133 function _pkgMountApt
134 {
135     :
136 }
137
138 function _pkgMountRPM
139 {
140     _pkgMountBindDir "$RPMETCDIR" /etc/rpm
141     test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
142     "$_SECURE_MOUNT" "$VDIR" "$RPMSTATEDIR" /var/lib/rpm
143 }
144
145 function _pkgSetEnvBase
146 {
147     test "$EXECDIR"   = "/" || {
148         PATH=$EXECDIR:$PATH
149         LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
150     }
151
152     export PATH LD_LIBRARY_PATH
153 }
154
155 function _pkgSetEnvApt
156 {
157     :
158 }
159
160 function _pkgSetEnvRPM
161 {
162     CUR_VSERVER=$vserver
163     RPM_FAKE_NAMESPACE_MOUNTS=/var/lib/rpm
164     RPM_BINARY=$_VRPM_PRELOAD
165
166     export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
167 }
168
169 function pkgInit
170 {
171     local i
172     local vserver=$1
173     shift
174     
175     _pkgSetVarsBase
176     for i; do
177         case "$i" in
178             rpm)        _pkgSetVarsRPM;;
179             apt)        _pkgSetVarsApt;;
180             *)          echo "Unknown packaging flavor"; exit 1;;
181         esac
182     done
183
184     _pkgMountBase
185     for i; do
186         case "$i" in
187             rpm)        _pkgMountRPM;;
188             apt)        _pkgMountApt;;
189         esac
190     done
191
192     _pkgSetEnvBase
193     for i; do
194         case "$i" in
195             rpm)        _pkgSetEnvRPM;;
196             apt)        _pkgSetEnvApt;;
197         esac
198     done
199
200     _PKG_FLAVORS="$@"
201     _PKG_VSERVER=$vserver
202 }
203
204 function getAllVservers
205 {
206     local i
207     local var=$1
208
209     for i in $CONFDIR/*; do
210         test   -d "$i"          || continue
211         test ! -e "$i"/disabled || continue
212         test   -d "$i"/vdir     || continue
213
214         eval "$var=\"$var ${i##$CONFDIR/}\""
215     done
216 }