added a more efficient stop method for fakeinit vservers
[util-vserver.git] / util-vserver / scripts / vpkg
1 #!/bin/bash
2
3 # Copyright (C) 2004 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; either version 2, or (at your option)
8 # any later version.
9 #  
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #  
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
20 test -e "$UTIL_VSERVER_VARS" || {
21     echo "Can not find util-vserver installation; aborting..." >&2
22     exit 1
23 }
24 . "$UTIL_VSERVER_VARS"
25
26 function showHelp()
27 {
28     echo \
29 $"Usage: $0 <vserver-name> <tag>
30
31 Report bugs to <$PACKAGE_BUGREPORT>."
32     exit 0
33 }
34
35 function showVersion()
36 {
37     echo $"\
38 pkgmgmt-info $PACKAGE_VERSION -- shows information about packages in vservers
39 This program is part of $PACKAGE_STRING
40
41 Copyright (C) 2004 Enrico Scholz
42 This program is free software; you may redistribute it under the terms of
43 the GNU General Public License.  This program has absolutely no warranty."
44     exit 0
45 }
46
47 function guessStyle()
48 {
49     local vdir=$($_VSERVER_INFO "$1" VDIR) || {
50         echo $"Can not determine vserver-root" >&2
51         exit 1
52     }
53
54     if test -e "$vdir"/etc/redhat-release -o -e "$vdir"/etc/fedora-release; then
55         style=redhat
56     elif test -e "$vdir"/etc/mandrake-release; then
57         style=mandrake
58     elif test -e "$vdir"/etc/debian_version; then
59         style=debian
60     else
61         echo $"Can not determine packagemanagement style" >&2
62     fi
63 }
64
65 case "$1" in
66     (--help)    showHelp $(basename $0);;
67     (--version) showVersion ;;
68 esac
69
70 test "$1" -a "$2" || {
71     echo $"No vserver and/or tag given; use '--help' for more information" >&2
72     exit 1
73 }
74
75 vserver=$1
76 tag=$2
77 shift 2
78
79 case "$tag" in
80     get-conffiles|install)      ;;
81     *)          echo $"Unsupport tag '$tag'" >&2; exit 1;;
82 esac
83
84 cfgdir=$($_VSERVER_INFO "$vserver" APPDIR pkgmgmt) || :
85 vdir=$($_VSERVER_INFO "$1" VDIR) || :
86
87 style=
88 is_external=
89 {   read style <"$cfgdir"/style; } 2>/dev/null
90 test "$style" || guessStyle "$vserver"
91
92 test ! -d "$cfgdir" -o -e "$cfgdir"/internal || is_external=1
93
94
95 cmd=()
96
97 case "$style" in
98     (redhat|mandrake)
99         rpm_param=
100         apt_param=
101         case "$tag" in
102             ## rpm outputs sometimes '(contains no files)', so return
103             ## only the valid output
104             (get-conffiles)
105                 rpm_param=( -qac --pipe "sed '\!^/!p;d'" );;
106             (install)
107                 rpm_param=( -Uvh "$@" )
108                 apt_param=( install "$@" )
109                 ;;
110         esac
111         
112         if test "$is_external"; then
113             have_apt=1
114             test -d "$cfgdir"/base/apt -o -d "$cfgdir"/aptetc || have_apt=
115         else
116             have_apt=
117             for i in /bin /usr/bin /usr/local/bin; do
118                 test ! -x "$vdir$i"/apt-get || { have_apt=1; break; }
119             done
120         fi
121         
122         if test "$is_external"; then
123             if test "$have_apt" -a "$apt_param"; then
124                 cmd=( "$_VAPT_GET" "$vserver" -- "${apt_param[@]}" )
125             else
126                 cmd=( "$_VRPM" "$vserver" -- "${rpm_param[@]}" )
127             fi
128         else
129             if test "$have_apt" -a "$apt_param"; then
130                 cmd=( "$_VSERVER" --silent "$vserver" exec apt-get "${apt_param[@]}" )
131             else
132                 cmd=( "$_VSERVER" --silent "$vserver" exec rpm "${rpm_param[@]}" )
133             fi
134         fi
135         ;;
136     (debian)
137         case "$tag" in
138             (get-conffiles)
139                 cmd=( sh -c "cat /var/lib/dpkg/info/*.conffiles 2>/dev/null" )
140                 ;;
141             (install)
142                 cmd=( apt-get install "$@" )
143                 ;;
144         esac
145
146         if test "$is_external"; then
147             echo $"'external' packagemanagement is not supported for Debian" >&2
148             exit 1
149         else
150             cmd=( "$_VSERVER" --silent "$vserver" exec "${cmd[@]}" )
151         fi
152         ;;
153 esac
154
155 export LANG=C
156 exec "${cmd[@]}"