|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Shared cleanup routines between different steps |
| 4 | +# |
| 5 | +# Please source .ci/functions/imports.sh as a whole not just this file |
| 6 | +# |
| 7 | +# Version 1.0.0 |
| 8 | +# - Initial version after refactor |
| 9 | + |
| 10 | +function cleanup_volume { |
| 11 | + if [[ "$(docker volume ls -q -f name=$1)" ]]; then |
| 12 | + echo -e "\033[34;1mINFO:\033[0m Removing volume $1\033[0m" |
| 13 | + (docker volume rm "$1") || true |
| 14 | + fi |
| 15 | +} |
| 16 | +function container_running { |
| 17 | + if [[ "$(docker ps -q -f name=$1)" ]]; then |
| 18 | + return 0; |
| 19 | + else return 1; |
| 20 | + fi |
| 21 | +} |
| 22 | +function cleanup_node { |
| 23 | + if container_running "$1"; then |
| 24 | + echo -e "\033[34;1mINFO:\033[0m Removing container $1\033[0m" |
| 25 | + (docker container rm --force --volumes "$1") || true |
| 26 | + fi |
| 27 | + if [[ -n "$1" ]]; then |
| 28 | + echo -e "\033[34;1mINFO:\033[0m Removing volume $1-${suffix}-data\033[0m" |
| 29 | + cleanup_volume "$1-${suffix}-data" |
| 30 | + fi |
| 31 | +} |
| 32 | +function cleanup_network { |
| 33 | + if [[ "$(docker network ls -q -f name=$1)" ]]; then |
| 34 | + echo -e "\033[34;1mINFO:\033[0m Removing network $1\033[0m" |
| 35 | + (docker network rm "$1") || true |
| 36 | + fi |
| 37 | +} |
| 38 | + |
| 39 | +function cleanup_trap { |
| 40 | + status=$? |
| 41 | + set +x |
| 42 | + if [[ "$DETACH" != "true" ]]; then |
| 43 | + echo -e "\033[34;1mINFO:\033[0m clean the network if not detached (start and exit)\033[0m" |
| 44 | + cleanup_all_in_network "$1" |
| 45 | + fi |
| 46 | + # status is 0 or SIGINT |
| 47 | + if [[ "$status" == "0" || "$status" == "130" ]]; then |
| 48 | + echo -e "\n\033[32;1mSUCCESS run-tests\033[0m" |
| 49 | + exit 0 |
| 50 | + else |
| 51 | + echo -e "\n\033[31;1mFAILURE during run-tests\033[0m" |
| 52 | + exit ${status} |
| 53 | + fi |
| 54 | +}; |
| 55 | +function cleanup_all_in_network { |
| 56 | + |
| 57 | + if [[ -z "$(docker network ls -q -f name="^$1\$")" ]]; then |
| 58 | + echo -e "\033[34;1mINFO:\033[0m $1 is already deleted\033[0m" |
| 59 | + return 0 |
| 60 | + fi |
| 61 | + containers=$(docker network inspect -f '{{ range $key, $value := .Containers }}{{ printf "%s\n" .Name}}{{ end }}' $1) |
| 62 | + while read -r container; do |
| 63 | + cleanup_node "$container" |
| 64 | + done <<< "$containers" |
| 65 | + cleanup_network $1 |
| 66 | + echo -e "\033[32;1mSUCCESS:\033[0m Cleaned up and exiting\033[0m" |
| 67 | +}; |
0 commit comments