searx.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh -*-
  3. # shellcheck disable=SC2119
  4. # shellcheck source=utils/lib.sh
  5. source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
  6. # ----------------------------------------------------------------------------
  7. # config
  8. # ----------------------------------------------------------------------------
  9. SERVICE_NAME="searx"
  10. SERVICE_USER="${SERVICE_NAME}"
  11. # shellcheck disable=SC2034
  12. SERVICE_GROUP="${SERVICE_USER}"
  13. SERVICE_HOME="/home/${SERVICE_USER}"
  14. SEARX_GIT_URL="https://github.com/asciimoo/searx.git"
  15. SEARX_GIT_BRANCH="origin/master"
  16. # FIXME: Arch Linux & RHEL should be added
  17. SEARX_APT_PACKAGES="\
  18. libapache2-mod-uwsgi uwsgi uwsgi-plugin-python3 \
  19. git build-essential libxslt-dev python3-dev python3-babel zlib1g-dev \
  20. libffi-dev libssl-dev"
  21. SEARX_VENV="${SEARX_HOME}/searx-venv"
  22. SEARX_SRC="${SEARX_HOME}/searx-src"
  23. SEARX_SETTINGS="${SEARX_SRC}/searx/settings.yml"
  24. SEARX_INSTANCE_NAME="${SEARX_INSTANCE_NAME:-searx@$(uname -n)}"
  25. SEARX_UWSGI_APP="${uWSGI_SETUP}/apps-available/searx.ini"
  26. # shellcheck disable=SC2034
  27. CONFIG_FILES=(
  28. "${SEARX_UWSGI_APP}"
  29. )
  30. # shellcheck disable=SC2034
  31. CONFIG_BACKUP_ENCRYPTED=(
  32. "${SEARX_SETTINGS}"
  33. )
  34. # ----------------------------------------------------------------------------
  35. usage(){
  36. # ----------------------------------------------------------------------------
  37. # shellcheck disable=SC1117
  38. cat <<EOF
  39. usage:
  40. $(basename "$0") shell
  41. $(basename "$0") install [all|user]
  42. $(basename "$0") update [searx]
  43. $(basename "$0") remove [all]
  44. $(basename "$0") activate [service]
  45. $(basename "$0") deactivate [service]
  46. $(basename "$0") show [service]
  47. shell
  48. start interactive shell from user ${SERVICE_USER}
  49. install / remove all
  50. complete setup of searx service
  51. update searx
  52. Update searx installation of user ${SERVICE_USER}
  53. activate
  54. activate and start service daemon (systemd unit)
  55. deactivate service
  56. stop and deactivate service daemon (systemd unit)
  57. install user
  58. add service user '$SERVICE_USER' at $SERVICE_HOME
  59. show service
  60. show service status and log
  61. EOF
  62. [ ! -z ${1+x} ] && echo -e "$1"
  63. }
  64. main(){
  65. rst_title "$SERVICE_NAME" part
  66. local _usage="ERROR: unknown or missing $1 command $2"
  67. case $1 in
  68. --source-only) ;;
  69. -h|--help) usage; exit 0;;
  70. shell)
  71. sudo_or_exit
  72. interactive_shell
  73. ;;
  74. show)
  75. case $2 in
  76. service)
  77. sudo_or_exit
  78. show_service
  79. ;;
  80. *) usage "$_usage"; exit 42;;
  81. esac ;;
  82. install)
  83. sudo_or_exit
  84. case $2 in
  85. all) install_all ;;
  86. user) assert_user ;;
  87. *) usage "$_usage"; exit 42;;
  88. esac ;;
  89. update)
  90. sudo_or_exit
  91. case $2 in
  92. searx) update_searx;;
  93. *) usage "$_usage"; exit 42;;
  94. esac ;;
  95. remove)
  96. sudo_or_exit
  97. case $2 in
  98. all) remove_all;;
  99. user) remove_user ;;
  100. *) usage "$_usage"; exit 42;;
  101. esac ;;
  102. activate)
  103. sudo_or_exit
  104. case $2 in
  105. service) activate_service ;;
  106. *) usage "$_usage"; exit 42;;
  107. esac ;;
  108. deactivate)
  109. sudo_or_exit
  110. case $2 in
  111. service) deactivate_service ;;
  112. *) usage "$_usage"; exit 42;;
  113. esac ;;
  114. *) usage "ERROR: unknown or missing command $1"; exit 42;;
  115. esac
  116. }
  117. _service_prefix=" |$SERVICE_USER| "
  118. install_all() {
  119. rst_title "Install $SERVICE_NAME (service)"
  120. pkg_install "$SEARX_APT_PACKAGES"
  121. wait_key
  122. assert_user
  123. wait_key
  124. clone_searx
  125. wait_key
  126. create_venv
  127. wait_key
  128. configure_searx
  129. wait_key
  130. test_local_searx
  131. wait_key
  132. install_searx_uwsgi
  133. wait_key
  134. # ToDo ...
  135. # install_apache_site
  136. # test_public_searx
  137. # info_msg "searX --> https://${SEARX_APACHE_DOMAIN}${SEARX_APACHE_URL}"
  138. }
  139. update_searx() {
  140. rst_title "Update searx instance"
  141. echo
  142. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  143. cd ${SEARX_SRC}
  144. cp -f ${SEARX_SETTINGS} ${SEARX_SETTINGS}.backup
  145. git stash push -m "BACKUP -- 'update server' at ($(date))"
  146. git checkout -b "$(basename "$SEARX_GIT_BRANCH")" --track "$SEARX_GIT_BRANCH"
  147. git pull "$SEARX_GIT_BRANCH"
  148. ${SEARX_SRC}/manage.sh update_packages
  149. EOF
  150. configure_searx
  151. rst_title "${SEARX_SETTINGS}" section
  152. rstBlock 'Diff between new setting file (<) and backup (>):'
  153. echo
  154. diff "$SEARX_SETTINGS}" "${SEARX_SETTINGS}.backup"
  155. local action
  156. choose_one action "What should happen to the settings file? " \
  157. "keep new configuration" \
  158. "revert to the old configuration (backup file)" \
  159. "start interactiv shell"
  160. case $action in
  161. "keep new configuration")
  162. info_msg "continue using new settings file"
  163. ;;
  164. "revert to the old configuration (backup file)")
  165. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  166. cp -f ${SEARX_SETTINGS}.backup ${SEARX_SETTINGS}
  167. EOF
  168. ;;
  169. "start interactiv shell")
  170. interactive_shell
  171. ;;
  172. esac
  173. chown "${SERVICE_USER}:${SERVICE_USER}" "${SEARX_SETTINGS}"
  174. # shellcheck disable=SC2016
  175. rst_para 'Diff between local modified settings (<) and $SEARX_GIT_BRANCH branch (>):'
  176. echo
  177. git_diff
  178. wait_key
  179. uWSGI_restart
  180. }
  181. remove_all() {
  182. rst_title "De-Install $SERVICE_NAME (service)"
  183. remove_service
  184. wait_key
  185. remove_user
  186. }
  187. assert_user() {
  188. rst_title "user $SERVICE_USER" section
  189. echo
  190. tee_stderr 1 <<EOF | bash | prefix_stdout
  191. sudo -H adduser --shell /bin/bash --system --home "$SERVICE_HOME" \
  192. --disabled-password --group --gecos 'searx' $SERVICE_USER
  193. sudo -H usermod -a -G shadow $SERVICE_USER
  194. groups $SERVICE_USER
  195. EOF
  196. #SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
  197. #export SERVICE_HOME
  198. #echo "export SERVICE_HOME=$SERVICE_HOME"
  199. }
  200. remove_user() {
  201. rst_title "Drop $SERVICE_USER HOME" section
  202. if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
  203. userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
  204. else
  205. rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
  206. fi
  207. }
  208. # shellcheck disable=SC2164
  209. clone_searx(){
  210. rst_title "Clone searx sources" section
  211. echo
  212. git_clone "$SEARX_GIT_URL" "$SEARX_SRC" \
  213. "$SEARX_GIT_BRANCH" "$SERVICE_USER"
  214. pushd "${SEARX_SRC}" > /dev/null
  215. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  216. cd "${SEARX_SRC}"
  217. git config user.email "$ADMIN_EMAIL"
  218. git config user.name "$ADMIN_NAME"
  219. git checkout "$SEARX_GIT_BRANCH"
  220. EOF
  221. popd > /dev/null
  222. }
  223. create_venv(){
  224. rst_title "Create virtualenv (python)" section
  225. rst_para "Create venv in ${SEARX_VENV} and install needed python packages."
  226. echo
  227. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  228. rm -rf "${SEARX_VENV}"
  229. python3 -m venv "${SEARX_VENV}"
  230. . ${SEARX_VENV}/bin/activate
  231. ${SEARX_SRC}/manage.sh update_packages
  232. EOF
  233. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  234. grep -qFs -- 'source ${SEARX_VENV}/bin/activate' ~/.profile \
  235. || echo 'source ${SEARX_VENV}/bin/activate' >> ~/.profile
  236. EOF
  237. }
  238. configure_searx(){
  239. rst_title "Configure searx" section
  240. rst_para "Setup searx config located at $SEARX_SETTINGS"
  241. echo
  242. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  243. cd ${SEARX_SRC}
  244. sed -i -e "s/ultrasecretkey/$(openssl rand -hex 16)/g" "$SEARX_SETTINGS"
  245. sed -i -e "s/{instance_name}/${SEARX_INSTANCE_NAME}/g" "$SEARX_SETTINGS"
  246. EOF
  247. }
  248. test_local_searx(){
  249. rstHeading "Testing searx instance localy" section
  250. echo
  251. tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
  252. cd ${SEARX_SRC}
  253. sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS"
  254. timeout 5 python3 searx/webapp.py &
  255. sleep 1
  256. curl --location --verbose --head --insecure http://127.0.0.1:8888/
  257. sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS"
  258. EOF
  259. waitKEY
  260. }
  261. install_searx_uwsgi() {
  262. rst_title "Install searx's uWSGI app (searx.ini)" section
  263. echo
  264. uWSGI_install_app "$SEARX_UWSGI_APP"
  265. }
  266. remove_searx_uwsgi() {
  267. rst_title "Remove searx's uWSGI app (searx.ini)" section
  268. echo
  269. uWSGI_remove_app "$SEARX_UWSGI_APP"
  270. }
  271. activate_service () {
  272. rst_title "Activate $SERVICE_NAME (service)" section
  273. uWSGI_enable_app "$SEARX_UWSGI_APP"
  274. }
  275. deactivate_service () {
  276. rst_title "De-Activate $SERVICE_NAME (service)" section
  277. uWSGI_disable_app "$SEARX_UWSGI_APP"
  278. }
  279. interactive_shell(){
  280. echo "// exit with CTRL-D"
  281. sudo -H -u "${SERVICE_USER}" -i
  282. }
  283. git_diff(){
  284. sudo -H -u "${SERVICE_USER}" -i <<EOF
  285. cd ${SEARX_REPO_FOLDER}
  286. git --no-pager diff
  287. EOF
  288. }
  289. show_service () {
  290. rst_title "service status & log"
  291. echo
  292. systemctl status uwsgi.service
  293. echo
  294. read -r -s -n1 -t 5 -p "// use CTRL-C to stop monitoring the log"
  295. echo
  296. while true; do
  297. trap break 2
  298. journalctl -f -u uwsgi.service
  299. done
  300. return 0
  301. }
  302. # ----------------------------------------------------------------------------
  303. main "$@"
  304. # ----------------------------------------------------------------------------