searx.sh 26 KB

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