searx.sh 27 KB

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