searx.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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
  5. # shellcheck source=utils/lib.sh
  6. source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
  7. source_dot_config
  8. source "${REPO_ROOT}/utils/lxc-searx.env"
  9. in_container && lxc_set_suite_env
  10. # ----------------------------------------------------------------------------
  11. # config
  12. # ----------------------------------------------------------------------------
  13. SEARX_INTERNAL_URL="${SEARX_INTERNAL_URL:-127.0.0.1:8888}"
  14. SEARX_URL_PATH="${SEARX_URL_PATH:-$(echo "${PUBLIC_URL}" \
  15. | sed -e 's,^.*://[^/]*\(/.*\),\1,g')}"
  16. [[ "${SEARX_URL_PATH}" == "${PUBLIC_URL}" ]] && SEARX_URL_PATH=/
  17. SEARX_INSTANCE_NAME="${SEARX_INSTANCE_NAME:-searx@$(echo "$PUBLIC_URL" \
  18. | sed -e 's,^.*://\([^\:/]*\).*,\1,g') }"
  19. SERVICE_NAME="searx"
  20. SERVICE_USER="${SERVICE_USER:-${SERVICE_NAME}}"
  21. SERVICE_HOME_BASE="${SERVICE_HOME_BASE:-/usr/local}"
  22. SERVICE_HOME="${SERVICE_HOME_BASE}/${SERVICE_USER}"
  23. # shellcheck disable=SC2034
  24. SERVICE_GROUP="${SERVICE_USER}"
  25. SEARX_GIT_URL="${SEARX_GIT_URL:-https://github.com/asciimoo/searx.git}"
  26. SEARX_GIT_BRANCH="${SEARX_GIT_BRANCH:-master}"
  27. SEARX_PYENV="${SERVICE_HOME}/searx-pyenv"
  28. SEARX_SRC="${SERVICE_HOME}/searx-src"
  29. SEARX_SETTINGS_PATH="/etc/searx/settings.yml"
  30. SEARX_UWSGI_APP="searx.ini"
  31. # shellcheck disable=SC2034
  32. SEARX_UWSGI_SOCKET="/run/uwsgi/app/searx/socket"
  33. # apt packages
  34. SEARX_PACKAGES_debian="\
  35. python3-dev python3-babel python3-venv
  36. uwsgi uwsgi-plugin-python3
  37. git build-essential libxslt-dev zlib1g-dev libffi-dev libssl-dev"
  38. # pacman packages
  39. SEARX_PACKAGES_arch="\
  40. python python-pip python-lxml python-babel
  41. uwsgi uwsgi-plugin-python
  42. git base-devel libxml2"
  43. # dnf packages
  44. SEARX_PACKAGES_fedora="\
  45. python python-pip python-lxml python-babel
  46. uwsgi uwsgi-plugin-python3
  47. git @development-tools libxml2"
  48. case $DIST_ID in
  49. ubuntu|debian) SEARX_PACKAGES="${SEARX_PACKAGES_debian}" ;;
  50. arch) SEARX_PACKAGES="${SEARX_PACKAGES_arch}" ;;
  51. fedora) SEARX_PACKAGES="${SEARX_PACKAGES_fedora}" ;;
  52. esac
  53. # Apache Settings
  54. APACHE_APT_PACKAGES="\
  55. libapache2-mod-uwsgi \
  56. "
  57. APACHE_SEARX_SITE="searx.conf"
  58. # shellcheck disable=SC2034
  59. CONFIG_FILES=(
  60. "${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}"
  61. )
  62. # shellcheck disable=SC2034
  63. CONFIG_BACKUP_ENCRYPTED=(
  64. "${SEARX_SETTINGS_PATH}"
  65. )
  66. # ----------------------------------------------------------------------------
  67. usage() {
  68. # ----------------------------------------------------------------------------
  69. # shellcheck disable=SC1117
  70. cat <<EOF
  71. usage::
  72. $(basename "$0") shell
  73. $(basename "$0") install [all|user|searx-src|pyenv|uwsgi|apache]
  74. $(basename "$0") update [searx]
  75. $(basename "$0") remove [all|user|pyenv|searx-src]
  76. $(basename "$0") activate [service]
  77. $(basename "$0") deactivate [service]
  78. $(basename "$0") inspect [service]
  79. $(basename "$0") option [debug-on|debug-off]
  80. $(basename "$0") apache [install|remove]
  81. shell
  82. start interactive shell from user ${SERVICE_USER}
  83. install / remove
  84. :all: complete (de-) installation of searx service
  85. :user: add/remove service user '$SERVICE_USER' ($SERVICE_HOME)
  86. :searx-src: clone $SEARX_GIT_URL
  87. :pyenv: create/remove virtualenv (python) in $SEARX_PYENV
  88. :uwsgi: install searx uWSGI application
  89. :settings: reinstall settings from ${REPO_ROOT}/searx/settings.yml
  90. update searx
  91. Update searx installation ($SERVICE_HOME)
  92. activate service
  93. activate and start service daemon (systemd unit)
  94. deactivate service
  95. stop and deactivate service daemon (systemd unit)
  96. inspect service
  97. run some small tests and inspect service's status and log
  98. option
  99. set one of the available options
  100. apache
  101. :install: apache site with the searx uwsgi app
  102. :remove: apache site ${APACHE_FILTRON_SITE}
  103. searx settings: ${SEARX_SETTINGS_PATH}
  104. If needed, set PUBLIC_URL of your WEB service in the '${DOT_CONFIG#"$REPO_ROOT/"}' file::
  105. PUBLIC_URL : ${PUBLIC_URL}
  106. PUBLIC_HOST : ${PUBLIC_HOST}
  107. SEARX_INSTANCE_NAME : ${SEARX_INSTANCE_NAME}
  108. SERVICE_USER : ${SERVICE_USER}
  109. EOF
  110. [[ -n ${1} ]] && err_msg "$1"
  111. }
  112. main() {
  113. rst_title "$SEARX_INSTANCE_NAME" part
  114. required_commands \
  115. sudo systemctl install git wget curl \
  116. || exit
  117. local _usage="unknown or missing $1 command $2"
  118. case $1 in
  119. --source-only) ;;
  120. -h|--help) usage; exit 0;;
  121. shell)
  122. sudo_or_exit
  123. interactive_shell "${SERVICE_USER}"
  124. ;;
  125. inspect)
  126. case $2 in
  127. service)
  128. sudo_or_exit
  129. inspect_service
  130. ;;
  131. *) usage "$_usage"; exit 42;;
  132. esac ;;
  133. install)
  134. sudo_or_exit
  135. case $2 in
  136. all) install_all ;;
  137. user) assert_user ;;
  138. pyenv) create_pyenv ;;
  139. searx-src) clone_searx ;;
  140. settings) install_settings ;;
  141. uwsgi) install_searx_uwsgi;;
  142. *) usage "$_usage"; exit 42;;
  143. esac ;;
  144. update)
  145. sudo_or_exit
  146. case $2 in
  147. searx) update_searx;;
  148. *) usage "$_usage"; exit 42;;
  149. esac ;;
  150. remove)
  151. sudo_or_exit
  152. case $2 in
  153. all) remove_all;;
  154. user) drop_service_account "${SERVICE_USER}";;
  155. pyenv) remove_pyenv ;;
  156. searx-src) remove_searx ;;
  157. *) usage "$_usage"; exit 42;;
  158. esac ;;
  159. activate)
  160. sudo_or_exit
  161. case $2 in
  162. service)
  163. activate_service ;;
  164. *) usage "$_usage"; exit 42;;
  165. esac ;;
  166. deactivate)
  167. sudo_or_exit
  168. case $2 in
  169. service) deactivate_service ;;
  170. *) usage "$_usage"; exit 42;;
  171. esac ;;
  172. option)
  173. sudo_or_exit
  174. case $2 in
  175. debug-on) echo; enable_debug ;;
  176. debug-off) echo; disable_debug ;;
  177. *) usage "$_usage"; exit 42;;
  178. esac ;;
  179. apache)
  180. sudo_or_exit
  181. case $2 in
  182. install) install_apache_site ;;
  183. remove) remove_apache_site ;;
  184. *) usage "$_usage"; exit 42;;
  185. esac ;;
  186. doc) rst-doc;;
  187. *) usage "unknown or missing command $1"; exit 42;;
  188. esac
  189. }
  190. _service_prefix=" ${_Yellow}|$SERVICE_USER|${_creset} "
  191. install_all() {
  192. rst_title "Install $SEARX_INSTANCE_NAME (service)"
  193. pkg_install "$SEARX_PACKAGES"
  194. wait_key
  195. case $DIST_ID-$DIST_VERS in
  196. fedora-*)
  197. systemctl enable uwsgi
  198. ;;
  199. esac
  200. assert_user
  201. wait_key
  202. clone_searx
  203. wait_key
  204. create_pyenv
  205. wait_key
  206. install_settings
  207. wait_key
  208. test_local_searx
  209. wait_key
  210. install_searx_uwsgi
  211. if ! service_is_available "http://$SEARX_INTERNAL_URL"; then
  212. err_msg "URL http://$SEARX_INTERNAL_URL not available, check searx & uwsgi setup!"
  213. fi
  214. if ask_yn "Do you want to inspect the installation?" Ny; then
  215. inspect_service
  216. fi
  217. }
  218. update_searx() {
  219. rst_title "Update searx instance"
  220. echo
  221. tee_stderr 0.3 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  222. cd ${SEARX_SRC}
  223. git checkout -B "$SEARX_GIT_BRANCH"
  224. git pull
  225. ${SEARX_SRC}/manage.sh update_packages
  226. EOF
  227. install_settings
  228. uWSGI_restart "$SEARX_UWSGI_APP"
  229. }
  230. remove_all() {
  231. rst_title "De-Install $SEARX_INSTANCE_NAME (service)"
  232. rst_para "\
  233. It goes without saying that this script can only be used to remove
  234. installations that were installed with this script."
  235. if ! ask_yn "Do you really want to deinstall $SEARX_INSTANCE_NAME?"; then
  236. return
  237. fi
  238. remove_searx_uwsgi
  239. drop_service_account "${SERVICE_USER}"
  240. remove_settings
  241. wait_key
  242. if service_is_available "${PUBLIC_URL}"; then
  243. MSG="** Don't forgett to remove your public site! (${PUBLIC_URL}) **" wait_key 10
  244. fi
  245. }
  246. assert_user() {
  247. rst_title "user $SERVICE_USER" section
  248. echo
  249. tee_stderr 1 <<EOF | bash | prefix_stdout
  250. useradd --shell /bin/bash --system \
  251. --home-dir "$SERVICE_HOME" \
  252. --comment 'Privacy-respecting metasearch engine' $SERVICE_USER
  253. mkdir "$SERVICE_HOME"
  254. chown -R "$SERVICE_GROUP:$SERVICE_GROUP" "$SERVICE_HOME"
  255. groups $SERVICE_USER
  256. EOF
  257. #SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
  258. #export SERVICE_HOME
  259. #echo "export SERVICE_HOME=$SERVICE_HOME"
  260. }
  261. clone_is_available() {
  262. [[ -f "$SEARX_SRC/.git/config" ]]
  263. }
  264. # shellcheck disable=SC2164
  265. clone_searx() {
  266. rst_title "Clone searx sources" section
  267. echo
  268. SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME 2>/dev/null)"
  269. if [[ ! "${SERVICE_HOME}" ]]; then
  270. err_msg "to clone searx sources, user $SERVICE_USER hast to be created first"
  271. return 42
  272. fi
  273. export SERVICE_HOME
  274. git_clone "$REPO_ROOT" "$SEARX_SRC" \
  275. "$SEARX_GIT_BRANCH" "$SERVICE_USER"
  276. pushd "${SEARX_SRC}" > /dev/null
  277. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  278. cd "${SEARX_SRC}"
  279. git remote set-url origin ${SEARX_GIT_URL}
  280. git config user.email "$ADMIN_EMAIL"
  281. git config user.name "$ADMIN_NAME"
  282. git config --list
  283. EOF
  284. popd > /dev/null
  285. }
  286. install_settings() {
  287. rst_title "${SEARX_SETTINGS_PATH}" section
  288. if ! clone_is_available; then
  289. err_msg "you have to install searx first"
  290. exit 42
  291. fi
  292. mkdir -p "$(dirname ${SEARX_SETTINGS_PATH})"
  293. if [[ ! -f ${SEARX_SETTINGS_PATH} ]]; then
  294. info_msg "install settings ${REPO_ROOT}/searx/settings.yml"
  295. info_msg " --> ${SEARX_SETTINGS_PATH}"
  296. cp "${REPO_ROOT}/searx/settings.yml" "${SEARX_SETTINGS_PATH}"
  297. configure_searx
  298. return
  299. fi
  300. rst_para "Diff between origin's setting file (+) and current (-):"
  301. echo
  302. $DIFF_CMD "${SEARX_SETTINGS_PATH}" "${SEARX_SRC}/searx/settings.yml"
  303. local action
  304. choose_one action "What should happen to the settings file? " \
  305. "keep configuration unchanged" \
  306. "use origin settings" \
  307. "start interactiv shell"
  308. case $action in
  309. "keep configuration unchanged")
  310. info_msg "leave settings file unchanged"
  311. ;;
  312. "use origin settings")
  313. backup_file "${SEARX_SETTINGS_PATH}"
  314. info_msg "install origin settings"
  315. cp "${SEARX_SRC}/searx/settings.yml" "${SEARX_SETTINGS_PATH}"
  316. ;;
  317. "start interactiv shell")
  318. backup_file "${SEARX_SETTINGS_PATH}"
  319. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  320. sudo -H -i
  321. rst_para 'Diff between new setting file (-) and current (+):'
  322. echo
  323. $DIFF_CMD "${SEARX_SRC}/searx/settings.yml" "${SEARX_SETTINGS_PATH}"
  324. wait_key
  325. ;;
  326. esac
  327. }
  328. remove_settings() {
  329. rst_title "remove searx settings" section
  330. echo
  331. info_msg "delete ${SEARX_SETTINGS_PATH}"
  332. rm -f "${SEARX_SETTINGS_PATH}"
  333. }
  334. remove_searx() {
  335. rst_title "Drop searx sources" section
  336. if ask_yn "Do you really want to drop searx sources ($SEARX_SRC)?"; then
  337. rm -rf "$SEARX_SRC"
  338. else
  339. rst_para "Leave searx sources unchanged."
  340. fi
  341. }
  342. pyenv_is_available() {
  343. [[ -f "${SEARX_PYENV}/bin/activate" ]]
  344. }
  345. create_pyenv() {
  346. rst_title "Create virtualenv (python)" section
  347. echo
  348. if [[ ! -f "${SEARX_SRC}/manage.sh" ]]; then
  349. err_msg "to create pyenv for searx, searx has to be cloned first"
  350. return 42
  351. fi
  352. info_msg "create pyenv in ${SEARX_PYENV}"
  353. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  354. rm -rf "${SEARX_PYENV}"
  355. python3 -m venv "${SEARX_PYENV}"
  356. grep -qFs -- 'source ${SEARX_PYENV}/bin/activate' ~/.profile \
  357. || echo 'source ${SEARX_PYENV}/bin/activate' >> ~/.profile
  358. EOF
  359. info_msg "inspect python's virtual environment"
  360. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  361. command -v python && python --version
  362. EOF
  363. wait_key
  364. info_msg "install needed python packages"
  365. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  366. ${SEARX_SRC}/manage.sh update_packages
  367. EOF
  368. }
  369. remove_pyenv() {
  370. rst_title "Remove virtualenv (python)" section
  371. if ! ask_yn "Do you really want to drop ${SEARX_PYENV} ?"; then
  372. return
  373. fi
  374. info_msg "remove pyenv activation from ~/.profile"
  375. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  376. grep -v 'source ${SEARX_PYENV}/bin/activate' ~/.profile > ~/.profile.##
  377. mv ~/.profile.## ~/.profile
  378. EOF
  379. rm -rf "${SEARX_PYENV}"
  380. }
  381. configure_searx() {
  382. rst_title "Configure searx" section
  383. rst_para "Setup searx config located at $SEARX_SETTINGS_PATH"
  384. echo
  385. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  386. cd ${SEARX_SRC}
  387. sed -i -e "s/ultrasecretkey/$(openssl rand -hex 16)/g" "$SEARX_SETTINGS_PATH"
  388. sed -i -e "s/{instance_name}/${SEARX_INSTANCE_NAME}/g" "$SEARX_SETTINGS_PATH"
  389. EOF
  390. }
  391. test_local_searx() {
  392. rst_title "Testing searx instance localy" section
  393. echo
  394. if service_is_available "http://$SEARX_INTERNAL_URL" &>/dev/null; then
  395. err_msg "URL/port http://$SEARX_INTERNAL_URL is already in use, you"
  396. err_msg "should stop that service before starting local tests!"
  397. if ! ask_yn "Continue with local tests?"; then
  398. return
  399. fi
  400. fi
  401. sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS_PATH"
  402. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  403. export SEARX_SETTINGS_PATH="${SEARX_SETTINGS_PATH}"
  404. cd ${SEARX_SRC}
  405. timeout 10 python searx/webapp.py &
  406. sleep 3
  407. curl --location --verbose --head --insecure $SEARX_INTERNAL_URL
  408. EOF
  409. sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS_PATH"
  410. }
  411. install_searx_uwsgi() {
  412. rst_title "Install searx's uWSGI app (searx.ini)" section
  413. echo
  414. uWSGI_install_app "$SEARX_UWSGI_APP"
  415. }
  416. remove_searx_uwsgi() {
  417. rst_title "Remove searx's uWSGI app (searx.ini)" section
  418. echo
  419. uWSGI_remove_app "$SEARX_UWSGI_APP"
  420. }
  421. activate_service() {
  422. rst_title "Activate $SEARX_INSTANCE_NAME (service)" section
  423. echo
  424. uWSGI_enable_app "$SEARX_UWSGI_APP"
  425. uWSGI_restart "$SEARX_UWSGI_APP"
  426. }
  427. deactivate_service() {
  428. rst_title "De-Activate $SEARX_INSTANCE_NAME (service)" section
  429. echo
  430. uWSGI_disable_app "$SEARX_UWSGI_APP"
  431. uWSGI_restart "$SEARX_UWSGI_APP"
  432. }
  433. enable_debug() {
  434. warn_msg "Do not enable debug in production enviroments!!"
  435. info_msg "try to enable debug mode ..."
  436. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  437. cd ${SEARX_SRC}
  438. sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS_PATH"
  439. EOF
  440. uWSGI_restart "$SEARX_UWSGI_APP"
  441. }
  442. disable_debug() {
  443. info_msg "try to disable debug mode ..."
  444. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  445. cd ${SEARX_SRC}
  446. sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS_PATH"
  447. EOF
  448. uWSGI_restart "$SEARX_UWSGI_APP"
  449. }
  450. inspect_service() {
  451. rst_title "service status & log"
  452. cat <<EOF
  453. sourced ${DOT_CONFIG#"$REPO_ROOT/"} :
  454. PUBLIC_URL : ${PUBLIC_URL}
  455. PUBLIC_HOST : ${PUBLIC_HOST}
  456. SEARX_URL_PATH : ${SEARX_URL_PATH}
  457. SEARX_INSTANCE_NAME : ${SEARX_INSTANCE_NAME}
  458. SEARX_INTERNAL_URL : ${SEARX_INTERNAL_URL}
  459. EOF
  460. apache_is_installed && info_msg "Apache is installed."
  461. if service_account_is_available "$SERVICE_USER"; then
  462. info_msg "Service account $SERVICE_USER exists."
  463. else
  464. err_msg "Service account $SERVICE_USER does not exists!"
  465. fi
  466. if pyenv_is_available; then
  467. info_msg "~$SERVICE_USER: python environment is available."
  468. else
  469. err_msg "~$SERVICE_USER: python environment is not available!"
  470. fi
  471. if clone_is_available; then
  472. info_msg "~$SERVICE_USER: Searx software is installed."
  473. else
  474. err_msg "~$SERVICE_USER: Missing searx software!"
  475. fi
  476. if uWSGI_app_enabled "$SEARX_UWSGI_APP"; then
  477. info_msg "uWSGI app $SEARX_UWSGI_APP is enabled."
  478. else
  479. err_msg "uWSGI app $SEARX_UWSGI_APP not enabled!"
  480. fi
  481. uWSGI_app_available "$SEARX_UWSGI_APP" \
  482. || err_msg "uWSGI app $SEARX_UWSGI_APP not available!"
  483. if in_container; then
  484. lxc_suite_info
  485. else
  486. info_msg "public URL --> ${PUBLIC_URL}"
  487. info_msg "internal URL --> http://${SEARX_INTERNAL_URL}"
  488. fi
  489. if ! service_is_available "http://${SEARX_INTERNAL_URL}"; then
  490. err_msg "uWSGI app (service) at http://${SEARX_INTERNAL_URL} is not available!"
  491. MSG="${_Green}[${_BCyan}CTRL-C${_Green}] to stop or [${_BCyan}KEY${_Green}] to continue"\
  492. wait_key
  493. fi
  494. if ! service_is_available "${PUBLIC_URL}"; then
  495. warn_msg "Public service at ${PUBLIC_URL} is not available!"
  496. if ! in_container; then
  497. warn_msg "Check if public name is correct and routed or use the public IP from above."
  498. fi
  499. fi
  500. local _debug_on
  501. if ask_yn "Enable searx debug mode?"; then
  502. enable_debug
  503. _debug_on=1
  504. fi
  505. echo
  506. case $DIST_ID-$DIST_VERS in
  507. ubuntu-*|debian-*)
  508. systemctl --no-pager -l status "${SERVICE_NAME}"
  509. ;;
  510. arch-*)
  511. systemctl --no-pager -l status "uwsgi@${SERVICE_NAME%.*}"
  512. ;;
  513. fedora-*)
  514. systemctl --no-pager -l status uwsgi
  515. ;;
  516. esac
  517. # shellcheck disable=SC2059
  518. printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
  519. read -r -s -n1 -t 5
  520. echo
  521. while true; do
  522. trap break 2
  523. case $DIST_ID-$DIST_VERS in
  524. ubuntu-*|debian-*) tail -f /var/log/uwsgi/app/searx.log ;;
  525. arch-*) journalctl -f -u "uwsgi@${SERVICE_NAME%.*}" ;;
  526. fedora-*) journalctl -f -u uwsgi ;;
  527. esac
  528. done
  529. if [[ $_debug_on == 1 ]]; then
  530. disable_debug
  531. fi
  532. return 0
  533. }
  534. install_apache_site() {
  535. rst_title "Install Apache site $APACHE_SEARX_SITE"
  536. rst_para "\
  537. This installs the searx uwsgi app as apache site. If your server ist public to
  538. the internet you should instead use a reverse proxy (filtron) to block
  539. excessively bot queries."
  540. ! apache_is_installed && err_msg "Apache is not installed."
  541. if ! ask_yn "Do you really want to install apache site for searx-uwsgi?"; then
  542. return
  543. fi
  544. pkg_install "$APACHE_APT_PACKAGES"
  545. a2enmod uwsgi
  546. echo
  547. apache_install_site --variant=uwsgi "${APACHE_SEARX_SITE}"
  548. if ! service_is_available "${PUBLIC_URL}"; then
  549. err_msg "Public service at ${PUBLIC_URL} is not available!"
  550. fi
  551. }
  552. remove_apache_site() {
  553. rst_title "Remove Apache site ${APACHE_SEARX_SITE}"
  554. rst_para "\
  555. This removes apache site ${APACHE_SEARX_SITE}."
  556. ! apache_is_installed && err_msg "Apache is not installed."
  557. if ! ask_yn "Do you really want to continue?"; then
  558. return
  559. fi
  560. apache_remove_site "${APACHE_SEARX_SITE}"
  561. }
  562. rst-doc() {
  563. local debian="${SEARX_PACKAGES_debian}"
  564. local arch="${SEARX_PACKAGES_arch}"
  565. local fedora="${SEARX_PACKAGES_fedora}"
  566. debian="$(echo "${debian}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  567. arch="$(echo "${arch}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  568. fedora="$(echo "${fedora}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  569. eval "echo \"$(< "${REPO_ROOT}/docs/build-templates/searx.rst")\""
  570. # I use ubuntu-20.04 here to demonstrate that versions are also suported,
  571. # normaly debian-* and ubuntu-* are most the same.
  572. for DIST_NAME in ubuntu-20.04 arch fedora; do
  573. (
  574. DIST_ID=${DIST_NAME%-*}
  575. DIST_VERS=${DIST_NAME#*-}
  576. [[ $DIST_VERS =~ $DIST_ID ]] && DIST_VERS=
  577. uWSGI_distro_setup
  578. echo -e "\n.. START searx uwsgi-description $DIST_NAME"
  579. case $DIST_ID-$DIST_VERS in
  580. ubuntu-*|debian-*) cat <<EOF
  581. # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
  582. # For uWSGI debian uses the LSB init process, this might be changed
  583. # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
  584. create ${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}
  585. enable: sudo -H ln -s ${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP} ${uWSGI_APPS_ENABLED}/
  586. start: sudo -H service uwsgi start ${SEARX_UWSGI_APP%.*}
  587. restart: sudo -H service uwsgi restart ${SEARX_UWSGI_APP%.*}
  588. stop: sudo -H service uwsgi stop ${SEARX_UWSGI_APP%.*}
  589. disable: sudo -H rm ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  590. EOF
  591. ;;
  592. arch-*) cat <<EOF
  593. # systemd --> /usr/lib/systemd/system/uwsgi@.service
  594. # For uWSGI archlinux uses systemd template units, see
  595. # - http://0pointer.de/blog/projects/instances.html
  596. # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
  597. create: ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  598. enable: sudo -H systemctl enable uwsgi@${SEARX_UWSGI_APP%.*}
  599. start: sudo -H systemctl start uwsgi@${SEARX_UWSGI_APP%.*}
  600. restart: sudo -H systemctl restart uwsgi@${SEARX_UWSGI_APP%.*}
  601. stop: sudo -H systemctl stop uwsgi@${SEARX_UWSGI_APP%.*}
  602. disable: sudo -H systemctl disable uwsgi@${SEARX_UWSGI_APP%.*}
  603. EOF
  604. ;;
  605. fedora-*) cat <<EOF
  606. # systemd --> /usr/lib/systemd/system/uwsgi.service
  607. # The unit file starts uWSGI in emperor mode (/etc/uwsgi.ini), see
  608. # - https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
  609. create: ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  610. restart: sudo -H touch ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  611. disable: sudo -H rm ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  612. EOF
  613. ;;
  614. esac
  615. echo -e ".. END searx uwsgi-description $DIST_NAME"
  616. echo -e "\n.. START searx uwsgi-appini $DIST_NAME"
  617. eval "echo \"$(< "${TEMPLATES}/${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}")\""
  618. echo -e "\n.. END searx uwsgi-appini $DIST_NAME"
  619. )
  620. done
  621. }
  622. # ----------------------------------------------------------------------------
  623. main "$@"
  624. # ----------------------------------------------------------------------------