forked from digitalshadows/docker-cve-search
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdocker-entrypoint.sh
executable file
·166 lines (148 loc) · 3.74 KB
/
docker-entrypoint.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# Small script to control container behavior.
cd $CVE_BASE
function show_help {
printf "Used to launch CVE database.\n \
\t-h|--help: this help menu\n \
\t-i|--initialize: initialize database for the first time\n \
\t-u|--update: update database\n \
\t-r|--repopulate: repopulate database\n \
\t-w|--web: start the web app\n \
\t-a|--autoupdate: activate auto-updates\n \
\t-t|--tail-autoupdate: never exit and run a tail -f on the auto-updates log file\n"
}
function log {
echo "==================== $1 ===================="
}
function update_repo {
log "Updating repo"
curl -sL https://github.com/cve-search/cve-search/archive/master.tar.gz | \
tar xz -C ${CVE_BASE} --strip-components 1 --keep-newer-files
pip3 install -r requirements.txt
}
function start_mongodb {
if ! pidof -x "mongod" >/dev/null; then
log "Starting mongodb server"
if [ -d /persist ]; then
log "Recovering persisted data"
shopt -s dotglob
mv /persist/data/db/* /data/db/
mv /persist/data/configdb/* /data/configdb/
mv /persist/data/first.init.lock /data/first.init.lock
mv /persist/var/lib/redis/* /var/lib/redis/
rm -R /persist
fi
mongo-entrypoint.sh mongod --logpath /var/log/mongodb/mongod.log &
sleep 5
fi
}
function stop_mongodb {
if pidof -x "mongod" >/dev/null; then
log "Restarting mongodb server"
kill $(pidof -x "mongod")
sleep 5
fi
}
function start_redis {
if ! pidof -x "redis-server" >/dev/null; then
log "Starting redis server"
/etc/init.d/redis-server start
sleep 2
fi
}
function stop_redis {
if pidof -x "redis-server" >/dev/null; then
log "Restarting redis server"
kill $(pidof -x "redis-server")
sleep 5
fi
}
function start_cron {
if ! pidof -x "cron" >/dev/null; then
log "Starting cron for auto updates"
/usr/sbin/cron
fi
}
function populate_database {
log "db_mgmt_cpe_dictionary"
./sbin/db_mgmt_cpe_dictionary.py -p
log "Populating database"
./sbin/db_mgmt_json.py -p
log "db_updater -c"
./sbin/db_updater.py -c
log "db_mgmt_ref"
./sbin/db_mgmt_ref.py
log "Updating CPE browsing cache: will take few minutes"
./sbin/db_cpe_browser.py
log "Create /data/first.init.lock file"
date > /data/first.init.lock
}
function update_database {
log "Updating database"
./sbin/db_updater.py -v
log "Updating CPE browsing cache: will take few minutes"
./sbin/db_cpe_browser.py
}
function update_database_background {
log "Updating database and CPE browsing cache in background"
(./sbin/db_updater.py -v ; ./sbin/db_cpe_browser.py) &
}
function repopulate_database {
log "REPOPULATING database"
./sbin/db_updater.py -v -f
}
# Validate number of arguments
if [ $# -eq 0 ]
then
show_help
exit 0
fi
# Parse arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
show_help
;;
-i|--initialize)
start_mongodb
start_redis
if [ -f "/data/first.init.lock" ]; then
log "Do not initialize DB from scratch as /data/first.init.lock exists already"
update_database_background
else
populate_database
stop_mongodb
stop_redis
fi
;;
-u|--update)
start_mongodb
start_redis
update_database
;;
-r|--repopulate)
start_mongodb
start_redis
repopulate_database
;;
-w|--web)
start_mongodb
start_redis
log "Starting web app"
python3 ./web/index.py
;;
-a|--autoupdate)
start_cron
;;
-t|--tail-autoupdate)
INFINITE_TAIL=1
;;
*)
show_help
;;
esac
shift
done
[ x$INFINITE_TAIL = x1 ] && tail -f /opt/cve/log/auto-update.log