gentoo: use /var/run for new /run compatibility
[util-vserver.git] / scripts / start-vservers
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; 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 # Usage: start-vservers [-c <CFGDIR>] [-m <MARK>] [-j <NUM>] [--start|--stop|--status|--condrestart|--restart] [--test] [--all] [--debug] -- <name>+
19
20 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
21 test -e "$UTIL_VSERVER_VARS" || {
22     echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
23     exit 1
24 }
25 . "$UTIL_VSERVER_VARS"
26 . "$_LIB_FUNCTIONS"
27
28 ### Some local functions
29
30 function showHelp()
31 {
32     echo \
33 $"Usage: $(basename $0) [-c <CFGDIR>] [-m <MARK>] [-j <NUM] [--test]
34              [--start|--stop] [--all|--[un]marked|--running|--stopped] -- <name>+
35
36 Please report bugs to $PACKAGE_BUGREPORT"
37     exit 0
38 }
39
40
41 function showVersion()
42 {
43     echo \
44 $"start-vserver $PACKAGE_VERSION -- starts/stops a bunch of vservers
45 This program is part of $PACKAGE_STRING
46
47 Copyright (C) 2004 Enrico Scholz
48 This program is free software; you may redistribute it under the terms of
49 the GNU General Public License.  This program has absolutely no warranty."
50     exit 0
51 }
52
53 function verifyVserver()
54 {
55     local xtra="${2:+ mentioned in '$2'}"
56     case x$1 in
57         (x\#*|x)
58                 return 1;;      # skip empty and comment lines
59         ($__CONFDIR/*)
60                 warning "This version of 'start-vservers' supports only short vserver names; try to remove the '$__CONFDIR' from '$1'$xtra"
61                 return 1
62                 ;;
63         (/*)
64                 warning "This version of 'start-vservers' supports only short vserver names; '$1'$xtra is not possible"
65                 return 1
66                 ;;
67         (*)
68                 $_VSERVER_INFO -q "$__CONFDIR/$1" VDIR || {
69                         warning "Vserver '$1'$xtra does not exist; skipping it..."
70                         return 1;
71                 }
72                 ;;
73     esac
74
75     return 0
76 }
77
78 ###
79
80 set +e
81
82
83 tmp=$(getopt -o c:j:m: \
84       --long debug,help,version,start,stop,test,$VS_ALLVSERVERS_ARGS \
85       -n "$0" -- "$@") || exit 1
86 eval set -- "$tmp"
87
88 declare -a tmp_vservers=()
89 declare -r TAB=$(echo -en "\t")
90 OPTION_PARALLEL=99
91 OPTION_DEBUG=
92 NOOPTION_DEBUG=1
93
94 case "`basename $0`" in
95     start-*)    OPTION_FLAVOR=start;;
96     stop-*)     OPTION_FLAVOR=stop;;
97     *)          OPTION_FLAVOR=;;
98 esac
99
100 gav_args=()
101 while true; do
102     case "$1" in
103         (--help)        showHelp    $0 ;;
104         (--version)     showVersion $0 ;;
105         (-c)            CONFDIR=$2;     shift;;
106         (-j)            OPTION_PARALLEL=$2; shift;;
107         (--start)       OPTION_FLAVOR=start;;
108         (--stop)        OPTION_FLAVOR=stop;;
109         (--debug)       OPTION_DEBUG=1; NOOPTION_DEBUG=; set -x;;
110         (--)            shift; break;;
111         (-m)            gav_args=( "${gav_args[@]}" --mark "$2" )
112                         shift
113                         ;;
114         (*)             gav_args=( "${gav_args[@]}" "$1" );;
115     esac
116     shift
117 done
118
119 if test "${#gav_args[@]}" -gt 0; then
120     if ! getAllVserversByArg tmp_vservers "${gav_args[@]}"; then
121         echo $"$0: internal error; arg=='$1'" >&2
122         exit 1
123     fi
124 fi
125
126 test -n "$OPTION_FLAVOR" || {
127     echo "$0: unknown invocation method; aborting..." >&2
128     exit 1
129 }
130
131 vservers=( "$@" "${tmp_vservers[@]}" )
132
133 makedir=$($_MKTEMPDIR vserver-init.XXXXXX)
134 okfile=$($_MKTEMP     vserver-init.XXXXXX)
135 passedfile=$($_MKTEMP vserver-init.XXXXXX)
136 trap "$_RM -rf $makedir $okfile $passedfile" EXIT
137
138 test_cmd=false
139 case "$OPTION_FLAVOR" in
140     start)      test_cmd="${_VSERVER}   --silent '\$*' status";;
141     stop)       test_cmd="! ${_VSERVER} --silent '\$*' status";;
142 esac
143
144 {
145     cat <<EOF
146 .%.stamp:
147 ${TAB}$test_cmd || { \
148 ${TAB}echo -n '.' >>$passedfile ; \
149 ${TAB}$_VSERVER --defaulttty --sync ${OPTION_DEBUG:+--debug} "\$*" ${OPTION_FLAVOR}; }
150 ${TAB}echo -n '.' >>$okfile
151 ${TAB}@touch "\$@"
152 EOF
153
154     echo -ne "all:\t"
155     for i in "${vservers[@]}"; do
156         verifyVserver "$i" || continue
157
158         echo -n ".$i.stamp "
159     done
160     echo
161 } >$makedir/Makefile
162
163 for i in "${vservers[@]}"; do
164     d="$__CONFDIR/$i"/apps/init
165     echo "$i"
166     test -e "$d"/depends || continue
167     cat "$d"/depends
168 done | sort -u | while read vserver; do
169     verifyVserver "$vserver" || continue
170     d="$__CONFDIR/$vserver"/apps/init
171
172     case "$OPTION_FLAVOR" in
173         (start)
174             if test -e "$d"/depends; then
175                 echo -ne ".$vserver.stamp:\t"
176                 cat "$d"/depends | while read dep; do
177                     verifyVserver "$dep" "$d"/depends || continue
178                     echo -n ".$dep.stamp "
179                 done
180                 echo
181             fi >>$makedir/Makefile
182             ;;
183         (stop)
184             if test -e "$d"/depends; then
185                 cat "$d"/depends | while read dep; do
186                     verifyVserver "$dep" "$d"/depends || continue
187                     echo -e ".$dep.stamp:\t.$vserver.stamp"
188                 done
189                 echo
190             fi >>$makedir/Makefile
191     esac
192 done
193
194 #cat $makedir/Makefile
195 make -k ${NOOPTION_DEBUG:+-s} ${OPTION_PARALLEL:+-j$OPTION_PARALLEL} -C $makedir
196
197 test  -s "$passedfile"           || exit 0
198 test  -s "$okfile"               || exit 1
199 $_CMP -s "$passedfile" "$okfile" || exit 2
200 exit 0