manage.sh 5.9 KB

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