Skip to content

Commit ba67058

Browse files
authored
feat(upgrade): Add automatic Snuba migration for upgrades (getsentry#292)
Upgrades existing events from last `$SENTRY_EVENT_RETENTION_DAYS` to Snuba automatically. Relies on getsentry/sentry#15934.
1 parent 3c9e8c2 commit ba67058

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

install.sh

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cleanup () {
1616
return 0;
1717
fi
1818
echo "Cleaning up..."
19-
docker-compose down &> /dev/null
19+
docker-compose stop &> /dev/null
2020
DID_CLEAN_UP=1
2121
}
2222
trap cleanup ERR INT TERM
@@ -55,30 +55,8 @@ if [ "$RAM_AVAILABLE_IN_DOCKER" -lt "$MIN_RAM" ]; then
5555
exit -1
5656
fi
5757

58-
# Very naively check whether there's an existing sentry-postgres volume and the PG version in it
59-
if [[ $(docker volume ls -q --filter name=sentry-postgres) && $(docker run --rm -v sentry-postgres:/db busybox cat /db/PG_VERSION 2>/dev/null) == "9.5" ]]; then
60-
# If this is Postgres 9.5 data, start upgrading it to 9.6 in a new volume
61-
docker run --rm \
62-
-v sentry-postgres:/var/lib/postgresql/9.5/data \
63-
-v sentry-postgres-new:/var/lib/postgresql/9.6/data \
64-
tianon/postgres-upgrade:9.5-to-9.6
65-
66-
# Get rid of the old volume as we'll rename the new one to that
67-
docker volume rm sentry-postgres
68-
docker volume create --name sentry-postgres
69-
# There's no rename volume in Docker so copy the contents from old to new name
70-
# Also append the `host all all all trust` line as `tianon/postgres-upgrade:9.5-to-9.6`
71-
# doesn't do that automatically.
72-
docker run --rm -v sentry-postgres-new:/from -v sentry-postgres:/to alpine ash -c \
73-
"cd /from ; cp -av . /to ; echo 'host all all all trust' >> /to/pg_hba.conf"
74-
# Finally, remove the new old volume as we are all in sentry-postgres now
75-
docker volume rm sentry-postgres-new
76-
fi
77-
78-
echo ""
79-
ensure_file_from_example $SENTRY_CONFIG_PY
80-
ensure_file_from_example $SENTRY_CONFIG_YML
81-
ensure_file_from_example $SENTRY_EXTRA_REQUIREMENTS
58+
# Ensure nothing is working while we install/update
59+
docker-compose stop
8260

8361
echo ""
8462
echo "Creating volumes for persistent storage..."
@@ -90,6 +68,11 @@ echo "Created $(docker volume create --name=sentry-kafka)."
9068
echo "Created $(docker volume create --name=sentry-clickhouse)."
9169
echo "Created $(docker volume create --name=sentry-symbolicator)."
9270

71+
echo ""
72+
ensure_file_from_example $SENTRY_CONFIG_PY
73+
ensure_file_from_example $SENTRY_CONFIG_YML
74+
ensure_file_from_example $SENTRY_EXTRA_REQUIREMENTS
75+
9376
echo ""
9477
echo "Generating secret key..."
9578
# This is to escape the secret key to be used in sed below
@@ -106,6 +89,26 @@ docker-compose build --force-rm
10689
echo ""
10790
echo "Docker images built."
10891

92+
# Very naively check whether there's an existing sentry-postgres volume and the PG version in it
93+
if [[ $(docker volume ls -q --filter name=sentry-postgres) && $(docker run --rm -v sentry-postgres:/db busybox cat /db/PG_VERSION 2>/dev/null) == "9.5" ]]; then
94+
# If this is Postgres 9.5 data, start upgrading it to 9.6 in a new volume
95+
docker run --rm \
96+
-v sentry-postgres:/var/lib/postgresql/9.5/data \
97+
-v sentry-postgres-new:/var/lib/postgresql/9.6/data \
98+
tianon/postgres-upgrade:9.5-to-9.6
99+
100+
# Get rid of the old volume as we'll rename the new one to that
101+
docker volume rm sentry-postgres
102+
docker volume create --name sentry-postgres
103+
# There's no rename volume in Docker so copy the contents from old to new name
104+
# Also append the `host all all all trust` line as `tianon/postgres-upgrade:9.5-to-9.6`
105+
# doesn't do that automatically.
106+
docker run --rm -v sentry-postgres-new:/from -v sentry-postgres:/to alpine ash -c \
107+
"cd /from ; cp -av . /to ; echo 'host all all all trust' >> /to/pg_hba.conf"
108+
# Finally, remove the new old volume as we are all in sentry-postgres now
109+
docker volume rm sentry-postgres-new
110+
fi
111+
109112
echo ""
110113
echo "Setting up database..."
111114
if [ $CI ]; then
@@ -120,6 +123,13 @@ else
120123
docker-compose run --rm web upgrade
121124
fi
122125

126+
SENTRY_DATA_NEEDS_MIGRATION=$(docker run --rm -v sentry-data:/data alpine ash -c "[ ! -d '/data/files' ] && ls -A1x /data | wc -l")
127+
if [ "$SENTRY_DATA_NEEDS_MIGRATION" ]; then
128+
echo "Migrating file storage..."
129+
docker run --rm -v sentry-data:/data alpine ash -c \
130+
"mkdir -p /tmp/files; mv /data/* /tmp/files/; mv /tmp/files /data/files"
131+
fi
132+
123133
echo "Boostrapping Snuba..."
124134
docker-compose up -d kafka redis clickhouse
125135
until $(docker-compose run --rm clickhouse clickhouse-client -h clickhouse --query="SHOW TABLES;" | grep -q sentry_local); do
@@ -130,12 +140,12 @@ until $(docker-compose run --rm clickhouse clickhouse-client -h clickhouse --que
130140
done;
131141
echo ""
132142

133-
SENTRY_DATA_NEEDS_MIGRATION=$(docker run --rm -v sentry-data:/data alpine ash -c "[ ! -d '/data/files' ] && ls -A1x /data | wc -l")
134-
if [ "$SENTRY_DATA_NEEDS_MIGRATION" ]; then
135-
echo "Migrating file storage..."
136-
docker run --rm -v sentry-data:/data alpine ash -c \
137-
"mkdir -p /tmp/files; mv /data/* /tmp/files/; mv /tmp/files /data/files"
138-
fi
143+
set -o allexport
144+
source .env
145+
set +o allexport
146+
echo "Migrating old events for the last $SENTRY_EVENT_RETENTION_DAYS days..."
147+
docker-compose run --rm web django backfill_eventstream --no-input --last-days $SENTRY_EVENT_RETENTION_DAYS
148+
echo ""
139149

140150
cleanup
141151

0 commit comments

Comments
 (0)