lib.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 or updated
  36. # You will know that Docker is ready for devservices when the icon on the menu bar stops flashing
  37. if query-mac && ! require docker && [ -d "/Applications/Docker.app" ]; then
  38. echo "Making some changes to complete Docker initialization"
  39. # allow the app to run without confirmation
  40. xattr -d -r com.apple.quarantine /Applications/Docker.app
  41. # preemptively do docker.app's setup to avoid any gui prompts
  42. # This path is not available for brand new MacBooks
  43. sudo-askpass /bin/mkdir -p /Library/PrivilegedHelperTools
  44. sudo-askpass /bin/chmod 754 /Library/PrivilegedHelperTools
  45. sudo-askpass /bin/cp /Applications/Docker.app/Contents/Library/LaunchServices/com.docker.vmnetd /Library/PrivilegedHelperTools/
  46. sudo-askpass /bin/chmod 544 /Library/PrivilegedHelperTools/com.docker.vmnetd
  47. # This file used to be generated as part of brew's installation
  48. if [ -f /Applications/Docker.app/Contents/Resources/com.docker.vmnetd.plist ]; then
  49. sudo-askpass /bin/cp /Applications/Docker.app/Contents/Resources/com.docker.vmnetd.plist /Library/LaunchDaemons/
  50. else
  51. sudo-askpass /bin/cp .github/workflows/files/com.docker.vmnetd.plist /Library/LaunchDaemons/
  52. fi
  53. sudo-askpass /bin/chmod 644 /Library/LaunchDaemons/com.docker.vmnetd.plist
  54. sudo-askpass /bin/launchctl load /Library/LaunchDaemons/com.docker.vmnetd.plist
  55. fi
  56. start-docker
  57. }
  58. # This is mainly to be used by CI
  59. # We need this for Mac since the executable docker won't work properly
  60. # until the app is opened once
  61. start-docker() {
  62. if query-mac && ! docker system info &>/dev/null; then
  63. echo "About to open Docker.app"
  64. # At a later stage in the script, we're going to execute
  65. # ensure_docker_server which waits for it to be ready
  66. if ! open -g -a Docker.app; then
  67. # If the step above fails, at least we can get some debugging information to determine why
  68. sudo-askpass ls -l /Library/PrivilegedHelperTools/com.docker.vmnetd
  69. ls -l /Library/LaunchDaemons/
  70. cat /Library/LaunchDaemons/com.docker.vmnetd.plist
  71. ls -l /Applications/Docker.app
  72. fi
  73. fi
  74. }
  75. upgrade-pip() {
  76. pip install --upgrade "pip==21.1.2" "wheel==0.36.2"
  77. }
  78. install-py-dev() {
  79. upgrade-pip
  80. # It places us within top src dir to be at the same path as setup.py
  81. # This helps when getsentry calls into this script
  82. cd "${HERE}/.." || exit
  83. echo "--> Installing Sentry (for development)"
  84. # SENTRY_LIGHT_BUILD=1 disables webpacking during setup.py.
  85. # Webpacked assets are only necessary for devserver (which does it lazily anyways)
  86. # and acceptance tests, which webpack automatically if run.
  87. SENTRY_LIGHT_BUILD=1 pip install -e '.[dev]'
  88. }
  89. setup-git-config() {
  90. git config --local branch.autosetuprebase always
  91. git config --local core.ignorecase false
  92. git config --local blame.ignoreRevsFile .git-blame-ignore-revs
  93. }
  94. setup-git() {
  95. setup-git-config
  96. echo "--> Installing git hooks"
  97. mkdir -p .git/hooks && cd .git/hooks && ln -sf ../../config/hooks/* ./ && cd - || exit
  98. # shellcheck disable=SC2016
  99. python3 -c '' || (
  100. echo 'Please run `make setup-pyenv` to install the required Python 3 version.'
  101. exit 1
  102. )
  103. pip install -r requirements-pre-commit.txt
  104. pre-commit install --install-hooks
  105. echo ""
  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('./package.json').volta.node )))" ||
  110. (
  111. echo 'Unexpected node version. Recommended to use https://github.com/volta-cli/volta'
  112. exit 1
  113. )
  114. }
  115. install-js-dev() {
  116. node-version-check
  117. echo "--> Installing Yarn packages (for development)"
  118. # Use NODE_ENV=development so that yarn installs both dependencies + devDependencies
  119. NODE_ENV=development yarn install --frozen-lockfile
  120. # A common problem is with node packages not existing in `node_modules` even though `yarn install`
  121. # says everything is up to date. Even though `yarn install` is run already, it doesn't take into
  122. # account the state of the current filesystem (it only checks .yarn-integrity).
  123. # Add an additional check against `node_modules`
  124. yarn check --verify-tree || yarn install --check-files
  125. }
  126. develop() {
  127. setup-git
  128. install-js-dev
  129. install-py-dev
  130. }
  131. init-config() {
  132. sentry init --dev
  133. }
  134. run-dependent-services() {
  135. sentry devservices up
  136. }
  137. create-db() {
  138. # shellcheck disable=SC2155
  139. local CREATEDB=$(command -v createdb 2>/dev/null)
  140. if [[ -z "$CREATEDB" ]]; then
  141. CREATEDB="docker exec sentry_postgres createdb"
  142. fi
  143. echo "--> Creating 'sentry' database"
  144. ${CREATEDB} -h 127.0.0.1 -U postgres -E utf-8 sentry || true
  145. }
  146. apply-migrations() {
  147. echo "--> Applying migrations"
  148. sentry upgrade --noinput
  149. }
  150. create-user() {
  151. if [[ -n "${GITHUB_ACTIONS+x}" ]]; then
  152. sentry createuser --superuser --email foo@tbd.com --no-password
  153. else
  154. sentry createuser --superuser
  155. fi
  156. }
  157. build-platform-assets() {
  158. echo "--> Building platform assets"
  159. echo "from sentry.utils.integrationdocs import sync_docs; sync_docs(quiet=True)" | sentry exec
  160. }
  161. bootstrap() {
  162. develop
  163. init-config
  164. run-dependent-services
  165. create-db
  166. apply-migrations
  167. create-user
  168. build-platform-assets
  169. }
  170. clean() {
  171. echo "--> Cleaning static cache"
  172. rm -rf dist/* src/sentry/static/sentry/dist/*
  173. echo "--> Cleaning integration docs cache"
  174. rm -rf src/sentry/integration-docs
  175. echo "--> Cleaning pyc files"
  176. find . -name "*.pyc" -delete
  177. echo "--> Cleaning python build artifacts"
  178. rm -rf build/ dist/ src/sentry/assets.json
  179. echo ""
  180. }
  181. drop-db() {
  182. # shellcheck disable=SC2155
  183. local DROPDB=$(command -v dropdb 2>/dev/null)
  184. if [[ -z "$DROPDB" ]]; then
  185. DROPDB="docker exec sentry_postgres dropdb"
  186. fi
  187. echo "--> Dropping existing 'sentry' database"
  188. ${DROPDB} -h 127.0.0.1 -U postgres sentry || true
  189. }
  190. reset-db() {
  191. drop-db
  192. create-db
  193. apply-migrations
  194. }
  195. direnv-help() {
  196. cat >&2 <<EOF
  197. If you're a Sentry employee and you're stuck or have questions, ask in #discuss-dev-tooling.
  198. If you're not, please file an issue under https://github.com/getsentry/sentry/issues/new/choose and mention @getsentry/owners-sentry-dev
  199. You can configure the behaviour of direnv by adding the following variables to a .env file:
  200. - SENTRY_DIRENV_DEBUG=1: This will allow printing debug messages
  201. - SENTRY_DEVENV_NO_REPORT=1: Do not report development environment errors to Sentry.io
  202. EOF
  203. }