docker-entrypoint.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/sh
  2. help() {
  3. printf "Command line:\n\n"
  4. printf " -h Display this help\n"
  5. printf " -d Dry run to update the configuration files.\n"
  6. printf " -f Always update on the configuration files (existing files are renamed with the .old suffix)\n"
  7. printf " Without this option, the new configuration files are copied with the .new suffix\n"
  8. printf "\nEnvironment variables:\n\n"
  9. printf " INSTANCE_NAME settings.yml : general.instance_name\n"
  10. printf " AUTOCOMPLETE settings.yml : search.autocomplete\n"
  11. printf " BASE_URL settings.yml : server.base_url\n"
  12. printf " MORTY_URL settings.yml : result_proxy.url\n"
  13. printf " MORTY_KEY settings.yml : result_proxy.key\n"
  14. printf " BIND_ADDRESS uwsgi bind to the specified TCP socket using HTTP protocol. Default value: \"${DEFAULT_BIND_ADDRESS}\"\n"
  15. printf "\nVolume:\n\n"
  16. printf " /etc/searx the docker entry point copies settings.yml and uwsgi.ini in this directory (see the -f command line option)\n"
  17. echo
  18. exit 0
  19. }
  20. export DEFAULT_BIND_ADDRESS="0.0.0.0:8080"
  21. if [ -z "${BIND_ADDRESS}" ]; then
  22. export BIND_ADDRESS="${DEFAULT_BIND_ADDRESS}"
  23. fi
  24. # Parse special command line
  25. # see docs/admin/installation-docker.rst
  26. # display the help message without the version
  27. if [ "$1" = "help" ]; then
  28. help
  29. fi
  30. # Version
  31. export SEARX_VERSION=$(su searx -c 'python3 -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)"' 2>/dev/null)
  32. printf 'searx version %s\n\n' "${SEARX_VERSION}"
  33. # Parse command line
  34. FORCE_CONF_UPDATE=0
  35. DRY_RUN=0
  36. while getopts "fdh" option
  37. do
  38. case $option in
  39. f)
  40. FORCE_CONF_UPDATE=1
  41. ;;
  42. d)
  43. DRY_RUN=1
  44. ;;
  45. h)
  46. help
  47. esac
  48. done
  49. # helpers to update the configuration files
  50. patch_uwsgi_settings() {
  51. CONF="$1"
  52. # Nothing
  53. }
  54. patch_searx_settings() {
  55. CONF="$1"
  56. # Make sure that there is trailing slash at the end of BASE_URL
  57. # see https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
  58. export BASE_URL="${BASE_URL%/}/"
  59. # update settings.yml
  60. sed -i -e "s|base_url : False|base_url : ${BASE_URL}|g" \
  61. -e "s/instance_name : \"searx\"/instance_name : \"${INSTANCE_NAME}\"/g" \
  62. -e "s/autocomplete : \"\"/autocomplete : \"${AUTOCOMPLETE}\"/g" \
  63. -e "s/ultrasecretkey/$(openssl rand -hex 32)/g" \
  64. "${CONF}"
  65. # Morty configuration
  66. if [ ! -z "${MORTY_KEY}" -a ! -z "${MORTY_URL}" ]; then
  67. sed -i -e "s/image_proxy : False/image_proxy : True/g" \
  68. "${CONF}"
  69. cat >> "${CONF}" <<-EOF
  70. # Morty configuration
  71. result_proxy:
  72. url : ${MORTY_URL}
  73. key : !!binary "${MORTY_KEY}"
  74. EOF
  75. fi
  76. }
  77. update_conf() {
  78. FORCE_CONF_UPDATE=$1
  79. CONF="$2"
  80. NEW_CONF="${2}.new"
  81. OLD_CONF="${2}.old"
  82. REF_CONF="$3"
  83. PATCH_REF_CONF="$4"
  84. if [ -f "${CONF}" ]; then
  85. if [ "${REF_CONF}" -nt "${CONF}" ]; then
  86. # There is a new version
  87. if [ $FORCE_CONF_UPDATE -ne 0 ]; then
  88. # Replace the current configuration
  89. printf '⚠️ Automatically update %s to the new version\n' "${CONF}"
  90. if [ ! -f "${OLD_CONF}" ]; then
  91. printf 'The previous configuration is saved to %s\n' "${OLD_CONF}"
  92. mv "${CONF}" "${OLD_CONF}"
  93. fi
  94. cp "${REF_CONF}" "${CONF}"
  95. $PATCH_REF_CONF "${CONF}"
  96. else
  97. # Keep the current configuration
  98. printf '⚠️ Check new version %s to make sure searx is working properly\n' "${NEW_CONF}"
  99. cp "${REF_CONF}" "${NEW_CONF}"
  100. $PATCH_REF_CONF "${NEW_CONF}"
  101. fi
  102. else
  103. printf 'Use existing %s\n' "${CONF}"
  104. fi
  105. else
  106. printf 'Create %s\n' "${CONF}"
  107. cp "${REF_CONF}" "${CONF}"
  108. $PATCH_REF_CONF "${CONF}"
  109. fi
  110. }
  111. # make sure there are uwsgi settings
  112. update_conf ${FORCE_CONF_UPDATE} "${UWSGI_SETTINGS_PATH}" "/usr/local/searx/dockerfiles/uwsgi.ini" "patch_uwsgi_settings"
  113. # make sure there are searx settings
  114. update_conf "${FORCE_CONF_UPDATE}" "${SEARX_SETTINGS_PATH}" "/usr/local/searx/searx/settings.yml" "patch_searx_settings"
  115. # dry run (to update configuration files, then inspect them)
  116. if [ $DRY_RUN -eq 1 ]; then
  117. printf 'Dry run\n'
  118. exit
  119. fi
  120. #
  121. touch /var/run/uwsgi-logrotate
  122. chown -R searx:searx /var/log/uwsgi /var/run/uwsgi-logrotate
  123. unset MORTY_KEY
  124. # Start uwsgi
  125. printf 'Listen on %s\n' "${BIND_ADDRESS}"
  126. exec su-exec searx:searx uwsgi --master --http-socket "${BIND_ADDRESS}" "${UWSGI_SETTINGS_PATH}"