blob: 421444edafcb5577fc73c06d838675e005722e93 [file] [log] [blame]
stevenvanrossem8372e2f2016-07-16 23:47:11 +02001#! /bin/bash -e
2
3# docker stop trap signals
4# https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.5d6q01x7q
5
6pid=0
7command=""
8term_recvd=0
9
10# send SIGTERM also to the executed command in the docker container (the containernet topology)
11# SIGTERM-handler
12function term_handler() {
13 echo $command
14 pid=$(pgrep -f "$command" | sed -n 1p)
15
16 pid="$!"
17 # avoid that the process triggers its own handler by sending sigterm
18 if [ $pid -ne 0 ] && [ $term_recvd -eq 0 ]; then
19 echo "sigterm received"
20 echo $pid
21 term_recvd=1
22 kill -SIGTERM "$pid"
23 fi
24
25 wait "$pid"
26
27 # do some manual cleanup
28 # remove all containers started by son-emu
29 docker ps -a -q --filter="name=mn.*" | xargs -r docker rm -f
30 # cleanup remaining mininet
31 mn -c
32
33 sleep 5
34 exit 143; # 128 + 15 -- SIGTERM
35}
36
37# setup handlers
38# on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
39trap 'term_handler' SIGTERM
40
41
42service openvswitch-switch start
43
44if [ ! -S /var/run/docker.sock ]; then
45 echo 'Error: the Docker socket file "/var/run/docker.sock" was not found. It should be mounted as a volume.'
46 exit 1
47fi
48
49# this cannot be done from the Dockerfile since we have the socket not mounted during build
50echo 'Pulling the "ubuntu:trusty" image ... please wait'
51docker pull 'ubuntu:trusty'
52
53echo "Welcome to Containernet running within a Docker container ..."
54
55if [[ $# -eq 0 ]]; then
56 exec /bin/bash
57else
58 #remember command to send it also the SIGTERM via the handler
59 command=$*
60 echo $command
61 exec $* &
62 # wait indefinetely
63 while true
64 do
65 sleep 1
66 done
67 echo "done"
68fi