made it more mature by using arrays for the vserver-list
[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) [--] <vserver> <command> <args>*
35
36 <vserver> is the name of a vserver.
37
38 Possible commands are:
39     start       ... starts the specified vserver
40     stop        ... stops the specified vserver
41     restart     ... restarts the specified vserver; this is the subsequent
42                     execution of a synchronized 'stop' and a 'start'
43     condrestart ... restarts the vserver when it is running already
44     suexec <user> <shell-command> <args*>
45                 ... executes a command as the specified user in the vserver
46     exec <shell-command> <args*>
47                 ... executes a command as root in the vserver
48     enter       ... executes the configured shell in the vserver
49     chkconfig <chkconfig-options*>
50                 ... modifies the init-system; currently, only Red Hat's
51                     chkconfig is supported
52     running     ... succeeds iff the vserver is running
53     status      ... gives out some human readable status information about
54                     the vserver, and succeeds iff the vserver is running
55
56     build <buildopts>*
57                 ... builds a new vserver from scratch
58
59 Please report bugs to $PACKAGE_BUGREPORT"
60     exit 0
61 }
62
63 function showVersion()
64 {
65     echo \
66 $"vserver $PACKAGE_VERSION -- manages the state of vservers
67 This program is part of $PACKAGE_STRING
68
69 Copyright (C) 2003 Enrico Scholz
70 This program is free software; you may redistribute it under the terms of
71 the GNU General Public License.  This program has absolutely no warranty."
72     exit 0
73 }
74
75
76 ### main starts here
77
78 set +e
79
80 tmp=$(getopt -o + --long help,version -n "$0" -- "$@") || exit 1
81 eval set -- "$tmp"
82
83 while true; do
84     case "$1" in
85         --help)    showHelp $0 ;;
86         --version) showVersion ;;
87         --)        shift; break;;
88         *)         echo $"vserver: internal error; arg=='$1'"; exit 1;;
89     esac
90 done
91
92 test "$2" != build || { shift; exec "$_VSERVER_BUILD" "$@"; }
93
94 vserver=$1
95 cmd=$2
96
97 allow_legacy=
98
99 case "$vserver" in
100     ./*) VSERVER_DIR=`pwd`/$vserver;;
101     /*)  VSERVER_DIR=$vserver;;
102     *)   VSERVER_DIR=$CONFDIR/$vserver
103          allow_legacy=1
104          ;;
105 esac
106
107 if test "$allow_legacy"; then
108     do_legacy=
109     test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
110     test -d "$VSERVER_DIR" -o ! -e "$CONFDIR/$vserver.conf" || do_legacy=1
111
112     test -z "$do_legacy" || {
113         echo $"WARNING: can not find configuration, assuming legacy method"
114         exec "$_VSERVER_LEGACY" "$@"
115     }
116 fi
117
118 test -d "$VSERVER_DIR" || {
119     echo "Can not find vserver-setup"
120     exit 1
121 }
122
123 if test -e "$VSERVER_DIR"/name; then
124     read VSERVER_NAME <"$VSERVER_DIR"/name
125 else
126     VSERVER_NAME=$(basename "$VSERVER_DIR")
127 fi
128
129 . $PKGLIBDIR/vserver.functions
130 case "$2" in
131     start|stop|suexec)
132         shift 2
133         . $PKGLIBDIR/vserver.$cmd
134         ;;
135     restart)
136         "$0" "$1" stop
137         exec "$0" "$1" start
138         ;;
139     condrestart)
140         test ! isVserverRunning "$VSERVER_DIR" || exec "$0" "$1" restart
141         ;;
142     exec)
143         shift 2
144         exec "$0" "$vserver" suexec root "$@"
145         ;;
146     chkconfig)
147         shift 2
148         exec "$0" "$vserver" suexec root chkconfig "$@"
149         ;;
150     enter)
151         getEnterShell "$VSERVER_DIR"
152         exec "$0" "$1" suexec root "${ENTER_SHELL[@]}"
153         ;;
154     running)
155         isVserverRunning "$VSERVER_DIR"
156         ;;
157     status)
158         if getVserverStatus "$VSERVER_DIR" ctx procnum; then
159             echo $"Vserver '$1' is running at context '$ctx'"
160
161             if test "$2" = status; then
162                 echo    $"Number of processes:" $procnum
163                 echo -n $"Uptime:              "
164                 "$_FILETIME" "$VSERVER_DIR/run"
165             fi
166             exit 0
167         else
168             echo $"Vserver '$1' is not running"
169             exit 1
170         fi
171         ;;
172     *)
173         echo $"Usage: $0 {start|stop|suexec|restart|condrestart|exec|enter|chkconfig|running|status}"
174         exit 2
175         ;;
176 esac