cleanups; full parenthesis for case ... esac statements
[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:=/usr/lib/util-vserver/util-vserver-vars}
22 test -e "$UTIL_VSERVER_VARS" || {
23     echo "Can not find util-vserver installation; aborting..." >&2
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] [--silent]
35              [--] <vserver> <command> <args>*
36
37 <vserver> is the name of a vserver.
38
39 Possible commands are:
40     start [--nodeps] <start-vservers-opts>*
41                 ... starts the specified vserver
42     stop [--nodeps] <start-vservers-opts>*
43                 ... stops the specified vserver
44     restart     ... restarts the specified vserver; this is the subsequent
45                     execution of a synchronized 'stop' and a 'start'
46     condrestart ... restarts the vserver when it is running already
47     suexec <user> <shell-command> <args*>
48                 ... executes a command as the specified user in the vserver
49     exec <shell-command> <args*>
50                 ... executes a command as root in the vserver
51     enter       ... executes the configured shell in the vserver
52     chkconfig <chkconfig-options*>
53                 ... modifies the init-system; currently, only Red Hat's
54                     chkconfig is supported
55     running     ... succeeds iff the vserver is running
56     status      ... gives out some human readable status information about
57                     the vserver, and succeeds iff the vserver is running
58
59     build <buildopts>*
60                 ... builds a new vserver from scratch
61
62     unify [-R]
63                 ... (de)unify vserver
64                 
65     pkg install <pkg>
66                 ... installs package(s) in the vserver
67                 
68     apt-get,apt-config,apt-cache <apt-opts>*
69                 ... execute the apt-* command for the given vserver
70     rpm <rpm-opts>*
71                 ... execute the rpm command for the given vserver
72
73     pkgmgmt externalize|internalize [-y]
74                 ... externalize or internalize the package-management for the
75                     given vserver. 'Externalize' means that package metadata
76                     and management tools (apt-get,rpm) are living in the host,
77                     while 'internalize' means that data and programs from the
78                     vserver will be used.
79
80     unify <vunify-opts>*
81                 ... unify the vserver with its reference vserver(s).
82                     
83
84 Please report bugs to $PACKAGE_BUGREPORT"
85     exit 0
86 }
87
88 function showVersion()
89 {
90     echo \
91 $"vserver $PACKAGE_VERSION -- manages the state of vservers
92 This program is part of $PACKAGE_STRING
93
94 Copyright (C) 2003 Enrico Scholz
95 This program is free software; you may redistribute it under the terms of
96 the GNU General Public License.  This program has absolutely no warranty."
97     exit 0
98 }
99
100 function suexec()
101 {
102     . $PKGLIBDIR/vserver.suexec
103 }
104
105 function restart()
106 {
107     "${SELF[@]}" --sync "$vserver" stop
108     exec "${SELF[@]}" "$vserver" start
109 }
110
111 function msg()
112 {
113     test -z "$OPTION_SILENT" || return 0
114     echo "$@"
115 }
116
117 ### main starts here
118
119 set +e
120
121 OPTIONS_ORIG=( "$@" )
122 tmp=$(getopt -o +sv --long nonamespace,--nonamespace,help,debug,version,sync,verbose,silent -n "$0" -- "$@") || exit 1
123 eval set -- "$tmp"
124
125 OPTION_FORCE_SYNC=
126 OPTION_VERBOSE=
127 OPTION_SILENT=
128 OPTION_DEBUG=
129 OPTION_NONAMESPACE=
130
131 while true; do
132     case "$1" in
133         (--help)        showHelp $0 ;;
134         (--version)     showVersion ;;
135         (--debug)       OPTION_DEBUG=$1; set -x;;
136         (-v|--verbose)  OPTION_VERBOSE=$1;;
137         (-s|--sync)     OPTION_FORCE_SYNC=$1;;
138         (--silent)      OPTION_SILENT=$1;;
139         (----nonamespace)OPTION_NONAMESPACE=$1;;
140         (--)            shift; break;;
141         (*)             echo $"vserver: internal error; arg=='$1'" >&2; exit 1;;
142     esac
143     shift
144 done
145
146 OPTION_ALL=( $OPTION_SILENT $OPTION_VERBOSE $OPTION_DEBUG )
147 SELF=( "$0" "${OPTION_ALL[@]}" )
148
149 vserver=$1
150 cmd=$2
151
152 test "$cmd" != build || { shift 2; exec "$_VSERVER_BUILD" -n "$vserver" "$@"; }
153
154
155 allow_legacy=
156
157 case "$vserver" in
158     (./*) VSERVER_DIR=`pwd`/$vserver;;
159     (/*)  VSERVER_DIR=$vserver;;
160     (*)   VSERVER_DIR=$CONFDIR/$vserver
161           allow_legacy=1
162           ;;
163 esac
164
165 if test "$allow_legacy"; then
166     do_legacy=
167     test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
168     test -d "$VSERVER_DIR" -o ! -e "$CONFDIR/$vserver.conf" || do_legacy=1
169
170     test -z "$do_legacy" || {
171         echo $"WARNING: can not find configuration, assuming legacy method" >&2
172         exec "$_VSERVER_LEGACY" "$@"
173     }
174 fi
175
176 test -d "$VSERVER_DIR" || {
177     echo $"\
178 Can not find vserver-setup; please make sure that the vserver configuration
179 is located at $VSERVER_DIR/."
180     exit 1
181 } >&2
182
183 if test -e "$VSERVER_DIR"/name; then
184     read VSERVER_NAME <"$VSERVER_DIR"/name
185 else
186     VSERVER_NAME=$(basename "$VSERVER_DIR")
187 fi
188
189 test "$2" != start -o "$OPTION_NONAMESPACE" || isAvoidNamespace "$VSERVER_DIR" || \
190     exec $_VNAMESPACE --new -- $_VSERVER ----nonamespace "${OPTIONS_ORIG[@]}"
191
192 . $PKGLIBDIR/vserver.functions
193 case "$2" in
194     (start|stop)
195         shift 2
196         . $PKGLIBDIR/vserver.$cmd
197         ;;
198     (suexec|restart)
199         shift 2
200         $cmd "$@"
201         ;;
202     (condrestart)
203         test ! isVserverRunning "$VSERVER_DIR" || restart
204         ;;
205     (exec)
206         shift 2
207         suexec root "$@"
208         ;;
209     (chkconfig)
210         shift 2
211         suexec root chkconfig "$@"
212         ;;
213     (enter)
214         getEnterShell "$VSERVER_DIR"
215         suexec root "${ENTER_SHELL[@]}"
216         ;;
217     (running)
218         isVserverRunning "$VSERVER_DIR"
219         ;;
220
221     (unify)
222         shift 2
223         exec $_VUNIFY "$@" "$vserver"
224         ;;
225         
226     (pkg)
227         shift 2
228         exec $_VPKG "$vserver" "$@"
229         ;;
230
231     (apt-get|apt-config|apt-cache)
232         export _APT_GET=$2
233         shift 2
234         exec $_VAPT_GET -- "$@"
235         ;;
236     (rpm)
237         exec $_VRPM -- "$@"
238         ;;
239         
240     (status)
241         if getVserverStatus "$VSERVER_DIR" ctx procnum; then
242             msg $"Vserver '$vserver' is running at context '$ctx'"
243
244             if test "$2" = status; then
245                 msg $"Number of processes: " $procnum
246                 msg $"Uptime:              "    $("$_FILETIME" "$VSERVER_DIR/run")
247             fi
248             exit 0
249         else
250             msg $"Vserver '$vserver' is not running"
251             exit 1
252         fi
253         ;;
254     (*)
255         echo $"Usage: $0 {start|stop|suexec|restart|condrestart|exec|enter|chkconfig|running|status}" >&2
256         exit 2
257         ;;
258 esac