Skip to content

Commit 854d366

Browse files
author
josh
authored
meta(devtools): initial direnv configuration (#16785)
1 parent 6854335 commit 854d366

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

.envrc

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# This is the .envrc for sentry, for use with direnv.
2+
# It's responsible for enforcing a standard dev environment by checking as much state as possible, and either performing
3+
# initialization (e.g. activating the venv) or giving recommendations on how to reach the desired state.
4+
# It also sets useful environment variables.
5+
# If you'd like to override or set any custom environment variables, this .envrc will read a .env file at the end.
6+
7+
set -e
8+
9+
bold="$(tput bold)"
10+
red="$(tput setaf 1)"
11+
green="$(tput setaf 2)"
12+
reset="$(tput sgr0)"
13+
14+
# XXX: we can't trap bash EXIT, because it'll override direnv's finalizing routines.
15+
# consequently, using "exit" anywhere will skip this notice from showing.
16+
# so need to use set -e, and return 1.
17+
trap notice ERR
18+
19+
notice () {
20+
[ $? -eq 0 ] && return
21+
cat <<EOF
22+
${red}${bold}direnv wasn't able to complete execution.
23+
You may have been given some recommendations in the error message.
24+
Follow them, and then you'll need to redo direnv by running "direnv allow".${reset}
25+
26+
direnv tooling is in an ALPHA state!
27+
If you're having trouble, or have questions, please ask in #discuss-dev-tooling
28+
and/or reach out to @josh.
29+
EOF
30+
}
31+
32+
require () {
33+
command -v "$1" 2>&1 > /dev/null
34+
}
35+
36+
info () {
37+
cat <<EOF
38+
${bold}direnv: ${1}${reset}
39+
EOF
40+
}
41+
42+
die () {
43+
>&2 cat <<EOF
44+
${red}${bold}direnv FATAL: ${1}
45+
${reset}
46+
EOF
47+
return 1
48+
}
49+
50+
init_venv () {
51+
deactivate 2>/dev/null || true
52+
info "Creating a virtualenv for you."
53+
require python2.7 || \
54+
die "You'll need to install python2.7, or make it available on your PATH.
55+
It's recommended to use pyenv - please refer to https://docs.sentry.io/development/contribute/environment"
56+
python2.7 -m virtualenv .venv
57+
}
58+
59+
### Environment ###
60+
61+
# don't write *.pyc files; using stale python code occasionally causes subtle problems
62+
export PYTHONDONTWRITEBYTECODE=1
63+
64+
# don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
65+
export PIP_DISABLE_PIP_VERSION_CHECK=on
66+
67+
# increase node's memory limit, required for our webpacking
68+
export NODE_OPTIONS=--max-old-space-size=4096
69+
70+
71+
### System ###
72+
73+
for pkg in \
74+
make \
75+
docker \
76+
chromedriver \
77+
pkg-config \
78+
openssl ;
79+
do
80+
if ! require "$pkg"; then
81+
die "You seem to be missing the system dependency: ${pkg}
82+
Please install homebrew, and run brew bundle."
83+
fi
84+
done
85+
86+
87+
### Python ###
88+
89+
info "Checking virtualenv..."
90+
91+
# direnv set -u's, so we need to do this.
92+
VIRTUAL_ENV="${VIRTUAL_ENV:-}"
93+
94+
if [ -n "$VIRTUAL_ENV" ]; then
95+
# we're enforcing that virtualenv be in .venv, since future tooling e.g. venv-update will rely on this.
96+
if [ "$VIRTUAL_ENV" != "${PWD}/.venv" ]; then
97+
info "You're in a virtualenv, but it's not in the expected location (${PWD}/.venv)"
98+
init_venv
99+
fi
100+
else
101+
if [ ! -f ".venv/bin/activate" ]; then
102+
info "You don't seem to have a virtualenv."
103+
init_venv
104+
fi
105+
fi
106+
107+
info "Activating virtualenv."
108+
source .venv/bin/activate
109+
[ "$(command -v python)" != "${PWD}/.venv/bin/python" ] && die "Failed to activate virtualenv."
110+
111+
python -c "import sys; sys.exit(sys.version_info[:2] != (2, 7))" || \
112+
die "For some reason, the virtualenv isn't Python 2.7."
113+
114+
if [ "$(command -v sentry)" != "${PWD}/.venv/bin/sentry" ]; then
115+
info "Your .venv is activated, but sentry doesn't seem to be installed. Let's install it."
116+
make ensure-pinned-pip
117+
SENTRY_LIGHT_BUILD=1 make install-sentry-dev
118+
fi
119+
120+
121+
### pre-commit ###
122+
123+
info "Checking pre-commit..."
124+
125+
# this is cheap, so we'll just do it every time
126+
ln -sf config/hooks/* .git/hooks
127+
128+
if ! require pre-commit; then
129+
info "Looks like you don't have pre-commit installed. Let's install it."
130+
make setup-git
131+
fi
132+
133+
# this hotfix is cheap too, so just run it every time
134+
rm -f .git/hooks/pre-commit.legacy
135+
136+
137+
### devservices ###
138+
139+
info "Checking devservices..."
140+
141+
# XXX: these container names are hardcoded for now
142+
# NOTE: sentry_symbolicator isn't started up by devservices, and is behind a config flag, so we're not checking for it
143+
for container in \
144+
sentry_postgres \
145+
sentry_clickhouse \
146+
sentry_snuba \
147+
sentry_redis ;
148+
do
149+
docker exec "$container" true || die "The docker container ${container} doesn't seem to be running.
150+
Please run sentry devservices up."
151+
done
152+
153+
154+
### database ###
155+
156+
info "Checking database..."
157+
158+
if ! docker exec sentry_postgres sh \
159+
-c "psql -U postgres -h 127.0.0.1 sentry --command 'select 1 from sentry_useremail'" >/dev/null;
160+
then
161+
die "It doesn't look like you have a database for sentry yet - you'll need to run 'make reset-db'."
162+
fi
163+
164+
165+
### Node ###
166+
167+
info "Checking node..."
168+
169+
node_version="10.16.3"
170+
171+
# It would be nice to enforce that node is installed via volta (and is therefore a shim that will check against
172+
# the node pin in package.json), but for now, let's just explicitly check the node version.
173+
174+
if ! require node; then
175+
die "You don't seem to have node installed. We want version ${node_version}.
176+
It's recommended to use volta - please refer to https://docs.sentry.io/development/contribute/environment"
177+
fi
178+
179+
if [ "$(node -v)" != "v${node_version}" ]; then
180+
die "Your node version doesn't match ${node_version}.
181+
It's recommended to use volta - please refer to https://docs.sentry.io/development/contribute/environment"
182+
fi
183+
184+
if [ ! -x "node_modules/.bin/webpack" ]; then
185+
info "You don't seem to have yarn packages installed. Let's install them."
186+
make install-yarn-pkgs
187+
fi
188+
189+
PATH_add node_modules/.bin
190+
191+
192+
### Overrides ###
193+
194+
if [ -f '.env' ]; then
195+
info ".env found. Reading it..."
196+
dotenv .env
197+
fi
198+
199+
cat <<EOF
200+
${green}${bold}direnv: SUCCESS!
201+
${reset}
202+
direnv tooling is in an ALPHA state!
203+
If you're having trouble, or have questions, please ask in #discuss-dev-tooling
204+
and/or reach out to @josh.
205+
206+
You can safely ignore the followiing "PS1 cannot be exported" message.
207+
208+
EOF

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
.cache/
23
.coverage
34
.storybook-out/

Brewfile

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ brew 'pkgconfig'
99
brew 'libxmlsec1'
1010
brew 'geoip'
1111

12+
brew 'direnv'
13+
1214
tap 'homebrew/cask'
1315

1416
# required for acceptance testing

0 commit comments

Comments
 (0)