lib.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. # NOTE: This file is sourced in CI across different repos (e.g. snuba),
  3. # thus, renaming this file or any functions can break CI!
  4. #
  5. # Module containing code shared across various shell scripts
  6. # Execute functions from this module via the script do.sh
  7. # shellcheck disable=SC2034 # Unused variables
  8. # shellcheck disable=SC2001 # https://github.com/koalaman/shellcheck/wiki/SC2001
  9. POSTGRES_CONTAINER="sentry_postgres"
  10. USE_NEW_DEVSERVICES=${USE_NEW_DEVSERVICES:-"0"}
  11. if [ "$USE_NEW_DEVSERVICES" == "1" ]; then
  12. POSTGRES_CONTAINER="sentry-postgres-1"
  13. fi
  14. # This block is a safe-guard since in CI calling tput will fail and abort scripts
  15. if [ -z "${CI+x}" ]; then
  16. bold="$(tput bold)"
  17. red="$(tput setaf 1)"
  18. green="$(tput setaf 2)"
  19. yellow="$(tput setaf 3)"
  20. reset="$(tput sgr0)"
  21. fi
  22. venv_name=".venv"
  23. # XDG paths' standardized defaults:
  24. # (see https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables )
  25. export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
  26. export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
  27. export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
  28. export XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}"
  29. export XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS:-/etc/xdg}"
  30. export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
  31. export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/var/run}"
  32. # Check if a command is available
  33. require() {
  34. command -v "$1" >/dev/null 2>&1
  35. }
  36. query-valid-python-version() {
  37. python_version=$(python3 -V 2>&1 | awk '{print $2}')
  38. if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
  39. if [ "$python_version" != "$SENTRY_PYTHON_VERSION" ]; then
  40. cat <<EOF
  41. ${red}${bold}
  42. ERROR: You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}),
  43. but it doesn't match the value of python's version: ${python_version}
  44. You should create a new ${SENTRY_PYTHON_VERSION} virtualenv by running "rm -rf ${venv_name} && devenv sync".
  45. ${reset}
  46. EOF
  47. return 1
  48. else
  49. cat <<EOF
  50. ${yellow}${bold}
  51. You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}). You're on your own.
  52. ${reset}
  53. EOF
  54. return 0
  55. fi
  56. else
  57. minor=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\1/')
  58. patch=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\2/')
  59. if [ "$minor" -ne 12 ] || [ "$patch" -lt 1 ]; then
  60. cat <<EOF
  61. ${red}${bold}
  62. ERROR: You're running a virtualenv with Python ${python_version}.
  63. We only support >= 3.12.1, < 3.13.
  64. Either run "rm -rf ${venv_name} && direnv allow" to
  65. OR set SENTRY_PYTHON_VERSION=${python_version} to an .env file to bypass this check."
  66. EOF
  67. return 1
  68. fi
  69. fi
  70. }
  71. sudo-askpass() {
  72. if [ -z "${sudo-askpass-x}" ]; then
  73. sudo --askpass "$@"
  74. else
  75. sudo "$@"
  76. fi
  77. }
  78. init-config() {
  79. sentry init --dev --no-clobber
  80. }
  81. run-dependent-services() {
  82. sentry devservices up
  83. }
  84. create-db() {
  85. container_name=${POSTGRES_CONTAINER}
  86. echo "--> Creating 'sentry' database"
  87. docker exec "${container_name}" createdb -h 127.0.0.1 -U postgres -E utf-8 sentry || true
  88. echo "--> Creating 'control', 'region' and 'secondary' database"
  89. docker exec "${container_name}" createdb -h 127.0.0.1 -U postgres -E utf-8 control || true
  90. docker exec "${container_name}" createdb -h 127.0.0.1 -U postgres -E utf-8 region || true
  91. docker exec "${container_name}" createdb -h 127.0.0.1 -U postgres -E utf-8 secondary || true
  92. }
  93. apply-migrations() {
  94. create-db
  95. echo "--> Applying migrations"
  96. sentry upgrade --noinput
  97. }
  98. create-superuser() {
  99. echo "--> Creating a superuser account"
  100. if [[ -n "${GITHUB_ACTIONS+x}" ]]; then
  101. sentry createuser --superuser --email foo@tbd.com --no-password --no-input
  102. else
  103. sentry createuser --superuser --email admin@sentry.io --password admin --no-input
  104. echo "Password is admin."
  105. fi
  106. }
  107. build-platform-assets() {
  108. echo "--> Building platform assets"
  109. python3 -m sentry.build._integration_docs
  110. # make sure this didn't silently do nothing
  111. test -f src/sentry/integration-docs/android.json
  112. }
  113. clean() {
  114. echo "--> Cleaning static cache"
  115. rm -rf dist/* src/sentry/static/sentry/dist/*
  116. echo "--> Cleaning integration docs cache"
  117. rm -rf src/sentry/integration-docs
  118. echo "--> Cleaning pyc files"
  119. find . -name "*.pyc" -delete
  120. echo "--> Cleaning python build artifacts"
  121. rm -rf build/ dist/ src/sentry/assets.json
  122. echo ""
  123. }
  124. drop-db() {
  125. container_name=${POSTGRES_CONTAINER}
  126. echo "--> Dropping existing 'sentry' database"
  127. docker exec "${container_name}" dropdb --if-exists -h 127.0.0.1 -U postgres sentry
  128. echo "--> Dropping 'control' and 'region' database"
  129. docker exec "${container_name}" dropdb --if-exists -h 127.0.0.1 -U postgres control
  130. docker exec "${container_name}" dropdb --if-exists -h 127.0.0.1 -U postgres region
  131. docker exec "${container_name}" dropdb --if-exists -h 127.0.0.1 -U postgres secondary
  132. }
  133. reset-db() {
  134. drop-db
  135. apply-migrations
  136. create-superuser
  137. echo 'Finished resetting database. To load mock data, run `./bin/load-mocks`'
  138. }