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