-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchaos.sh
executable file
·87 lines (68 loc) · 2.35 KB
/
chaos.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
SKIP_WARNING=${SKIP_WARNING:-"0"}
DOCKER_LABEL=${DOCKER_LABEL:-"role=disposable"}
function delim {
echo "----------------------------------------------------------------------------"
}
if [ -z "${DOCKER_LABEL}" ]; then
echo 'DOCKER_LABEL not provided (empty). Usage: DOCKER_LABEL="role=disposable" ./chaos.sh'
exit 1
fi
color_yellow="\e[93m"
color_white="\e[97m"
color_green="\e[92m"
color_reset="\e[0m"
SERVICES=$(docker service ls --filter "label=${DOCKER_LABEL}" | tail -n +2)
if [ -z "${SERVICES}" ]; then
echo "No services with label: ${DOCKER_LABEL} - nothing to do."
exit 1
fi
if [[ "${SKIP_WARNING}" == "0" ]]; then
delim
echo "| Running this script will kill off 1 docker image with label: ${DOCKER_LABEL}"
echo "| You have 5 seconds to change your mind and CTRL+C out of this."
delim
echo -e "${color_white}${SERVICES}${color_reset}"
delim
sleep 5
echo
fi
# For example:
# mmqe8ua6xcdp gotwitter replicated 5/5 titpetric/gotwitter:latest
# zlca3i0z6hln gotwitter2 replicated 1/1 titpetric/gotwitter:latest
while read -r SERVICE; do
service=($SERVICE)
service_id=${service[0]}
service_name=${service[1]}
service_mode=${service[2]}
service_state=(${service[3]/\// })
echo -n "${service_id} ${service_name}: "
# don't kill services less than full replica set
if [[ "${service_state[0]}" != "${service_state[1]}" ]]; then
echo -e "service is degraded ${service[3]} - ${color_yellow}skipping${color_reset}"
continue
fi
# don't kill services with only one container
if [[ "${service_state[1]}" == "1" ]]; then
echo -e "service has only one running container - ${color_yellow}skipping${color_reset}"
continue
fi
# find a running container on the current node
NODE=$(uname -n)
echo $NODE
CONTAINER=$(docker service ps ${service_id} -f node=${NODE} -f 'desired-state=running' --no-trunc | tail -n +2 | head -n 1 | awk '{print $2 "." $1}')
if [ -z "${CONTAINER}" ]; then
echo -e "no containers running on current host - ${color_yellow}skipping${color_reset}"
continue
fi
# use docker rm -f (force remove) - container should disappear not just exit gracefully.
echo -e "${color_green}removing a container${color_reset}"
echo -e -n "${color_white}> "
docker rm -f $CONTAINER
echo -e "${color_reset}"
done <<< "$SERVICES"
# print out some final state
echo
delim
echo "Final service status:"
docker service ls