docker-entrypoint.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Volume:
  15. /etc/searxng the docker entry point copies settings.yml and uwsgi.ini in
  16. this directory (see the -f command line option)"
  17. EOF
  18. }
  19. # Parse command line
  20. FORCE_CONF_UPDATE=0
  21. DRY_RUN=0
  22. while getopts "fdh" option
  23. do
  24. case $option in
  25. f) FORCE_CONF_UPDATE=1 ;;
  26. d) DRY_RUN=1 ;;
  27. h)
  28. help
  29. exit 0
  30. ;;
  31. *)
  32. echo "unknow option ${option}"
  33. exit 42
  34. ;;
  35. esac
  36. done
  37. echo "SearXNG version $SEARXNG_VERSION"
  38. # helpers to update the configuration files
  39. patch_uwsgi_settings() {
  40. CONF="$1"
  41. # update uwsg.ini
  42. sed -i \
  43. -e "s|workers = .*|workers = ${UWSGI_WORKERS:-%k}|g" \
  44. -e "s|threads = .*|threads = ${UWSGI_THREADS:-4}|g" \
  45. "${CONF}"
  46. }
  47. patch_searxng_settings() {
  48. CONF="$1"
  49. # Make sure that there is trailing slash at the end of BASE_URL
  50. # see https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
  51. export BASE_URL="${BASE_URL%/}/"
  52. # update settings.yml
  53. sed -i \
  54. -e "s|base_url: false|base_url: ${BASE_URL}|g" \
  55. -e "s/instance_name: \"SearXNG\"/instance_name: \"${INSTANCE_NAME}\"/g" \
  56. -e "s/autocomplete: \"\"/autocomplete: \"${AUTOCOMPLETE}\"/g" \
  57. -e "s/ultrasecretkey/$(head -c 24 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')/g" \
  58. "${CONF}"
  59. }
  60. update_conf() {
  61. FORCE_CONF_UPDATE=$1
  62. CONF="$2"
  63. NEW_CONF="${2}.new"
  64. OLD_CONF="${2}.old"
  65. REF_CONF="$3"
  66. PATCH_REF_CONF="$4"
  67. if [ -f "${CONF}" ]; then
  68. if [ "${REF_CONF}" -nt "${CONF}" ]; then
  69. # There is a new version
  70. if [ "$FORCE_CONF_UPDATE" -ne 0 ]; then
  71. # Replace the current configuration
  72. printf '⚠️ Automatically update %s to the new version\n' "${CONF}"
  73. if [ ! -f "${OLD_CONF}" ]; then
  74. printf 'The previous configuration is saved to %s\n' "${OLD_CONF}"
  75. mv "${CONF}" "${OLD_CONF}"
  76. fi
  77. cp "${REF_CONF}" "${CONF}"
  78. $PATCH_REF_CONF "${CONF}"
  79. else
  80. # Keep the current configuration
  81. printf '⚠️ Check new version %s to make sure SearXNG is working properly\n' "${NEW_CONF}"
  82. cp "${REF_CONF}" "${NEW_CONF}"
  83. $PATCH_REF_CONF "${NEW_CONF}"
  84. fi
  85. else
  86. printf 'Use existing %s\n' "${CONF}"
  87. fi
  88. else
  89. printf 'Create %s\n' "${CONF}"
  90. cp "${REF_CONF}" "${CONF}"
  91. $PATCH_REF_CONF "${CONF}"
  92. fi
  93. }
  94. # make sure there are uwsgi settings
  95. update_conf "${FORCE_CONF_UPDATE}" "${UWSGI_SETTINGS_PATH}" "/usr/local/searxng/container/uwsgi.ini" "patch_uwsgi_settings"
  96. # make sure there are searxng settings
  97. update_conf "${FORCE_CONF_UPDATE}" "${SEARXNG_SETTINGS_PATH}" "/usr/local/searxng/searx/settings.yml" "patch_searxng_settings"
  98. # dry run (to update configuration files, then inspect them)
  99. if [ $DRY_RUN -eq 1 ]; then
  100. printf 'Dry run\n'
  101. exit
  102. fi
  103. printf 'Listen on %s\n' "${BIND_ADDRESS}"
  104. # Start uwsgi
  105. # TODO: "--http-socket" will be removed in the future (see uwsgi.ini.new config file): https://github.com/searxng/searxng/pull/4578
  106. exec /usr/local/searxng/venv/bin/uwsgi --http-socket "${BIND_ADDRESS}" "${UWSGI_SETTINGS_PATH}"