docker-entrypoint.sh 4.2 KB

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