Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup promgen bootstrap and some settings #270

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions promgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@
# These sources are released under the terms of the MIT license: see LICENSE

import os
import envdir
import pathlib

# Django environment defaults
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "promgen.settings")
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!@#$%^&*(-_=+)"
)
67 changes: 42 additions & 25 deletions promgen/management/commands/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
12 changes: 1 addition & 11 deletions promgen/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down Expand Up @@ -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!@#$%^&*(-_=+)')