manage.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/sh
  2. export LANG=C
  3. BASE_DIR="$(dirname -- "`readlink -f -- "$0"`")"
  4. cd -- "$BASE_DIR"
  5. set -e
  6. # subshell
  7. PYTHONPATH="$BASE_DIR"
  8. SEARX_DIR="$BASE_DIR/searx"
  9. ACTION="$1"
  10. . "${BASE_DIR}/utils/brand.env"
  11. #
  12. # Python
  13. #
  14. update_packages() {
  15. pip install --upgrade pip
  16. pip install --upgrade setuptools
  17. pip install -Ur "$BASE_DIR/requirements.txt"
  18. }
  19. update_dev_packages() {
  20. update_packages
  21. pip install -Ur "$BASE_DIR/requirements-dev.txt"
  22. }
  23. install_geckodriver() {
  24. echo '[!] Checking geckodriver'
  25. # TODO : check the current geckodriver version
  26. set -e
  27. geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
  28. set +e
  29. if [ -z "$NOTFOUND" ]; then
  30. return
  31. fi
  32. GECKODRIVER_VERSION="v0.28.0"
  33. PLATFORM="`python3 -c "import platform; print(platform.system().lower(), platform.architecture()[0])"`"
  34. case "$PLATFORM" in
  35. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  36. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  37. "windows 32 bit") ARCH="win32";;
  38. "windows 64 bit") ARCH="win64";;
  39. "mac 64bit") ARCH="macos";;
  40. esac
  41. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  42. if [ -z "$1" ]; then
  43. if [ -z "$VIRTUAL_ENV" ]; then
  44. printf "geckodriver can't be installed because VIRTUAL_ENV is not set, you should download it from\n %s" "$GECKODRIVER_URL"
  45. exit
  46. else
  47. GECKODRIVER_DIR="$VIRTUAL_ENV/bin"
  48. fi
  49. else
  50. GECKODRIVER_DIR="$1"
  51. mkdir -p -- "$GECKODRIVER_DIR"
  52. fi
  53. printf "Installing %s/geckodriver from\n %s" "$GECKODRIVER_DIR" "$GECKODRIVER_URL"
  54. FILE="`mktemp`"
  55. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "$GECKODRIVER_DIR" -f "$FILE" geckodriver
  56. rm -- "$FILE"
  57. chmod 777 -- "$GECKODRIVER_DIR/geckodriver"
  58. }
  59. locales() {
  60. pybabel compile -d "$SEARX_DIR/translations"
  61. }
  62. #
  63. # Web
  64. #
  65. npm_path_setup() {
  66. which npm || (printf 'Error: npm is not found\n'; exit 1)
  67. export PATH="$(npm bin)":$PATH
  68. }
  69. npm_packages() {
  70. npm_path_setup
  71. echo '[!] install NPM packages'
  72. cd -- "$BASE_DIR"
  73. npm install less@2.7 less-plugin-clean-css grunt-cli
  74. echo '[!] install NPM packages for oscar theme'
  75. cd -- "$BASE_DIR/searx/static/themes/oscar"
  76. npm install
  77. echo '[!] install NPM packages for simple theme'
  78. cd -- "$BASE_DIR/searx/static/themes/simple"
  79. npm install
  80. }
  81. docker_build() {
  82. # Check if it is a git repository
  83. if [ ! -d .git ]; then
  84. echo "This is not Git repository"
  85. exit 1
  86. fi
  87. if [ ! -x "$(which git)" ]; then
  88. echo "git is not installed"
  89. exit 1
  90. fi
  91. if [ ! git remote get-url origin 2> /dev/null ]; then
  92. echo "there is no remote origin"
  93. exit 1
  94. fi
  95. # This is a git repository
  96. # "git describe" to get the Docker version (for example : v0.15.0-89-g0585788e)
  97. # awk to remove the "v" and the "g"
  98. SEARX_GIT_VERSION=$(git describe --match "v[0-9]*\.[0-9]*\.[0-9]*" HEAD 2>/dev/null | awk -F'-' '{OFS="-"; $1=substr($1, 2); if ($3) { $3=substr($3, 2); } print}')
  99. # add the suffix "-dirty" if the repository has uncommited change
  100. # /!\ HACK for searx/searx: ignore utils/brand.env
  101. git update-index -q --refresh
  102. if [ ! -z "$(git diff-index --name-only HEAD -- | grep -v 'utils/brand.env')" ]; then
  103. SEARX_GIT_VERSION="${SEARX_GIT_VERSION}-dirty"
  104. fi
  105. # Get the last git commit id, will be added to the Searx version (see Dockerfile)
  106. VERSION_GITCOMMIT=$(echo $SEARX_GIT_VERSION | cut -d- -f2-4)
  107. echo "Last commit : $VERSION_GITCOMMIT"
  108. # Check consistency between the git tag and the searx/version.py file
  109. # /!\ HACK : parse Python file with bash /!\
  110. # otherwise it is not possible build the docker image without all Python dependencies ( version.py loads __init__.py )
  111. # SEARX_PYTHON_VERSION=$(python3 -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)")
  112. SEARX_PYTHON_VERSION=$(cat searx/version.py | grep "\(VERSION_MAJOR\|VERSION_MINOR\|VERSION_BUILD\) =" | cut -d\= -f2 | sed -e 's/^[[:space:]]*//' | paste -sd "." -)
  113. if [ $(echo "$SEARX_GIT_VERSION" | cut -d- -f1) != "$SEARX_PYTHON_VERSION" ]; then
  114. echo "Inconsistency between the last git tag and the searx/version.py file"
  115. echo "git tag: $SEARX_GIT_VERSION"
  116. echo "searx/version.py: $SEARX_PYTHON_VERSION"
  117. exit 1
  118. fi
  119. # define the docker image name
  120. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  121. SEARX_IMAGE_NAME="${GITHUB_USER:-searx}/searx"
  122. # build Docker image
  123. echo "Building image ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
  124. sudo docker build \
  125. --build-arg GIT_URL="${GIT_URL}" \
  126. --build-arg SEARX_GIT_VERSION="${SEARX_GIT_VERSION}" \
  127. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  128. --build-arg LABEL_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
  129. --build-arg LABEL_VCS_REF=$(git rev-parse HEAD) \
  130. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  131. --build-arg TIMESTAMP_SETTINGS=$(git log -1 --format="%cd" --date=unix -- searx/settings.yml) \
  132. --build-arg TIMESTAMP_UWSGI=$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini) \
  133. -t ${SEARX_IMAGE_NAME}:latest -t ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION} .
  134. if [ "$1" = "push" ]; then
  135. sudo docker push ${SEARX_IMAGE_NAME}:latest
  136. sudo docker push ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}
  137. fi
  138. }
  139. #
  140. # Help
  141. #
  142. help() {
  143. [ -z "$1" ] || printf 'Error: %s\n' "$1"
  144. echo "Searx manage.sh help
  145. Commands
  146. ========
  147. help - This text
  148. Build requirements
  149. ------------------
  150. update_packages - Check & update production dependency changes
  151. update_dev_packages - Check & update development and production dependency changes
  152. install_geckodriver - Download & install geckodriver if not already installed (required for robot_tests)
  153. npm_packages - Download & install npm dependencies
  154. Build
  155. -----
  156. locales - Compile locales
  157. Environment:
  158. GIT_URL: ${GIT_URL}
  159. ISSUE_URL: ${ISSUE_URL}
  160. SEARX_URL: ${SEARX_URL}
  161. DOCS_URL: ${DOCS_URL}
  162. PUBLIC_INSTANCES: ${PUBLIC_INSTANCES}
  163. "
  164. }
  165. [ "$(command -V "$ACTION" | grep ' function$')" = "" ] \
  166. && help "action not found" \
  167. || "$ACTION" "$2"