Automated setup for sentry including new project

Since we are often setting up new environments from scratch we would like to automated setting up sentry in each environment as well. Right now we are using the official sentry docker from dockerhub

This includes:

  • create the admin user (maybe solvable by automating the steps in docker and sending text into that)
  • creating a project
  • retrieving the DSN for a project

I had a look into the sentry-cli, but that looks more for solving issues and sending test messages, not for setting it up.

Are there any resources or examples for our kind of problem?

Thanks in advance,
Berend

I just learned that there is
https://docs.sentry.io/hosted/learn/cli/
but also
https://docs.sentry.io/on-premise/server/cli/ which I should look into :slight_smile:

sentry-cli won’t help you here. You’d want to leverage our API. https://docs.sentry.io/on-premise/api/

Sorry to revive such an old thread, but thought I would chime in with how I did this sort of thing with via Python scripts and SaltStack.

Note: Sentry automatically creates a DSN when a Project model is saved so I had to delete that an re-create it later with a known DSN.

Note 2: I’ve installed sentry in /opt, if you install via pip I expect the location will be different.

#The Scripts
###create_project.py

import sys

from sentry.runner import configure
configure()

from sentry.models import Project, ProjectKey

project_id = sys.argv[1]
project_name = sys.argv[2]
slug = project_name.lower()

p = Project.objects.create(id=project_id, name=project_name, public=False, slug=slug, team_id=1, organization_id=1)
ProjectKey.objects.get(project=p).delete()

###create_projectkey.py

import sys

from sentry.runner import configure
configure()

from sentry.models import Project, ProjectKey

project_name = sys.argv[1]
slug = project_name.lower()
public_key = sys.argv[2]
secret_key = sys.argv[3]

p = Project.objects.get(slug=slug)
ProjectKey.objects.create(project=p, public_key=public_key, secret_key=secret_key, roles=1)

#The Salt

foo-project:
  cmd.run:
    - name: SENTRY_CONF=/etc/sentry /opt/sentry/bin/python /etc/sentry/scripts/create_project.py 2 Foo
    - unless: echo "from sentry.models import Project; print Project.objects.get(name='Foo')" | SENTRY_CONF=/etc/sentry sentry shell | grep -q -i Foo

Then a similar state for the create_projectkey.py command:

...
- name: SENTRY_CONF=/etc/sentry /opt/sentry/bin/python /etc/sentry/scripts/create_projectkey.py Foo FooPublicKey FooSecretKey
- unless: echo "from sentry.models import ProjectKey; print ProjectKey.objects.get(project__name='Foo').project.name" | SENTRY_CONF=/etc/sentry sentry shell | grep -q -i Foo

Users are simpler, you can just execute straight through sentry CLI:

- name: SENTRY_CONF=/etc/sentry sentry createuser --email admin --password admin --superuser --no-input
- unless: echo "from django.contrib.auth import get_user_model; print get_user_model().objects.all()" | SENTRY_CONF=/etc/sentry sentry shell | grep -q -i admin

Of course executing these states assumes you’ve already put the Python scripts in place. I put them in /etc/sentry/scripts.

For anyone else arriving here after searching how to do this for Sentry On-Premise version 20, use the logic of this gist ->