lib.sh 8.5 KB

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