Skip to content

Commit

Permalink
Updates to CONFIG_DIR, and STATIC_ROOT
Browse files Browse the repository at this point in the history
* Rename CONFIG_DIR to PROMGEN_CONFIG_DIR to be more descriptive
* Add PROMGEN_CONFIG as the path to promgen.yml
* Add STATIC_ROOT (was unintentionally hardcoded)
* Move SECRET_KEY within settings.py to allow override in promgen.yml
  • Loading branch information
kfdm committed Feb 6, 2018
1 parent efb811c commit 1ef8f4d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
6 changes: 3 additions & 3 deletions promgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import envdir

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'promgen.settings')
os.environ.setdefault('CONFIG_DIR', os.path.expanduser('~/.config/promgen'))
os.environ.setdefault('PROMGEN_CONFIG_DIR', os.path.expanduser('~/.config/promgen'))
default_app_config = 'promgen.apps.PromgenConfig'

if os.path.exists(os.environ['CONFIG_DIR']):
envdir.open(os.environ['CONFIG_DIR'])
if os.path.exists(os.environ['PROMGEN_CONFIG_DIR']):
envdir.open(os.environ['PROMGEN_CONFIG_DIR'])
8 changes: 4 additions & 4 deletions promgen/management/commands/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def write(self, fmtstr, *args, **kwargs):
self.stdout.write(fmtstr.format(*args, **kwargs))

def write_setting(self, key, default='', test=None):
path = os.path.join(settings.CONFIG_DIR, key)
path = os.path.join(settings.PROMGEN_CONFIG_DIR, key)
if os.path.exists(path):
self.write('Setting {} exists', key)
return
Expand All @@ -44,9 +44,9 @@ def write_setting(self, key, default='', test=None):
def handle(self, **kwargs):
self.write('Bootstrapping Promgen')

if not os.path.exists(settings.CONFIG_DIR):
self.write('Creating config directory {} ', settings.CONFIG_DIR)
os.makedirs(settings.CONFIG_DIR)
if not os.path.exists(settings.PROMGEN_CONFIG_DIR):
self.write('Creating config directory {} ', settings.PROMGEN_CONFIG_DIR)
os.makedirs(settings.PROMGEN_CONFIG_DIR)

if not os.path.exists(settings.PROMGEN_CONFIG):
path = os.path.join(settings.BASE_DIR, 'promgen', 'tests', 'examples', 'promgen.yml')
Expand Down
25 changes: 17 additions & 8 deletions promgen/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,22 @@

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_DIR = os.environ['CONFIG_DIR']

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')
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!@#$%^&*(-_=+)')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG', '0') != '0'

# Settings for Prometheus paths and such
PROMGEN_CONFIG = os.path.join(CONFIG_DIR, 'promgen.yml')
PROMGEN_CONFIG_DIR = os.environ['PROMGEN_CONFIG_DIR']
PROMGEN_CONFIG = os.environ.get(
'PROMGEN_CONFIG',
os.path.join(PROMGEN_CONFIG_DIR, 'promgen.yml')
)
if os.path.exists(PROMGEN_CONFIG):
with open(PROMGEN_CONFIG) as fp:
PROMGEN = yaml.load(fp)
Expand Down Expand Up @@ -154,7 +152,9 @@
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.expanduser('~/.cache/promgen')
STATIC_ROOT = os.environ.get(
'STATIC_ROOT', os.path.expanduser('~/.cache/promgen')
)

SITE_ID = 1

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

0 comments on commit 1ef8f4d

Please sign in to comment.