|
2 | 2 |
|
3 | 3 | set -euo pipefail
|
4 | 4 |
|
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 |
8 | 8 |
|
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..." |
10 | 43 | 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=$! |
11 | 62 |
|
12 |
| -wait -n |
| 63 | +# Wait on Server PID to complete, if it ends then terminate |
| 64 | +wait $serverPid |
13 | 65 |
|
14 |
| -exit $? |
| 66 | +# Clean up everything |
| 67 | +term_handler |
0 commit comments