docker-entrypoint.sh 4.4 KB

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