lib.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. # This block is a safe-guard since in CI calling tput will fail and abort scripts
  10. if [ -z "${CI+x}" ]; then
  11. bold="$(tput bold)"
  12. red="$(tput setaf 1)"
  13. green="$(tput setaf 2)"
  14. yellow="$(tput setaf 3)"
  15. reset="$(tput sgr0)"
  16. fi
  17. venv_name=".venv"
  18. # Check if a command is available
  19. require() {
  20. command -v "$1" >/dev/null 2>&1
  21. }
  22. configure-sentry-cli() {
  23. if [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  24. if ! require sentry-cli; then
  25. curl -sL https://sentry.io/get-cli/ | SENTRY_CLI_VERSION=2.0.4 bash
  26. fi
  27. fi
  28. }
  29. query-valid-python-version() {
  30. python_version=$(python3 -V 2>&1 | awk '{print $2}')
  31. if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
  32. if [ "$python_version" != "$SENTRY_PYTHON_VERSION" ]; then
  33. cat <<EOF
  34. ${red}${bold}
  35. ERROR: You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}),
  36. but it doesn't match the value of python's version: ${python_version}
  37. You should create a new ${SENTRY_PYTHON_VERSION} virtualenv by running "rm -rf ${venv_name} && direnv allow".
  38. ${reset}
  39. EOF
  40. return 1
  41. else
  42. cat <<EOF
  43. ${yellow}${bold}
  44. You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}). You're on your own.
  45. ${reset}
  46. EOF
  47. return 0
  48. fi
  49. else
  50. minor=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\1/')
  51. patch=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\2/')
  52. if [ "$minor" -ne 8 ] || [ "$patch" -lt 10 ]; then
  53. cat <<EOF
  54. ${red}${bold}
  55. ERROR: You're running a virtualenv with Python ${python_version}.
  56. We only support >= 3.8.10, < 3.9.
  57. Either run "rm -rf ${venv_name} && direnv allow" to
  58. OR set SENTRY_PYTHON_VERSION=${python_version} to an .env file to bypass this check."
  59. EOF
  60. return 1
  61. fi
  62. fi
  63. }
  64. sudo-askpass() {
  65. if [ -z "${sudo-askpass-x}" ]; then
  66. sudo --askpass "$@"
  67. else
  68. sudo "$@"
  69. fi
  70. }
  71. upgrade-pip() {
  72. pip install --constraint requirements-dev-frozen.txt pip setuptools wheel
  73. }
  74. install-py-dev() {
  75. upgrade-pip
  76. # It places us within top src dir to be at the same path as setup.py
  77. # This helps when getsentry calls into this script
  78. cd "${HERE}/.." || exit
  79. echo "--> Installing Sentry (for development)"
  80. # pip doesn't do well with swapping drop-ins
  81. pip uninstall -qqy uwsgi
  82. # SENTRY_LIGHT_BUILD=1 disables webpacking during setup.py.
  83. # Webpacked assets are only necessary for devserver (which does it lazily anyways)
  84. # and acceptance tests, which webpack automatically if run.
  85. SENTRY_LIGHT_BUILD=1 pip install --constraint requirements-dev-frozen.txt -e '.[dev]'
  86. }
  87. setup-git-config() {
  88. git config --local branch.autosetuprebase always
  89. git config --local core.ignorecase false
  90. git config --local blame.ignoreRevsFile .git-blame-ignore-revs
  91. }
  92. setup-git() {
  93. setup-git-config
  94. # if hooks are explicitly turned off do nothing
  95. if [[ "$(git config core.hooksPath)" == '/dev/null' ]]; then
  96. echo "--> core.hooksPath set to /dev/null. Skipping git hook setup"
  97. echo ""
  98. return
  99. fi
  100. echo "--> Installing git hooks"
  101. mkdir -p .git/hooks && cd .git/hooks && ln -sf ../../config/hooks/* ./ && cd - || exit
  102. # shellcheck disable=SC2016
  103. python3 -c '' || (
  104. echo 'Please run `make setup-pyenv` to install the required Python 3 version.'
  105. exit 1
  106. )
  107. if ! require pre-commit; then
  108. pip install -r requirements-dev-only-frozen.txt
  109. fi
  110. pre-commit install --install-hooks
  111. echo ""
  112. }
  113. node-version-check() {
  114. # Checks to see if node's version matches the one specified in package.json for Volta.
  115. node -pe "process.exit(Number(!(process.version == 'v' + require('./package.json').volta.node )))" ||
  116. (
  117. echo 'Unexpected node version. Recommended to use https://github.com/volta-cli/volta'
  118. echo 'Run `volta install node` and `volta install yarn` to update your toolchain.'
  119. echo 'If you do not have volta installed run `curl https://get.volta.sh | bash` or visit https://volta.sh'
  120. exit 1
  121. )
  122. }
  123. install-js-dev() {
  124. node-version-check
  125. echo "--> Installing Yarn packages (for development)"
  126. # Use NODE_ENV=development so that yarn installs both dependencies + devDependencies
  127. NODE_ENV=development yarn install --frozen-lockfile
  128. # A common problem is with node packages not existing in `node_modules` even though `yarn install`
  129. # says everything is up to date. Even though `yarn install` is run already, it doesn't take into
  130. # account the state of the current filesystem (it only checks .yarn-integrity).
  131. # Add an additional check against `node_modules`
  132. yarn check --verify-tree || yarn install --check-files
  133. }
  134. develop() {
  135. install-js-dev
  136. install-py-dev
  137. setup-git
  138. }
  139. init-config() {
  140. sentry init --dev
  141. }
  142. run-dependent-services() {
  143. sentry devservices up
  144. }
  145. create-db() {
  146. echo "--> Creating 'sentry' database"
  147. docker exec sentry_postgres createdb -h 127.0.0.1 -U postgres -E utf-8 sentry || true
  148. }
  149. apply-migrations() {
  150. echo "--> Applying migrations"
  151. sentry upgrade --noinput
  152. }
  153. create-user() {
  154. echo "--> Creating a superuser account"
  155. if [[ -n "${GITHUB_ACTIONS+x}" ]]; then
  156. sentry createuser --superuser --email foo@tbd.com --no-password --no-input
  157. else
  158. sentry createuser --superuser
  159. fi
  160. }
  161. build-platform-assets() {
  162. echo "--> Building platform assets"
  163. echo "from sentry.utils.integrationdocs import sync_docs; sync_docs(quiet=True)" | sentry exec
  164. # make sure this didn't silently do nothing
  165. test -f src/sentry/integration-docs/android.json
  166. }
  167. bootstrap() {
  168. develop
  169. init-config
  170. run-dependent-services
  171. create-db
  172. apply-migrations
  173. create-user
  174. # Load mocks requires a super user to exist, thus, we execute after create-user
  175. bin/load-mocks
  176. build-platform-assets
  177. }
  178. clean() {
  179. echo "--> Cleaning static cache"
  180. rm -rf dist/* src/sentry/static/sentry/dist/*
  181. echo "--> Cleaning integration docs cache"
  182. rm -rf src/sentry/integration-docs
  183. echo "--> Cleaning pyc files"
  184. find . -name "*.pyc" -delete
  185. echo "--> Cleaning python build artifacts"
  186. rm -rf build/ dist/ src/sentry/assets.json
  187. echo ""
  188. }
  189. drop-db() {
  190. echo "--> Dropping existing 'sentry' database"
  191. docker exec sentry_postgres dropdb -h 127.0.0.1 -U postgres sentry || true
  192. }
  193. reset-db() {
  194. drop-db
  195. create-db
  196. apply-migrations
  197. create-user
  198. echo "Finished resetting database. To load mock data, run `./bin/load-mocks`"
  199. }
  200. prerequisites() {
  201. if [ -z "${CI+x}" ]; then
  202. brew update -q && brew bundle -q
  203. else
  204. HOMEBREW_NO_AUTO_UPDATE=on brew install pyenv
  205. fi
  206. }
  207. direnv-help() {
  208. cat >&2 <<EOF
  209. If you're a Sentry employee and you're stuck or have questions, ask in #discuss-dev-infra.
  210. If you're not, please file an issue under https://github.com/getsentry/sentry/issues/new/choose and mention @getsentry/owners-sentry-dev
  211. You can configure the behaviour of direnv by adding the following variables to a .env file:
  212. - SENTRY_DIRENV_DEBUG=1: This will allow printing debug messages
  213. - SENTRY_DEVENV_NO_REPORT=1: Do not report development environment errors to Sentry.io
  214. EOF
  215. }