searx.sh 23 KB

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