docker-entrypoint.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/sh
  2. help() {
  3. cat <<EOF
  4. Command line:
  5. -h Display this help
  6. -d Dry run to update the configuration files.
  7. -f Always update on the configuration files (existing files are renamed with
  8. the .old suffix). Without this option, the new configuration files are
  9. copied with the .new suffix
  10. Environment variables:
  11. INSTANCE_NAME settings.yml : general.instance_name
  12. AUTOCOMPLETE settings.yml : search.autocomplete
  13. BASE_URL settings.yml : server.base_url
  14. MORTY_URL settings.yml : result_proxy.url
  15. MORTY_KEY settings.yml : result_proxy.key
  16. BIND_ADDRESS uwsgi bind to the specified TCP socket using HTTP protocol.
  17. Default value: ${DEFAULT_BIND_ADDRESS}
  18. Volume:
  19. /etc/searxng the docker entry point copies settings.yml and uwsgi.ini in
  20. this directory (see the -f command line option)"
  21. EOF
  22. }
  23. export DEFAULT_BIND_ADDRESS="0.0.0.0:8080"
  24. export BIND_ADDRESS="${BIND_ADDRESS:-${DEFAULT_BIND_ADDRESS}}"
  25. # Parse command line
  26. FORCE_CONF_UPDATE=0
  27. DRY_RUN=0
  28. while getopts "fdh" option
  29. do
  30. case $option in
  31. f) FORCE_CONF_UPDATE=1 ;;
  32. d) DRY_RUN=1 ;;
  33. h)
  34. help
  35. exit 0
  36. ;;
  37. *)
  38. echo "unknow option ${option}"
  39. exit 42
  40. ;;
  41. esac
  42. done
  43. get_searxng_version(){
  44. su searxng -c \
  45. 'python3 -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)"' \
  46. 2>/dev/null
  47. }
  48. SEARXNG_VERSION="$(get_searxng_version)"
  49. export SEARXNG_VERSION
  50. echo "SearXNG version ${SEARXNG_VERSION}"
  51. # helpers to update the configuration files
  52. patch_uwsgi_settings() {
  53. CONF="$1"
  54. # update uwsg.ini
  55. sed -i \
  56. -e "s|workers = .*|workers = ${UWSGI_WORKERS:-%k}|g" \
  57. -e "s|threads = .*|threads = ${UWSGI_THREADS:-4}|g" \
  58. "${CONF}"
  59. }
  60. patch_searxng_settings() {
  61. CONF="$1"
  62. # Make sure that there is trailing slash at the end of BASE_URL
  63. # see https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
  64. export BASE_URL="${BASE_URL%/}/"
  65. # update settings.yml
  66. sed -i \
  67. -e "s|base_url: false|base_url: ${BASE_URL}|g" \
  68. -e "s/instance_name: \"SearXNG\"/instance_name: \"${INSTANCE_NAME}\"/g" \
  69. -e "s/autocomplete: \"\"/autocomplete: \"${AUTOCOMPLETE}\"/g" \
  70. -e "s/ultrasecretkey/$(openssl rand -hex 32)/g" \
  71. "${CONF}"
  72. # Morty configuration
  73. if [ -n "${MORTY_KEY}" ] && [ -n "${MORTY_URL}" ]; then
  74. sed -i -e "s/image_proxy: false/image_proxy: true/g" \
  75. "${CONF}"
  76. cat >> "${CONF}" <<-EOF
  77. # Morty configuration
  78. result_proxy:
  79. url: ${MORTY_URL}
  80. key: !!binary "${MORTY_KEY}"
  81. EOF
  82. fi
  83. }
  84. update_conf() {
  85. FORCE_CONF_UPDATE=$1
  86. CONF="$2"
  87. NEW_CONF="${2}.new"
  88. OLD_CONF="${2}.old"
  89. REF_CONF="$3"
  90. PATCH_REF_CONF="$4"
  91. if [ -f "${CONF}" ]; then
  92. if [ "${REF_CONF}" -nt "${CONF}" ]; then
  93. # There is a new version
  94. if [ "$FORCE_CONF_UPDATE" -ne 0 ]; then
  95. # Replace the current configuration
  96. printf '⚠️ Automatically update %s to the new version\n' "${CONF}"
  97. if [ ! -f "${OLD_CONF}" ]; then
  98. printf 'The previous configuration is saved to %s\n' "${OLD_CONF}"
  99. mv "${CONF}" "${OLD_CONF}"
  100. fi
  101. cp "${REF_CONF}" "${CONF}"
  102. $PATCH_REF_CONF "${CONF}"
  103. else
  104. # Keep the current configuration
  105. printf '⚠️ Check new version %s to make sure SearXNG is working properly\n' "${NEW_CONF}"
  106. cp "${REF_CONF}" "${NEW_CONF}"
  107. $PATCH_REF_CONF "${NEW_CONF}"
  108. fi
  109. else
  110. printf 'Use existing %s\n' "${CONF}"
  111. fi
  112. else
  113. printf 'Create %s\n' "${CONF}"
  114. cp "${REF_CONF}" "${CONF}"
  115. $PATCH_REF_CONF "${CONF}"
  116. fi
  117. }
  118. # searx compatibility: copy /etc/searx/* to /etc/searxng/*
  119. SEARX_CONF=0
  120. if [ -f "/etc/searx/settings.yml" ]; then
  121. if [ ! -f "${SEARXNG_SETTINGS_PATH}" ]; then
  122. printf '⚠️ /etc/searx/settings.yml is copied to /etc/searxng\n'
  123. cp "/etc/searx/settings.yml" "${SEARXNG_SETTINGS_PATH}"
  124. fi
  125. SEARX_CONF=1
  126. fi
  127. if [ -f "/etc/searx/uwsgi.ini" ]; then
  128. printf '⚠️ /etc/searx/uwsgi.ini is ignored. Use the volume /etc/searxng\n'
  129. SEARX_CONF=1
  130. fi
  131. if [ "$SEARX_CONF" -eq "1" ]; then
  132. printf '⚠️ The deprecated volume /etc/searx is mounted. Please update your configuration to use /etc/searxng ⚠️\n'
  133. cat << EOF > /etc/searx/deprecated_volume_read_me.txt
  134. This Docker image uses the volume /etc/searxng
  135. Update your configuration:
  136. * remove uwsgi.ini (or very carefully update your existing uwsgi.ini using https://github.com/searxng/searxng/blob/master/dockerfiles/uwsgi.ini )
  137. * mount /etc/searxng instead of /etc/searx
  138. EOF
  139. fi
  140. # end of searx compatibility
  141. # make sure there are uwsgi settings
  142. update_conf "${FORCE_CONF_UPDATE}" "${UWSGI_SETTINGS_PATH}" "/usr/local/searxng/dockerfiles/uwsgi.ini" "patch_uwsgi_settings"
  143. # make sure there are searxng settings
  144. update_conf "${FORCE_CONF_UPDATE}" "${SEARXNG_SETTINGS_PATH}" "/usr/local/searxng/searx/settings.yml" "patch_searxng_settings"
  145. # dry run (to update configuration files, then inspect them)
  146. if [ $DRY_RUN -eq 1 ]; then
  147. printf 'Dry run\n'
  148. exit
  149. fi
  150. unset MORTY_KEY
  151. # Start uwsgi
  152. printf 'Listen on %s\n' "${BIND_ADDRESS}"
  153. exec uwsgi --master --uid searxng --gid searxng --http-socket "${BIND_ADDRESS}" "${UWSGI_SETTINGS_PATH}"