Skip to content

Commit 85ab5a7

Browse files
TehsmashSam Betts
authored andcommitted
Improve entrypoint to ensure fresh startup and termination works
Add signal trap's to the script to ensure that the container doesn't hang on either SIGINT or SIGTERM.
1 parent 585896f commit 85ab5a7

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

entrypoint.sh

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,66 @@
22

33
set -euo pipefail
44

5-
# Run update once and then start cron in the background which will run the
6-
# updater script periodically
7-
(/update.sh; crond -f) &
5+
cronPid=0
6+
serverPid=0
7+
firstUpdatePid=0
88

9-
# Start go-exploitdb server
9+
# gracefully handlei SIGINT and SIGTERM
10+
term_handler() {
11+
set +e
12+
echo "Terminating..."
13+
14+
if [ $firstUpdatePid -ne 0 ]; then
15+
echo "Ending firstUpdate..."
16+
kill -SIGTERM "$firstUpdatePid"
17+
wait $firstUpdatePid
18+
echo "firstUpdate ended."
19+
fi
20+
21+
if [ $cronPid -ne 0 ]; then
22+
echo "Ending cron..."
23+
kill -SIGTERM "$cronPid"
24+
wait $cronPid
25+
echo "Cron ended."
26+
fi
27+
28+
if [ $serverPid -ne 0 ]; then
29+
echo "Ending server..."
30+
kill -SIGTERM "$serverPid"
31+
wait $serverPid
32+
echo "Server ended."
33+
fi
34+
35+
exit 143
36+
}
37+
38+
trap 'term_handler' SIGTERM
39+
trap 'term_handler' SIGINT
40+
41+
# Start go-exploitdb server listening and setup/migrate exploit db file
42+
echo "Starting server listening on 0.0.0.0:1326..."
1043
go-exploitdb server --bind 0.0.0.0 --dbpath /vuls/go-exploitdb.sqlite3 &
44+
serverPid=$!
45+
46+
# Wait until the server is up and running healthy and can respond to a query
47+
# before starting the updater to avoid migration conflicts between the server
48+
# command and the updating commands.
49+
# TODO(sambetts) use curl here if we can get it installed in the container
50+
sleep 5
51+
52+
# Run update once on container start to ensure we're up to date.
53+
/update.sh &
54+
firstUpdatePid=$!
55+
wait $firstUpdatePid
56+
firstUpdatePid=0
57+
58+
# Start cron in the background which will run the updater script periodically,
59+
echo "Starting periodic updates..."
60+
crond -f &
61+
cronPid=$!
1162

12-
wait -n
63+
# Wait on Server PID to complete, if it ends then terminate
64+
wait $serverPid
1365

14-
exit $?
66+
# Clean up everything
67+
term_handler

update.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#!/bin/sh
22

3+
set -euo pipefail
4+
5+
echo "Updating from awesomepoc..."
36
go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch awesomepoc
7+
8+
echo "Updating from exploitdb..."
49
go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch exploitdb
10+
11+
echo "Updating from githubrepos..."
512
go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch githubrepos
13+
14+
echo "Updating from inthewild..."
615
go-exploitdb --dbpath /vuls/go-exploitdb.sqlite3 fetch inthewild

0 commit comments

Comments
 (0)