on engine-data help sign in
searx/utils/searx.sh
914 lines 30 kB history raw
1#!/usr/bin/env bash
2# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
3# SPDX-License-Identifier: AGPL-3.0-or-later
4# shellcheck disable=SC2001
6# shellcheck source=utils/lib.sh
7source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
8# shellcheck source=utils/brand.env
9source "${REPO_ROOT}/utils/brand.env"
10source_dot_config
11source "${REPO_ROOT}/utils/lxc-searx.env"
12in_container && lxc_set_suite_env
14# ----------------------------------------------------------------------------
15# config
16# ----------------------------------------------------------------------------
18PUBLIC_URL="${PUBLIC_URL:-http://$(uname -n)/searx}"
20SEARX_INTERNAL_HTTP="${SEARX_INTERNAL_HTTP:-127.0.0.1:8888}"
22SEARX_URL_PATH="${SEARX_URL_PATH:-$(echo "${PUBLIC_URL}" \
23| sed -e 's,^.*://[^/]*\(/.*\),\1,g')}"
24[[ "${SEARX_URL_PATH}" == "${PUBLIC_URL}" ]] && SEARX_URL_PATH=/
25SEARX_INSTANCE_NAME="${SEARX_INSTANCE_NAME:-searx@$(echo "$PUBLIC_URL" \
26| sed -e 's,^.*://\([^\:/]*\).*,\1,g') }"
28SERVICE_NAME="searx"
29SERVICE_USER="${SERVICE_USER:-${SERVICE_NAME}}"
30SERVICE_HOME_BASE="${SERVICE_HOME_BASE:-/usr/local}"
31SERVICE_HOME="${SERVICE_HOME_BASE}/${SERVICE_USER}"
32# shellcheck disable=SC2034
33SERVICE_GROUP="${SERVICE_USER}"
35GIT_BRANCH="${GIT_BRANCH:-master}"
36SEARX_PYENV="${SERVICE_HOME}/searx-pyenv"
37SEARX_SRC="${SERVICE_HOME}/searx-src"
38SEARX_SETTINGS_PATH="/etc/searx/settings.yml"
39SEARX_SETTINGS_TEMPLATE="${SEARX_SETTINGS_TEMPLATE:-${REPO_ROOT}/utils/templates/etc/searx/use_default_settings.yml}"
40SEARX_UWSGI_APP="searx.ini"
41# shellcheck disable=SC2034
42SEARX_UWSGI_SOCKET="/run/uwsgi/app/searx/socket"
44# apt packages
45SEARX_PACKAGES_debian="\
46python3-dev python3-babel python3-venv
47uwsgi uwsgi-plugin-python3
48git build-essential libxslt-dev zlib1g-dev libffi-dev libssl-dev
49shellcheck"
51BUILD_PACKAGES_debian="\
52firefox graphviz imagemagick texlive-xetex librsvg2-bin
53texlive-latex-recommended texlive-extra-utils fonts-dejavu
54latexmk"
56# pacman packages
57SEARX_PACKAGES_arch="\
58python python-pip python-lxml python-babel
59uwsgi uwsgi-plugin-python
60git base-devel libxml2
61shellcheck"
63BUILD_PACKAGES_arch="\
64firefox graphviz imagemagick texlive-bin extra/librsvg
65texlive-core texlive-latexextra ttf-dejavu"
67# dnf packages
68SEARX_PACKAGES_fedora="\
69python python-pip python-lxml python-babel
70uwsgi uwsgi-plugin-python3
71git @development-tools libxml2
72ShellCheck"
74BUILD_PACKAGES_fedora="\
75firefox graphviz graphviz-gd ImageMagick librsvg2-tools
76texlive-xetex-bin texlive-collection-fontsrecommended
77texlive-collection-latex dejavu-sans-fonts dejavu-serif-fonts
78dejavu-sans-mono-fonts"
80# yum packages
81SEARX_PACKAGES_centos="\
82python36 python36-pip python36-lxml python-babel
83uwsgi uwsgi-plugin-python3
84git @development-tools libxml2
85ShellCheck"
87BUILD_PACKAGES_centos="\
88firefox graphviz graphviz-gd ImageMagick librsvg2-tools
89texlive-xetex-bin texlive-collection-fontsrecommended
90texlive-collection-latex dejavu-sans-fonts dejavu-serif-fonts
91dejavu-sans-mono-fonts"
93case $DIST_ID-$DIST_VERS in
94 ubuntu-16.04|ubuntu-18.04)
95 SEARX_PACKAGES="${SEARX_PACKAGES_debian}"
96 BUILD_PACKAGES="${BUILD_PACKAGES_debian}"
97 APACHE_PACKAGES="$APACHE_PACKAGES libapache2-mod-proxy-uwsgi"
98 ;;
99 ubuntu-20.04)
100 # https://askubuntu.com/a/1224710
101 SEARX_PACKAGES="${SEARX_PACKAGES_debian} python-is-python3"
102 BUILD_PACKAGES="${BUILD_PACKAGES_debian}"
103 ;;
104 ubuntu-*|debian-*)
105 SEARX_PACKAGES="${SEARX_PACKAGES_debian}"
106 BUILD_PACKAGES="${BUILD_PACKAGES_debian}"
107 ;;
108 arch-*)
109 SEARX_PACKAGES="${SEARX_PACKAGES_arch}"
110 BUILD_PACKAGES="${BUILD_PACKAGES_arch}"
111 ;;
112 fedora-*)
113 SEARX_PACKAGES="${SEARX_PACKAGES_fedora}"
114 BUILD_PACKAGES="${BUILD_PACKAGES_fedora}"
115 ;;
116 centos-7)
117 SEARX_PACKAGES="${SEARX_PACKAGES_centos}"
118 BUILD_PACKAGES="${BUILD_PACKAGES_centos}"
119 ;;
120esac
122# Apache Settings
123APACHE_SEARX_SITE="searx.conf"
125# shellcheck disable=SC2034
126CONFIG_FILES=(
127 "${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}"
130# shellcheck disable=SC2034
131CONFIG_BACKUP_ENCRYPTED=(
132 "${SEARX_SETTINGS_PATH}"
135# ----------------------------------------------------------------------------
136usage() {
137# ----------------------------------------------------------------------------
139 # shellcheck disable=SC1117
140 cat <<EOF
141usage::
142 $(basename "$0") shell
143 $(basename "$0") install [all|user|searx-src|pyenv|uwsgi|packages|settings|buildhost]
144 $(basename "$0") update [searx]
145 $(basename "$0") remove [all|user|pyenv|searx-src]
146 $(basename "$0") activate [service]
147 $(basename "$0") deactivate [service]
148 $(basename "$0") inspect [service]
149 $(basename "$0") option [debug-[on|off]|image-proxy-[on|off]|result-proxy <url> <key>]
150 $(basename "$0") apache [install|remove]
152shell
153 start interactive shell from user ${SERVICE_USER}
154install / remove
155 :all: complete (de-) installation of searx service
156 :user: add/remove service user '$SERVICE_USER' ($SERVICE_HOME)
157 :searx-src: clone $GIT_URL
158 :pyenv: create/remove virtualenv (python) in $SEARX_PYENV
159 :uwsgi: install searx uWSGI application
160 :settings: reinstall settings from ${SEARX_SETTINGS_TEMPLATE}
161 :packages: install needed packages from OS package manager
162 :buildhost: install packages from OS package manager needed by buildhosts
163update searx
164 Update searx installation ($SERVICE_HOME)
165activate service
166 activate and start service daemon (systemd unit)
167deactivate service
168 stop and deactivate service daemon (systemd unit)
169inspect service
170 run some small tests and inspect service's status and log
171option
172 set one of the available options
173apache
174 :install: apache site with the searx uwsgi app
175 :remove: apache site ${APACHE_FILTRON_SITE}
177searx settings: ${SEARX_SETTINGS_PATH}
179If needed, set PUBLIC_URL of your WEB service in the '${DOT_CONFIG#"$REPO_ROOT/"}' file::
180 PUBLIC_URL : ${PUBLIC_URL}
181 SEARX_INSTANCE_NAME : ${SEARX_INSTANCE_NAME}
182 SERVICE_USER : ${SERVICE_USER}
183 SEARX_INTERNAL_HTTP : http://${SEARX_INTERNAL_HTTP}
184EOF
185 if in_container; then
186 # searx is listening on 127.0.0.1 and not available from outside container
187 # in containers the service is listening on 0.0.0.0 (see lxc-searx.env)
188 echo -e "${_BBlack}HINT:${_creset} searx only listen on loopback device" \
189 "${_BBlack}inside${_creset} the container."
190 for ip in $(global_IPs) ; do
191 if [[ $ip =~ .*:.* ]]; then
192 echo " container (IPv6): [${ip#*|}]"
193 else
194 # IPv4:
195 echo " container (IPv4): ${ip#*|}"
196 fi
197 done
198 fi
199 [[ -n ${1} ]] && err_msg "$1"
202main() {
203 required_commands \
204 sudo systemctl install git wget curl \
205 || exit
207 local _usage="unknown or missing $1 command $2"
209 case $1 in
210 --getenv) var="$2"; echo "${!var}"; exit 0;;
211 -h|--help) usage; exit 0;;
212 shell)
213 sudo_or_exit
214 interactive_shell "${SERVICE_USER}"
215 ;;
216 inspect)
217 case $2 in
218 service)
219 sudo_or_exit
220 inspect_service
221 ;;
222 *) usage "$_usage"; exit 42;;
223 esac ;;
224 install)
225 rst_title "$SEARX_INSTANCE_NAME" part
226 sudo_or_exit
227 case $2 in
228 all) install_all ;;
229 user) assert_user ;;
230 pyenv) create_pyenv ;;
231 searx-src) clone_searx ;;
232 settings) install_settings ;;
233 uwsgi)
234 install_searx_uwsgi
235 if ! service_is_available "http://${SEARX_INTERNAL_HTTP}"; then
236 err_msg "URL http://${SEARX_INTERNAL_HTTP} not available, check searx & uwsgi setup!"
237 fi
238 ;;
239 packages)
240 pkg_install "$SEARX_PACKAGES"
241 ;;
242 buildhost)
243 pkg_install "$SEARX_PACKAGES"
244 pkg_install "$BUILD_PACKAGES"
245 ;;
246 *) usage "$_usage"; exit 42;;
247 esac ;;
248 update)
249 sudo_or_exit
250 case $2 in
251 searx) update_searx;;
252 *) usage "$_usage"; exit 42;;
253 esac ;;
254 remove)
255 sudo_or_exit
256 case $2 in
257 all) remove_all;;
258 user) drop_service_account "${SERVICE_USER}";;
259 pyenv) remove_pyenv ;;
260 searx-src) remove_searx ;;
261 *) usage "$_usage"; exit 42;;
262 esac ;;
263 activate)
264 sudo_or_exit
265 case $2 in
266 service)
267 activate_service ;;
268 *) usage "$_usage"; exit 42;;
269 esac ;;
270 deactivate)
271 sudo_or_exit
272 case $2 in
273 service) deactivate_service ;;
274 *) usage "$_usage"; exit 42;;
275 esac ;;
276 option)
277 sudo_or_exit
278 case $2 in
279 debug-on) echo; enable_debug ;;
280 debug-off) echo; disable_debug ;;
281 result-proxy) set_result_proxy "$3" "$4" ;;
282 image-proxy-on) enable_image_proxy ;;
283 image-proxy-off) disable_image_proxy ;;
284 *) usage "$_usage"; exit 42;;
285 esac ;;
286 apache)
287 sudo_or_exit
288 case $2 in
289 install) install_apache_site ;;
290 remove) remove_apache_site ;;
291 *) usage "$_usage"; exit 42;;
292 esac ;;
293 doc) rst-doc;;
294 *) usage "unknown or missing command $1"; exit 42;;
295 esac
298_service_prefix=" ${_Yellow}|$SERVICE_USER|${_creset} "
300install_all() {
301 rst_title "Install $SEARX_INSTANCE_NAME (service)"
302 pkg_install "$SEARX_PACKAGES"
303 wait_key
304 assert_user
305 wait_key
306 clone_searx
307 wait_key
308 create_pyenv
309 wait_key
310 install_settings
311 wait_key
312 test_local_searx
313 wait_key
314 install_searx_uwsgi
315 if ! service_is_available "http://${SEARX_INTERNAL_HTTP}"; then
316 err_msg "URL http://${SEARX_INTERNAL_HTTP} not available, check searx & uwsgi setup!"
317 fi
318 if ask_yn "Do you want to inspect the installation?" Ny; then
319 inspect_service
320 fi
323update_searx() {
324 rst_title "Update searx instance"
326 echo
327 tee_stderr 0.3 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
328cd ${SEARX_SRC}
329git checkout -B "$GIT_BRANCH"
330git pull
331pip install -U pip
332pip install -U setuptools
333pip install -U wheel
334pip install -U pyyaml
335pip install -U -e .
336EOF
337 install_settings
338 uWSGI_restart "$SEARX_UWSGI_APP"
341remove_all() {
342 rst_title "De-Install $SEARX_INSTANCE_NAME (service)"
344 rst_para "\
345It goes without saying that this script can only be used to remove
346installations that were installed with this script."
348 if ! ask_yn "Do you really want to deinstall $SEARX_INSTANCE_NAME?"; then
349 return
350 fi
351 remove_searx_uwsgi
352 drop_service_account "${SERVICE_USER}"
353 remove_settings
354 wait_key
355 if service_is_available "${PUBLIC_URL}"; then
356 MSG="** Don't forgett to remove your public site! (${PUBLIC_URL}) **" wait_key 10
357 fi
360assert_user() {
361 rst_title "user $SERVICE_USER" section
362 echo
363 tee_stderr 1 <<EOF | bash | prefix_stdout
364useradd --shell /bin/bash --system \
365 --home-dir "$SERVICE_HOME" \
366 --comment 'Privacy-respecting metasearch engine' $SERVICE_USER
367mkdir "$SERVICE_HOME"
368chown -R "$SERVICE_GROUP:$SERVICE_GROUP" "$SERVICE_HOME"
369groups $SERVICE_USER
370EOF
371 #SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
372 #export SERVICE_HOME
373 #echo "export SERVICE_HOME=$SERVICE_HOME"
376clone_is_available() {
377 [[ -f "$SEARX_SRC/.git/config" ]]
380# shellcheck disable=SC2164
381clone_searx() {
382 rst_title "Clone searx sources" section
383 echo
384 if ! sudo -i -u "$SERVICE_USER" ls -d "$REPO_ROOT" > /dev/null; then
385 die 42 "user '$SERVICE_USER' missed read permission: $REPO_ROOT"
386 fi
387 SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME 2>/dev/null)"
388 if [[ ! "${SERVICE_HOME}" ]]; then
389 err_msg "to clone searx sources, user $SERVICE_USER hast to be created first"
390 return 42
391 fi
392 if [[ ! $(git show-ref "refs/heads/${GIT_BRANCH}") ]]; then
393 warn_msg "missing local branch ${GIT_BRANCH}"
394 info_msg "create local branch ${GIT_BRANCH} from start point: origin/${GIT_BRANCH}"
395 git branch "${GIT_BRANCH}" "origin/${GIT_BRANCH}"
396 fi
397 if [[ ! $(git rev-parse --abbrev-ref HEAD) == "${GIT_BRANCH}" ]]; then
398 warn_msg "take into account, installing branch $GIT_BRANCH while current branch is $(git rev-parse --abbrev-ref HEAD)"
399 fi
400 export SERVICE_HOME
401 git_clone "$REPO_ROOT" "$SEARX_SRC" \
402 "$GIT_BRANCH" "$SERVICE_USER"
404 pushd "${SEARX_SRC}" > /dev/null
405 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
406cd "${SEARX_SRC}"
407git remote set-url origin ${GIT_URL}
408git config user.email "$ADMIN_EMAIL"
409git config user.name "$ADMIN_NAME"
410git config --list
411EOF
412 popd > /dev/null
415install_settings() {
416 rst_title "${SEARX_SETTINGS_PATH}" section
417 if ! clone_is_available; then
418 err_msg "you have to install searx first"
419 exit 42
420 fi
421 mkdir -p "$(dirname ${SEARX_SETTINGS_PATH})"
423 if [[ ! -f ${SEARX_SETTINGS_PATH} ]]; then
424 info_msg "install settings ${SEARX_SETTINGS_TEMPLATE}"
425 info_msg " --> ${SEARX_SETTINGS_PATH}"
426 cp "${SEARX_SETTINGS_TEMPLATE}" "${SEARX_SETTINGS_PATH}"
427 configure_searx
428 return
429 fi
431 rst_para "Diff between origin's setting file (+) and current (-):"
432 echo "${SEARX_SETTINGS_PATH}" "${SEARX_SETTINGS_TEMPLATE}"
433 $DIFF_CMD "${SEARX_SETTINGS_PATH}" "${SEARX_SETTINGS_TEMPLATE}"
435 local action
436 choose_one action "What should happen to the settings file? " \
437 "keep configuration unchanged" \
438 "use origin settings" \
439 "start interactiv shell"
440 case $action in
441 "keep configuration unchanged")
442 info_msg "leave settings file unchanged"
443 ;;
444 "use origin settings")
445 backup_file "${SEARX_SETTINGS_PATH}"
446 info_msg "install origin settings"
447 cp "${SEARX_SETTINGS_TEMPLATE}" "${SEARX_SETTINGS_PATH}"
448 ;;
449 "start interactiv shell")
450 backup_file "${SEARX_SETTINGS_PATH}"
451 echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
452 sudo -H -i
453 rst_para 'Diff between new setting file (-) and current (+):'
454 echo
455 $DIFF_CMD "${SEARX_SETTINGS_TEMPLATE}" "${SEARX_SETTINGS_PATH}"
456 wait_key
457 ;;
458 esac
461remove_settings() {
462 rst_title "remove searx settings" section
463 echo
464 info_msg "delete ${SEARX_SETTINGS_PATH}"
465 rm -f "${SEARX_SETTINGS_PATH}"
468remove_searx() {
469 rst_title "Drop searx sources" section
470 if ask_yn "Do you really want to drop searx sources ($SEARX_SRC)?"; then
471 rm -rf "$SEARX_SRC"
472 else
473 rst_para "Leave searx sources unchanged."
474 fi
477pyenv_is_available() {
478 [[ -f "${SEARX_PYENV}/bin/activate" ]]
481create_pyenv() {
482 rst_title "Create virtualenv (python)" section
483 echo
484 if [[ ! -f "${SEARX_SRC}/manage.sh" ]]; then
485 err_msg "to create pyenv for searx, searx has to be cloned first"
486 return 42
487 fi
488 info_msg "create pyenv in ${SEARX_PYENV}"
489 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
490rm -rf "${SEARX_PYENV}"
491python3 -m venv "${SEARX_PYENV}"
492grep -qFs -- 'source ${SEARX_PYENV}/bin/activate' ~/.profile \
493 || echo 'source ${SEARX_PYENV}/bin/activate' >> ~/.profile
494EOF
495 info_msg "inspect python's virtual environment"
496 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
497command -v python && python --version
498EOF
499 wait_key
500 info_msg "install needed python packages"
501 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
502pip install -U pip
503pip install -U setuptools
504pip install -U wheel
505pip install -U pyyaml
506cd ${SEARX_SRC}
507pip install -e .
508EOF
511remove_pyenv() {
512 rst_title "Remove virtualenv (python)" section
513 if ! ask_yn "Do you really want to drop ${SEARX_PYENV} ?"; then
514 return
515 fi
516 info_msg "remove pyenv activation from ~/.profile"
517 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
518grep -v 'source ${SEARX_PYENV}/bin/activate' ~/.profile > ~/.profile.##
519mv ~/.profile.## ~/.profile
520EOF
521 rm -rf "${SEARX_PYENV}"
524configure_searx() {
525 rst_title "Configure searx" section
526 rst_para "Setup searx config located at $SEARX_SETTINGS_PATH"
527 echo
528 tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
529cd ${SEARX_SRC}
530sed -i -e "s/ultrasecretkey/$(openssl rand -hex 16)/g" "$SEARX_SETTINGS_PATH"
531sed -i -e "s/{instance_name}/${SEARX_INSTANCE_NAME}/g" "$SEARX_SETTINGS_PATH"
532EOF
535test_local_searx() {
536 rst_title "Testing searx instance localy" section
537 echo
539 if service_is_available "http://${SEARX_INTERNAL_HTTP}" &>/dev/null; then
540 err_msg "URL/port http://${SEARX_INTERNAL_HTTP} is already in use, you"
541 err_msg "should stop that service before starting local tests!"
542 if ! ask_yn "Continue with local tests?"; then
543 return
544 fi
545 fi
546 sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS_PATH"
547 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
548export SEARX_SETTINGS_PATH="${SEARX_SETTINGS_PATH}"
549cd ${SEARX_SRC}
550timeout 10 python searx/webapp.py &
551sleep 3
552curl --location --verbose --head --insecure $SEARX_INTERNAL_HTTP
553EOF
554 sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS_PATH"
557install_searx_uwsgi() {
558 rst_title "Install searx's uWSGI app (searx.ini)" section
559 echo
560 install_uwsgi
561 uWSGI_install_app "$SEARX_UWSGI_APP"
564remove_searx_uwsgi() {
565 rst_title "Remove searx's uWSGI app (searx.ini)" section
566 echo
567 uWSGI_remove_app "$SEARX_UWSGI_APP"
570activate_service() {
571 rst_title "Activate $SEARX_INSTANCE_NAME (service)" section
572 echo
573 uWSGI_enable_app "$SEARX_UWSGI_APP"
574 uWSGI_restart "$SEARX_UWSGI_APP"
577deactivate_service() {
578 rst_title "De-Activate $SEARX_INSTANCE_NAME (service)" section
579 echo
580 uWSGI_disable_app "$SEARX_UWSGI_APP"
581 uWSGI_restart "$SEARX_UWSGI_APP"
584enable_image_proxy() {
585 info_msg "try to enable image_proxy ..."
586 tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
587cd ${SEARX_SRC}
588sed -i -e "s/image_proxy : False/image_proxy : True/g" "$SEARX_SETTINGS_PATH"
589EOF
590 uWSGI_restart "$SEARX_UWSGI_APP"
593disable_image_proxy() {
594 info_msg "try to enable image_proxy ..."
595 tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
596cd ${SEARX_SRC}
597sed -i -e "s/image_proxy : True/image_proxy : False/g" "$SEARX_SETTINGS_PATH"
598EOF
599 uWSGI_restart "$SEARX_UWSGI_APP"
602enable_debug() {
603 warn_msg "Do not enable debug in production enviroments!!"
604 info_msg "try to enable debug mode ..."
605 tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
606cd ${SEARX_SRC}
607sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS_PATH"
608EOF
609 uWSGI_restart "$SEARX_UWSGI_APP"
612disable_debug() {
613 info_msg "try to disable debug mode ..."
614 tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
615cd ${SEARX_SRC}
616sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS_PATH"
617EOF
618 uWSGI_restart "$SEARX_UWSGI_APP"
621set_result_proxy() {
623 # usage: set_result_proxy <URL> [<key>]
625 info_msg "try to set result proxy: '$1' ($2)"
626 cp "${SEARX_SETTINGS_PATH}" "${SEARX_SETTINGS_PATH}.bak"
627 _set_result_proxy "$1" "$2" > "${SEARX_SETTINGS_PATH}"
630_set_result_proxy() {
631 local line
632 local stage=0
633 local url=" url: $1"
634 local key=" key: !!binary \"$2\""
635 if [[ -z $2 ]]; then
636 key=
637 fi
639 while IFS= read -r line
640 do
641 if [[ $stage = 0 ]] || [[ $stage = 2 ]] ; then
642 if [[ $line =~ ^[[:space:]]*#*[[:space:]]*result_proxy[[:space:]]*:[[:space:]]*$ ]]; then
643 if [[ $stage = 0 ]]; then
644 stage=1
645 echo "result_proxy:"
646 continue
647 elif [[ $stage = 2 ]]; then
648 continue
649 fi
650 fi
651 fi
652 if [[ $stage = 1 ]] || [[ $stage = 2 ]] ; then
653 if [[ $line =~ ^[[:space:]]*#*[[:space:]]*url[[:space:]]*:[[:space:]] ]]; then
654 [[ $stage = 1 ]] && echo "$url"
655 continue
656 elif [[ $line =~ ^[[:space:]]*#*[[:space:]]*key[[:space:]]*:[[:space:]] ]]; then
657 [[ $stage = 1 ]] && [[ -n $key ]] && echo "$key"
658 continue
659 elif [[ $line =~ ^[[:space:]]*$ ]]; then
660 stage=2
661 fi
662 fi
663 echo "$line"
664 done < "${SEARX_SETTINGS_PATH}.bak"
667function has_substring() {
668 [[ "$1" != "${2/$1/}" ]]
670inspect_service() {
671 rst_title "service status & log"
672 cat <<EOF
674sourced ${DOT_CONFIG#"$REPO_ROOT/"} :
676 PUBLIC_URL : ${PUBLIC_URL}
677 SEARX_URL_PATH : ${SEARX_URL_PATH}
678 SEARX_INSTANCE_NAME : ${SEARX_INSTANCE_NAME}
679 SEARX_INTERNAL_HTTP : ${SEARX_INTERNAL_HTTP}
681EOF
683 if service_account_is_available "$SERVICE_USER"; then
684 info_msg "Service account $SERVICE_USER exists."
685 else
686 err_msg "Service account $SERVICE_USER does not exists!"
687 fi
689 if pyenv_is_available; then
690 info_msg "~$SERVICE_USER: python environment is available."
691 else
692 err_msg "~$SERVICE_USER: python environment is not available!"
693 fi
695 if clone_is_available; then
696 info_msg "~$SERVICE_USER: Searx software is installed."
697 else
698 err_msg "~$SERVICE_USER: Missing searx software!"
699 fi
701 if uWSGI_app_enabled "$SEARX_UWSGI_APP"; then
702 info_msg "uWSGI app $SEARX_UWSGI_APP is enabled."
703 else
704 err_msg "uWSGI app $SEARX_UWSGI_APP not enabled!"
705 fi
707 uWSGI_app_available "$SEARX_UWSGI_APP" \
708 || err_msg "uWSGI app $SEARX_UWSGI_APP not available!"
710 if in_container; then
711 lxc_suite_info
712 else
713 info_msg "public URL --> ${PUBLIC_URL}"
714 info_msg "internal URL --> http://${SEARX_INTERNAL_HTTP}"
715 fi
717 if ! service_is_available "http://${SEARX_INTERNAL_HTTP}"; then
718 err_msg "uWSGI app (service) at http://${SEARX_INTERNAL_HTTP} is not available!"
719 MSG="${_Green}[${_BCyan}CTRL-C${_Green}] to stop or [${_BCyan}KEY${_Green}] to continue"\
720 wait_key
721 fi
723 if ! service_is_available "${PUBLIC_URL}"; then
724 warn_msg "Public service at ${PUBLIC_URL} is not available!"
725 if ! in_container; then
726 warn_msg "Check if public name is correct and routed or use the public IP from above."
727 fi
728 fi
730 local _debug_on
731 if ask_yn "Enable searx debug mode?"; then
732 enable_debug
733 _debug_on=1
734 fi
735 echo
737 case $DIST_ID-$DIST_VERS in
738 ubuntu-*|debian-*)
739 systemctl --no-pager -l status "${SERVICE_NAME}"
740 ;;
741 arch-*)
742 systemctl --no-pager -l status "uwsgi@${SERVICE_NAME%.*}"
743 ;;
744 fedora-*|centos-7)
745 systemctl --no-pager -l status uwsgi
746 ;;
747 esac
749 # shellcheck disable=SC2059
750 printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
751 read -r -s -n1 -t 5
752 echo
754 while true; do
755 trap break 2
756 case $DIST_ID-$DIST_VERS in
757 ubuntu-*|debian-*) tail -f /var/log/uwsgi/app/searx.log ;;
758 arch-*) journalctl -f -u "uwsgi@${SERVICE_NAME%.*}" ;;
759 fedora-*|centos-7) journalctl -f -u uwsgi ;;
760 esac
761 done
763 if [[ $_debug_on == 1 ]]; then
764 disable_debug
765 fi
766 return 0
769install_apache_site() {
770 rst_title "Install Apache site $APACHE_SEARX_SITE"
772 rst_para "\
773This installs the searx uwsgi app as apache site. If your server is public to
774the internet, you should instead use a reverse proxy (filtron) to block
775excessively bot queries."
777 ! apache_is_installed && err_msg "Apache is not installed."
779 if ! ask_yn "Do you really want to continue?" Yn; then
780 return
781 else
782 install_apache
783 fi
785 apache_install_site --variant=uwsgi "${APACHE_SEARX_SITE}"
787 rst_title "Install searx's uWSGI app (searx.ini)" section
788 echo
789 uWSGI_install_app --variant=socket "$SEARX_UWSGI_APP"
791 if ! service_is_available "${PUBLIC_URL}"; then
792 err_msg "Public service at ${PUBLIC_URL} is not available!"
793 fi
796remove_apache_site() {
798 rst_title "Remove Apache site ${APACHE_SEARX_SITE}"
800 rst_para "\
801This removes apache site ${APACHE_SEARX_SITE}."
803 ! apache_is_installed && err_msg "Apache is not installed."
805 if ! ask_yn "Do you really want to continue?" Yn; then
806 return
807 fi
809 apache_remove_site "${APACHE_SEARX_SITE}"
811 rst_title "Remove searx's uWSGI app (searx.ini)" section
812 echo
813 uWSGI_remove_app "$SEARX_UWSGI_APP"
816rst-doc() {
817 local debian="${SEARX_PACKAGES_debian}"
818 local arch="${SEARX_PACKAGES_arch}"
819 local fedora="${SEARX_PACKAGES_fedora}"
820 local centos="${SEARX_PACKAGES_centos}"
821 local debian_build="${BUILD_PACKAGES_debian}"
822 local arch_build="${BUILD_PACKAGES_arch}"
823 local fedora_build="${BUILD_PACKAGES_fedora}"
824 local centos_build="${SEARX_PACKAGES_centos}"
825 debian="$(echo "${debian}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
826 arch="$(echo "${arch}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
827 fedora="$(echo "${fedora}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
828 centos="$(echo "${centos}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
829 debian_build="$(echo "${debian_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
830 arch_build="$(echo "${arch_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
831 fedora_build="$(echo "${fedora_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
832 centos_build="$(echo "${centos_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
834 eval "echo \"$(< "${REPO_ROOT}/docs/build-templates/searx.rst")\""
836 # I use ubuntu-20.04 here to demonstrate that versions are also suported,
837 # normaly debian-* and ubuntu-* are most the same.
839 for DIST_NAME in ubuntu-20.04 arch fedora; do
840 (
841 DIST_ID=${DIST_NAME%-*}
842 DIST_VERS=${DIST_NAME#*-}
843 [[ $DIST_VERS =~ $DIST_ID ]] && DIST_VERS=
844 uWSGI_distro_setup
846 echo -e "\n.. START searx uwsgi-description $DIST_NAME"
848 case $DIST_ID-$DIST_VERS in
849 ubuntu-*|debian-*) cat <<EOF
851.. code:: bash
853 # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
854 # For uWSGI debian uses the LSB init process, this might be changed
855 # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
857 create ${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}
858 enable: sudo -H ln -s ${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP} ${uWSGI_APPS_ENABLED}/
859 start: sudo -H service uwsgi start ${SEARX_UWSGI_APP%.*}
860 restart: sudo -H service uwsgi restart ${SEARX_UWSGI_APP%.*}
861 stop: sudo -H service uwsgi stop ${SEARX_UWSGI_APP%.*}
862 disable: sudo -H rm ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
864EOF
865 ;;
866 arch-*) cat <<EOF
868.. code:: bash
870 # systemd --> /usr/lib/systemd/system/uwsgi@.service
871 # For uWSGI archlinux uses systemd template units, see
872 # - http://0pointer.de/blog/projects/instances.html
873 # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
875 create: ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
876 enable: sudo -H systemctl enable uwsgi@${SEARX_UWSGI_APP%.*}
877 start: sudo -H systemctl start uwsgi@${SEARX_UWSGI_APP%.*}
878 restart: sudo -H systemctl restart uwsgi@${SEARX_UWSGI_APP%.*}
879 stop: sudo -H systemctl stop uwsgi@${SEARX_UWSGI_APP%.*}
880 disable: sudo -H systemctl disable uwsgi@${SEARX_UWSGI_APP%.*}
882EOF
883 ;;
884 fedora-*|centos-7) cat <<EOF
886.. code:: bash
888 # systemd --> /usr/lib/systemd/system/uwsgi.service
889 # The unit file starts uWSGI in emperor mode (/etc/uwsgi.ini), see
890 # - https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
892 create: ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
893 restart: sudo -H touch ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
894 disable: sudo -H rm ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
896EOF
897 ;;
898 esac
899 echo -e ".. END searx uwsgi-description $DIST_NAME"
901 echo -e "\n.. START searx uwsgi-appini $DIST_NAME"
902 echo ".. code:: bash"
903 echo
904 eval "echo \"$(< "${TEMPLATES}/${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}")\"" | prefix_stdout " "
905 echo -e "\n.. END searx uwsgi-appini $DIST_NAME"
907 )
908 done
912# ----------------------------------------------------------------------------
913main "$@"
914# ----------------------------------------------------------------------------