searx.sh 28 KB

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