-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdocker-entrypoint.sh
executable file
·132 lines (106 loc) · 3.73 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
#!/bin/bash
set -eo pipefail
# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[@]}" -ge 2 ] &&
[ "${FUNCNAME[0]}" = '_is_sourced' ] &&
[ "${FUNCNAME[1]}" = 'source' ]
}
_searchd_want_help() {
local arg
for arg; do
case "$arg" in
-'?' | --help | -h | -v)
return 0
;;
esac
done
return 1
}
docker_setup_env() {
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
if [ -n "$QUERY_LOG_TO_STDOUT" ]; then
export searchd_query_log=/var/log/manticore/query.log
[ ! -f /var/log/manticore/query.log ] && ln -sf /dev/stdout /var/log/manticore/query.log
fi
if [ ! -f /etc/ssl/cert.pem ]; then
for cert in "/etc/ssl/certs/ca-certificates.crt" \
"/etc/pki/tls/certs/ca-bundle.crt" \
"/etc/ssl/ca-bundle.pem" \
"/etc/pki/tls/cacert.pem" \
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"; do
if [ -f "$cert" ]; then
ln -s "$cert" /etc/ssl/cert.pem
break
fi
done
fi
if [[ -n ${CREATE_PLAIN_TABLES} && ${CREATE_PLAIN_TABLES} != "1" ]]; then
INDEXER_TABLES_LIST=""
IFS=';' read -ra ITM <<<"${CREATE_PLAIN_TABLES}"
for item in "${ITM[@]}"; do
IFS=':' read -ra LINE <<<"$item"
if [ -z "${LINE[1]}" ]; then
INDEXER_TABLES_LIST+=" ${LINE[0]}"
continue
fi
if [[ ! "${LINE[1]}" =~ ^([0-9,\-\/\*]+ )([0-9,\-\/\*]+ )([0-9,\-\/\*]+ )([0-9,\-\/\*]+ )([0-9,\-\/\*]+)$ ]]; then
echo -e "${RED}Error:${NC} Wrong crontab syntax ${RED}${LINE[1]}${NC} for table: ${LINE[0]}"
continue
fi
md5=$(echo -n ${LINE[0]} | md5sum | awk '{print $1}')
echo "${LINE[1]} cd /var/lib/manticore && flock -w 0 /tmp/${md5}.lock indexer --rotate ${LINE[0]} | sed \"s/.*/$(date): Indexer_${LINE[0]}: &/\" >> /proc/1/fd/1" >> /etc/cron.d/manticore
CRONTAB_AFFECTED=1
done
if [ -n "$CRONTAB_AFFECTED" ]; then
crontab /etc/cron.d/manticore
cron -f &
fi
if [ -n "$INDEXER_TABLES_LIST" ]; then
indexer $INDEXER_TABLES_LIST
fi
fi
if [[ "${CREATE_PLAIN_TABLES}" == "1" ]]; then
indexer --all
fi
}
_main() {
# first arg is `h` or some `--option`
if [ "${1#-}" != "$1" ]; then
set -- searchd "$@"
fi
if ([ "$1" = 'searchd' ] || [ "$1" = 'indexer' ]) && ! _searchd_want_help "@"; then
# allow the container to be started with `--user`
if [ "$(id -u)" = '0' ]; then
find /var/lib/manticore /var/log/manticore /var/run/manticore /etc/manticoresearch /dev/stdout \! -user manticore -exec chown manticore:manticore '{}' +
exec gosu manticore "$0" "$@"
fi
fi
if ! _searchd_want_help "@"; then
docker_setup_env "$@"
fi
BACKUP_INIT_FOLDER="/docker-entrypoint-initdb.d"
if [ -f "${BACKUP_INIT_FOLDER}/versions.json" ]; then
if [ -f /var/lib/manticore/manticore.json ]; then
echo "Warning: Backup is available for restore, but it's being skipped because it's already initialized or the data directory is not empty."
else
# Check if manticore-backup is installed
if ! command -v manticore-backup > /dev/null; then
echo -e "${RED}manticore-backup isn't installed${NC}"
exit 1
fi
find ${BACKUP_INIT_FOLDER}/config -type f -exec sh -c 'rm -f "${1#/docker-entrypoint-initdb.d/config}"' sh {} \;
find ${BACKUP_INIT_FOLDER}/state -type f -exec sh -c 'rm -f "${1#/docker-entrypoint-initdb.d/state}"' sh {} \;
manticore-backup --version
manticore-backup --force --backup-dir='/' --restore='docker-entrypoint-initdb.d'
fi
fi
exec "$@"
}
# If we are sourced from elsewhere, don't perform any further actions
if ! _is_sourced; then
_main "$@"
fi