/
destroy_container.sh
executable file
·69 lines (55 loc) · 1.45 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
#!/bin/bash
exec 2>&1
set -o errexit
set -o errtrace
set -o pipefail
trap 'echo -e "\n\nExited due to script error! Exit value: $?\n\n"' ERR
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