#!/bin/bash
# nifty function copied from stackoverflow
kill_descendant_processes() {
local pid="$1"
local and_self="${2:-false}"
if children="$(pgrep -P "$pid")"; then
for child in $children; do
kill_descendant_processes "$child" true
done
fi
if [[ "$and_self" == true ]]; then
echo "killing $pid"
kill -9 "$pid"
fi
}
echo "Starting program at $(date)"
container_prefix=$1
# Spin 500 subprocess in parallel in background simulating 500 tasks sent to mesos slave/agent
# each of these subprocess then spawns another child subprocess in background that does docker run while it monitors the creation of the container
# if container creation not detected after a while, the subprocess kills all its child and its grandchild. So, both the child subprocess and docker process will get killed
# this closely mimics the mesos docker containerizer and mesos docker executor model
# program terminates when all subprocess exits
for count in {1..500}
do
(
(
docker pull nginx:latest
echo "Launching container ${container_prefix}_$count on $(date)"
docker run -p 80 --name ${container_prefix}_$count nginx
) &
COUNT=0
while true; do
sleep 1
docker inspect ${container_prefix}_$count > /dev/null
if [ $? -eq 0 ]; then
break
fi
let COUNT=COUNT+1
if [ $COUNT -gt 210 ]; then
echo "##################### Aborting tries of ${container_prefix}_$count at $(date)"
docker stop ${container_prefix}_$count
kill_descendant_processes $BASHPID
break
fi
done
)&
done
wait
echo "all work done at $(date)"