diff --git a/bijoe/hobo_agent/management/commands/hobo_deploy.py b/bijoe/hobo_agent/management/commands/hobo_deploy.py index bda970e..97e41dc 100644 --- a/bijoe/hobo_agent/management/commands/hobo_deploy.py +++ b/bijoe/hobo_agent/management/commands/hobo_deploy.py @@ -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') diff --git a/tests/test_hobo_deploy.py b/tests/test_hobo_deploy.py index 341621c..3d74f05 100644 --- a/tests/test_hobo_deploy.py +++ b/tests/test_hobo_deploy.py @@ -16,10 +16,15 @@ # along with this program. If not, see . 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) diff --git a/tox.ini b/tox.ini index c6ca4fe..724105b 100644 --- a/tox.ini +++ b/tox.ini @@ -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 =