lib_install.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if [ ! -r "${SEARX_SRC}" ]; then
  47. build_msg INSTANCE "not yet cloned: ${SEARX_SRC}"
  48. orig_source_dot_config
  49. return 0
  50. fi
  51. build_msg INSTANCE "using instance at: ${SEARX_SRC}"
  52. # set and log DOT_CONFIG
  53. if [ -r "${SEARX_SRC}/.config.sh" ]; then
  54. build_msg INSTANCE "switching to ${SEARX_SRC}/.config.sh"
  55. DOT_CONFIG="${SEARX_SRC}/.config.sh"
  56. else
  57. build_msg INSTANCE "using local config: ${DOT_CONFIG}"
  58. fi
  59. init_SEARX_SRC_INIT_FILES
  60. fi
  61. }
  62. init_SEARX_SRC_INIT_FILES(){
  63. # init environment SEARX_SRC_INIT_FILES
  64. # Monitor modified files in the working-tree from the local repository, only
  65. # if the local file differs to the corresponding file in the instance. Most
  66. # often theses are files like:
  67. #
  68. # - .config.sh
  69. # - searx/settings.yml
  70. # - utils/brand.env
  71. # - ...
  72. # keep list empty if there is no installation
  73. SEARX_SRC_INIT_FILES=()
  74. if [ ! -r "$SEARX_SRC" ]; then
  75. return 0
  76. fi
  77. local fname
  78. local msg=""
  79. # Monitor local modified files from the repository, only if the local file
  80. # differs to the corresponding file in the instance
  81. while IFS= read -r fname; do
  82. if [ -z "$fname" ]; then
  83. continue
  84. fi
  85. if [ -r "${SEARX_SRC}/${fname}" ]; then
  86. # diff "${REPO_ROOT}/${fname}" "${SEARX_SRC}/${fname}"
  87. if ! cmp --silent "${REPO_ROOT}/${fname}" "${SEARX_SRC}/${fname}"; then
  88. SEARX_SRC_INIT_FILES+=("${fname}")
  89. build_msg INSTANCE "local clone (workingtree), modified file: ./$fname"
  90. msg="to update use: sudo -H ./utils/searx.sh install init-src"
  91. fi
  92. fi
  93. done <<< "$(git diff --name-only)"
  94. [ -n "$msg" ] && build_msg INSTANCE "$msg"
  95. }
  96. install_log_searx_instance() {
  97. echo -e "---- SearXNG instance setup ${_BBlue}(status: $(install_searx_get_state))${_creset}"
  98. echo -e " SEARX_SETTINGS_PATH : ${_BBlue}${SEARX_SETTINGS_PATH}${_creset}"
  99. echo -e " SEARX_SRC : ${_BBlue}${SEARX_SRC:-none}${_creset}"
  100. echo -e " SEARX_URL : ${_BBlue}${SEARX_URL:-none}${_creset}"
  101. if in_container; then
  102. # searx is listening on 127.0.0.1 and not available from outside container
  103. # in containers the service is listening on 0.0.0.0 (see lxc-searx.env)
  104. echo -e "---- container setup"
  105. echo -e " ${_BBlack}HINT:${_creset} searx only listen on loopback device" \
  106. "${_BBlack}inside${_creset} the container."
  107. for ip in $(global_IPs) ; do
  108. if [[ $ip =~ .*:.* ]]; then
  109. echo " container (IPv6): [${ip#*|}]"
  110. else
  111. # IPv4:
  112. echo " container (IPv4): ${ip#*|}"
  113. fi
  114. done
  115. fi
  116. }
  117. install_searx_get_state(){
  118. # usage: install_searx_get_state
  119. #
  120. # Prompts a string indicating the status of the installation procedure
  121. #
  122. # missing-searx-clone:
  123. # There is no clone at ${SEARX_SRC}
  124. # missing-searx-pyenv:
  125. # There is no pyenv in ${SEARX_PYENV}
  126. # installer-modified:
  127. # There are files modified locally in the installer (clone),
  128. # see ${SEARX_SRC_INIT_FILES} description.
  129. # python-installed:
  130. # Scripts can be executed in instance's environment
  131. # - user: ${SERVICE_USER}
  132. # - pyenv: ${SEARX_PYENV}
  133. if ! [ -r "${SEARX_SRC}" ]; then
  134. echo "missing-searx-clone"
  135. return
  136. fi
  137. if ! [ -f "${SEARX_PYENV}/bin/activate" ]; then
  138. echo "missing-searx-pyenv"
  139. return
  140. fi
  141. if ! [ -r "${SEARX_SETTINGS_PATH}" ]; then
  142. echo "missing-settings"
  143. return
  144. fi
  145. if ! [ ${#SEARX_SRC_INIT_FILES[*]} -eq 0 ]; then
  146. echo "installer-modified"
  147. return
  148. fi
  149. echo "python-installed"
  150. }
  151. # Initialization of the installation procedure
  152. # --------------------------------------------
  153. # shellcheck source=utils/brand.env
  154. source "${REPO_ROOT}/utils/brand.env"
  155. source_dot_config
  156. # shellcheck source=utils/lxc-searx.env
  157. source "${REPO_ROOT}/utils/lxc-searx.env"
  158. in_container && lxc_set_suite_env