2019-07-01 21:48:12 +02:00
#!/usr/bin/env bash
set -e
2020-01-03 11:17:35 +01:00
2020-07-17 14:29:55 +02:00
source <( grep -v '^#' .env | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g' )
2020-01-03 20:06:33 +01:00
dc = "docker-compose --no-ansi"
dcr = " $dc run --rm "
2020-01-03 11:17:35 +01:00
# Thanks to https://unix.stackexchange.com/a/145654/108960
log_file = "sentry_install_log-`date +'%Y-%m-%d_%H-%M-%S'`.txt"
exec & > >( tee -a " $log_file " )
2019-07-01 21:48:12 +02:00
2019-07-17 21:13:23 +02:00
MIN_DOCKER_VERSION = '17.05.0'
2020-01-21 20:56:04 +01:00
MIN_COMPOSE_VERSION = '1.23.0'
2019-11-12 00:18:59 +01:00
MIN_RAM = 2400 # MB
SENTRY_CONFIG_PY = 'sentry/sentry.conf.py'
SENTRY_CONFIG_YML = 'sentry/config.yml'
2020-07-08 19:42:33 +02:00
SYMBOLICATOR_CONFIG_YML = 'symbolicator/config.yml'
2020-04-24 14:31:59 +02:00
RELAY_CONFIG_YML = 'relay/config.yml'
RELAY_CREDENTIALS_JSON = 'relay/credentials.json'
2019-11-12 00:18:59 +01:00
SENTRY_EXTRA_REQUIREMENTS = 'sentry/requirements.txt'
2019-07-01 21:48:12 +02:00
2019-11-12 00:18:59 +01:00
# Thanks to https://stackoverflow.com/a/25123013/90297 for the quick `sed` pattern
function ensure_file_from_example {
if [ -f " $1 " ] ; then
echo " $1 already exists, skipped creation. "
else
echo " Creating $1 ... "
cp -n $( echo " $1 " | sed 's/\.[^.]*$/.example&/' ) " $1 "
fi
}
2019-12-09 19:33:02 +01:00
ensure_file_from_example $SENTRY_CONFIG_PY
ensure_file_from_example $SENTRY_CONFIG_YML
ensure_file_from_example $SENTRY_EXTRA_REQUIREMENTS
2020-07-08 19:42:33 +02:00
ensure_file_from_example $SYMBOLICATOR_CONFIG_YML
2020-07-10 22:53:50 +02:00
ensure_file_from_example $RELAY_CONFIG_YML
2019-12-09 19:33:02 +01:00
2020-02-25 14:08:13 +01:00
if grep -xq "system.secret-key: '!!changeme!!'" $SENTRY_CONFIG_YML ; then
echo ""
echo "Generating secret key..."
# This is to escape the secret key to be used in sed below
# Note the need to set LC_ALL=C due to BSD tr and sed always trying to decode
# whatever is passed to them. Kudos to https://stackoverflow.com/a/23584470/90297
SECRET_KEY = $( export LC_ALL = C; head /dev/urandom | tr -dc "a-z0-9@#%^&*(-_=+)" | head -c 50 | sed -e 's/[\/&]/\\&/g' )
sed -i -e 's/^system.secret-key:.*$/system.secret-key: ' " ' $SECRET_KEY ' " '/' $SENTRY_CONFIG_YML
echo " Secret key written to $SENTRY_CONFIG_YML "
fi
2019-07-01 21:48:12 +02:00
2020-04-24 14:13:38 +02:00
echo ""
echo "Fetching and updating Docker images..."
echo ""
# We tag locally built images with an '-onpremise-local' suffix. docker-compose pull tries to pull these too and
# shows a 404 error on the console which is confusing and unnecessary. To overcome this, we add the stderr>stdout
# redirection below and pass it through grep, ignoring all lines having this '-onpremise-local' suffix.
$dc pull -q --ignore-pull-failures 2>& 1 | grep -v -- -onpremise-local || true
2020-05-25 00:11:19 +02:00
2020-07-17 14:29:55 +02:00
# We may not have the set image on the repo (local images) so allow fails
docker pull $SENTRY_IMAGE || true;
2020-04-24 14:13:38 +02:00
2019-07-01 21:48:12 +02:00
echo ""
echo "Building and tagging Docker images..."
echo ""
2019-11-12 00:18:59 +01:00
# Build the sentry onpremise image first as it is needed for the cron image
2020-01-03 20:06:33 +01:00
$dc build --force-rm web
2020-01-12 16:53:54 +01:00
$dc build --force-rm --parallel
2019-07-01 21:48:12 +02:00
echo ""
echo "Docker images built."
2020-06-29 15:16:42 +02:00
ZOOKEEPER_SNAPSHOT_FOLDER_EXISTS = $( $dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/data/version-2 | wc -l | tr -d ' [ :space:] '' )
2020-07-08 19:42:33 +02:00
if [ " $ZOOKEEPER_SNAPSHOT_FOLDER_EXISTS " -eq "1" ] ; then
2020-06-29 15:16:42 +02:00
ZOOKEEPER_LOG_FILE_COUNT = $( $dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/log/version-2/* | wc -l | tr -d ' [ :space:] '' )
ZOOKEEPER_SNAPSHOT_FILE_COUNT = $( $dcr zookeeper bash -c 'ls 2>/dev/null -Ubad1 -- /var/lib/zookeeper/data/version-2/* | wc -l | tr -d ' [ :space:] '' )
# This is a workaround for a ZK upgrade bug: https://issues.apache.org/jira/browse/ZOOKEEPER-3056
if [ " $ZOOKEEPER_LOG_FILE_COUNT " -gt "0" ] && [ " $ZOOKEEPER_SNAPSHOT_FILE_COUNT " -eq "0" ] ; then
$dcr -v $( pwd ) /zookeeper:/temp zookeeper bash -c 'cp /temp/snapshot.0 /var/lib/zookeeper/data/version-2/snapshot.0'
$dc run -d -e ZOOKEEPER_SNAPSHOT_TRUST_EMPTY = true zookeeper
fi
2020-05-24 22:52:30 +02:00
fi
2020-07-15 19:37:51 +02:00
# [begin] Snuba/Clickhouse transactions table rebuild
clickhouse_query ( ) { $dcr clickhouse clickhouse-client --host clickhouse -q " $1 " ; }
$dc up -d clickhouse
set +e
CLICKHOUSE_CLIENT_MAX_RETRY = 5
# Wait until clickhouse server is up
until clickhouse_query 'SELECT 1' > /dev/null; do
( ( CLICKHOUSE_CLIENT_MAX_RETRY--) )
[ [ CLICKHOUSE_CLIENT_MAX_RETRY -eq 0 ] ] && echo "Clickhouse server failed to come up in 5 tries." && exit 1;
echo " Trying again. Remaining tries # $CLICKHOUSE_CLIENT_MAX_RETRY "
sleep 0.5;
done
set -e
SNUBA_HAS_TRANSACTIONS_TABLE = $( clickhouse_query 'EXISTS TABLE transactions_local' | tr -d '\n\r' )
SNUBA_TRANSACTIONS_NEEDS_UPDATE = $( [ " $SNUBA_HAS_TRANSACTIONS_TABLE " = = "1" ] && clickhouse_query 'SHOW CREATE TABLE transactions_local' | grep -v 'SAMPLE BY' || echo '' )
if [ " $SNUBA_TRANSACTIONS_NEEDS_UPDATE " ] ; then
SNUBA_TRANSACTIONS_TABLE_CONTENTS = $( clickhouse_query "SELECT * FROM transactions_local LIMIT 1" )
if [ -z $SNUBA_TRANSACTIONS_TABLE_CONTENTS ] ; then
echo "Dropping the old transactions table from Clickhouse..." ;
clickhouse_query 'DROP TABLE transactions_local'
echo "Done."
else
echo "Seems like your Clickhouse transactions table is old and non-empty. You may experience issues if/when you have more than 10000 records in this table. See https://github.com/getsentry/sentry/pull/19882 for more information and consider disabling the 'discover2.tags_facet_enable_sampling' feature flag." ;
fi
fi
# [end] Snuba/Clickhouse transactions table rebuild
2020-05-18 08:16:30 +02:00
echo "Bootstrapping and migrating Snuba..."
2020-01-21 20:56:45 +01:00
$dcr snuba-api bootstrap --force
2020-01-03 20:52:22 +01:00
echo ""
2019-07-01 21:48:12 +02:00
echo ""
echo "Setting up database..."
if [ $CI ] ; then
2020-01-03 20:06:33 +01:00
$dcr web upgrade --noinput
2019-07-01 21:48:12 +02:00
echo ""
echo "Did not prompt for user creation due to non-interactive shell."
echo "Run the following command to create one yourself (recommended):"
echo ""
echo " docker-compose run --rm web createuser"
echo ""
else
2020-01-03 20:06:33 +01:00
$dcr web upgrade
2019-07-01 21:48:12 +02:00
fi
2020-01-03 20:06:33 +01:00
2019-12-30 21:07:42 +01:00
SENTRY_DATA_NEEDS_MIGRATION = $( docker run --rm -v sentry-data:/data alpine ash -c "[ ! -d '/data/files' ] && ls -A1x /data | wc -l || true" )
2019-12-09 19:33:02 +01:00
if [ " $SENTRY_DATA_NEEDS_MIGRATION " ] ; then
echo "Migrating file storage..."
2019-12-30 21:07:42 +01:00
# Use the web (Sentry) image so the file owners are kept as sentry:sentry
2020-01-09 20:55:20 +01:00
# The `\"` escape pattern is to make this compatible w/ Git Bash on Windows. See #329.
$dcr --entrypoint \" /bin/bash\" web -c \
2020-01-13 20:25:27 +01:00
"mkdir -p /tmp/files; mv /data/* /tmp/files/; mv /tmp/files /data/files; chown -R sentry:sentry /data"
2019-12-09 19:33:02 +01:00
fi
2020-04-24 14:31:59 +02:00
if [ ! -f " $RELAY_CREDENTIALS_JSON " ] ; then
2020-05-04 20:44:34 +02:00
echo ""
echo "Generating Relay credentials..."
# We need the ugly hack below as `relay generate credentials` tries to read the config and the credentials
# even with the `--stdout` and `--overwrite` flags and then errors out when the credentials file exists but
# not valid JSON. We hit this case as we redirect output to the same config folder, creating an empty
# credentials file before relay runs.
$dcr --no-deps -v $( pwd ) /$RELAY_CONFIG_YML :/tmp/config.yml relay --config /tmp credentials generate --stdout > " $RELAY_CREDENTIALS_JSON "
echo " Relay credentials written to $RELAY_CREDENTIALS_JSON "
fi
2019-07-01 21:48:12 +02:00
echo ""
echo "----------------"
2019-11-12 00:18:59 +01:00
echo "You're all done! Run the following command to get Sentry running:"
2019-07-01 21:48:12 +02:00
echo ""
echo " docker-compose up -d"
2019-07-17 21:13:23 +02:00
echo ""