-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrazy-monkey.sh
executable file
·70 lines (60 loc) · 1.56 KB
/
crazy-monkey.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
USAGE='Usage: ./crazy-monkey.sh [OPTIONS]
\n\nOptions:
\n\t --dead-time \t Sets the time the dead container should remain stopped (default 1)
\n\t --sleep-time \t Sets the time between each kill (default 5)
\n\t --parallel \t Defines the number of parallel kill executions (default 3)
\n\t --containers-regex \t Regex to define the list of containers to use (default all)
\n\nRuns a crazy-monkey that randomly kills running docker containers.'
DEADTIME=1
SLEEPTIME=5
PARALLEL=3
REGEX="."
for i in "$@"
do
case $i in
-h|--help)
echo -e $USAGE
exit 1
;;
--dead-time=*)
DEADTIME="${i#*=}"
;;
--sleep-time=*)
SLEEPTIME="${i#*=}"
;;
--parallel=*)
PARALLEL="${i#*=}"
;;
--containers-regex=*)
REGEX="${i#*=}"
;;
esac
done
echo "$(date -u +"%F %T") Running crazy-monkey with parameters: dead-time=$DEADTIME; sleep-time=$SLEEPTIME; parallel=$PARALLEL; regex=$REGEX."
kill () {
DEADID=$(docker ps -a --format '{{.Names}} {{.ID }}' | grep -P $REGEX | awk '{print $2}' | xargs shuf -n1 -e)
DEADNAME=$(docker ps -a --format '{{.Names}} {{.ID }}' | grep $DEADID | awk '{print $1}')
trap 'start $DEADID $DEADNAME; exit 1' INT
stop $DEADID $DEADNAME
sleep $DEADTIME
start $DEADID $DEADNAME
}
start () {
docker start $1 > /dev/null
echo "$(date -u +"%F %T") Container $2 is back alive."
}
stop () {
echo "$(date -u +"%F %T") Killing container $2."
docker stop $1 > /dev/null
}
while true
do
for ((i=1; i<=$PARALLEL; i++))
do
kill &
done
wait
echo -e "$(date -u +"%F %T") Killed containers are back alive.\n"
sleep $SLEEPTIME
done