lib_install.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. # https://github.com/koalaman/shellcheck/issues/356#issuecomment-853515285
  4. # shellcheck source=utils/lib.sh
  5. . /dev/null
  6. # Initialize installation procedures:
  7. #
  8. # - Modified source_dot_config function that
  9. # - loads .config.sh from an existing installation (at SEARXNG_SRC).
  10. # - initialize **SEARX_SRC_INIT_FILES**
  11. # - functions like:
  12. # - install_log_searx_instance()
  13. # - install_searx_get_state()
  14. #
  15. # usage:
  16. # source lib_install.sh
  17. #
  18. # **Installation scripts**
  19. #
  20. # The utils/lib_install.sh is sourced by the installations scripts:
  21. #
  22. # - utils/searx.sh
  23. # - utils/morty.sh
  24. # - utils/filtron.sh
  25. #
  26. # If '${SEARXNG_SRC}/.config.sh' exists, the modified source_dot_config() function
  27. # loads this configuration (instead of './.config.sh').
  28. # **SEARX_SRC_INIT_FILES**
  29. #
  30. # Array of file names to sync into a installation at $SEARXNG_SRC. The file names
  31. # are relative to the $REPO_ROOT. Set by function init_SEARXNG_SRC_INIT_FILES().
  32. # Most often theses are files like:
  33. # - .config.sh
  34. # - searx/settings.yml
  35. # - utils/brand.env
  36. # - ...
  37. SEARX_SRC_INIT_FILES=()
  38. eval orig_"$(declare -f source_dot_config)"
  39. source_dot_config() {
  40. # Modified source_dot_config function that
  41. # - loads .config.sh from an existing installation (at SEARXNG_SRC).
  42. # - initialize SEARX_SRC_INIT_FILES
  43. if [ -z "$eval_SEARXNG_SRC" ]; then
  44. export eval_SEARXNG_SRC='true'
  45. SEARXNG_SRC=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARXNG_SRC)
  46. SEARXNG_PYENV=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARXNG_PYENV)
  47. SEARXNG_SETTINGS_PATH=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARXNG_SETTINGS_PATH)
  48. if [ ! -r "${SEARXNG_SRC}" ]; then
  49. info_msg "not yet cloned: ${SEARXNG_SRC}"
  50. orig_source_dot_config
  51. return 0
  52. fi
  53. info_msg "using instance at: ${SEARXNG_SRC}"
  54. # set and log DOT_CONFIG
  55. if [ -r "${SEARXNG_SRC}/.config.sh" ]; then
  56. info_msg "switching to ${SEARXNG_SRC}/.config.sh"
  57. DOT_CONFIG="${SEARXNG_SRC}/.config.sh"
  58. else
  59. info_msg "using local config: ${DOT_CONFIG}"
  60. fi
  61. init_SEARX_SRC_INIT_FILES
  62. fi
  63. }
  64. init_SEARX_SRC_INIT_FILES(){
  65. # init environment SEARX_SRC_INIT_FILES
  66. # Monitor modified files in the working-tree from the local repository, only
  67. # if the local file differs to the corresponding file in the instance. Most
  68. # often theses are files like:
  69. #
  70. # - .config.sh
  71. # - searx/settings.yml
  72. # - utils/brand.env
  73. # - ...
  74. # keep list empty if there is no installation
  75. SEARX_SRC_INIT_FILES=()
  76. if [ ! -r "$SEARXNG_SRC" ]; then
  77. return 0
  78. fi
  79. local fname
  80. local msg=""
  81. local _prefix=""
  82. if [[ -n ${SUDO_USER} ]]; then
  83. _prefix="sudo -u ${SUDO_USER}"
  84. fi
  85. # Monitor local modified files from the repository, only if the local file
  86. # differs to the corresponding file in the instance
  87. while IFS= read -r fname; do
  88. if [ -z "$fname" ]; then
  89. continue
  90. fi
  91. if [ -r "${SEARXNG_SRC}/${fname}" ]; then
  92. # diff "${REPO_ROOT}/${fname}" "${SEARXNG_SRC}/${fname}"
  93. if ! cmp --silent "${REPO_ROOT}/${fname}" "${SEARXNG_SRC}/${fname}"; then
  94. SEARX_SRC_INIT_FILES+=("${fname}")
  95. info_msg "local clone (workingtree), modified file: ./$fname"
  96. msg="to update use: sudo -H ./utils/searx.sh install init-src"
  97. fi
  98. fi
  99. done <<< "$($_prefix git diff --name-only)"
  100. [ -n "$msg" ] && info_msg "$msg"
  101. }
  102. install_log_searx_instance() {
  103. echo -e "---- SearXNG instance setup ${_BBlue}(status: $(install_searx_get_state))${_creset}"
  104. echo -e " SEARXNG_SETTINGS_PATH : ${_BBlue}${SEARXNG_SETTINGS_PATH}${_creset}"
  105. echo -e " SEARXNG_PYENV : ${_BBlue}${SEARXNG_PYENV}${_creset}"
  106. echo -e " SEARXNG_SRC : ${_BBlue}${SEARXNG_SRC:-none}${_creset}"
  107. echo -e " SEARXNG_URL : ${_BBlue}${SEARXNG_URL:-none}${_creset}"
  108. if in_container; then
  109. # SearXNG is listening on 127.0.0.1 and not available from outside container
  110. # in containers the service is listening on 0.0.0.0 (see lxc-searx.env)
  111. echo -e "---- container setup"
  112. echo -e " ${_BBlack}HINT:${_creset} SearXNG only listen on loopback device" \
  113. "${_BBlack}inside${_creset} the container."
  114. for ip in $(global_IPs) ; do
  115. if [[ $ip =~ .*:.* ]]; then
  116. echo " container (IPv6): [${ip#*|}]"
  117. else
  118. # IPv4:
  119. echo " container (IPv4): ${ip#*|}"
  120. fi
  121. done
  122. fi
  123. }
  124. install_searx_get_state(){
  125. # usage: install_searx_get_state
  126. #
  127. # Prompts a string indicating the status of the installation procedure
  128. #
  129. # missing-searx-clone:
  130. # There is no clone at ${SEARXNG_SRC}
  131. # missing-searx-pyenv:
  132. # There is no pyenv in ${SEARXNG_PYENV}
  133. # installer-modified:
  134. # There are files modified locally in the installer (clone),
  135. # see ${SEARX_SRC_INIT_FILES} description.
  136. # python-installed:
  137. # Scripts can be executed in instance's environment
  138. # - user: ${SERVICE_USER}
  139. # - pyenv: ${SEARXNG_PYENV}
  140. if [ -f /etc/searx/settings.yml ]; then
  141. err_msg "settings.yml in /etc/searx/ is deprecated, move file to folder /etc/searxng/"
  142. fi
  143. if ! [ -r "${SEARXNG_SRC}" ]; then
  144. echo "missing-searx-clone"
  145. return
  146. fi
  147. if ! [ -f "${SEARXNG_PYENV}/bin/activate" ]; then
  148. echo "missing-searx-pyenv"
  149. return
  150. fi
  151. if ! [ -r "${SEARXNG_SETTINGS_PATH}" ]; then
  152. echo "missing-settings"
  153. return
  154. fi
  155. if ! [ ${#SEARX_SRC_INIT_FILES[*]} -eq 0 ]; then
  156. echo "installer-modified"
  157. return
  158. fi
  159. echo "python-installed"
  160. }
  161. # Initialization of the installation procedure
  162. # --------------------------------------------
  163. # shellcheck source=utils/brand.env
  164. source "${REPO_ROOT}/utils/brand.env"
  165. # SEARXNG_URL aka PUBLIC_URL: the public URL of the instance (e.g.
  166. # "https://example.org/searx"). The value is taken from environment $SEARXNG_URL
  167. # in ./utils/brand.env. This variable is a empty string if server.base_url in
  168. # the settings.yml is set to 'false'.
  169. SEARXNG_URL="${SEARXNG_URL:-http://$(uname -n)}"
  170. if in_container; then
  171. # hint: Linux containers do not have DNS entries, lets use IPs
  172. SEARXNG_URL="http://$(primary_ip)"
  173. fi
  174. PUBLIC_URL="${SEARXNG_URL}"
  175. source_dot_config
  176. # shellcheck source=utils/lxc-searx.env
  177. source "${REPO_ROOT}/utils/lxc-searx.env"
  178. in_container && lxc_set_suite_env