searx.sh 23 KB

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