docker-entrypoint.sh 5.1 KB

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