docker-entrypoint.sh 5.3 KB

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