lib.sh 8.2 KB

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