lib_install.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 SEARX_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 '${SEARX_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 $SEARX_SRC. The file names
  31. # are relative to the $REPO_ROOT. Set by function init_SEARX_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 SEARX_SRC).
  42. # - initialize SEARX_SRC_INIT_FILES
  43. if [ -z "$eval_SEARX_SRC" ]; then
  44. export eval_SEARX_SRC='true'
  45. SEARX_SRC=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARX_SRC)
  46. SEARX_PYENV=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARX_PYENV)
  47. SEARXNG_SETTINGS_PATH=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARXNG_SETTINGS_PATH)
  48. if [ ! -r "${SEARX_SRC}" ]; then
  49. info_msg "not yet cloned: ${SEARX_SRC}"
  50. orig_source_dot_config
  51. return 0
  52. fi
  53. info_msg "using instance at: ${SEARX_SRC}"
  54. # set and log DOT_CONFIG
  55. if [ -r "${SEARX_SRC}/.config.sh" ]; then
  56. info_msg "switching to ${SEARX_SRC}/.config.sh"
  57. DOT_CONFIG="${SEARX_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 "$SEARX_SRC" ]; then
  77. return 0
  78. fi
  79. local fname
  80. local msg=""
  81. # Monitor local modified files from the repository, only if the local file
  82. # differs to the corresponding file in the instance
  83. while IFS= read -r fname; do
  84. if [ -z "$fname" ]; then
  85. continue
  86. fi
  87. if [ -r "${SEARX_SRC}/${fname}" ]; then
  88. # diff "${REPO_ROOT}/${fname}" "${SEARX_SRC}/${fname}"
  89. if ! cmp --silent "${REPO_ROOT}/${fname}" "${SEARX_SRC}/${fname}"; then
  90. SEARX_SRC_INIT_FILES+=("${fname}")
  91. info_msg "local clone (workingtree), modified file: ./$fname"
  92. msg="to update use: sudo -H ./utils/searx.sh install init-src"
  93. fi
  94. fi
  95. done <<< "$(git diff --name-only)"
  96. [ -n "$msg" ] && info_msg "$msg"
  97. }
  98. install_log_searx_instance() {
  99. echo -e "---- SearXNG instance setup ${_BBlue}(status: $(install_searx_get_state))${_creset}"
  100. echo -e " SEARXNG_SETTINGS_PATH : ${_BBlue}${SEARXNG_SETTINGS_PATH}${_creset}"
  101. echo -e " SEARX_PYENV : ${_BBlue}${SEARX_PYENV}${_creset}"
  102. echo -e " SEARX_SRC : ${_BBlue}${SEARX_SRC:-none}${_creset}"
  103. echo -e " SEARXNG_URL : ${_BBlue}${SEARXNG_URL:-none}${_creset}"
  104. if in_container; then
  105. # searx is listening on 127.0.0.1 and not available from outside container
  106. # in containers the service is listening on 0.0.0.0 (see lxc-searx.env)
  107. echo -e "---- container setup"
  108. echo -e " ${_BBlack}HINT:${_creset} searx only listen on loopback device" \
  109. "${_BBlack}inside${_creset} the container."
  110. for ip in $(global_IPs) ; do
  111. if [[ $ip =~ .*:.* ]]; then
  112. echo " container (IPv6): [${ip#*|}]"
  113. else
  114. # IPv4:
  115. echo " container (IPv4): ${ip#*|}"
  116. fi
  117. done
  118. fi
  119. }
  120. install_searx_get_state(){
  121. # usage: install_searx_get_state
  122. #
  123. # Prompts a string indicating the status of the installation procedure
  124. #
  125. # missing-searx-clone:
  126. # There is no clone at ${SEARX_SRC}
  127. # missing-searx-pyenv:
  128. # There is no pyenv in ${SEARX_PYENV}
  129. # installer-modified:
  130. # There are files modified locally in the installer (clone),
  131. # see ${SEARX_SRC_INIT_FILES} description.
  132. # python-installed:
  133. # Scripts can be executed in instance's environment
  134. # - user: ${SERVICE_USER}
  135. # - pyenv: ${SEARX_PYENV}
  136. if [ -f /etc/searx/settings.yml ]; then
  137. err_msg "settings.yml in /etc/searx/ is deprecated, move file to folder /etc/searxng/"
  138. fi
  139. if ! [ -r "${SEARX_SRC}" ]; then
  140. echo "missing-searx-clone"
  141. return
  142. fi
  143. if ! [ -f "${SEARX_PYENV}/bin/activate" ]; then
  144. echo "missing-searx-pyenv"
  145. return
  146. fi
  147. if ! [ -r "${SEARXNG_SETTINGS_PATH}" ]; then
  148. echo "missing-settings"
  149. return
  150. fi
  151. if ! [ ${#SEARX_SRC_INIT_FILES[*]} -eq 0 ]; then
  152. echo "installer-modified"
  153. return
  154. fi
  155. echo "python-installed"
  156. }
  157. # Initialization of the installation procedure
  158. # --------------------------------------------
  159. # shellcheck source=utils/brand.env
  160. source "${REPO_ROOT}/utils/brand.env"
  161. # SEARXNG_URL aka PUBLIC_URL: the public URL of the instance (e.g.
  162. # "https://example.org/searx"). The value is taken from environment $SEARXNG_URL
  163. # in ./utils/brand.env. This variable is a empty string if server.base_url in
  164. # the settings.yml is set to 'false'.
  165. SEARXNG_URL="${SEARXNG_URL:-http://$(uname -n)}"
  166. if in_container; then
  167. # hint: Linux containers do not have DNS entries, lets use IPs
  168. SEARXNG_URL="http://$(primary_ip)"
  169. fi
  170. PUBLIC_URL="${SEARXNG_URL}"
  171. source_dot_config
  172. # shellcheck source=utils/lxc-searx.env
  173. source "${REPO_ROOT}/utils/lxc-searx.env"
  174. in_container && lxc_set_suite_env