-
Notifications
You must be signed in to change notification settings - Fork 37
/
finish_container.sh
executable file
·56 lines (48 loc) · 1.39 KB
/
finish_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
#!/bin/bash
groups | grep docker > /dev/null
NEED_SUDO=$?
if [ $NEED_SUDO -eq 1 ]; then
echo "Hey, we gonna use sudo for running docker"
SUDO_CMD="sudo"
else
echo "Hey, you are in docker group, sudo is not needed"
SUDO_CMD=""
fi
if [ $# -ne 2 ]; then
echo "usage: $0 kill/nokill out_dir"
echo " kill/nokill -- how to finish: kill the container and then clean up / only clean up"
echo " out_dir -- build output directory used by this container (with container.id file)"
exit 1
fi
ACTION="$1"
OUT="$2"
CID_FILE="$OUT/container.id"
if [ $ACTION != "kill" -a $ACTION != "nokill" ]; then
echo "You have to choose: kill or nokill"
echo "usage: $0 kill/nokill out_dir"
exit 1
fi
echo "Search \"container.id\" file in build output directory \"$OUT\""
if [ ! -f $CID_FILE ]; then
echo "NO such file, nothing to do, exit"
exit 2
fi
echo "OK, \"container.id\" file exists, removing it"
ID=$(awk 'NR==1 {print $1}' $CID_FILE)
$SUDO_CMD rm -f $CID_FILE
if [ $ACTION = "kill" ]; then
echo "Killing the docker container $ID"
$SUDO_CMD docker kill "$ID"
if [ $? -ne 0 ]; then
echo "Something goes wrong, failed to kill container $ID"
exit 3
fi
echo "Container $ID is killed"
else
STATUS=$($SUDO_CMD docker container inspect -f '{{.State.Status}}' "$ID" 2>&1)
if [ "$STATUS" = "running" ]; then
echo "Something goes wrong, container $ID is running!"
exit 4
fi
echo "OK, container $ID doesn't run"
fi