lib.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/bash
  2. # Module containing code shared across various shell scripts
  3. # Execute functions from this module via the script do.sh
  4. # Check if a command is available
  5. require() {
  6. command -v "$1" >/dev/null 2>&1
  7. }
  8. configure-sentry-cli() {
  9. if [ -n "${SENTRY_DSN+x}" ] && [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  10. if ! require sentry-cli; then
  11. curl -sL https://sentry.io/get-cli/ | bash
  12. fi
  13. eval "$(sentry-cli bash-hook)"
  14. fi
  15. }
  16. query-mac() {
  17. [[ $(uname -s) = 'Darwin' ]]
  18. }
  19. query_big_sur() {
  20. if require sw_vers && sw_vers -productVersion | grep -E "11\." > /dev/null; then
  21. return 0
  22. fi
  23. return 1
  24. }
  25. sudo-askpass() {
  26. if [ -z "${sudo-askpass-x}" ]; then
  27. sudo --askpass "$@"
  28. else
  29. sudo "$@"
  30. fi
  31. }
  32. # After using homebrew to install docker, we need to do some magic to remove the need to interact with the GUI
  33. # See: https://github.com/docker/for-mac/issues/2359#issuecomment-607154849 for why we need to do things below
  34. init-docker() {
  35. # Need to start docker if it was freshly installed (docker server is not running)
  36. if query-mac && ! require docker && [ -d "/Applications/Docker.app" ]; then
  37. echo "Making some changes to complete Docker initialization"
  38. # allow the app to run without confirmation
  39. xattr -d -r com.apple.quarantine /Applications/Docker.app
  40. # preemptively do docker.app's setup to avoid any gui prompts
  41. sudo-askpass /bin/mkdir -p /Library/PrivilegedHelperTools
  42. sudo-askpass /bin/chmod 754 /Library/PrivilegedHelperTools
  43. sudo-askpass /bin/cp /Applications/Docker.app/Contents/Library/LaunchServices/com.docker.vmnetd /Library/PrivilegedHelperTools/
  44. sudo-askpass /bin/cp /Applications/Docker.app/Contents/Resources/com.docker.vmnetd.plist /Library/LaunchDaemons/
  45. sudo-askpass /bin/chmod 544 /Library/PrivilegedHelperTools/com.docker.vmnetd
  46. sudo-askpass /bin/chmod 644 /Library/LaunchDaemons/com.docker.vmnetd.plist
  47. sudo-askpass /bin/launchctl load /Library/LaunchDaemons/com.docker.vmnetd.plist
  48. fi
  49. start-docker
  50. }
  51. # This is mainly to be used by CI
  52. # We need this for Mac since the executable docker won't work properly
  53. # until the app is opened once
  54. start-docker() {
  55. if query-mac && ! docker system info &>/dev/null; then
  56. echo "About to open Docker.app"
  57. # At a later stage in the script, we're going to execute
  58. # ensure_docker_server which waits for it to be ready
  59. open -g -a Docker.app
  60. fi
  61. }
  62. upgrade-pip() {
  63. # pip versions before 20.1 do not have `pip cache` as a command which is necessary for the CI
  64. pip install --no-cache-dir --upgrade "pip>=20.1"
  65. # The Python version installed via pyenv does not come with wheel pre-installed
  66. # Installing wheel will speed up installation of Python dependencies
  67. require wheel || pip install wheel
  68. }
  69. install-py-dev() {
  70. upgrade-pip
  71. # It places us within top src dir to be at the same path as setup.py
  72. # This helps when getsentry calls into this script
  73. cd "${HERE}/.." || exit
  74. echo "--> Installing Sentry (for development)"
  75. # In Big Sur, versions of pip before 20.3 require SYSTEM_VERSION_COMPAT set
  76. if query_big_sur && python -c 'from sys import exit; import pip; from pip._vendor.packaging.version import parse; exit(1 if parse(pip.__version__) < parse("20.3") else 0)'; then
  77. SENTRY_LIGHT_BUILD=1 SYSTEM_VERSION_COMPAT=1 pip install -e '.[dev]'
  78. else
  79. # SENTRY_LIGHT_BUILD=1 disables webpacking during setup.py.
  80. # Webpacked assets are only necessary for devserver (which does it lazily anyways)
  81. # and acceptance tests, which webpack automatically if run.
  82. SENTRY_LIGHT_BUILD=1 pip install -e '.[dev]'
  83. fi
  84. }
  85. setup-git-config() {
  86. git config --local branch.autosetuprebase always
  87. git config --local core.ignorecase false
  88. git config --local blame.ignoreRevsFile .git-blame-ignore-revs
  89. }
  90. setup-git() {
  91. setup-git-config
  92. echo "--> Installing git hooks"
  93. mkdir -p .git/hooks && cd .git/hooks && ln -sf ../../config/hooks/* ./ && cd - || exit
  94. # shellcheck disable=SC2016
  95. python3 -c '' || (echo 'Please run `make setup-pyenv` to install the required Python 3 version.'; exit 1)
  96. pip install -r requirements-pre-commit.txt
  97. pre-commit install --install-hooks
  98. echo ""
  99. }
  100. node-version-check() {
  101. # Checks to see if node's version matches the one specified in package.json for Volta.
  102. node -pe "process.exit(Number(!(process.version == 'v' + require('./package.json').volta.node )))" || \
  103. (echo 'Unexpected node version. Recommended to use https://github.com/volta-cli/volta'; exit 1)
  104. }
  105. install-js-dev() {
  106. node-version-check
  107. echo "--> Installing Yarn packages (for development)"
  108. # Use NODE_ENV=development so that yarn installs both dependencies + devDependencies
  109. NODE_ENV=development yarn install --frozen-lockfile
  110. # A common problem is with node packages not existing in `node_modules` even though `yarn install`
  111. # says everything is up to date. Even though `yarn install` is run already, it doesn't take into
  112. # account the state of the current filesystem (it only checks .yarn-integrity).
  113. # Add an additional check against `node_modules`
  114. yarn check --verify-tree || yarn install --check-files
  115. }
  116. develop() {
  117. setup-git
  118. install-js-dev
  119. install-py-dev
  120. }
  121. init-config() {
  122. sentry init --dev
  123. }
  124. run-dependent-services() {
  125. sentry devservices up
  126. }
  127. create-db() {
  128. # shellcheck disable=SC2155
  129. local CREATEDB=$(command -v createdb 2> /dev/null)
  130. if [[ -z "$CREATEDB" ]]; then
  131. CREATEDB="docker exec sentry_postgres createdb"
  132. fi
  133. echo "--> Creating 'sentry' database"
  134. ${CREATEDB} -h 127.0.0.1 -U postgres -E utf-8 sentry || true
  135. }
  136. apply-migrations() {
  137. echo "--> Applying migrations"
  138. sentry upgrade --noinput
  139. }
  140. create-user() {
  141. if [[ -n "$GITHUB_ACTIONS" ]]; then
  142. sentry createuser --superuser --email foo@tbd.com --no-password
  143. else
  144. sentry createuser --superuser
  145. fi
  146. }
  147. build-platform-assets() {
  148. echo "--> Building platform assets"
  149. echo "from sentry.utils.integrationdocs import sync_docs; sync_docs(quiet=True)" | sentry exec
  150. }
  151. bootstrap() {
  152. develop
  153. init-config
  154. run-dependent-services
  155. create-db
  156. apply-migrations
  157. create-user
  158. build-platform-assets
  159. }
  160. clean() {
  161. echo "--> Cleaning static cache"
  162. rm -rf dist/* static/dist/*
  163. echo "--> Cleaning integration docs cache"
  164. rm -rf src/sentry/integration-docs
  165. echo "--> Cleaning pyc files"
  166. find . -name "*.pyc" -delete
  167. echo "--> Cleaning python build artifacts"
  168. rm -rf build/ dist/ src/sentry/assets.json
  169. echo ""
  170. }
  171. drop-db() {
  172. # shellcheck disable=SC2155
  173. local DROPDB=$(command -v dropdb 2> /dev/null)
  174. if [[ -z "$DROPDB" ]]; then
  175. DROPDB="docker exec sentry_postgres dropdb"
  176. fi
  177. echo "--> Dropping existing 'sentry' database"
  178. ${DROPDB} -h 127.0.0.1 -U postgres sentry || true
  179. }
  180. reset-db() {
  181. drop-db
  182. create-db
  183. apply-migrations
  184. }