diff --git a/promgen/__init__.py b/promgen/__init__.py index 9fe1ce40b..68583b795 100644 --- a/promgen/__init__.py +++ b/promgen/__init__.py @@ -2,7 +2,6 @@ # These sources are released under the terms of the MIT license: see LICENSE import os -import envdir import pathlib # Django environment defaults @@ -10,11 +9,25 @@ default_app_config = "promgen.apps.PromgenConfig" # Promgen environment defaults -PROMGEN_CONFIG_DIR = os.environ.setdefault( - "PROMGEN_CONFIG_DIR", os.path.expanduser("~/.config/promgen") +PROMGEN_CONFIG_DIR = pathlib.Path( + os.environ.setdefault("PROMGEN_CONFIG_DIR", os.path.expanduser("~/.config/promgen")) ) + PROMGEN_CONFIG_FILE = pathlib.Path( - os.environ.get("PROMGEN_CONFIG", os.path.join(PROMGEN_CONFIG_DIR, "promgen.yml")) + os.environ.get("PROMGEN_CONFIG", str(PROMGEN_CONFIG_DIR / "promgen.yml")) ) -if os.path.exists(PROMGEN_CONFIG_DIR): + +if PROMGEN_CONFIG_DIR.exists(): + import envdir + envdir.open(PROMGEN_CONFIG_DIR) + + +if "SECRET_KEY" not in os.environ: + from django.utils.crypto import get_random_string + import warnings + + warnings.warn("Unset SECRET_KEY setting to random for now") + os.environ["SECRET_KEY"] = get_random_string( + 50, "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)" + ) diff --git a/promgen/management/commands/bootstrap.py b/promgen/management/commands/bootstrap.py index a2eedf1e4..55e86ab50 100644 --- a/promgen/management/commands/bootstrap.py +++ b/promgen/management/commands/bootstrap.py @@ -3,25 +3,34 @@ import os import shutil - +from django.apps import apps from django.conf import settings from django.core.management.base import BaseCommand +from promgen import PROMGEN_CONFIG_DIR, PROMGEN_CONFIG_FILE + +PROMGEN_CONFIG_DEFAULT = ( + settings.BASE_DIR / "promgen" / "tests" / "examples" / "promgen.yml" +) + class Command(BaseCommand): def prompt(self, prompt, *args, **kwargs): return input(prompt.format(*args, **kwargs)) - def heading(self, str): - self.stdout.write(self.style.MIGRATE_HEADING(str)) + def write(self, str, color): + self.stdout.write(color(str)) + + def success(self, fmtstr, *args, **kwargs): + self.write(fmtstr.format(*args, **kwargs), color=self.style.SUCCESS) - def write(self, fmtstr, *args, **kwargs): - self.stdout.write(fmtstr.format(*args, **kwargs)) + def warning(self, fmtstr, *args, **kwargs): + self.write(fmtstr.format(*args, **kwargs), color=self.style.WARNING) - def write_setting(self, key, default=None, value=None): - path = settings.PROMGEN_CONFIG_DIR / key + def setting(self, key, default=None, value=None): + path = PROMGEN_CONFIG_DIR / key if path.exists(): - self.write(" Setting {} exists", key) + self.success("{:20} : {}", key, os.environ[key]) return if default: @@ -31,24 +40,32 @@ def write_setting(self, key, default=None, value=None): while not value: value = self.prompt("Please enter a value for {}: ", key).strip() - self.write("Writing {} to {}", value, path) + self.warning("{:20} : {}", key, value) with path.open("w", encoding="utf8") as fp: fp.write(value) def handle(self, **kwargs): - self.heading("Bootstrapping Promgen") - - if not settings.PROMGEN_CONFIG_DIR.exists(): - self.write("Creating config directory {} ", settings.PROMGEN_CONFIG_DIR) - os.makedirs(settings.PROMGEN_CONFIG_DIR) - - if not settings.PROMGEN_CONFIG.exists(): - path = settings.BASE_DIR / "promgen" / "tests" / "examples" / "promgen.yml" - self.write(" Creating promgen config {} from {}", settings.PROMGEN_CONFIG, path) - shutil.copy(path, settings.PROMGEN_CONFIG) - - self.write_setting("SECRET_KEY", default=settings.SECRET_KEY) - self.write_setting("DATABASE_URL") - # Schemes based on list of supported brokers - # http://docs.celeryproject.org/en/latest/getting-started/brokers/index.html - self.write_setting("CELERY_BROKER_URL") + self.write("Bootstrapping Promgen", color=self.style.MIGRATE_HEADING) + + if not PROMGEN_CONFIG_DIR.exists(): + self.warning("Config {} Created", PROMGEN_CONFIG_DIR) + os.makedirs(PROMGEN_CONFIG_DIR) + + if not PROMGEN_CONFIG_FILE.exists(): + self.warning( + "Creating promgen config {} from {}", + PROMGEN_CONFIG_FILE, + PROMGEN_CONFIG_DEFAULT, + ) + shutil.copy(PROMGEN_CONFIG_DEFAULT, PROMGEN_CONFIG_FILE) + else: + self.success("Config {} Exists", PROMGEN_CONFIG_FILE) + + self.write("Checking environment settings", color=self.style.MIGRATE_HEADING) + self.setting("SECRET_KEY", default=settings.SECRET_KEY) + self.setting("DATABASE_URL") + self.setting("CELERY_BROKER_URL") + self.stdout.write('') + + self.write("Running django system checks", color=self.style.MIGRATE_HEADING) + self.check(display_num_errors=True) diff --git a/promgen/settings.py b/promgen/settings.py index d78d86386..bfb1a5861 100644 --- a/promgen/settings.py +++ b/promgen/settings.py @@ -15,14 +15,13 @@ import os import pathlib -import warnings import environ import yaml from django.urls import reverse_lazy -from promgen import PROMGEN_CONFIG_FILE, PROMGEN_CONFIG_DIR +from promgen import PROMGEN_CONFIG_FILE from promgen.plugins import apps_from_setuptools from promgen.version import __version__ @@ -220,12 +219,3 @@ # Load overrides from PROMGEN to replace Django settings for k, v in PROMGEN.pop('django', {}).items(): globals()[k] = v - -# If we still don't have a SECRET_KEY set, we can set something random here. -# This is placed at the very bottom to allow SECRET_KEY to be written into the -# config file instead of an environment variable -if not SECRET_KEY: - warnings.warn('Unset SECRET_KEY setting to random for now') - # Taken from Django's generation function - from django.utils.crypto import get_random_string - SECRET_KEY = get_random_string(50, 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')