added package-management options (atleast their documention)
[util-vserver.git] / util-vserver / scripts / vserver
1 #! /bin/bash
2 # $Id$
3
4 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 #  
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
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 # set -e
20
21 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
22 test -e "$UTIL_VSERVER_VARS" || {
23     echo "Can not find util-vserver installation; aborting..."
24     exit 1
25 }
26 . "$UTIL_VSERVER_VARS"
27 . "$_LIB_FUNCTIONS"
28
29 ### Some local functions
30
31 function showHelp()
32 {
33     echo \
34 $"Usage: $(basename $0) [-s|--sync] [-v|--verbose]
35              [--] <vserver> <command> <args>*
36
37 <vserver> is the name of a vserver.
38
39 Possible commands are:
40     start       ... starts the specified vserver
41     stop        ... stops the specified vserver
42     restart     ... restarts the specified vserver; this is the subsequent
43                     execution of a synchronized 'stop' and a 'start'
44     condrestart ... restarts the vserver when it is running already
45     suexec <user> <shell-command> <args*>
46                 ... executes a command as the specified user in the vserver
47     exec <shell-command> <args*>
48                 ... executes a command as root in the vserver
49     enter       ... executes the configured shell in the vserver
50     chkconfig <chkconfig-options*>
51                 ... modifies the init-system; currently, only Red Hat's
52                     chkconfig is supported
53     running     ... succeeds iff the vserver is running
54     status      ... gives out some human readable status information about
55                     the vserver, and succeeds iff the vserver is running
56
57     build <buildopts>*
58                 ... builds a new vserver from scratch
59
60     apt-get,apt-config,apt-cache <apt-opts>*
61                 ... execute the apt-* command for the given vserver
62     rpm <rpm-opts>*
63                 ... execute the rpm command for the given vserver
64
65     pkgmgmt externalize|internalize [-y]
66                 ... externalize or internalize the package-management for the
67                     given vserver. 'Externalize' means that package metadata
68                     and management tools (apt-get,rpm) are living in the host,
69                     while 'internalize' means that data and programs from the
70                     vserver will be used.
71
72     unify <vunify-opts>*
73                 ... unify the vserver with its reference vserver(s).
74                     
75
76 Please report bugs to $PACKAGE_BUGREPORT"
77     exit 0
78 }
79
80 function showVersion()
81 {
82     echo \
83 $"vserver $PACKAGE_VERSION -- manages the state of vservers
84 This program is part of $PACKAGE_STRING
85
86 Copyright (C) 2003 Enrico Scholz
87 This program is free software; you may redistribute it under the terms of
88 the GNU General Public License.  This program has absolutely no warranty."
89     exit 0
90 }
91
92
93 ### main starts here
94
95 set +e
96
97 tmp=$(getopt -o +sv --long help,version,sync,verbose -n "$0" -- "$@") || exit 1
98 eval set -- "$tmp"
99
100 OPTION_FORCE_SYNC=
101 OPTION_VERBOSE=
102
103 while true; do
104     case "$1" in
105         --help)         showHelp $0 ;;
106         --version)      showVersion ;;
107         -v|--verbose)   OPTION_VERBOSE=1;    shift;;
108         -s|--sync)      OPTION_FORCE_SYNC=1; shift;;
109         --)             shift; break;;
110         *)              echo $"vserver: internal error; arg=='$1'"; exit 1;;
111     esac
112 done
113
114
115 vserver=$1
116 cmd=$2
117
118 test "$cmd" != build || { shift 2; exec "$_VSERVER_BUILD" -n "$vserver" "$@"; }
119
120
121 allow_legacy=
122
123 case "$vserver" in
124     ./*) VSERVER_DIR=`pwd`/$vserver;;
125     /*)  VSERVER_DIR=$vserver;;
126     *)   VSERVER_DIR=$CONFDIR/$vserver
127          allow_legacy=1
128          ;;
129 esac
130
131 if test "$allow_legacy"; then
132     do_legacy=
133     test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
134     test -d "$VSERVER_DIR" -o ! -e "$CONFDIR/$vserver.conf" || do_legacy=1
135
136     test -z "$do_legacy" || {
137         echo $"WARNING: can not find configuration, assuming legacy method"
138         exec "$_VSERVER_LEGACY" "$@"
139     }
140 fi
141
142 test -d "$VSERVER_DIR" || {
143     echo "Can not find vserver-setup"
144     exit 1
145 }
146
147 if test -e "$VSERVER_DIR"/name; then
148     read VSERVER_NAME <"$VSERVER_DIR"/name
149 else
150     VSERVER_NAME=$(basename "$VSERVER_DIR")
151 fi
152
153 . $PKGLIBDIR/vserver.functions
154 case "$2" in
155     start|stop|suexec)
156         shift 2
157         . $PKGLIBDIR/vserver.$cmd
158         ;;
159     restart)
160         "$0" --sync "$1" stop
161         exec "$0" "$1" start
162         ;;
163     condrestart)
164         test ! isVserverRunning "$VSERVER_DIR" || exec "$0" "$1" restart
165         ;;
166     exec)
167         shift 2
168         exec "$0" "$vserver" suexec root "$@"
169         ;;
170     chkconfig)
171         shift 2
172         exec "$0" "$vserver" suexec root chkconfig "$@"
173         ;;
174     enter)
175         getEnterShell "$VSERVER_DIR"
176         exec "$0" "$1" suexec root "${ENTER_SHELL[@]}"
177         ;;
178     running)
179         isVserverRunning "$VSERVER_DIR"
180         ;;
181
182     apt-get|apt-config|apt-cache)
183         export _APT_GET=$2
184         shift 2
185         exec $_VAPT_GET -- "$@"
186         ;;
187     rpm)
188         exec $_VRPM -- "$@"
189         ;;
190         
191     status)
192         if getVserverStatus "$VSERVER_DIR" ctx procnum; then
193             echo $"Vserver '$1' is running at context '$ctx'"
194
195             if test "$2" = status; then
196                 echo    $"Number of processes:" $procnum
197                 echo -n $"Uptime:              "
198                 "$_FILETIME" "$VSERVER_DIR/run"
199             fi
200             exit 0
201         else
202             echo $"Vserver '$1' is not running"
203             exit 1
204         fi
205         ;;
206     *)
207         echo $"Usage: $0 {start|stop|suexec|restart|condrestart|exec|enter|chkconfig|running|status}"
208         exit 2
209         ;;
210 esac