-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathcleanup.sh
67 lines (64 loc) · 1.91 KB
/
cleanup.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
#!/usr/bin/env bash
#
# Shared cleanup routines between different steps
#
# Please source .ci/functions/imports.sh as a whole not just this file
#
# Version 1.0.0
# - Initial version after refactor
function cleanup_volume {
if [[ "$(docker volume ls -q -f name=$1)" ]]; then
echo -e "\033[34;1mINFO:\033[0m Removing volume $1\033[0m"
(docker volume rm "$1") || true
fi
}
function container_running {
if [[ "$(docker ps -q -f name=$1)" ]]; then
return 0;
else return 1;
fi
}
function cleanup_node {
if container_running "$1"; then
echo -e "\033[34;1mINFO:\033[0m Removing container $1\033[0m"
(docker container rm --force --volumes "$1") || true
fi
if [[ -n "$1" ]]; then
echo -e "\033[34;1mINFO:\033[0m Removing volume $1-${suffix}-data\033[0m"
cleanup_volume "$1-${suffix}-data"
fi
}
function cleanup_network {
if [[ "$(docker network ls -q -f name=$1)" ]]; then
echo -e "\033[34;1mINFO:\033[0m Removing network $1\033[0m"
(docker network rm "$1") || true
fi
}
function cleanup_trap {
status=$?
set +x
if [[ "$DETACH" != "true" ]]; then
echo -e "\033[34;1mINFO:\033[0m clean the network if not detached (start and exit)\033[0m"
cleanup_all_in_network "$1"
fi
# status is 0 or SIGINT
if [[ "$status" == "0" || "$status" == "130" ]]; then
echo -e "\n\033[32;1mSUCCESS run-tests\033[0m"
exit 0
else
echo -e "\n\033[31;1mFAILURE during run-tests\033[0m"
exit ${status}
fi
};
function cleanup_all_in_network {
if [[ -z "$(docker network ls -q -f name="^$1\$")" ]]; then
echo -e "\033[34;1mINFO:\033[0m $1 is already deleted\033[0m"
return 0
fi
containers=$(docker network inspect -f '{{ range $key, $value := .Containers }}{{ printf "%s\n" .Name}}{{ end }}' $1)
while read -r container; do
cleanup_node "$container"
done <<< "$containers"
cleanup_network $1
echo -e "\033[32;1mSUCCESS:\033[0m Cleaned up and exiting\033[0m"
};