-
Notifications
You must be signed in to change notification settings - Fork 12
/
entrypoint.sh
executable file
·302 lines (266 loc) · 9.5 KB
/
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
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
#!/bin/bash
set -eo pipefail
# enabled debug for entrypoint script
is_debug() {
[[ -n "${DEBUG_ENTRYPOINT}" ]] && [[ "${DEBUG_ENTRYPOINT}" == "true" ]]
}
export -f is_debug
is_debug && set -x
print_lt_help() {
# Print help information for LanguageTool
if [[ $# -ne 0 ]] && [[ "${1}" == "help" ]]; then
local print_line=false
while IFS= read -r line; do
if grep -q '\-\-config FILE' <<< "${line}"; then
print_line=true
fi
if grep -q '\-\-port' <<< "${line}"; then
print_line=false
fi
[[ "${print_line}" == "true" ]] && echo "${line}"
done <<< "$(java -cp languagetool-server.jar org.languagetool.server.HTTPServer --help)"
exit 0
fi
}
is_root() {
[[ $(id -u) -eq 0 ]]
}
download_and_extract_ngram_language_model(){
is_debug && set -x
local _LANG
_LANG="${1}"
local _BASE_URL
_BASE_URL="https://languagetool.org/download/ngram-data"
# mapping for ngram language models
declare -A ngrams_filesnames
ngrams_filesnames[en]=ngrams-en-20150817.zip
ngrams_filesnames[de]=ngrams-de-20150819.zip
ngrams_filesnames[es]=ngrams-es-20150915.zip
ngrams_filesnames[fr]=ngrams-fr-20150913.zip
ngrams_filesnames[nl]=ngrams-nl-20181229.zip
if [[ ! -d "${langtool_languageModel}/${_LANG}" ]]; then
if [[ ! -e "${langtool_languageModel}/ngrams-${_LANG}.zip" ]] || ! unzip -t "${langtool_languageModel}/ngrams-${_LANG}.zip" > /dev/null 2>&1; then
echo "INFO: Downloading \"${_LANG}\" ngrams."
wget -O "${langtool_languageModel}/ngrams-${_LANG}.zip" "${_BASE_URL}/${ngrams_filesnames[${_LANG}]}" || {
echo "ERROR: Failed to download ngrams for language ${lang}."
exit 1
}
fi
if [[ -e "${langtool_languageModel}/ngrams-${_LANG}.zip" ]]; then
echo "INFO: Extracting \"${_LANG}\" ngram language model."
unzip "${langtool_languageModel}/ngrams-${_LANG}.zip" -d "${langtool_languageModel}" || {
echo "ERROR: Failed to extract ngrams for language ${lang}."
rm "${langtool_languageModel}/ngrams-${_LANG}.zip"
exit 1
}
rm "${langtool_languageModel}/ngrams-${_LANG}.zip"
fi
else
echo "INFO: Skipping download of ngram model for language ${_LANG}: already exists."
fi
}
# Export the function so it's available to the new shell
export -f download_and_extract_ngram_language_model
handle_ngram_language_models(){
is_debug && set -x
if [[ -z "${langtool_languageModel}" ]]; then
echo "ERROR: No base path for ngram language models provided. Language Tool will not download or use any ngram models."
fi
dir_uid=$( stat -c '%u' "${langtool_languageModel}")
actual_uid=$(id -u)
if [[ "${dir_uid}" == "${actual_uid}" ]];then
echo "INFO: directory ${langtool_languageModel} is owned by ${dir_uid}."
else
echo "ERROR: directory ${langtool_languageModel} is owned by ${dir_uid}, but should be owned by ${actual_uid}."
exit 1
fi
if [[ -z "${download_ngrams_for_langs}" ]]; then
echo "WARNING: download_ngrams_for_langs not provided, no ngrams will be downloaded."
fi
IFS=',' read -ra langs <<< "${download_ngrams_for_langs}"
for lang in "${langs[@]}"; do
case "${lang}" in
en|de|es|fr|nl)
download_and_extract_ngram_language_model "${lang}"
;;
none)
;;
*)
echo "ERROR: Unknown ngrams language. Supported languages are \"en\", \"de\", \"es\", \"fr\" and \"nl\"."
exit 1
esac
done
}
# Export the function so it's available to the new shell
export -f handle_ngram_language_models
download_fasttext_model(){
is_debug && set -x
if [[ -z "${langtool_fasttextModel}" ]] || [[ "${DISABLE_FASTTEXT}" == "true" ]]; then
echo "INFO: \"langtool_fasttextModel\" not specified or \"DISABLE_FASTTEXT\" is set to \"true\". Skipping download of fasttext model."
unset langtool_fasttextModel
return
fi
if [[ ! -e "${langtool_fasttextModel}" ]]; then
dir_uid=$( stat -c '%u' "${langtool_fasttextModel%/*}")
actual_uid=$(id -u)
if [[ "${dir_uid}" == "${actual_uid}" ]];then
echo "Info: directory ${langtool_fasttextModel%/*} is owned by ${dir_uid}."
else
echo "ERROR: directory ${langtool_fasttextModel%/*} is owned by ${dir_uid}, but should be owned by ${actual_uid}."
exit 1
fi
echo "INFO: Downloading fasttext model."
wget -O "${langtool_fasttextModel}" "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin"
else
echo "INFO: Skipping download of fasttext model: already exists."
file_uid=$( stat -c '%u' "${langtool_fasttextModel}")
actual_uid=$(id -u)
if [[ "${file_uid}" == "${actual_uid}" ]];then
echo "INFO: file ${langtool_fasttextModel} is owned by userid ${file_uid}."
else
echo "ERROR: file ${langtool_fasttextModel} is owned by ${file_uid}, but should be owned by ${actual_uid}."
exit 1
fi
fi
}
# Export the function so it's available to the new shell
export -f download_fasttext_model
fix_dir_owner(){
local _PATH
_PATH="${1}"
if [ -x "${_PATH}" ] ; then
find "${_PATH}" ! \( -user languagetool -group languagetool \) -exec chown languagetool:languagetool {} \;
else
cat << EOF
WARN: permissions of directory "${_PATH}" prevents operation.
If this is intentional, please set environment variable DISABLE_PERMISSION_FIX to true to disable this feature.
If not run "sudo chmod o+x,o+r -R {path}" (replace {path} with your real host path!)
EOF
fi
}
fix_ownership(){
if [[ -n "${langtool_languageModel}" ]]; then
echo "INFO: Fixing ownership for ngrams base folder if necessary."
fix_dir_owner "${langtool_languageModel}"
fi
if [[ -n "${langtool_fasttextModel}" ]]; then
echo "INFO: Fixing ownership for fasttext model file if necessary."
fix_dir_owner "$(dirname "${langtool_fasttextModel}")"
fi
}
create_config(){
local _config_file="${1}"
echo "INFO: Creating new LanguageTool config file."
if [[ -e "${_config_file}" ]]; then
rm "${_config_file}"
touch "${_config_file}"
fi
for varname in ${!langtool_*}; do
config_injected=true
echo "${varname#'langtool_'}=${!varname}" >> "${_config_file}"
done
}
user_map(){
if [[ -n "${MAP_UID}" ]]; then
CURRENT_UID="$(id languagetool -u)"
if [[ "${MAP_UID}" != "${CURRENT_UID}" ]]; then
echo "INFO: Setting uid for user \"languagetool\" to ${MAP_UID}."
usermod -u "${MAP_UID}" languagetool
fi
fi
if [[ -n "${MAP_GID}" ]]; then
CURRENT_GID="$(id languagetool -g)"
if [[ "${MAP_GID}" != "${CURRENT_GID}" ]]; then
echo "INFO: Setting gid for group \"languagetool\" to ${MAP_GID}."
groupmod -g "${MAP_GID}" languagetool
fi
fi
}
print_info(){
echo "INFO: Version Information:"
local _ALPINE_VERSION
_ALPINE_VERSION=$(grep "VERSION_ID=" /etc/os-release | cut -d'=' -f2)
echo "Alpine Linux v${_ALPINE_VERSION}" | indent
java --version | indent
}
indent() { sed 's/^/ /'; }
# main
print_lt_help "$@"
# actions that require root user and permission fixes
if is_root && [[ "${DISABLE_PERMISSION_FIX}" != "true" ]]; then
user_map
fix_ownership
else
if is_root; then
EXPECTED_IDS="${MAP_UID}:${MAP_GID}"
else
EXPECTED_IDS="$(id -u):$(id -g)"
fi
cat << EOF
INFO: Container started as UID:GID $(id -u):$(id -g) with DISABLE_PERMISSION_FIX=${DISABLE_PERMISSION_FIX}.
- Usermapping is not available. Make sure your volumes are owned by that user!
- Ownership of directory can not be fixed. Makes sure your volume have the correct permissions set and are owned by ${EXPECTED_IDS}.
EOF
fi
# download ngram models and fasttext model as user
if is_root; then
su -s /bin/bash languagetool -c 'handle_ngram_language_models'
su -s /bin/bash languagetool -c 'download_fasttext_model'
else
DISABLE_PERMISSION_FIX=false
handle_ngram_language_models
download_fasttext_model
fi
# create config
CONFIG_FILE=/tmp/config.properties
create_config "${CONFIG_FILE}"
print_info
# show current languagetool config
if [[ "$config_injected" = true ]]; then
echo 'INFO: Using following LanguageTool configuration:'
indent < "${CONFIG_FILE}"
fi
# set default JAVA_OPTS with default memory limits and garbage collector
if [[ -z "${JAVA_OPTS}" ]]; then
JAVA_GC="${JAVA_GC:-ShenandoahGC}"
for gc in SerialGC ParallelGC ParNewGC G1GC ZGC ShenandoahGC; do
if [[ "${JAVA_GC}" == "${gc}" ]]; then
JAVA_GC_OPT="-XX:+Use${JAVA_GC}"
if [[ "${JAVA_GC}" == "G1GC" ]]; then
JAVA_GC_OPT+=" -XX:+UseStringDeduplication"
fi;
break
fi
done
JAVA_OPTS="-Xms${JAVA_XMS:-256m} -Xmx${JAVA_XMX:-1536m} ${JAVA_GC_OPT}"
echo "INFO: Using JAVA_OPTS=${JAVA_OPTS}"
else
echo "JAVA_OPTS environment variables detected."
echo "INFO: Using JAVA_OPTS=${JAVA_OPTS}"
fi
read -ra FINAL_JAVA_OPTS <<< "${JAVA_OPTS}"
#setloglevel
cp "${LOGBACK_CONFIG}" /tmp/logback.xml
xml edit --inplace --update "/configuration/logger[@name='org.languagetool']/@level" --value "${LOG_LEVEL}" /tmp/logback.xml
if [[ $# -gt 0 ]] && [[ "$1" != "help" ]]; then
if is_root; then
exec su-exec languagetool:languagetool "$@"
else
exec "$@"
fi
exit 0
fi
# start languagetool
echo "INFO: StartingLanguage Tool Standalone Server (or custom command)"
if is_root; then
EXECUTE_ARGS="su-exec languagetool:languagetool"
else
EXECUTE_ARGS=""
fi
read -ra FINAL_EXECUTE_ARGS <<< "${EXECUTE_ARGS}"
exec "${FINAL_EXECUTE_ARGS[@]}" \
java "${FINAL_JAVA_OPTS[@]}" -Dlogback.configurationFile="/tmp/logback.xml" -cp languagetool-server.jar org.languagetool.server.HTTPServer \
--port "${LISTEPORT:-8010}" \
--public \
--allow-origin "*" \
--config /tmp/config.properties