docker-entrypoint.sh 4.3 KB

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