@@ -0,0 +1,128 @@
1+#!/bin/sh
2+
3+export SEARX_VERSION=$(su searx -c 'python3 -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)"')
4+printf 'searx version %s\n\n' "${SEARX_VERSION}"
5+
6+export UWSGI_SETTINGS_PATH=/etc/searx/uwsgi.ini
7+export SEARX_SETTINGS_PATH=/etc/searx/settings.yml
8+
9+if [ -z "${BIND_ADDRESS}" ]; then
10+ export BIND_ADDRESS=":8080"
11+fi
12+
13+# Parse command line
14+FORCE_CONF_UPDATE=0
15+DRY_RUN=0
16+while getopts "fdh" option
17+do
18+ case $option in
19+ f)
20+ FORCE_CONF_UPDATE=1
21+ ;;
22+ d)
23+ DRY_RUN=1
24+ ;;
25+ h)
26+ printf "Command line:\n\n"
27+ printf " -h Display this help\n"
28+ printf " -d Dry run to update the configuration files.\n"
29+ printf " -f Always update on the configuration files (existing files are renamed with the .old suffix)\n"
30+ printf " Without this option, new configuration files are copied with the .new suffix\n"
31+ printf "\nEnvironment variables:\n\n"
32+ printf " BASE_URL settings.yml : server.base_url\n"
33+ printf " MORTY_URL settings.yml : result_proxy.url\n"
34+ printf " MORTY_KEY settings.yml : result_proxy.key\n"
35+ printf " BIND_ADDRESS where uwsgi will accept HTTP request (format : host:port)\n"
36+ exit 0
37+ esac
38+done
39+
40+# helpers to update the configuration files
41+patch_uwsgi_settings() {
42+ CONF="$1"
43+
44+ # Nothing
45+}
46+
47+patch_searx_settings() {
48+ CONF="$1"
49+
50+ # Make sure that there is trailing slash at the end of BASE_URL
51+ # see http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
52+ export BASE_URL="${BASE_URL%/}/"
53+
54+ # update settings.yml
55+ sed -i -e "s|base_url : False|base_url : ${BASE_URL}|g" \
56+ -e "s/ultrasecretkey/$(openssl rand -hex 32)/g" \
57+ "${CONF}"
58+
59+ # Morty configuration
60+ if [ ! -z "${MORTY_KEY}" -a ! -z "${MORTY_URL}" ]; then
61+ sed -i -e "s/image_proxy : False/image_proxy : True/g" \
62+ "${CONF}"
63+ cat >> "${CONF}" <<-EOF
64+
65+# Morty configuration
66+result_proxy:
67+ url : ${MORTY_URL}
68+ key : !!binary "${MORTY_KEY}"
69+EOF
70+ fi
71+}
72+
73+update_conf() {
74+ FORCE_CONF_UPDATE="$1"
75+ CONF="$2"
76+ NEW_CONF="${2}.new"
77+ OLD_CONF="${2}.old"
78+ REF_CONF="$3"
79+ PATCH_REF_CONF="$4"
80+
81+ if [ -f "${CONF}" ]; then
82+ if [ "${REF_CONF}" -nt "${CONF}" ]; then
83+ # There is a new version
84+ if [ $FORCE_CONF_UPDATE ]; then
85+ # Replace the current configuration
86+ printf '⚠️ Automaticaly update %s to the new version\n' "${CONF}"
87+ if [ ! -f "${OLD_CONF}" ]; then
88+ printf 'The previous configuration is saved to %s\n' "${OLD_CONF}"
89+ mv "${CONF}" "${OLD_CONF}"
90+ fi
91+ cp "${REF_CONF}" "${CONF}"
92+ $PATCH_REF_CONF "${CONF}"
93+ else
94+ # Keep the current configuration
95+ printf '⚠️ Check new version %s to make sure searx is working properly\n' "${NEW_CONF}"
96+ cp "${REF_CONF}" "${NEW_CONF}"
97+ $PATCH_REF_CONF "${NEW_CONF}"
98+ fi
99+ else
100+ printf 'Use existing %s\n' "${CONF}"
101+ fi
102+ else
103+ printf 'Create %s\n' "${CONF}"
104+ cp "${REF_CONF}" "${CONF}"
105+ $PATCH_REF_CONF "${CONF}"
106+ fi
107+}
108+
109+# make sure there are uwsgi settings
110+update_conf "${FORCE_CONF_UPDATE}" "${UWSGI_SETTINGS_PATH}" "/usr/local/searx/dockerfiles/uwsgi.ini" "patch_uwsgi_settings"
111+
112+# make sure there are searx settings
113+update_conf "${FORCE_CONF_UPDATE}" "${SEARX_SETTINGS_PATH}" "/usr/local/searx/searx/settings.yml" "patch_searx_settings"
114+
115+# dry run (to update configuration files, then inspect them)
116+if [ $DRY_RUN -eq 1 ]; then
117+ printf 'Dry run\n'
118+ exit
119+fi
120+
121+#
122+touch /var/run/uwsgi-logrotate
123+chown -R searx:searx /var/log/uwsgi /var/run/uwsgi-logrotate
124+unset MORTY_KEY
125+
126+# Start uwsgi
127+printf 'Listen on %s\n' "${BIND_ADDRESS}"
128+exec su-exec searx:searx uwsgi --master --http-socket "${BIND_ADDRESS}" "${UWSGI_SETTINGS_PATH}"