on master help sign in
utils/searx.sh: add script to install isolated searx service

First version which serves searx over uwsgi at http://127.0.0.1:8888 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commit9b5a7f7559faf121e44ba3e7260290bd7efe74bf authorMarkus Heiser <markus.heiser@darmarit.de> dateMon 20 Jan 2020, 16:55 (7 years ago) parent89df9d9
3 files changed+160−47
utils/lib.sh+41−23 utils/searx.sh+116−21 utils/templates/etc/uwsgi/apps-available/searx.ini+3−3
utils/lib.sh+41−23
@@ -373,66 +373,84 @@ uWSGI_restart() {
373373 sudo -H systemctl restart uwsgi
374374}
375375
376+uWSGI_app_available() {
377+ # usage: uWSGI_app_available <myapp.ini>
378+ local CONF="$1"
379+ if [[ -z $CONF ]]; then
380+ err_msg "uWSGI_app_available: missing arguments"
381+ return 42
382+ fi
383+ [[ -f "${uWSGI_SETUP}/apps-available/${CONF}" ]]
384+}
385+
376386uWSGI_install_app() {
377387
378− # usage: uWSGI_install_app [--no-eval] /etc/uwsgi/apps-available/myapp.ini
388+ # usage: uWSGI_install_app [--no-eval] <myapp.ini>
379389
380390 local no_eval=""
381− local CONF=""
391+ local CONF="$1"
382392
383393 if [[ "$1" == "--no-eval" ]]; then
384394 no_eval=$1; shift
385395 fi
386396
387− CONF=$1
388397 # shellcheck disable=SC2086
389− install_template $no_eval "${CONF}" root root 644
390− uWSGI_enable_app "$(basename "${CONF}")"
398+ install_template $no_eval "${uWSGI_SETUP}/apps-available/${CONF}" root root 644
399+ uWSGI_enable_app "${CONF}"
391400 uWSGI_restart
392− info_msg "installed uWSGI app: $(basename "${CONF}")"
401+ info_msg "installed uWSGI app: ${CONF}"
393402}
394403
395404uWSGI_remove_app() {
396405
397− # usage: uWSGI_remove_app <path.ini>
406+ # usage: uWSGI_remove_app <myapp.ini>
398407
399− local CONF=$1
400− uWSGI_disable_app "$(basename "${CONF}")"
408+ local CONF="$1"
409+ uWSGI_disable_app "${CONF}"
401410 uWSGI_restart
402− rm -f "$CONF"
403− info_msg "removed uWSGI app: $(basename "${CONF}")"
411+ rm -f "${uWSGI_SETUP}/apps-available/${CONF}"
412+ info_msg "removed uWSGI app: ${CONF}"
413+}
414+
415+uWSGI_app_enabled() {
416+ # usage: uWSGI_app_enabled <myapp.ini>
417+ local CONF="$1"
418+ if [[ -z $CONF ]]; then
419+ err_msg "uWSGI_app_enabled: missing arguments"
420+ return 42
421+ fi
422+ [[ -f "${uWSGI_SETUP}/apps-enabled/${CONF}" ]]
404423}
405424
406425# shellcheck disable=SC2164
407426uWSGI_enable_app() {
408427
409− # usage: uWSGI_enable_app <path.ini>
428+ # usage: uWSGI_enable_app <myapp.ini>
410429
411− local CONF=$1
430+ local CONF="$1"
412431 if [[ -z $CONF ]]; then
413− err_msg "uWSGI_enable_app missing arguments"
432+ err_msg "uWSGI_enable_app: missing arguments"
414433 return 42
415434 fi
416435 pushd "${uWSGI_SETUP}/apps-enabled" >/dev/null
417− rm -f "$(basename "${CONF}")"
436+ rm -f "$CONF"
418437 # shellcheck disable=SC2226
419− ln -s "../apps-available/$(basename "${CONF}")"
420− info_msg "enabled uWSGI app: $(basename "${CONF}") (restart uWSGI required)"
438+ ln -s "../apps-available/${CONF}"
439+ info_msg "enabled uWSGI app: ${CONF} (restart uWSGI required)"
421440 popd >/dev/null
422441}
423442
424443uWSGI_disable_app() {
425444
426− # usage: uWSGI_disable_app <path.ini>
445+ # usage: uWSGI_disable_app <myapp.ini>
427446
428− local CONF=$1
447+ local CONF="$1"
429448 if [[ -z $CONF ]]; then
430− err_msg "uWSGI_enable_app missing arguments"
449+ err_msg "uWSGI_enable_app: missing arguments"
431450 return 42
432451 fi
433−
434− rm -f "${uWSGI_SETUP}/apps-enabled/$CONF"
435− info_msg "disabled uWSGI app: $(basename "${CONF}") (restart uWSGI required)"
452+ rm -f "${uWSGI_SETUP}/apps-enabled/${CONF}"
453+ info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
436454}
437455
438456# distro's package manager
utils/searx.sh+116−21
@@ -32,11 +32,11 @@ SEARX_PYENV="${SERVICE_HOME}/searx-pyenv"
3232SEARX_SRC="${SERVICE_HOME}/searx-src"
3333SEARX_SETTINGS="${SEARX_SRC}/searx/settings.yml"
3434SEARX_INSTANCE_NAME="${SEARX_INSTANCE_NAME:-searx@$(uname -n)}"
35−SEARX_UWSGI_APP="${uWSGI_SETUP}/apps-available/searx.ini"
35+SEARX_UWSGI_APP="searx.ini"
3636
3737# shellcheck disable=SC2034
3838CONFIG_FILES=(
39− "${SEARX_UWSGI_APP}"
39+ "${uWSGI_SETUP}/apps-available/${SEARX_UWSGI_APP}"
4040)
4141
4242# shellcheck disable=SC2034
@@ -45,7 +45,7 @@ CONFIG_BACKUP_ENCRYPTED=(
4545)
4646
4747# ----------------------------------------------------------------------------
48−usage(){
48+usage() {
4949# ----------------------------------------------------------------------------
5050
5151 # shellcheck disable=SC1117
@@ -60,6 +60,7 @@ usage:
6060 $(basename "$0") activate [service]
6161 $(basename "$0") deactivate [service]
6262 $(basename "$0") show [service]
63+ $(basename "$0") option [debug-on|debug-off]
6364
6465shell
6566 start interactive shell from user ${SERVICE_USER}
@@ -75,12 +76,14 @@ activate
7576deactivate service
7677 stop and deactivate service daemon (systemd unit)
7778show service
78− show service status and log
79+ run some small tests and show service's status and log
80+option
81+ set one of te available options
7982EOF
8083 [ ! -z ${1+x} ] && echo -e "$1"
8184}
8285
83−main(){
86+main() {
8487 rst_title "$SERVICE_NAME" part
8588
8689 local _usage="ERROR: unknown or missing $1 command $2"
@@ -128,13 +131,21 @@ main(){
128131 activate)
129132 sudo_or_exit
130133 case $2 in
131− service) activate_service ;;
134+ service)
135+ activate_service; uWSGI_restart ;;
132136 *) usage "$_usage"; exit 42;;
133137 esac ;;
134138 deactivate)
135139 sudo_or_exit
136140 case $2 in
137− service) deactivate_service ;;
141+ service) deactivate_service; uWSGI_restart ;;
142+ *) usage "$_usage"; exit 42;;
143+ esac ;;
144+ option)
145+ sudo_or_exit
146+ case $2 in
147+ debug-on) echo; enable_debug ;;
148+ debug-off) echo; disable_debug ;;
138149 *) usage "$_usage"; exit 42;;
139150 esac ;;
140151 *) usage "ERROR: unknown or missing command $1"; exit 42;;
@@ -158,6 +169,11 @@ install_all() {
158169 test_local_searx
159170 wait_key
160171 install_searx_uwsgi
172+ if service_is_available; then
173+ info_msg "URL http://$SEARX_URL is available."
174+ else
175+ err_msg "URL http://$SEARX_URL not available, check searx & uwsgi setup!"
176+ fi
161177 wait_key
162178
163179 # ToDo ...
@@ -224,6 +240,10 @@ remove_all() {
224240 remove_user
225241}
226242
243+user_is_available() {
244+ sudo -i -u "$SERVICE_USER" echo \$HOME &>/dev/null
245+}
246+
227247assert_user() {
228248 rst_title "user $SERVICE_USER" section
229249 echo
@@ -247,8 +267,12 @@ remove_user() {
247267 fi
248268}
249269
270+clone_is_available() {
271+ [[ -f "$SEARX_SETTINGS" ]]
272+}
273+
250274# shellcheck disable=SC2164
251−clone_searx(){
275+clone_searx() {
252276 rst_title "Clone searx sources" section
253277 echo
254278 SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME 2>/dev/null)"
@@ -280,7 +304,11 @@ remove_searx() {
280304 fi
281305}
282306
283−create_pyenv(){
307+pyenv_is_available() {
308+ [[ -f "${SEARX_PYENV}/bin/activate" ]]
309+}
310+
311+create_pyenv() {
284312 rst_title "Create virtualenv (python)" section
285313 echo
286314 if [[ ! -f "${SEARX_SRC}/manage.sh" ]]; then
@@ -305,7 +333,7 @@ ${SEARX_SRC}/manage.sh update_packages
305333EOF
306334}
307335
308−remove_pyenv(){
336+remove_pyenv() {
309337 rst_title "Remove virtualenv (python)" section
310338 if ! ask_yn "Do you really want to drop ${SEARX_PYENV} ?"; then
311339 return
@@ -318,7 +346,7 @@ EOF
318346 rm -rf "${SEARX_PYENV}"
319347}
320348
321−configure_searx(){
349+configure_searx() {
322350 rst_title "Configure searx" section
323351 rst_para "Setup searx config located at $SEARX_SETTINGS"
324352 echo
@@ -329,7 +357,7 @@ sed -i -e "s/{instance_name}/${SEARX_INSTANCE_NAME}/g" "$SEARX_SETTINGS"
329357EOF
330358}
331359
332−test_local_searx(){
360+test_local_searx() {
333361 rst_title "Testing searx instance localy" section
334362 echo
335363 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
@@ -337,7 +365,7 @@ cd ${SEARX_SRC}
337365sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS"
338366timeout 5 python3 searx/webapp.py &
339367sleep 1
340−curl --location --verbose --head --insecure http://127.0.0.1:8888/
368+curl --location --verbose --head --insecure $SEARX_URL
341369sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS"
342370EOF
343371}
@@ -354,39 +382,106 @@ remove_searx_uwsgi() {
354382 uWSGI_remove_app "$SEARX_UWSGI_APP"
355383}
356384
357−activate_service () {
385+activate_service() {
358386 rst_title "Activate $SERVICE_NAME (service)" section
387+ echo
359388 uWSGI_enable_app "$SEARX_UWSGI_APP"
360389}
361390
362−deactivate_service () {
391+deactivate_service() {
363392 rst_title "De-Activate $SERVICE_NAME (service)" section
393+ echo
364394 uWSGI_disable_app "$SEARX_UWSGI_APP"
365395}
366396
367−interactive_shell(){
397+interactive_shell() {
368398 echo "// exit with CTRL-D"
369399 sudo -H -u "${SERVICE_USER}" -i
370400}
371401
372−git_diff(){
402+git_diff() {
373403 sudo -H -u "${SERVICE_USER}" -i <<EOF
374404cd ${SEARX_REPO_FOLDER}
375405git --no-pager diff
376406EOF
377407}
378408
379−show_service () {
409+service_is_available() {
410+ curl --insecure "http://$SEARX_URL" &>/dev/null
411+}
412+
413+enable_debug() {
414+ info_msg "try to enable debug mode ..."
415+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
416+cd ${SEARX_SRC}
417+sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS"
418+EOF
419+ uWSGI_restart
420+}
421+
422+disable_debug() {
423+ info_msg "try to disable debug mode ..."
424+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
425+cd ${SEARX_SRC}
426+sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS"
427+EOF
428+ uWSGI_restart
429+}
430+
431+
432+show_service() {
380433 rst_title "service status & log"
381434 echo
382− systemctl status uwsgi.service
435+
436+ if user_is_available; then
437+ info_msg "service account $SERVICE_USER available."
438+ else
439+ err_msg "service account $SERVICE_USER not available!"
440+ fi
441+ if service_is_available; then
442+ info_msg "URL http://$SEARX_URL is available."
443+ else
444+ err_msg "URL http://$SEARX_URL not available!"
445+ fi
446+ if pyenv_is_available; then
447+ info_msg "${SEARX_PYENV}/bin/activate is available."
448+ else
449+ err_msg "${SEARX_PYENV}/bin/activate not available!"
450+ fi
451+ if clone_is_available; then
452+ info_msg "Searx software is installed."
453+ else
454+ err_msg "Missing searx software!"
455+ fi
456+
457+ uWSGI_app_available "$SEARX_UWSGI_APP" \
458+ || err_msg "uWSGI app $SEARX_UWSGI_APP not available!"
459+
460+ if uWSGI_app_enabled "$SEARX_UWSGI_APP"; then
461+ info_msg "uWSGI app $SEARX_UWSGI_APP is enabled."
462+ else
463+ err_msg "uWSGI app $SEARX_UWSGI_APP not enabled!"
464+ fi
465+
466+ local _debug_on
467+ if ask_yn "Enable searx debug mode?"; then
468+ enable_debug
469+ _debug_on=1
470+ fi
471+ wait_key
383472 echo
384− read -r -s -n1 -t 5 -p "// use CTRL-C to stop monitoring the log"
473+ systemctl status uwsgi.service
474+ read -r -s -n1 -t 2 -p "// use CTRL-C to stop monitoring the log"
385475 echo
386476 while true; do
387477 trap break 2
388− journalctl -f -u uwsgi.service
478+ #journalctl -f -u uwsgi.service
479+ tail -f /var/log/uwsgi/app/searx.log
389480 done
481+
482+ if [[ $_debug_on == 1 ]]; then
483+ disable_debug
484+ fi
390485 return 0
391486}
392487
utils/templates/etc/uwsgi/apps-available/searx.ini+3−3
@@ -10,7 +10,7 @@ uid = ${SERVICE_USER}
1010gid = ${SERVICE_GROUP}
1111
1212# chdir to specified directory before apps loading
13−chdir = ${SEARX_SRC}
13+chdir = ${SEARX_SRC}/searx
1414
1515# disable logging for privacy
1616disable-logging = true
@@ -28,7 +28,7 @@ master = true
2828lazy-apps = true
2929
3030# load uWSGI plugins
31−plugin = python3
31+plugin = python3,http
3232
3333# By default the Python plugin does not initialize the GIL. This means your
3434# app-generated threads will not run. If you need threads, remember to enable
@@ -50,7 +50,7 @@ module = searx.webapp
5050virtualenv = ${SEARX_PYENV}
5151
5252# add directory (or glob) to pythonpath
53−pythonpath = ${SERVICE_HOME}
53+pythonpath = ${SEARX_SRC}
5454
5555
5656# plugin http