forked from UKHomeOffice/docker-clamav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci-build.sh
executable file
·137 lines (116 loc) · 3.55 KB
/
ci-build.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
set -e
TAG=clamav
COUNT=0
PORT=3310
function tear_down() {
if [ "${TEAR_DOWN}" == "true" ]; then
if docker ps -a | grep ${INSTANCE} &>/dev/null ; then
if docker ps | grep ${INSTANCE} &>/dev/null ; then
${SUDO_CMD} docker stop ${INSTANCE}
fi
${SUDO_CMD} docker rm ${INSTANCE}
fi
fi
}
function wait_until_started() {
max_retries=10
wait_time=${WAIT_TIME:-30}
retries=0
cmd="$*"
if [[ $cmd == WARNING* ]]; then
cmd=""
fi
while ! eval $cmd; do
echo "waiting for command to succeed ('$cmd')"
((retries++))
if ((retries==max_retries)); then
echo "Test Failed"
return 1
fi
sleep "$wait_time"
done
echo "Test Succeeded"
return 0
}
# Removes any old containers and images (to avoid container name conflicts)
function clean_up() {
if docker ps -a --filter "name=clamav" | grep clamav &>/dev/null; then
echo "Removing old clamav container..."
docker stop clamav &>/dev/null && docker rm clamav &>/dev/null
else
echo "No clamav container found."
fi
if docker ps -a --filter "name=go-clamav-rest" | grep go-clamav-rest &>/dev/null; then
echo "Removing old clamav-rest container..."
docker stop go-clamav-rest &>/dev/null && docker rm go-clamav-rest &>/dev/null
else
echo "No clamav-rest container found."
fi
action="$*"
if [[ "$action" == "delete-images" ]]; then
if docker images clamav | grep clamav &>/dev/null; then
echo "Removing clamav image..."
docker rmi clamav
else
echo "No clamav image found."
fi
if docker images go-clamav-rest | grep go-clamav-rest &>/dev/null; then
echo "Removing clamav-rest image..."
docker rmi go-clamav-rest
else
echo "No clamav-rest image found."
fi
fi
}
echo "========"
echo "REMOVING OLD CONTAINERS AND IMAGES..."
echo "========"
clean_up
echo "========"
echo "BUILD..."
echo "========"
${SUDO_CMD} docker build -t ${TAG} .
echo "=========="
echo "STARTING CLAMAV CONTAINER..."
echo "=========="
docker run -d --name=clamav -p ${PORT}:3310 ${TAG}
echo "=========="
echo "TESTING FRESHCLAM PROCESS..."
echo "=========="
RUN_FRESHCLAM_TEST='docker exec -t clamav sh -c "freshclam" | grep -q "bytecode.cvd already up-to-date"'
if ! wait_until_started "${RUN_FRESHCLAM_TEST}"; then
echo "Error, not started in time..."
docker logs clamav
exit 1
fi
echo "=========="
echo "TESTING CLAMD PROCESS..."
echo "=========="
RUN_CLAMD_TEST='docker exec -t clamav sh -c "clamdscan /eicar.com" | grep -q "Infected files: 1"'
if ! wait_until_started "${RUN_CLAMD_TEST}"; then
echo "Error, not started in time..."
docker logs clamav
exit 1
fi
#testing clamd-rest container.
echo "=========="
echo "TESTING REST API..."
echo "=========="
docker build -t go-${TAG}-rest go-clamav-rest
#start container.
docker run -id -p 8080:8080 --name=go-clamav-rest --link clamav:clamav go-clamav-rest -host clamav
sleep 30 #wait for app to start
REST_CMD=$(curl -w %{http_code} -s --output /dev/null 172.17.0.1:8080)
VIRUS_TEST=$(curl -s -F "name=test-virus" -F "file=@eicar.com" 172.17.0.1:8080/scan | grep -o false)
if [ $REST_CMD == "200" ]; then
if [ $VIRUS_TEST == "false" ]; then
echo "SUCCESS rest api working and detecting viruses correctly"
clean_up
exit 0
fi
echo "FAILED rest api not detecting correctly"
exit 1
fi
echo "rest api not starting."
exit 1