entrypoint.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/sh
  2. # shellcheck shell=dash
  3. set -eu
  4. check_file() {
  5. local target="$1"
  6. if [ ! -f "$target" ]; then
  7. cat <<EOF
  8. !!!
  9. !!! ERROR
  10. !!! "$target" is not a valid file, exiting...
  11. !!!
  12. EOF
  13. exit 127
  14. fi
  15. }
  16. check_directory() {
  17. local target="$1"
  18. if [ ! -d "$target" ]; then
  19. cat <<EOF
  20. !!!
  21. !!! ERROR
  22. !!! "$target" is not a valid directory, exiting...
  23. !!!
  24. EOF
  25. exit 127
  26. fi
  27. }
  28. setup_ownership() {
  29. local target="$1"
  30. local type="$2"
  31. case "$type" in
  32. file | directory) ;;
  33. *)
  34. cat <<EOF
  35. !!!
  36. !!! ERROR
  37. !!! "$type" is not a valid type, exiting...
  38. !!!
  39. EOF
  40. exit 1
  41. ;;
  42. esac
  43. if [ "$(stat -c %U:%G "$target")" != "searxng:searxng" ]; then
  44. if [ "$(id -u)" -eq 0 ]; then
  45. chown -R searxng:searxng "$target"
  46. else
  47. cat <<EOF
  48. !!!
  49. !!! WARNING
  50. !!! "$target" $type is not owned by "searxng"
  51. !!! This may cause issues when running SearXNG
  52. !!!
  53. !!! Run the container as root to fix this issue automatically
  54. !!! Alternatively, you can chown the $type manually:
  55. !!! $ chown -R searxng:searxng "$target"
  56. !!!
  57. EOF
  58. fi
  59. fi
  60. }
  61. # Apply envs to uwsgi.ini
  62. setup_uwsgi() {
  63. local timestamp
  64. timestamp=$(stat -c %Y "$UWSGI_SETTINGS_PATH")
  65. sed -i \
  66. -e "s|workers = .*|workers = ${UWSGI_WORKERS:-%k}|g" \
  67. -e "s|threads = .*|threads = ${UWSGI_THREADS:-4}|g" \
  68. "$UWSGI_SETTINGS_PATH"
  69. # Restore timestamp
  70. touch -c -d "@$timestamp" "$UWSGI_SETTINGS_PATH"
  71. }
  72. # Apply envs to settings.yml
  73. setup_searxng() {
  74. local timestamp
  75. timestamp=$(stat -c %Y "$SEARXNG_SETTINGS_PATH")
  76. # Ensure trailing slash in BASE_URL
  77. # https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
  78. export BASE_URL="${BASE_URL%/}/"
  79. sed -i \
  80. -e "s|base_url: false|base_url: ${BASE_URL:-false}|g" \
  81. -e "s/instance_name: \"SearXNG\"/instance_name: \"${INSTANCE_NAME:-SearXNG}\"/g" \
  82. -e "s/autocomplete: \"\"/autocomplete: \"${AUTOCOMPLETE:-}\"/g" \
  83. -e "s/ultrasecretkey/$(head -c 24 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')/g" \
  84. "$SEARXNG_SETTINGS_PATH"
  85. # Restore timestamp
  86. touch -c -d "@$timestamp" "$SEARXNG_SETTINGS_PATH"
  87. }
  88. # Handle volume mounts
  89. volume_handler() {
  90. local target="$1"
  91. # Check if it's a valid directory
  92. check_directory "$target"
  93. setup_ownership "$target" "directory"
  94. }
  95. # Handle configuration file updates
  96. config_handler() {
  97. local target="$1"
  98. local template="$2"
  99. local new_template_target="$target.new"
  100. # Create/Update the configuration file
  101. if [ -f "$target" ]; then
  102. setup_ownership "$target" "file"
  103. if [ "$template" -nt "$target" ]; then
  104. cp -pfT "$template" "$new_template_target"
  105. cat <<EOF
  106. ...
  107. ... INFORMATION
  108. ... Update available for "$target"
  109. ... It is recommended to update the configuration file to ensure proper functionality
  110. ...
  111. ... New version placed at "$new_template_target"
  112. ... Please review and merge changes
  113. ...
  114. EOF
  115. fi
  116. else
  117. cat <<EOF
  118. ...
  119. ... INFORMATION
  120. ... "$target" does not exist, creating from template...
  121. ...
  122. EOF
  123. cp -pfT "$template" "$target"
  124. fi
  125. # Check if it's a valid file
  126. check_file "$target"
  127. }
  128. echo "SearXNG $SEARXNG_VERSION"
  129. # Check for volume mounts
  130. volume_handler "$CONFIG_PATH"
  131. volume_handler "$DATA_PATH"
  132. # Check for updates in files
  133. config_handler "$UWSGI_SETTINGS_PATH" "/usr/local/searxng/.template/uwsgi.ini"
  134. config_handler "$SEARXNG_SETTINGS_PATH" "/usr/local/searxng/searx/settings.yml"
  135. # Update files
  136. setup_uwsgi
  137. setup_searxng
  138. exec /usr/local/searxng/venv/bin/uwsgi --http-socket "$BIND_ADDRESS" "$UWSGI_SETTINGS_PATH"