searx.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. if ! sudo -i -u "$SERVICE_USER" ls -d "$REPO_ROOT" > /dev/null; then
  327. die 42 "user '$SERVICE_USER' missed read permission: $REPO_ROOT"
  328. fi
  329. SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME 2>/dev/null)"
  330. if [[ ! "${SERVICE_HOME}" ]]; then
  331. err_msg "to clone searx sources, user $SERVICE_USER hast to be created first"
  332. return 42
  333. fi
  334. export SERVICE_HOME
  335. git_clone "$REPO_ROOT" "$SEARX_SRC" \
  336. "$GIT_BRANCH" "$SERVICE_USER"
  337. pushd "${SEARX_SRC}" > /dev/null
  338. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  339. cd "${SEARX_SRC}"
  340. git remote set-url origin ${GIT_URL}
  341. git config user.email "$ADMIN_EMAIL"
  342. git config user.name "$ADMIN_NAME"
  343. git config --list
  344. EOF
  345. popd > /dev/null
  346. }
  347. install_settings() {
  348. rst_title "${SEARX_SETTINGS_PATH}" section
  349. if ! clone_is_available; then
  350. err_msg "you have to install searx first"
  351. exit 42
  352. fi
  353. mkdir -p "$(dirname ${SEARX_SETTINGS_PATH})"
  354. if [[ ! -f ${SEARX_SETTINGS_PATH} ]]; then
  355. info_msg "install settings ${REPO_ROOT}/searx/settings.yml"
  356. info_msg " --> ${SEARX_SETTINGS_PATH}"
  357. cp "${REPO_ROOT}/searx/settings.yml" "${SEARX_SETTINGS_PATH}"
  358. configure_searx
  359. return
  360. fi
  361. rst_para "Diff between origin's setting file (+) and current (-):"
  362. echo
  363. $DIFF_CMD "${SEARX_SETTINGS_PATH}" "${SEARX_SRC}/searx/settings.yml"
  364. local action
  365. choose_one action "What should happen to the settings file? " \
  366. "keep configuration unchanged" \
  367. "use origin settings" \
  368. "start interactiv shell"
  369. case $action in
  370. "keep configuration unchanged")
  371. info_msg "leave settings file unchanged"
  372. ;;
  373. "use origin settings")
  374. backup_file "${SEARX_SETTINGS_PATH}"
  375. info_msg "install origin settings"
  376. cp "${SEARX_SRC}/searx/settings.yml" "${SEARX_SETTINGS_PATH}"
  377. ;;
  378. "start interactiv shell")
  379. backup_file "${SEARX_SETTINGS_PATH}"
  380. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  381. sudo -H -i
  382. rst_para 'Diff between new setting file (-) and current (+):'
  383. echo
  384. $DIFF_CMD "${SEARX_SRC}/searx/settings.yml" "${SEARX_SETTINGS_PATH}"
  385. wait_key
  386. ;;
  387. esac
  388. }
  389. remove_settings() {
  390. rst_title "remove searx settings" section
  391. echo
  392. info_msg "delete ${SEARX_SETTINGS_PATH}"
  393. rm -f "${SEARX_SETTINGS_PATH}"
  394. }
  395. remove_searx() {
  396. rst_title "Drop searx sources" section
  397. if ask_yn "Do you really want to drop searx sources ($SEARX_SRC)?"; then
  398. rm -rf "$SEARX_SRC"
  399. else
  400. rst_para "Leave searx sources unchanged."
  401. fi
  402. }
  403. pyenv_is_available() {
  404. [[ -f "${SEARX_PYENV}/bin/activate" ]]
  405. }
  406. create_pyenv() {
  407. rst_title "Create virtualenv (python)" section
  408. echo
  409. if [[ ! -f "${SEARX_SRC}/manage.sh" ]]; then
  410. err_msg "to create pyenv for searx, searx has to be cloned first"
  411. return 42
  412. fi
  413. info_msg "create pyenv in ${SEARX_PYENV}"
  414. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  415. rm -rf "${SEARX_PYENV}"
  416. python3 -m venv "${SEARX_PYENV}"
  417. grep -qFs -- 'source ${SEARX_PYENV}/bin/activate' ~/.profile \
  418. || echo 'source ${SEARX_PYENV}/bin/activate' >> ~/.profile
  419. EOF
  420. info_msg "inspect python's virtual environment"
  421. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  422. command -v python && python --version
  423. EOF
  424. wait_key
  425. info_msg "install needed python packages"
  426. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  427. pip install wheel
  428. ${SEARX_SRC}/manage.sh update_packages
  429. EOF
  430. }
  431. remove_pyenv() {
  432. rst_title "Remove virtualenv (python)" section
  433. if ! ask_yn "Do you really want to drop ${SEARX_PYENV} ?"; then
  434. return
  435. fi
  436. info_msg "remove pyenv activation from ~/.profile"
  437. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  438. grep -v 'source ${SEARX_PYENV}/bin/activate' ~/.profile > ~/.profile.##
  439. mv ~/.profile.## ~/.profile
  440. EOF
  441. rm -rf "${SEARX_PYENV}"
  442. }
  443. configure_searx() {
  444. rst_title "Configure searx" section
  445. rst_para "Setup searx config located at $SEARX_SETTINGS_PATH"
  446. echo
  447. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  448. cd ${SEARX_SRC}
  449. sed -i -e "s/ultrasecretkey/$(openssl rand -hex 16)/g" "$SEARX_SETTINGS_PATH"
  450. sed -i -e "s/{instance_name}/${SEARX_INSTANCE_NAME}/g" "$SEARX_SETTINGS_PATH"
  451. EOF
  452. }
  453. test_local_searx() {
  454. rst_title "Testing searx instance localy" section
  455. echo
  456. if service_is_available "http://${SEARX_INTERNAL_HTTP}" &>/dev/null; then
  457. err_msg "URL/port http://${SEARX_INTERNAL_HTTP} is already in use, you"
  458. err_msg "should stop that service before starting local tests!"
  459. if ! ask_yn "Continue with local tests?"; then
  460. return
  461. fi
  462. fi
  463. sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS_PATH"
  464. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  465. export SEARX_SETTINGS_PATH="${SEARX_SETTINGS_PATH}"
  466. cd ${SEARX_SRC}
  467. timeout 10 python searx/webapp.py &
  468. sleep 3
  469. curl --location --verbose --head --insecure $SEARX_INTERNAL_HTTP
  470. EOF
  471. sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS_PATH"
  472. }
  473. install_searx_uwsgi() {
  474. rst_title "Install searx's uWSGI app (searx.ini)" section
  475. echo
  476. install_uwsgi
  477. uWSGI_install_app "$SEARX_UWSGI_APP"
  478. }
  479. remove_searx_uwsgi() {
  480. rst_title "Remove searx's uWSGI app (searx.ini)" section
  481. echo
  482. uWSGI_remove_app "$SEARX_UWSGI_APP"
  483. }
  484. activate_service() {
  485. rst_title "Activate $SEARX_INSTANCE_NAME (service)" section
  486. echo
  487. uWSGI_enable_app "$SEARX_UWSGI_APP"
  488. uWSGI_restart "$SEARX_UWSGI_APP"
  489. }
  490. deactivate_service() {
  491. rst_title "De-Activate $SEARX_INSTANCE_NAME (service)" section
  492. echo
  493. uWSGI_disable_app "$SEARX_UWSGI_APP"
  494. uWSGI_restart "$SEARX_UWSGI_APP"
  495. }
  496. enable_image_proxy() {
  497. info_msg "try to enable image_proxy ..."
  498. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  499. cd ${SEARX_SRC}
  500. sed -i -e "s/image_proxy : False/image_proxy : True/g" "$SEARX_SETTINGS_PATH"
  501. EOF
  502. uWSGI_restart "$SEARX_UWSGI_APP"
  503. }
  504. disable_image_proxy() {
  505. info_msg "try to enable image_proxy ..."
  506. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  507. cd ${SEARX_SRC}
  508. sed -i -e "s/image_proxy : True/image_proxy : False/g" "$SEARX_SETTINGS_PATH"
  509. EOF
  510. uWSGI_restart "$SEARX_UWSGI_APP"
  511. }
  512. enable_debug() {
  513. warn_msg "Do not enable debug in production enviroments!!"
  514. info_msg "try to enable debug mode ..."
  515. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  516. cd ${SEARX_SRC}
  517. sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS_PATH"
  518. EOF
  519. uWSGI_restart "$SEARX_UWSGI_APP"
  520. }
  521. disable_debug() {
  522. info_msg "try to disable debug mode ..."
  523. tee_stderr 0.1 <<EOF | sudo -H -i 2>&1 | prefix_stdout "$_service_prefix"
  524. cd ${SEARX_SRC}
  525. sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS_PATH"
  526. EOF
  527. uWSGI_restart "$SEARX_UWSGI_APP"
  528. }
  529. set_result_proxy() {
  530. # usage: set_result_proxy <URL> [<key>]
  531. info_msg "try to set result proxy: '$1' ($2)"
  532. cp "${SEARX_SETTINGS_PATH}" "${SEARX_SETTINGS_PATH}.bak"
  533. _set_result_proxy "$1" "$2" > "${SEARX_SETTINGS_PATH}"
  534. }
  535. _set_result_proxy() {
  536. local line
  537. local stage=0
  538. local url=" url: $1"
  539. local key=" key: !!binary \"$2\""
  540. if [[ -z $2 ]]; then
  541. key=
  542. fi
  543. while IFS= read -r line
  544. do
  545. if [[ $stage = 0 ]] || [[ $stage = 2 ]] ; then
  546. if [[ $line =~ ^[[:space:]]*#*[[:space:]]*result_proxy[[:space:]]*:[[:space:]]*$ ]]; then
  547. if [[ $stage = 0 ]]; then
  548. stage=1
  549. echo "result_proxy:"
  550. continue
  551. elif [[ $stage = 2 ]]; then
  552. continue
  553. fi
  554. fi
  555. fi
  556. if [[ $stage = 1 ]] || [[ $stage = 2 ]] ; then
  557. if [[ $line =~ ^[[:space:]]*#*[[:space:]]*url[[:space:]]*:[[:space:]] ]]; then
  558. [[ $stage = 1 ]] && echo "$url"
  559. continue
  560. elif [[ $line =~ ^[[:space:]]*#*[[:space:]]*key[[:space:]]*:[[:space:]] ]]; then
  561. [[ $stage = 1 ]] && [[ -n $key ]] && echo "$key"
  562. continue
  563. elif [[ $line =~ ^[[:space:]]*$ ]]; then
  564. stage=2
  565. fi
  566. fi
  567. echo "$line"
  568. done < "${SEARX_SETTINGS_PATH}.bak"
  569. }
  570. function has_substring() {
  571. [[ "$1" != "${2/$1/}" ]]
  572. }
  573. inspect_service() {
  574. rst_title "service status & log"
  575. cat <<EOF
  576. sourced ${DOT_CONFIG#"$REPO_ROOT/"} :
  577. PUBLIC_URL : ${PUBLIC_URL}
  578. SEARX_URL_PATH : ${SEARX_URL_PATH}
  579. SEARX_INSTANCE_NAME : ${SEARX_INSTANCE_NAME}
  580. SEARX_INTERNAL_HTTP : ${SEARX_INTERNAL_HTTP}
  581. EOF
  582. if service_account_is_available "$SERVICE_USER"; then
  583. info_msg "Service account $SERVICE_USER exists."
  584. else
  585. err_msg "Service account $SERVICE_USER does not exists!"
  586. fi
  587. if pyenv_is_available; then
  588. info_msg "~$SERVICE_USER: python environment is available."
  589. else
  590. err_msg "~$SERVICE_USER: python environment is not available!"
  591. fi
  592. if clone_is_available; then
  593. info_msg "~$SERVICE_USER: Searx software is installed."
  594. else
  595. err_msg "~$SERVICE_USER: Missing searx software!"
  596. fi
  597. if uWSGI_app_enabled "$SEARX_UWSGI_APP"; then
  598. info_msg "uWSGI app $SEARX_UWSGI_APP is enabled."
  599. else
  600. err_msg "uWSGI app $SEARX_UWSGI_APP not enabled!"
  601. fi
  602. uWSGI_app_available "$SEARX_UWSGI_APP" \
  603. || err_msg "uWSGI app $SEARX_UWSGI_APP not available!"
  604. if in_container; then
  605. lxc_suite_info
  606. else
  607. info_msg "public URL --> ${PUBLIC_URL}"
  608. info_msg "internal URL --> http://${SEARX_INTERNAL_HTTP}"
  609. fi
  610. if ! service_is_available "http://${SEARX_INTERNAL_HTTP}"; then
  611. err_msg "uWSGI app (service) at http://${SEARX_INTERNAL_HTTP} is not available!"
  612. MSG="${_Green}[${_BCyan}CTRL-C${_Green}] to stop or [${_BCyan}KEY${_Green}] to continue"\
  613. wait_key
  614. fi
  615. if ! service_is_available "${PUBLIC_URL}"; then
  616. warn_msg "Public service at ${PUBLIC_URL} is not available!"
  617. if ! in_container; then
  618. warn_msg "Check if public name is correct and routed or use the public IP from above."
  619. fi
  620. fi
  621. local _debug_on
  622. if ask_yn "Enable searx debug mode?"; then
  623. enable_debug
  624. _debug_on=1
  625. fi
  626. echo
  627. case $DIST_ID-$DIST_VERS in
  628. ubuntu-*|debian-*)
  629. systemctl --no-pager -l status "${SERVICE_NAME}"
  630. ;;
  631. arch-*)
  632. systemctl --no-pager -l status "uwsgi@${SERVICE_NAME%.*}"
  633. ;;
  634. fedora-*)
  635. systemctl --no-pager -l status uwsgi
  636. ;;
  637. esac
  638. # shellcheck disable=SC2059
  639. printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
  640. read -r -s -n1 -t 5
  641. echo
  642. while true; do
  643. trap break 2
  644. case $DIST_ID-$DIST_VERS in
  645. ubuntu-*|debian-*) tail -f /var/log/uwsgi/app/searx.log ;;
  646. arch-*) journalctl -f -u "uwsgi@${SERVICE_NAME%.*}" ;;
  647. fedora-*) journalctl -f -u uwsgi ;;
  648. esac
  649. done
  650. if [[ $_debug_on == 1 ]]; then
  651. disable_debug
  652. fi
  653. return 0
  654. }
  655. install_apache_site() {
  656. rst_title "Install Apache site $APACHE_SEARX_SITE"
  657. rst_para "\
  658. This installs the searx uwsgi app as apache site. If your server is public to
  659. the internet, you should instead use a reverse proxy (filtron) to block
  660. excessively bot queries."
  661. ! apache_is_installed && err_msg "Apache is not installed."
  662. if ! ask_yn "Do you really want to continue?" Yn; then
  663. return
  664. else
  665. install_apache
  666. fi
  667. apache_install_site --variant=uwsgi "${APACHE_SEARX_SITE}"
  668. rst_title "Install searx's uWSGI app (searx.ini)" section
  669. echo
  670. uWSGI_install_app --variant=socket "$SEARX_UWSGI_APP"
  671. if ! service_is_available "${PUBLIC_URL}"; then
  672. err_msg "Public service at ${PUBLIC_URL} is not available!"
  673. fi
  674. }
  675. remove_apache_site() {
  676. rst_title "Remove Apache site ${APACHE_SEARX_SITE}"
  677. rst_para "\
  678. This removes apache site ${APACHE_SEARX_SITE}."
  679. ! apache_is_installed && err_msg "Apache is not installed."
  680. if ! ask_yn "Do you really want to continue?" Yn; then
  681. return
  682. fi
  683. apache_remove_site "${APACHE_SEARX_SITE}"
  684. rst_title "Remove searx's uWSGI app (searx.ini)" section
  685. echo
  686. uWSGI_remove_app "$SEARX_UWSGI_APP"
  687. }
  688. rst-doc() {
  689. local debian="${SEARX_PACKAGES_debian}"
  690. local arch="${SEARX_PACKAGES_arch}"
  691. local fedora="${SEARX_PACKAGES_fedora}"
  692. local debian_build="${BUILD_PACKAGES_debian}"
  693. local arch_build="${BUILD_PACKAGES_arch}"
  694. local fedora_build="${BUILD_PACKAGES_fedora}"
  695. debian="$(echo "${debian}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  696. arch="$(echo "${arch}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  697. fedora="$(echo "${fedora}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  698. debian_build="$(echo "${debian_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  699. arch_build="$(echo "${arch_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  700. fedora_build="$(echo "${fedora_build}" | sed 's/.*/ & \\/' | sed '$ s/.$//')"
  701. eval "echo \"$(< "${REPO_ROOT}/docs/build-templates/searx.rst")\""
  702. # I use ubuntu-20.04 here to demonstrate that versions are also suported,
  703. # normaly debian-* and ubuntu-* are most the same.
  704. for DIST_NAME in ubuntu-20.04 arch fedora; do
  705. (
  706. DIST_ID=${DIST_NAME%-*}
  707. DIST_VERS=${DIST_NAME#*-}
  708. [[ $DIST_VERS =~ $DIST_ID ]] && DIST_VERS=
  709. uWSGI_distro_setup
  710. echo -e "\n.. START searx uwsgi-description $DIST_NAME"
  711. case $DIST_ID-$DIST_VERS in
  712. ubuntu-*|debian-*) cat <<EOF
  713. # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
  714. # For uWSGI debian uses the LSB init process, this might be changed
  715. # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
  716. create ${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}
  717. enable: sudo -H ln -s ${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP} ${uWSGI_APPS_ENABLED}/
  718. start: sudo -H service uwsgi start ${SEARX_UWSGI_APP%.*}
  719. restart: sudo -H service uwsgi restart ${SEARX_UWSGI_APP%.*}
  720. stop: sudo -H service uwsgi stop ${SEARX_UWSGI_APP%.*}
  721. disable: sudo -H rm ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  722. EOF
  723. ;;
  724. arch-*) cat <<EOF
  725. # systemd --> /usr/lib/systemd/system/uwsgi@.service
  726. # For uWSGI archlinux uses systemd template units, see
  727. # - http://0pointer.de/blog/projects/instances.html
  728. # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
  729. create: ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  730. enable: sudo -H systemctl enable uwsgi@${SEARX_UWSGI_APP%.*}
  731. start: sudo -H systemctl start uwsgi@${SEARX_UWSGI_APP%.*}
  732. restart: sudo -H systemctl restart uwsgi@${SEARX_UWSGI_APP%.*}
  733. stop: sudo -H systemctl stop uwsgi@${SEARX_UWSGI_APP%.*}
  734. disable: sudo -H systemctl disable uwsgi@${SEARX_UWSGI_APP%.*}
  735. EOF
  736. ;;
  737. fedora-*) cat <<EOF
  738. # systemd --> /usr/lib/systemd/system/uwsgi.service
  739. # The unit file starts uWSGI in emperor mode (/etc/uwsgi.ini), see
  740. # - https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
  741. create: ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  742. restart: sudo -H touch ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  743. disable: sudo -H rm ${uWSGI_APPS_ENABLED}/${SEARX_UWSGI_APP}
  744. EOF
  745. ;;
  746. esac
  747. echo -e ".. END searx uwsgi-description $DIST_NAME"
  748. echo -e "\n.. START searx uwsgi-appini $DIST_NAME"
  749. eval "echo \"$(< "${TEMPLATES}/${uWSGI_APPS_AVAILABLE}/${SEARX_UWSGI_APP}")\""
  750. echo -e "\n.. END searx uwsgi-appini $DIST_NAME"
  751. )
  752. done
  753. }
  754. # ----------------------------------------------------------------------------
  755. main "$@"
  756. # ----------------------------------------------------------------------------