2022-11-21 16:26:00 +01:00
|
|
|
import logging
|
|
|
|
|
import sys
|
|
|
|
|
|
2023-07-25 10:43:23 +01:00
|
|
|
from django.apps import AppConfig
|
2022-11-21 16:26:00 +01:00
|
|
|
from django.conf import settings
|
2023-05-10 08:36:23 -04:00
|
|
|
from django.db import OperationalError
|
2022-11-21 16:26:00 +01:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2023-03-07 19:07:42 +08:00
|
|
|
We only care to run this for OSS INSTALLATIONS. The STARTUP_COMMANDS check is to avoid running this check
|
2022-11-21 16:26:00 +01:00
|
|
|
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)
|
2023-03-07 19:07:42 +08:00
|
|
|
if is_not_migration_script and settings.IS_OPEN_SOURCE:
|
2023-05-10 08:36:23 -04:00
|
|
|
try:
|
2023-07-25 10:43:23 +01:00
|
|
|
from apps.user_management.models import Organization
|
|
|
|
|
|
2023-05-10 08:36:23 -04:00
|
|
|
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(
|
2023-07-26 14:45:44 +01:00
|
|
|
"For OSS installations, GRAFANA_API_URL is a required environment variable. Please set it and restart the application."
|
2023-05-10 08:36:23 -04:00
|
|
|
)
|
|
|
|
|
sys.exit()
|
|
|
|
|
except OperationalError:
|
|
|
|
|
pass
|