oncall-engine/engine/apps/grafana_plugin/apps.py
Yulya Artyukhina 381520ee13
Get rid of installation token + add a bunch of tests (#624)
* Get rid of installation token (for OSS installations)

This is done by being required to supply the grafana API URL as an
environment variable on the backend. Additionally, optionally an OnCall
API URL environment variable can be passed in to the frontend (this basically
allows completely skipping the need to configure anything).
- deduplicated a lot of the sync logic on the frontend + made
error message more useful and consistent
- Split PluginConfigPage component into several subcomponents
(making it easier to test each individual component)
- Moved RootWithLoader (from plugin/GrafanaPluginRootPage) into its own
subcomponent (making it easier to test)
- Added tests for pre-existing components that were touched:
  - PluginConfigPage component (and its new subcomponents)
  - state/plugin and state/rootBaseStore functions
  - apps.grafana_plugin django app

Helm changes:
- add GRAFANA_API_URL to oncall.env
- some yaml autoformatting changes
- remove reference to python manage.py issue_invite_for_the_frontend --override

Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
2022-11-21 16:26:00 +01:00

37 lines
1.6 KiB
Python

import logging
import sys
from django.apps import AppConfig, apps
from django.conf import settings
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
STARTUP_COMMANDS = ["runserver", "uwsgi"]
class GrafanaPluginConfig(AppConfig):
name = "apps.grafana_plugin"
def ready(self):
"""
For OSS installations, validate that GRAFANA_API_URL environment variable is specified, otherwise
abort app startup.
We only care to run this for OSS_INSTALLATIONS. The STARTUP_COMMANDS check is to avoid running this check
for the django migrate command. For a fresh installation this would crash because user_management table would
[not exist](https://stackoverflow.com/a/63326719).
"""
# TODO: this logic should probably be moved out to a common utility
is_not_migration_script = any(startup_command in sys.argv for startup_command in STARTUP_COMMANDS)
if is_not_migration_script and settings.OSS_INSTALLATION is True:
Organization = apps.get_model("user_management", "Organization")
has_existing_org = Organization.objects.first() is not None
# only enforce the following for new setups - if no organization exists in the database
# and the GRAFANA_API_URL env var is not specified, exit the application
if has_existing_org is False and settings.SELF_HOSTED_SETTINGS["GRAFANA_API_URL"] is None:
logger.error(
f"For OSS installations, GRAFANA_API_URL is a required environment variable. Please set it and restart the application."
)
sys.exit()