Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(relay): Add initialization script #4367

Closed
wants to merge 9 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix
  • Loading branch information
iambriccardo committed Dec 11, 2024
commit 703fe2b767f0668752156dac99b14712454bc5fd
109 changes: 76 additions & 33 deletions scripts/relay-init.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -2,51 +2,94 @@

set -euo pipefail

CONFIG_DIR="config"
# Usage: ./setup_relay.sh <DSN> <ORG_SLUG> [--open]
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <DSN> <ORG_SLUG> [--open]"
exit 1
fi

# Ensure the config directory is cleaned up on exit
trap "rm -rf ${CONFIG_DIR}" EXIT
DSN="$1"
ORG_SLUG="$2"
OPEN_PAGE="false"

if [[ "${3:-}" == "--open" ]]; then
OPEN_PAGE="true"
fi

# Prompt the user for the Sentry upstream URL
read -p "Enter your Sentry upstream URL (e.g. https://oXXXX.ingest.sentry.io): " SENTRY_UPSTREAM
# Extract the host part from the DSN
# DSN format could be: https://o[org_id].ingest.us.sentry.io/[project_id]
# or even just https://o[org_id].ingest.us.sentry.io
# We'll strip the protocol and any trailing path:
UPSTREAM_HOST=$(echo "$DSN" | sed -E 's@^https://([^/]+).*@\1@')
UPSTREAM="https://$UPSTREAM_HOST"

if [[ -z "$UPSTREAM_HOST" ]]; then
echo "Failed to parse upstream from DSN. Check the DSN format."
exit 1
fi

# Create a configuration directory for relay if it doesn't exist
mkdir -p "${CONFIG_DIR}"
# Create the config directory
mkdir -p config

echo "Initializing Relay configuration..."
# Run relay config init to create config files (.relay/config.yml and credentials.json)
# We assume you'll choose the default configuration by pressing enter.
docker run --rm -it \
-v "$(pwd)/${CONFIG_DIR}":/work/.relay \
-v "$(pwd)/config/:/work/.relay/:z" \
getsentry/relay \
config init
config init <<EOF
1
EOF

# At this point, config.yml and credentials.json should be in ${CONFIG_DIR}
CREDENTIALS_FILE="${CONFIG_DIR}/credentials.json"
# The default config is now in ./config/.relay/config.yml
# We'll edit it to set mode=managed, update the upstream, and ensure host/port
echo "Configuring Relay..."
sed -i 's/mode: .*/mode: managed/' config/.relay/config.yml
sed -i "s|upstream:.*|upstream: $UPSTREAM|" config/.relay/config.yml
sed -i 's|host:.*|host: 0.0.0.0|' config/.relay/config.yml
sed -i 's|port:.*|port: 3000|' config/.relay/config.yml

if [ ! -f "${CREDENTIALS_FILE}" ]; then
echo "Error: ${CREDENTIALS_FILE} not found. Make sure relay config init completed successfully."
echo "Retrieving Relay credentials (public key)..."
PUBLIC_KEY=$(docker run --rm -it \
-v "$(pwd)/config/:/work/.relay/" \
getsentry/relay \
credentials show | grep 'public key' | awk '{print $3}')

if [[ -z "$PUBLIC_KEY" ]]; then
echo "Failed to retrieve Relay public key."
exit 1
fi

echo "Reading credentials from ${CREDENTIALS_FILE}..."
# Parse the credentials file directly with jq
SECRET_KEY=$(jq -r '.secret_key' "${CREDENTIALS_FILE}")
PUBLIC_KEY=$(jq -r '.public_key' "${CREDENTIALS_FILE}")
RELAY_ID=$(jq -r '.id' "${CREDENTIALS_FILE}")

echo "Credentials and upstream URL obtained successfully."
echo "Relay public key: $PUBLIC_KEY"

cat <<EOF

Setup complete!
# Optionally open the Relay registration page in the browser
RELAY_URL="https://${ORG_SLUG}.sentry.io/settings/relay"
if [[ "$OPEN_PAGE" == "true" ]]; then
echo "Opening Relay registration page: $RELAY_URL"
if command -v xdg-open &>/dev/null; then
xdg-open "$RELAY_URL"
elif command -v open &>/dev/null; then
open "$RELAY_URL"
else
echo "Please open the following URL in your browser to register the Relay:"
echo "$RELAY_URL"
fi
else
echo "To register this Relay, visit:"
echo "$RELAY_URL"
echo "and add the public key: $PUBLIC_KEY"
fi

Your relay chart can now be installed with the following command:
helm install sentry-relay ../sentry-relay \\
--set relay.upstream="${SENTRY_UPSTREAM}" \\
--set credentials.secretKey="${SECRET_KEY}" \\
--set credentials.publicKey="${PUBLIC_KEY}" \\
--set credentials.relayId="${RELAY_ID}"
echo "Starting Relay on port 3000..."
docker run -d \
-v "$(pwd)/config/:/work/.relay/" \
-p 3000:3000 \
getsentry/relay \
run

Once deployed, you can port-forward and test sending events as noted in the chart's NOTES.txt.
EOF
echo "Relay is now running on http://localhost:3000"
echo "Using DSN: $DSN"
echo "Upstream is set to: $UPSTREAM"
echo "Relay public key: $PUBLIC_KEY"
echo
echo "To send events through Relay, modify your DSN to point to http://localhost:3000."
echo "For example, if your original DSN was: $DSN"
echo "Replace the host part with localhost:3000 (use http, not https)."