stop services in reverse order
[rrq/overlay-boot.git] / overlay-stop
index 2c57c52f529610d65d8c8ddaffa5dccce6524d57..82961ec118b88eb53a6a98a7aeee1b53cc1791bd 100755 (executable)
@@ -2,16 +2,23 @@
 #
 # Script to stop the nominated overlay subhost
 
+OVERLAYDIR="$(dirname $(realpath $0))"
+
 [ $(id -u) = 0 ] || exec sudo $0 $@
-. $(dirname $(realpath $0))/functions XXX
+. $OVERLAYDIR/functions $*
+
+CONFIG="$1"
+[ -r "$CONFIG" ] || die "Missing configuration $CONFIG"
 
-NAME="$1"
+config NAME $(basename $1 .${1##*.})
+config START "ssh networking"
+config LIVE
 
 read USPID RSPID <<EOF
 $(is_live $NAME)
 EOF
 
-[ -z "$USPID" ] && echo "$NAME i snot running" >&2 && exit 1
+[ -z "$USPID" ] && echo "$NAME is snot running" >&2 && exit 1
 
 if [ -z "$RSPID" ] ; then
     cat <<EOF >&2
@@ -21,5 +28,16 @@ EOF
     exit 1
 fi
 
-CHILDPIDS="$(nsenter -t $RSPID -p -m ps -hopid --ppid 1)"
-nsenter -t $RSPID -p -m kill $CHILDPIDS
+# function to reverse the $* words
+reverse() {
+    local OUT=""
+    for w in $* ; do ; OUT="$w $OUT" ; done
+    echo "${OUT% }"
+}
+
+START="$(reverse "$START")"
+if nsenter -t $RSPID -p -m -i -u ip netns exec $NAME chroot $LIVE /bin/sh \
+          -c "for srv in $START ; do service \$srv stop ; done" ; then
+    CHILDPIDS="$(nsenter -t $RSPID -p -m ps -hopid --ppid 1)"
+    nsenter -t $RSPID -p -m kill $CHILDPIDS
+fi