entrypoint.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/sh
  2. # shellcheck shell=dash
  3. set -u
  4. # Check if it's a valid file
  5. check_file() {
  6. local target="$1"
  7. if [ ! -f "$target" ]; then
  8. cat <<EOF
  9. !!!
  10. !!! ERROR
  11. !!! "$target" is not a valid file, exiting...
  12. !!!
  13. EOF
  14. exit 127
  15. fi
  16. }
  17. # Check if it's a valid directory
  18. check_directory() {
  19. local target="$1"
  20. if [ ! -d "$target" ]; then
  21. cat <<EOF
  22. !!!
  23. !!! ERROR
  24. !!! "$target" is not a valid directory, exiting...
  25. !!!
  26. EOF
  27. exit 127
  28. fi
  29. }
  30. setup_ownership() {
  31. local target="$1"
  32. local type="$2"
  33. case "$type" in
  34. file | directory) ;;
  35. *)
  36. cat <<EOF
  37. !!!
  38. !!! ERROR
  39. !!! "$type" is not a valid type, exiting...
  40. !!!
  41. EOF
  42. exit 1
  43. ;;
  44. esac
  45. target_ownership=$(stat -c %U:%G "$target")
  46. if [ "$target_ownership" != "searxng:searxng" ]; then
  47. if [ "${FORCE_OWNERSHIP:-true}" = true ] && [ "$(id -u)" -eq 0 ]; then
  48. chown -R searxng:searxng "$target"
  49. else
  50. cat <<EOF
  51. !!!
  52. !!! WARNING
  53. !!! "$target" $type is not owned by "searxng:searxng"
  54. !!! This may cause issues when running SearXNG
  55. !!!
  56. !!! Expected "searxng:searxng"
  57. !!! Got "$target_ownership"
  58. !!!
  59. EOF
  60. fi
  61. fi
  62. }
  63. # Handle volume mounts
  64. volume_handler() {
  65. local target="$1"
  66. check_directory "$target"
  67. setup_ownership "$target" "directory"
  68. }
  69. # Handle configuration file updates
  70. config_handler() {
  71. local target="$1"
  72. local template="$2"
  73. local new_template_target="$target.new"
  74. # Create/Update the configuration file
  75. if [ -f "$target" ]; then
  76. setup_ownership "$target" "file"
  77. if [ "$template" -nt "$target" ]; then
  78. cp -pfT "$template" "$new_template_target"
  79. cat <<EOF
  80. ...
  81. ... INFORMATION
  82. ... Update available for "$target"
  83. ... It is recommended to update the configuration file to ensure proper functionality
  84. ...
  85. ... New version placed at "$new_template_target"
  86. ... Please review and merge changes
  87. ...
  88. EOF
  89. fi
  90. else
  91. cat <<EOF
  92. ...
  93. ... INFORMATION
  94. ... "$target" does not exist, creating from template...
  95. ...
  96. EOF
  97. cp -pfT "$template" "$target"
  98. sed -i "s/ultrasecretkey/$(head -c 24 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')/g" "$target"
  99. fi
  100. check_file "$target"
  101. }
  102. cat <<EOF
  103. SearXNG $SEARXNG_VERSION
  104. EOF
  105. # Check for volume mounts
  106. volume_handler "$CONFIG_PATH"
  107. volume_handler "$DATA_PATH"
  108. # Check for files
  109. config_handler "$SEARXNG_SETTINGS_PATH" "/usr/local/searxng/searx/settings.yml"
  110. exec /usr/local/searxng/venv/bin/granian searx.webapp:app