-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinux.sh
executable file
·402 lines (291 loc) · 9.83 KB
/
linux.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env bash
# TheHive cli controller version 1.0
# Developed By: Prime Security 2020 - 2024
# color codes
red='\033[1;31m'
green='\033[1;32m'
yellow='\033[1;33m'
blue='\033[1;34m'
light_cyan='\033[1;96m'
reset='\033[0m'
#source "/etc/os-release"
# global const varables
CONFIG_PATH="config/config.json"
DATABASE_SCHEMA_PATH="sql/postgresql_schema.sql"
CONTAINER_SCHEMA_PATH="/postgresql_schema.sql"
GET_TARGET=$1
SCRIPT_VERSION="1.0"
PIP_PACKAGET_FILE="requirements.txt"
# print functions
p_error(){
printf "$red[-]$reset $1 \n"
}
p_info(){
printf "$green[+]$reset $1 \n"
}
# command checker
check_command(){
command -v $1 > /dev/null
if [[ "$?" != "0" ]]; then
p_error "$1 Not Found, install $1 and try run this script!"
exit 1
else
p_info "$1 detected in system\tOK..."
fi
}
# check all commands
check_reqiered_commands(){
p_info "CHECKING REQUIRED PACKAGETS"
printf "\n"
check_command "docker"
check_command "jq"
check_command "python3"
check_command "pip3"
check_command "cat"
check_command "make"
check_command "cmake"
check_command "gcc"
check_command "g++"
check_command "sed"
printf "\n"
}
# help menu printer
print_help_exit(){
printf "\n"
printf "$blue<-[ Help Menu ]->\n$reset"
p_info "Script Version:\t$SCRIPT_VERSION"
printf "\n"
printf "COMMANDS:\n"
printf "$0 --check-commands\t\t:Check the required commands status\n"
printf "$0 --generate-docker\t\t:Create the docker container for PostgreSQL\n"
printf "$0 --prepare-psql\t\t:Create database and execute database schema\n"
printf "$0 --start-container\t\t:Start the container\n"
printf "$0 --stop-container\t\t:Stop the running container\n"
printf "$0 --remove-container\t\t:Remove container and database !DANGER!\n"
printf "$0 --install-pip-packagets\t:Install Required pip packagets\n"
printf "$0 --install-model\t\t:Install insightface model\n"
printf "$0 --sql-shell\t\t\t:Connect PostgreSQL cli on \"$db_name\" database\n"
printf "$0 --wizard\t\t\t:Otomatik indirme aracı\n"
printf "$0 --help/-h\t\t\t:Open this menu\n"
printf "\n"
exit 0
}
# reading conf file
# conf file type : json
# bash json parser : jq
p_info "Reading Config File:\t$CONFIG_PATH"
db_name=$(cat $CONFIG_PATH | jq ".database_config.database")
db_username=$(cat $CONFIG_PATH | jq ".database_config.user")
db_password=$(cat $CONFIG_PATH | jq ".database_config.password")
db_hostname=$(cat $CONFIG_PATH | jq ".database_config.host")
db_port=$(cat $CONFIG_PATH | jq ".database_config.port")
# parse json data with sed rmove " chars
db_name=$(echo $db_name | sed 's/"//g')
db_username=$(echo $db_username | sed 's/"//g')
db_password=$(echo $db_password | sed 's/"//g')
db_hostname=$(echo $db_hostname | sed 's/"//g')
db_port=$(echo $db_port | sed 's/"//g')
# set container name and max postgreSQL connection
container_name="$db_name-postgresql-docker"
postgre_max_connections=200
container_package_name="postgres" # container package name
# parse the argumant and execute the protocol
# handle --generate-docker
if [[ "$1" == "--generate-docker" ]] ;then
printf "\n"
printf "$blue<-[ Starting Docker Generator ]->\n$reset"
# call all command checker func
check_reqiered_commands
# user feedback
p_info "Printing your database config...\n"
printf "[db_name] -> \t$db_name \n"
printf "[db_user] -> \t$db_username \n"
printf "[db_pass] -> \t$db_password \n"
printf "[db_host] -> \t$db_hostname \n"
printf "[db_port] -> \t$db_port\n"
printf "[max_conn] -> \t$postgre_max_connections\n"
printf "[container_name] -> \t$container_name\n"
printf "\n"
sleep 1
# execute main docker command
docker run --name $container_name -d -p 5432:$db_port -e POSTGRES_PASSWORD=$db_password $container_package_name -N $postgre_max_connections
# check command status
if [[ "$?" != "0" ]]; then
p_error "PostgreSQL docker setup failed!"
exit 1
else
p_info "PostgreSQL docker setup successfuly\tOK..."
fi
# feedback and exit with code 0
p_info "Docker generation successfuly !"
exit 0
# handle --prepare-sql
# create database and execute .sql database schema
elif [[ "$1" == "--prepare-psql" ]]; then
printf "\n"
printf "$blue<-[ Generating PostgreSQL databse and executing schema ]->\n$reset"
check_reqiered_commands
p_info "Printing your database config...\n"
printf "[db_name] -> \t$db_name \n"
printf "[db_user] -> \t$db_username \n"
printf "[db_pass] -> \t$db_password \n"
printf "[db_host] -> \t$db_hostname \n"
printf "[db_port] -> \t$db_port\n"
printf "[max_conn] -> \t$postgre_max_connections\n"
printf "[container_name] -> \t$container_name\n"
printf "\n"
sleep 1
printf "\n"
p_info "Creating database:\t$db_name"
docker exec -t $container_name psql -p $db_port -h $db_hostname -U $db_username -c "CREATE DATABASE \"$db_name\";"
if [[ "$?" != "0" ]]; then
p_error "Failed to create database: $db_name !"
exit 1
else
p_info "$db_name successfuly created"
fi
p_info "Copy $DATABASE_SCHEMA_PATH -> $container_name"
docker cp $DATABASE_SCHEMA_PATH $container_name:$CONTAINER_SCHEMA_PATH
if [[ "$?" != "0" ]]; then
p_error "Failed to copy database schema!"
exit 1
else
p_info "OK..."
fi
printf "\n"
p_info "Executing database schema:\t$DATABASE_SCHEMA_PATH"
docker exec -t $container_name psql -p $db_port -h $db_hostname -U $db_username -d "$db_name" -f $CONTAINER_SCHEMA_PATH
if [[ "$?" != "0" ]]; then
p_error "Failed to execute database schema !"
exit 1
else
p_info "Database schema successfuly executed"
fi
printf "\n"
p_info "PostgreSQL server is readdy!"
exit 0
# stop container for user
elif [[ "$1" == "--stop-container" ]]; then
printf "\n"
printf "$blue<-[ Stopping Docker Container ]->\n$reset"
printf "\n"
sleep 1
docker stop $container_name
if [[ "$?" != "0" ]]; then
p_error "Failed to stop docker container:\t$container_name !"
exit 1
else
p_info "OK..."
fi
printf "\n"
p_info "Container Successfully stopped:\t$container_name!"
exit 0
# stop container for user
elif [[ "$1" == "--start-container" ]]; then
printf "\n"
printf "$blue<-[ Starting Docker Container ]->\n$reset"
printf "\n"
sleep 1
docker start $container_name
if [[ "$?" != "0" ]]; then
p_error "Failed to start docker container:\t$container_name !"
exit 1
else
p_info "OK..."
fi
printf "\n"
p_info "Container Successfully start:\t$container_name!"
exit 0
# stop container for user
elif [[ "$1" == "--start-hive" ]]; then
printf "\n"
printf "$blue<-[ Starting TheHive ]->\n$reset"
printf "\n"
sleep 1
python3 main.py
elif [[ "$1" == "--install-model" ]]; then
printf "\n"
printf "$blue<-[ Installing Insightface Model ]->\n$reset"
printf "\n"
sleep 1
python3 hivelibrary/install_insightface_model.py
if [[ "$?" != "0" ]]; then
p_error "Failed to install insightface model..."
exit 1
else
p_info "OK..."
fi
# romove/delete container
# for uninstall or reinstall
elif [[ "$1" == "--remove-container" ]]; then
printf "\n"
printf "$blue<-[ Removing Docker Container ]->\n$reset"
printf "\n"
sleep 1
p_info "Stopping container..."
docker stop $container_name
if [[ "$?" != "0" ]]; then
p_error "Failed to stop docker container:\t$container_name !"
exit 1
else
p_info "OK..."
fi
p_info "Removing container..."
docker rm $container_name
if [[ "$?" != "0" ]]; then
p_error "Failed to delete docker container:\t$container_name !"
exit 1
else
p_info "OK..."
fi
printf "\n"
p_info "Container Successfully removed:\t$container_name!"
exit 0
# quick sql shell with psql
elif [[ "$1" == "--sql-shell" ]]; then
printf "\n"
printf "$blue<-[ Starting SQL shell on PostgreSQL container ]->\n$reset"
printf "\n"
docker exec -it $container_name psql -p $db_port -h $db_hostname -U $db_username -d "$db_name"
p_info "OK.."
exit 0
elif [[ "$1" == "--wizard" ]]; then
printf "\n"
printf "$blue<-[ Otomatik İndirme Aracı Başlatılıyor ]->\n$reset"
printf "\n"
checkRoot=1
if [[ "$checkRoot" == "1" && "$UID" == "0" ]] ;then
p_error "Otomatik indirme yardımcısı root olarak çalışamaz! Gerekli oldugu zaman sudo parolası ister!"
printf "\n"
printf "Eğer sistemi root olarak kullanıyorsanız 347. satırdaki \$checkRoot=1 değerini \$checkRoot=0 yapınız."
exit 1
fi
sudo bash ./linux.sh --generate-container
p_info "Docker'in hazırlanması için 10 saniye uyku moduna geçiliyor"
sleep 10
sudo bash ./linux.sh --prepare-psql
bash ./linux.sh --install-pip-packagets
bash ./linux.sh --start-hive
elif [[ "$1" == "--install-pip-packagets" ]]; then
printf "\n"
printf "$blue<-[ Starting pip package installing ]->\n$reset"
printf "\n"
sleep 1
p_info "Using file:\t$PIP_PACKAGET_FILE"
while read current_packaget
do
p_info "Installing packaget:\t$current_packaget"
python3 -m pip install $current_packaget
done < $PIP_PACKAGET_FILE
exit 0
# manuel command check for user
elif [[ "$1" == "--check-commands" ]]; then
check_reqiered_commands
# handle --help
elif [[ "$1" == "--help" || "$1" == "-h" ]]; then
print_help_exit
# invalid arg handler
else
p_error "Invalid args!"
print_help_exit
fi