docker-entrypoint.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 " INSTANCE_NAME settings.yml : general.instance_name\n"
  29. printf " AUTOCOMPLETE settings.yml : search.autocomplete\n"
  30. printf " BASE_URL settings.yml : server.base_url\n"
  31. printf " MORTY_URL settings.yml : result_proxy.url\n"
  32. printf " MORTY_KEY settings.yml : result_proxy.key\n"
  33. printf " BIND_ADDRESS where uwsgi will accept HTTP request (format : host:port)\n"
  34. exit 0
  35. esac
  36. done
  37. # helpers to update the configuration files
  38. patch_uwsgi_settings() {
  39. CONF="$1"
  40. # Nothing
  41. }
  42. patch_searx_settings() {
  43. CONF="$1"
  44. # Make sure that there is trailing slash at the end of BASE_URL
  45. # see http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
  46. export BASE_URL="${BASE_URL%/}/"
  47. # update settings.yml
  48. sed -i -e "s|base_url : False|base_url : ${BASE_URL}|g" \
  49. -e "s/instance_name : \"searx\"/instance_name : \"${INSTANCE_NAME}\"/g" \
  50. -e "s/autocomplete : \"\"/autocomplete : \"${AUTOCOMPLETE}\"/g" \
  51. -e "s/ultrasecretkey/$(openssl rand -hex 32)/g" \
  52. "${CONF}"
  53. # Morty configuration
  54. if [ ! -z "${MORTY_KEY}" -a ! -z "${MORTY_URL}" ]; then
  55. sed -i -e "s/image_proxy : False/image_proxy : True/g" \
  56. "${CONF}"
  57. cat >> "${CONF}" <<-EOF
  58. # Morty configuration
  59. result_proxy:
  60. url : ${MORTY_URL}
  61. key : !!binary "${MORTY_KEY}"
  62. EOF
  63. fi
  64. }
  65. update_conf() {
  66. FORCE_CONF_UPDATE=$1
  67. CONF="$2"
  68. NEW_CONF="${2}.new"
  69. OLD_CONF="${2}.old"
  70. REF_CONF="$3"
  71. PATCH_REF_CONF="$4"
  72. if [ -f "${CONF}" ]; then
  73. if [ "${REF_CONF}" -nt "${CONF}" ]; then
  74. # There is a new version
  75. if [ $FORCE_CONF_UPDATE -ne 0 ]; then
  76. # Replace the current configuration
  77. printf '⚠️ Automaticaly update %s to the new version\n' "${CONF}"
  78. if [ ! -f "${OLD_CONF}" ]; then
  79. printf 'The previous configuration is saved to %s\n' "${OLD_CONF}"
  80. mv "${CONF}" "${OLD_CONF}"
  81. fi
  82. cp "${REF_CONF}" "${CONF}"
  83. $PATCH_REF_CONF "${CONF}"
  84. else
  85. # Keep the current configuration
  86. printf '⚠️ Check new version %s to make sure searx is working properly\n' "${NEW_CONF}"
  87. cp "${REF_CONF}" "${NEW_CONF}"
  88. $PATCH_REF_CONF "${NEW_CONF}"
  89. fi
  90. else
  91. printf 'Use existing %s\n' "${CONF}"
  92. fi
  93. else
  94. printf 'Create %s\n' "${CONF}"
  95. cp "${REF_CONF}" "${CONF}"
  96. $PATCH_REF_CONF "${CONF}"
  97. fi
  98. }
  99. # make sure there are uwsgi settings
  100. update_conf ${FORCE_CONF_UPDATE} "${UWSGI_SETTINGS_PATH}" "/usr/local/searx/dockerfiles/uwsgi.ini" "patch_uwsgi_settings"
  101. # make sure there are searx settings
  102. update_conf "${FORCE_CONF_UPDATE}" "${SEARX_SETTINGS_PATH}" "/usr/local/searx/searx/settings.yml" "patch_searx_settings"
  103. # dry run (to update configuration files, then inspect them)
  104. if [ $DRY_RUN -eq 1 ]; then
  105. printf 'Dry run\n'
  106. exit
  107. fi
  108. #
  109. touch /var/run/uwsgi-logrotate
  110. chown -R searx:searx /var/log/uwsgi /var/run/uwsgi-logrotate
  111. unset MORTY_KEY
  112. # Start uwsgi
  113. printf 'Listen on %s\n' "${BIND_ADDRESS}"
  114. exec su-exec searx:searx uwsgi --master --http-socket "${BIND_ADDRESS}" "${UWSGI_SETTINGS_PATH}"