-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy_container.sh
executable file
·76 lines (61 loc) · 1.71 KB
/
destroy_container.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
#!/bin/bash
# Error trapping from https://gist.github.com/oldratlee/902ad9a398affca37bfcfab64612e7d1
__error_trapper() {
local parent_lineno="$1"
local code="$2"
local commands="$3"
echo "error exit status $code, at file $0 on or near line $parent_lineno: $commands"
}
trap '__error_trapper "${LINENO}/${BASH_LINENO}" "$?" "$BASH_COMMAND"' ERR
set -euE -o pipefail
shopt -s failglob
exec 2>&1
maindir="$(readlink -f "$(dirname "$0")")"
lbcsdir="$(dirname "$(readlink -f "$0")")"
if [[ ! $1 ]]
then
echo "Need container name as single argument."
exit 1
fi
container="$1"
containerdir="$maindir/containers/$container"
set -o nounset
if [[ ! -d $containerdir ]]
then
echo "Can't find container dir $containerdir"
exit 1
fi
# Make shellcheck happy
name=''
# shellcheck disable=SC1091
. "$lbcsdir/config"
# shellcheck disable=SC1091
. "$maindir/config"
# shellcheck disable=SC1091
. "$containerdir/config"
status="$($CONTAINER_BIN ps -a -f name="^$name$" --format '{{.ID}} {{.Status}}')"
# If it's up, try to kill it
pat='^[0-9a-f][0-9a-f]* Up'
if [[ $status =~ $pat ]]
then
echo -e "\nTrying to stop container $name\n"
$CONTAINER_BIN stop --time=30 "$name" || true
echo -e "\nTrying to kill container $name\n"
$CONTAINER_BIN kill "$name" || true
fi
# If it exists at all, try to remove it
if [[ $status ]]
then
echo -e "\nTrying to delete container $name\n"
$CONTAINER_BIN rm "$name" || true
fi
if [[ $($CONTAINER_BIN ps -a -f name="^$name$" --format '{{.ID}} {{.Status}}' | wc -l) -eq 0 ]]
then
echo -e "\nContainer $name stopped and removed\n"
exit 0
else
echo -e "\nContainer $name still seems to be around; this is bad\n"
exit 1
fi
echo 'How did we get here??'
exit 99