Skip to content

Commit 7e7401a

Browse files
authored
Refactor most of the rest (#903)
1 parent f086157 commit 7e7401a

11 files changed

+176
-163
lines changed

install.sh

Lines changed: 12 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ MIN_RAM_HARD=3800 # MB
1919
MIN_RAM_SOFT=7800 # MB
2020
MIN_CPU_HARD=2
2121
MIN_CPU_SOFT=4
22-
23-
# Increase the default 10 second SIGTERM timeout
24-
# to ensure celery queues are properly drained
25-
# between upgrades as task signatures may change across
26-
# versions
27-
STOP_TIMEOUT=60 # seconds
28-
SENTRY_CONFIG_PY='sentry/sentry.conf.py'
29-
SENTRY_CONFIG_YML='sentry/config.yml'
30-
SYMBOLICATOR_CONFIG_YML='symbolicator/config.yml'
31-
SENTRY_EXTRA_REQUIREMENTS='sentry/requirements.txt'
32-
MINIMIZE_DOWNTIME=
3322
echo $_endgroup
3423

3524
echo "${_group}Parsing command line ..."
@@ -48,6 +37,9 @@ Options:
4837
EOF
4938
}
5039

40+
SKIP_USER_PROMPT="${SKIP_USER_PROMPT:-}"
41+
MINIMIZE_DOWNTIME="${MINIMIZE_DOWNTIME:-}"
42+
5143
while (( $# )); do
5244
case "$1" in
5345
-h | --help) show_help; exit;;
@@ -140,131 +132,15 @@ fi
140132
echo "${_endgroup}"
141133

142134
source ./install/create-docker-volumes.sh
143-
144-
echo "${_group}Ensuring files from examples ..."
145-
ensure_file_from_example $SENTRY_CONFIG_PY
146-
ensure_file_from_example $SENTRY_CONFIG_YML
147-
ensure_file_from_example $SENTRY_EXTRA_REQUIREMENTS
148-
ensure_file_from_example $SYMBOLICATOR_CONFIG_YML
149-
echo "${_endgroup}"
150-
151-
echo "${_group}Generating secret key ..."
152-
if grep -xq "system.secret-key: '!!changeme!!'" $SENTRY_CONFIG_YML ; then
153-
# This is to escape the secret key to be used in sed below
154-
# Note the need to set LC_ALL=C due to BSD tr and sed always trying to decode
155-
# whatever is passed to them. Kudos to https://stackoverflow.com/a/23584470/90297
156-
SECRET_KEY=$(export LC_ALL=C; head /dev/urandom | tr -dc "a-z0-9@#%^&*(-_=+)" | head -c 50 | sed -e 's/[\/&]/\\&/g')
157-
sed -i -e 's/^system.secret-key:.*$/system.secret-key: '"'$SECRET_KEY'"'/' $SENTRY_CONFIG_YML
158-
echo "Secret key written to $SENTRY_CONFIG_YML"
159-
fi
160-
echo "${_endgroup}"
161-
162-
echo "${_group}Replacing TSDB ..."
163-
replace_tsdb() {
164-
if (
165-
[[ -f "$SENTRY_CONFIG_PY" ]] &&
166-
! grep -xq 'SENTRY_TSDB = "sentry.tsdb.redissnuba.RedisSnubaTSDB"' "$SENTRY_CONFIG_PY"
167-
); then
168-
# Do NOT indent the following string as it would be reflected in the end result,
169-
# breaking the final config file. See getsentry/onpremise#624.
170-
tsdb_settings="\
171-
SENTRY_TSDB = \"sentry.tsdb.redissnuba.RedisSnubaTSDB\"
172-
173-
# Automatic switchover 90 days after $(date). Can be removed afterwards.
174-
SENTRY_TSDB_OPTIONS = {\"switchover_timestamp\": $(date +%s) + (90 * 24 * 3600)}\
175-
"
176-
177-
if grep -q 'SENTRY_TSDB_OPTIONS = ' "$SENTRY_CONFIG_PY"; then
178-
echo "Not attempting automatic TSDB migration due to presence of SENTRY_TSDB_OPTIONS"
179-
else
180-
echo "Attempting to automatically migrate to new TSDB"
181-
# Escape newlines for sed
182-
tsdb_settings="${tsdb_settings//$'\n'/\\n}"
183-
cp "$SENTRY_CONFIG_PY" "$SENTRY_CONFIG_PY.bak"
184-
sed -i -e "s/^SENTRY_TSDB = .*$/${tsdb_settings}/g" "$SENTRY_CONFIG_PY" || true
185-
186-
if grep -xq 'SENTRY_TSDB = "sentry.tsdb.redissnuba.RedisSnubaTSDB"' "$SENTRY_CONFIG_PY"; then
187-
echo "Migrated TSDB to Snuba. Old configuration file backed up to $SENTRY_CONFIG_PY.bak"
188-
return
189-
fi
190-
191-
echo "Failed to automatically migrate TSDB. Reverting..."
192-
mv "$SENTRY_CONFIG_PY.bak" "$SENTRY_CONFIG_PY"
193-
echo "$SENTRY_CONFIG_PY restored from backup."
194-
fi
195-
196-
echo "WARN: Your Sentry configuration uses a legacy data store for time-series data. Remove the options SENTRY_TSDB and SENTRY_TSDB_OPTIONS from $SENTRY_CONFIG_PY and add:"
197-
echo ""
198-
echo "$tsdb_settings"
199-
echo ""
200-
echo "For more information please refer to https://github.com/getsentry/onpremise/pull/430"
201-
fi
202-
}
203-
204-
replace_tsdb
205-
echo "${_endgroup}"
206-
207-
echo "${_group}Fetching and updating Docker images ..."
208-
# We tag locally built images with an '-onpremise-local' suffix. docker-compose pull tries to pull these too and
209-
# shows a 404 error on the console which is confusing and unnecessary. To overcome this, we add the stderr>stdout
210-
# redirection below and pass it through grep, ignoring all lines having this '-onpremise-local' suffix.
211-
$dc pull -q --ignore-pull-failures 2>&1 | grep -v -- -onpremise-local || true
212-
213-
# We may not have the set image on the repo (local images) so allow fails
214-
docker pull ${SENTRY_IMAGE} || true;
215-
echo "${_endgroup}"
216-
217-
echo "${_group}Building and tagging Docker images ..."
218-
echo ""
219-
$dc build --force-rm
220-
echo ""
221-
echo "Docker images built."
222-
echo "${_endgroup}"
223-
224-
echo "${_group}Turning things off ..."
225-
if [[ -n "$MINIMIZE_DOWNTIME" ]]; then
226-
# Stop everything but relay and nginx
227-
$dc rm -fsv $($dc config --services | grep -v -E '^(nginx|relay)$')
228-
else
229-
# Clean up old stuff and ensure nothing is working while we install/update
230-
# This is for older versions of on-premise:
231-
$dc -p onpremise down -t $STOP_TIMEOUT --rmi local --remove-orphans
232-
# This is for newer versions
233-
$dc down -t $STOP_TIMEOUT --rmi local --remove-orphans
234-
fi
235-
echo "${_endgroup}"
236-
237-
echo "${_group}Setting up Zookeeper ..."
238-
ZOOKEEPER_SNAPSHOT_FOLDER_EXISTS=$($dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/data/version-2 | wc -l | tr -d '[:space:]'')
239-
if [[ "$ZOOKEEPER_SNAPSHOT_FOLDER_EXISTS" -eq 1 ]]; then
240-
ZOOKEEPER_LOG_FILE_COUNT=$($dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/log/version-2/* | wc -l | tr -d '[:space:]'')
241-
ZOOKEEPER_SNAPSHOT_FILE_COUNT=$($dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/data/version-2/* | wc -l | tr -d '[:space:]'')
242-
# This is a workaround for a ZK upgrade bug: https://issues.apache.org/jira/browse/ZOOKEEPER-3056
243-
if [[ "$ZOOKEEPER_LOG_FILE_COUNT" -gt 0 ]] && [[ "$ZOOKEEPER_SNAPSHOT_FILE_COUNT" -eq 0 ]]; then
244-
$dcr -v $(pwd)/zookeeper:/temp zookeeper bash -c 'cp /temp/snapshot.0 /var/lib/zookeeper/data/version-2/snapshot.0'
245-
$dc run -d -e ZOOKEEPER_SNAPSHOT_TRUST_EMPTY=true zookeeper
246-
fi
247-
fi
248-
echo "${_endgroup}"
249-
250-
echo "${_group}Bootstrapping and migrating Snuba ..."
251-
$dcr snuba-api bootstrap --no-migrate --force
252-
$dcr snuba-api migrations migrate --force
253-
echo "${_endgroup}"
254-
255-
echo "${_group}Creating additional Kafka topics ..."
256-
# NOTE: This step relies on `kafka` being available from the previous `snuba-api bootstrap` step
257-
# XXX(BYK): We cannot use auto.create.topics as Confluence and Apache hates it now (and makes it very hard to enable)
258-
EXISTING_KAFKA_TOPICS=$($dcr kafka kafka-topics --list --bootstrap-server kafka:9092 2>/dev/null)
259-
NEEDED_KAFKA_TOPICS="ingest-attachments ingest-transactions ingest-events"
260-
for topic in $NEEDED_KAFKA_TOPICS; do
261-
if ! echo "$EXISTING_KAFKA_TOPICS" | grep -wq $topic; then
262-
$dcr kafka kafka-topics --create --topic $topic --bootstrap-server kafka:9092
263-
echo ""
264-
fi
265-
done
266-
echo "${_endgroup}"
267-
135+
source ./install/ensure-files-from-examples.sh
136+
source ./install/generate-secret-key.sh
137+
source ./install/replace-tsdb.sh
138+
source ./install/update-docker-images.sh
139+
source ./install/build-docker-images.sh
140+
source ./install/turn-things-off.sh
141+
source ./install/set-up-zookeeper.sh
142+
source ./install/bootstrap-snuba.sh
143+
source ./install/create-kafka-topics.sh
268144
source ./install/upgrade-postgres.sh
269145
source ./install/set-up-and-migrate-database.sh
270146
source ./install/migrate-file-storage.sh

install/_lib.sh

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,36 @@ if [[ ! -d 'install' ]]; then echo 'Where are you?'; exit 1; fi
1111

1212
_ENV="$(realpath .env)"
1313

14-
define_stuff() {
15-
# Read .env for default values with a tip o' the hat to https://stackoverflow.com/a/59831605/90297
16-
t=$(mktemp) && export -p > "$t" && set -a && . $_ENV && set +a && . "$t" && rm "$t" && unset t
14+
# Read .env for default values with a tip o' the hat to https://stackoverflow.com/a/59831605/90297
15+
t=$(mktemp) && export -p > "$t" && set -a && . $_ENV && set +a && . "$t" && rm "$t" && unset t
1716

18-
if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
19-
_group="::group::"
20-
_endgroup="::endgroup::"
21-
else
22-
_group=""
23-
_endgroup=""
24-
fi
25-
26-
dc="docker-compose --no-ansi"
27-
dcr="$dc run --rm"
17+
if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
18+
_group="::group::"
19+
_endgroup="::endgroup::"
20+
else
21+
_group=""
22+
_endgroup=""
23+
fi
2824

29-
function ensure_file_from_example {
30-
if [[ -f "$1" ]]; then
31-
echo "$1 already exists, skipped creation."
32-
else
33-
echo "Creating $1..."
34-
cp -n $(echo "$1" | sed 's/\.[^.]*$/.example&/') "$1"
35-
# sed from https://stackoverflow.com/a/25123013/90297
36-
fi
37-
}
25+
dc="docker-compose --no-ansi"
26+
dcr="$dc run --rm"
3827

39-
stuff_defined="yes"
28+
# A couple of the config files are referenced from other subscripts, so they
29+
# get vars, while multiple subscripts call ensure_file_from_example.
30+
function ensure_file_from_example {
31+
if [[ -f "$1" ]]; then
32+
echo "$1 already exists, skipped creation."
33+
else
34+
echo "Creating $1..."
35+
cp -n $(echo "$1" | sed 's/\.[^.]*$/.example&/') "$1"
36+
# sed from https://stackoverflow.com/a/25123013/90297
37+
fi
4038
}
41-
42-
if [ "${stuff_defined:-''}" != "" ]; then
43-
define_stuff
44-
fi
39+
SENTRY_CONFIG_PY='sentry/sentry.conf.py'
40+
SENTRY_CONFIG_YML='sentry/config.yml'
41+
42+
# Increase the default 10 second SIGTERM timeout
43+
# to ensure celery queues are properly drained
44+
# between upgrades as task signatures may change across
45+
# versions
46+
STOP_TIMEOUT=60 # seconds

install/bootstrap-snuba.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
echo "${_group}Bootstrapping and migrating Snuba ..."
2+
3+
$dcr snuba-api bootstrap --no-migrate --force
4+
$dcr snuba-api migrations migrate --force
5+
6+
echo "${_endgroup}"

install/build-docker-images.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
echo "${_group}Building and tagging Docker images ..."
2+
3+
echo ""
4+
$dc build --force-rm
5+
echo ""
6+
echo "Docker images built."
7+
8+
echo "${_endgroup}"

install/create-kafka-topics.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
echo "${_group}Creating additional Kafka topics ..."
2+
3+
# NOTE: This step relies on `kafka` being available from the previous `snuba-api bootstrap` step
4+
# XXX(BYK): We cannot use auto.create.topics as Confluence and Apache hates it now (and makes it very hard to enable)
5+
EXISTING_KAFKA_TOPICS=$($dcr kafka kafka-topics --list --bootstrap-server kafka:9092 2>/dev/null)
6+
NEEDED_KAFKA_TOPICS="ingest-attachments ingest-transactions ingest-events"
7+
for topic in $NEEDED_KAFKA_TOPICS; do
8+
if ! echo "$EXISTING_KAFKA_TOPICS" | grep -wq $topic; then
9+
$dcr kafka kafka-topics --create --topic $topic --bootstrap-server kafka:9092
10+
echo ""
11+
fi
12+
done
13+
14+
echo "${_endgroup}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
echo "${_group}Ensuring files from examples ..."
2+
3+
ensure_file_from_example $SENTRY_CONFIG_PY
4+
ensure_file_from_example $SENTRY_CONFIG_YML
5+
ensure_file_from_example 'symbolicator/config.yml'
6+
ensure_file_from_example 'sentry/requirements.txt'
7+
8+
echo "${_endgroup}"

install/generate-secret-key.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
echo "${_group}Generating secret key ..."
2+
3+
if grep -xq "system.secret-key: '!!changeme!!'" $SENTRY_CONFIG_YML ; then
4+
# This is to escape the secret key to be used in sed below
5+
# Note the need to set LC_ALL=C due to BSD tr and sed always trying to decode
6+
# whatever is passed to them. Kudos to https://stackoverflow.com/a/23584470/90297
7+
SECRET_KEY=$(export LC_ALL=C; head /dev/urandom | tr -dc "a-z0-9@#%^&*(-_=+)" | head -c 50 | sed -e 's/[\/&]/\\&/g')
8+
sed -i -e 's/^system.secret-key:.*$/system.secret-key: '"'$SECRET_KEY'"'/' $SENTRY_CONFIG_YML
9+
echo "Secret key written to $SENTRY_CONFIG_YML"
10+
fi
11+
12+
echo "${_endgroup}"

install/replace-tsdb.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
echo "${_group}Replacing TSDB ..."
2+
3+
replace_tsdb() {
4+
if (
5+
[[ -f "$SENTRY_CONFIG_PY" ]] &&
6+
! grep -xq 'SENTRY_TSDB = "sentry.tsdb.redissnuba.RedisSnubaTSDB"' "$SENTRY_CONFIG_PY"
7+
); then
8+
# Do NOT indent the following string as it would be reflected in the end result,
9+
# breaking the final config file. See getsentry/onpremise#624.
10+
tsdb_settings="\
11+
SENTRY_TSDB = \"sentry.tsdb.redissnuba.RedisSnubaTSDB\"
12+
13+
# Automatic switchover 90 days after $(date). Can be removed afterwards.
14+
SENTRY_TSDB_OPTIONS = {\"switchover_timestamp\": $(date +%s) + (90 * 24 * 3600)}\
15+
"
16+
17+
if grep -q 'SENTRY_TSDB_OPTIONS = ' "$SENTRY_CONFIG_PY"; then
18+
echo "Not attempting automatic TSDB migration due to presence of SENTRY_TSDB_OPTIONS"
19+
else
20+
echo "Attempting to automatically migrate to new TSDB"
21+
# Escape newlines for sed
22+
tsdb_settings="${tsdb_settings//$'\n'/\\n}"
23+
cp "$SENTRY_CONFIG_PY" "$SENTRY_CONFIG_PY.bak"
24+
sed -i -e "s/^SENTRY_TSDB = .*$/${tsdb_settings}/g" "$SENTRY_CONFIG_PY" || true
25+
26+
if grep -xq 'SENTRY_TSDB = "sentry.tsdb.redissnuba.RedisSnubaTSDB"' "$SENTRY_CONFIG_PY"; then
27+
echo "Migrated TSDB to Snuba. Old configuration file backed up to $SENTRY_CONFIG_PY.bak"
28+
return
29+
fi
30+
31+
echo "Failed to automatically migrate TSDB. Reverting..."
32+
mv "$SENTRY_CONFIG_PY.bak" "$SENTRY_CONFIG_PY"
33+
echo "$SENTRY_CONFIG_PY restored from backup."
34+
fi
35+
36+
echo "WARN: Your Sentry configuration uses a legacy data store for time-series data. Remove the options SENTRY_TSDB and SENTRY_TSDB_OPTIONS from $SENTRY_CONFIG_PY and add:"
37+
echo ""
38+
echo "$tsdb_settings"
39+
echo ""
40+
echo "For more information please refer to https://github.com/getsentry/onpremise/pull/430"
41+
fi
42+
}
43+
44+
replace_tsdb
45+
46+
echo "${_endgroup}"

install/set-up-zookeeper.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
echo "${_group}Setting up Zookeeper ..."
2+
3+
ZOOKEEPER_SNAPSHOT_FOLDER_EXISTS=$($dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/data/version-2 | wc -l | tr -d '[:space:]'')
4+
if [[ "$ZOOKEEPER_SNAPSHOT_FOLDER_EXISTS" -eq 1 ]]; then
5+
ZOOKEEPER_LOG_FILE_COUNT=$($dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/log/version-2/* | wc -l | tr -d '[:space:]'')
6+
ZOOKEEPER_SNAPSHOT_FILE_COUNT=$($dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/data/version-2/* | wc -l | tr -d '[:space:]'')
7+
# This is a workaround for a ZK upgrade bug: https://issues.apache.org/jira/browse/ZOOKEEPER-3056
8+
if [[ "$ZOOKEEPER_LOG_FILE_COUNT" -gt 0 ]] && [[ "$ZOOKEEPER_SNAPSHOT_FILE_COUNT" -eq 0 ]]; then
9+
$dcr -v $(pwd)/zookeeper:/temp zookeeper bash -c 'cp /temp/snapshot.0 /var/lib/zookeeper/data/version-2/snapshot.0'
10+
$dc run -d -e ZOOKEEPER_SNAPSHOT_TRUST_EMPTY=true zookeeper
11+
fi
12+
fi
13+
14+
echo "${_endgroup}"

install/turn-things-off.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
echo "${_group}Turning things off ..."
2+
3+
if [[ -n "$MINIMIZE_DOWNTIME" ]]; then
4+
# Stop everything but relay and nginx
5+
$dc rm -fsv $($dc config --services | grep -v -E '^(nginx|relay)$')
6+
else
7+
# Clean up old stuff and ensure nothing is working while we install/update
8+
# This is for older versions of on-premise:
9+
$dc -p onpremise down -t $STOP_TIMEOUT --rmi local --remove-orphans
10+
# This is for newer versions
11+
$dc down -t $STOP_TIMEOUT --rmi local --remove-orphans
12+
fi
13+
14+
echo "${_endgroup}"

0 commit comments

Comments
 (0)