lib.sh 8.2 KB

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