hobo_agent: copy sentry configuration into wcs-olap.ini (#56048)

This commit is contained in:
Benjamin Dauvergne 2021-08-09 10:32:59 +02:00
parent b0d60a80f5
commit bee5061ace
3 changed files with 54 additions and 1 deletions

View File

@ -20,6 +20,11 @@ import os
from django.utils.six.moves import configparser as ConfigParser
from django.utils.six.moves.urllib import parse as urlparse
try:
import sentry_sdk
except ImportError:
sentry_sdk = None
from tenant_schemas.utils import tenant_context
from hobo.agent.common.management.commands import hobo_deploy
from hobo.multitenant.settings_loaders import KnownServices
@ -65,6 +70,7 @@ class Command(hobo_deploy.Command):
config = ConfigParser.SafeConfigParser()
config.read(ini_file)
self.configure_sentry(config)
if not os.path.exists(schemas_path):
os.mkdir(schemas_path)
if not config.has_section('wcs-olap'):
@ -114,3 +120,26 @@ class Command(hobo_deploy.Command):
config.set(base_url, 'cubes_slug', service['slug'])
with open(ini_file, 'w') as f:
config.write(f)
def has_sentry(self):
if not sentry_sdk:
return False
if not sentry_sdk.Hub.current or not sentry_sdk.Hub.current.client:
# not configured
return False
client = sentry_sdk.Hub.current.client
if not client.dsn:
# not configured
return False
return True
def configure_sentry(self, config):
if self.has_sentry():
if not config.has_section('sentry'):
config.add_section('sentry')
config.set('sentry', 'dsn', sentry_sdk.Hub.current.client.dsn)
if sentry_sdk.Hub.current.client.options.get('environment'):
config.set('sentry', 'environment', sentry_sdk.Hub.current.client.options['environment'])
else:
if config.has_section('sentry'):
config.remove_section('sentry')

View File

@ -16,10 +16,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from contextlib import contextmanager
from psycopg2.extensions import parse_dsn
import pytest
from django.utils.six.moves import configparser as ConfigParser
import sentry_sdk
from bijoe.hobo_agent.management.commands import hobo_deploy
@ -38,9 +43,22 @@ class FakeTenant(object):
return self.directory
def test_deploy_specifics(tmpdir, settings, monkeypatch):
@pytest.fixture
def sentry():
sentry_sdk.init(
dsn='https://1234@sentry.example.com/1',
environment='prod')
yield
sentry_sdk.init()
@pytest.mark.parametrize('with_sentry', [[True], [False]])
def test_deploy_specifics(tmpdir, settings, monkeypatch, request, with_sentry):
monkeypatch.setattr(hobo_deploy, 'tenant_context', donothing)
if with_sentry:
request.getfixturevalue('sentry')
settings.DATABASES = {
'default': {
'NAME': 'coucou',
@ -113,6 +131,11 @@ def test_deploy_specifics(tmpdir, settings, monkeypatch):
assert config.get(wcs_base_url, 'schema') == 'wcs_example_net'
assert config.get(wcs_base_url, 'cubes_label') == 'my läâàlel'
assert config.get(wcs_base_url, 'cubes_slug') == 'my_slug'
if with_sentry:
assert config.get('sentry', 'dsn') == 'https://1234@sentry.example.com/1'
assert config.get('sentry', 'environment') == 'prod'
else:
assert not config.has_section('sentry')
hobo_environment['services'][0]['base_url'] = 'https://new-bijoe.example.net'
command.deploy_specifics(hobo_environment, tenant)

View File

@ -26,6 +26,7 @@ deps =
mock
pyquery
tabulate
sentry_sdk<0.12.3
dj22: djangorestframework>=3.7
dj22: https://git.entrouvert.org/hobo.git/snapshot/hobo-main.tar.gz
commands =