lib.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. # XXX: For version 1.70.1 there's a bug hitting SENTRY_CLI_NO_EXIT_TRAP: unbound variable
  24. # We can remove this after it's fixed
  25. # https://github.com/getsentry/sentry-cli/pull/1059
  26. export SENTRY_CLI_NO_EXIT_TRAP=${SENTRY_CLI_NO_EXIT_TRAP-0}
  27. if [ -n "${SENTRY_DSN+x}" ] && [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  28. if ! require sentry-cli; then
  29. curl -sL https://sentry.io/get-cli/ | bash
  30. fi
  31. eval "$(sentry-cli bash-hook)"
  32. fi
  33. }
  34. query-mac() {
  35. [[ $(uname -s) = 'Darwin' ]]
  36. }
  37. query-big-sur() {
  38. if require sw_vers && sw_vers -productVersion | grep -E "11\." >/dev/null; then
  39. return 0
  40. fi
  41. return 1
  42. }
  43. query-apple-m1() {
  44. query-mac && [[ $(uname -m) = 'arm64' ]]
  45. }
  46. query-valid-python-version() {
  47. if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
  48. python_version=$(python3 -V 2>&1 | awk '{print $2}')
  49. if [ "$python_version" != "$SENTRY_PYTHON_VERSION" ]; then
  50. cat <<EOF
  51. ${red}${bold}
  52. ERROR: You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}),
  53. but it doesn't match the value of python's version: ${python_version}
  54. You should create a new ${SENTRY_PYTHON_VERSION} virtualenv by running "rm -rf ${venv_name} && direnv allow".
  55. ${reset}
  56. EOF
  57. return 1
  58. fi
  59. cat <<EOF
  60. ${yellow}${bold}
  61. You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}). You're on your own.
  62. ${reset}
  63. EOF
  64. return 0
  65. fi
  66. python_version=$(python3 -V 2>&1 | awk '{print $2}')
  67. minor=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\1/')
  68. patch=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\2/')
  69. if [ "$minor" -ne 8 ] || [ "$patch" -lt 10 ]; then
  70. cat <<EOF
  71. ${red}${bold}
  72. ERROR: You're running a virtualenv with Python ${python_version}.
  73. We only support >= 3.8.10, < 3.9.
  74. Either run "rm -rf ${venv_name} && direnv allow" to
  75. OR set SENTRY_PYTHON_VERSION=${python_version} to an .env file to bypass this check."
  76. EOF
  77. return 1
  78. fi
  79. }
  80. sudo-askpass() {
  81. if [ -z "${sudo-askpass-x}" ]; then
  82. sudo --askpass "$@"
  83. else
  84. sudo "$@"
  85. fi
  86. }
  87. upgrade-pip() {
  88. pip install --upgrade "pip==21.1.2" "wheel==0.36.2"
  89. }
  90. install-py-dev() {
  91. upgrade-pip
  92. # It places us within top src dir to be at the same path as setup.py
  93. # This helps when getsentry calls into this script
  94. cd "${HERE}/.." || exit
  95. echo "--> Installing Sentry (for development)"
  96. if query-apple-m1; then
  97. # This installs pyscopg-binary2 since there's no arm64 wheel
  98. # This saves having to install postgresql on the Developer's machine + using flags
  99. # https://github.com/psycopg/psycopg2/issues/1286
  100. pip install https://storage.googleapis.com/python-arm64-wheels/psycopg2_binary-2.8.6-cp38-cp38-macosx_11_0_arm64.whl
  101. # The CPATH is needed for confluent-kakfa --> https://github.com/confluentinc/confluent-kafka-python/issues/1190
  102. export CPATH="$(brew --prefix librdkafka)/include"
  103. # The LDFLAGS is needed for uWSGI --> https://github.com/unbit/uwsgi/issues/2361
  104. export LDFLAGS="-L$(brew --prefix gettext)/lib"
  105. fi
  106. # SENTRY_LIGHT_BUILD=1 disables webpacking during setup.py.
  107. # Webpacked assets are only necessary for devserver (which does it lazily anyways)
  108. # and acceptance tests, which webpack automatically if run.
  109. SENTRY_LIGHT_BUILD=1 pip install -e '.[dev]'
  110. patch-selenium
  111. }
  112. patch-selenium() {
  113. # XXX: getsentry repo calls this!
  114. # This hack is until we can upgrade to a newer version of Selenium
  115. fx_profile=.venv/lib/python3.8/site-packages/selenium/webdriver/firefox/firefox_profile.py
  116. # Remove this block when upgrading the selenium package
  117. if grep -q "or setting is" "${fx_profile}"; then
  118. echo "We are patching ${fx_profile}. You will see this message only once."
  119. patch -p0 <scripts/patches/firefox_profile.diff
  120. fi
  121. }
  122. setup-git-config() {
  123. git config --local branch.autosetuprebase always
  124. git config --local core.ignorecase false
  125. git config --local blame.ignoreRevsFile .git-blame-ignore-revs
  126. }
  127. setup-git() {
  128. setup-git-config
  129. echo "--> Installing git hooks"
  130. mkdir -p .git/hooks && cd .git/hooks && ln -sf ../../config/hooks/* ./ && cd - || exit
  131. # shellcheck disable=SC2016
  132. python3 -c '' || (
  133. echo 'Please run `make setup-pyenv` to install the required Python 3 version.'
  134. exit 1
  135. )
  136. pip install -r requirements-pre-commit.txt
  137. pre-commit install --install-hooks
  138. echo ""
  139. }
  140. node-version-check() {
  141. # Checks to see if node's version matches the one specified in package.json for Volta.
  142. node -pe "process.exit(Number(!(process.version == 'v' + require('./package.json').volta.node )))" ||
  143. (
  144. echo 'Unexpected node version. Recommended to use https://github.com/volta-cli/volta'
  145. echo 'Run `volta install node` and `volta install yarn` to update your toolchain.'
  146. echo 'If you do not have volta installed run `curl https://get.volta.sh | bash` or visit https://volta.sh'
  147. exit 1
  148. )
  149. }
  150. install-js-dev() {
  151. node-version-check
  152. echo "--> Installing Yarn packages (for development)"
  153. # Use NODE_ENV=development so that yarn installs both dependencies + devDependencies
  154. NODE_ENV=development yarn install --frozen-lockfile
  155. # A common problem is with node packages not existing in `node_modules` even though `yarn install`
  156. # says everything is up to date. Even though `yarn install` is run already, it doesn't take into
  157. # account the state of the current filesystem (it only checks .yarn-integrity).
  158. # Add an additional check against `node_modules`
  159. yarn check --verify-tree || yarn install --check-files
  160. }
  161. develop() {
  162. setup-git
  163. install-js-dev
  164. install-py-dev
  165. }
  166. init-config() {
  167. sentry init --dev
  168. }
  169. run-dependent-services() {
  170. sentry devservices up
  171. }
  172. create-db() {
  173. echo "--> Creating 'sentry' database"
  174. docker exec sentry_postgres createdb -h 127.0.0.1 -U postgres -E utf-8 sentry || true
  175. }
  176. apply-migrations() {
  177. echo "--> Applying migrations"
  178. sentry upgrade --noinput
  179. }
  180. create-user() {
  181. if [[ -n "${GITHUB_ACTIONS+x}" ]]; then
  182. sentry createuser --superuser --email foo@tbd.com --no-password --no-input
  183. else
  184. sentry createuser --superuser
  185. fi
  186. }
  187. build-platform-assets() {
  188. echo "--> Building platform assets"
  189. echo "from sentry.utils.integrationdocs import sync_docs; sync_docs(quiet=True)" | sentry exec
  190. }
  191. bootstrap() {
  192. develop
  193. init-config
  194. run-dependent-services
  195. create-db
  196. apply-migrations
  197. create-user
  198. # Load mocks requires a super user to exist, thus, we execute after create-user
  199. bin/load-mocks
  200. build-platform-assets
  201. }
  202. clean() {
  203. echo "--> Cleaning static cache"
  204. rm -rf dist/* src/sentry/static/sentry/dist/*
  205. echo "--> Cleaning integration docs cache"
  206. rm -rf src/sentry/integration-docs
  207. echo "--> Cleaning pyc files"
  208. find . -name "*.pyc" -delete
  209. echo "--> Cleaning python build artifacts"
  210. rm -rf build/ dist/ src/sentry/assets.json
  211. echo ""
  212. }
  213. drop-db() {
  214. echo "--> Dropping existing 'sentry' database"
  215. docker exec sentry_postgres dropdb -h 127.0.0.1 -U postgres sentry || true
  216. }
  217. reset-db() {
  218. drop-db
  219. create-db
  220. apply-migrations
  221. # This ensures that your set up as some data inside of it
  222. bin/load-mocks
  223. }
  224. prerequisites() {
  225. if [ -z "${CI+x}" ]; then
  226. brew update -q && brew bundle -q
  227. else
  228. HOMEBREW_NO_AUTO_UPDATE=on brew install libxmlsec1 pyenv
  229. fi
  230. }
  231. direnv-help() {
  232. cat >&2 <<EOF
  233. If you're a Sentry employee and you're stuck or have questions, ask in #discuss-dev-tooling.
  234. If you're not, please file an issue under https://github.com/getsentry/sentry/issues/new/choose and mention @getsentry/owners-sentry-dev
  235. You can configure the behaviour of direnv by adding the following variables to a .env file:
  236. - SENTRY_DIRENV_DEBUG=1: This will allow printing debug messages
  237. - SENTRY_DEVENV_NO_REPORT=1: Do not report development environment errors to Sentry.io
  238. EOF
  239. }