docker-entrypoint.sh 3.6 KB

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