From 3f04c30f4760aa999e996daa47f22d4c800ffdba Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Fri, 25 Jul 2014 15:14:08 -0700 Subject: [PATCH] Use two-scoop style multiple settings file. This also adds a config_helper to help create an ini-file that contains the randomly generated secret key. --- htsworkflow/settings/__init__.py | 0 htsworkflow/{settings.py => settings/base.py} | 61 +++++++------------ htsworkflow/settings/local.py | 26 ++++++++ htsworkflow/settings/myrada.py | 27 ++++++++ htsworkflow/settings/production.py | 10 +++ htsworkflow/util/config_helper.py | 43 +++++++++++++ manage.py | 2 +- 7 files changed, 129 insertions(+), 40 deletions(-) create mode 100644 htsworkflow/settings/__init__.py rename htsworkflow/{settings.py => settings/base.py} (67%) create mode 100644 htsworkflow/settings/local.py create mode 100644 htsworkflow/settings/myrada.py create mode 100644 htsworkflow/settings/production.py create mode 100644 htsworkflow/util/config_helper.py diff --git a/htsworkflow/settings/__init__.py b/htsworkflow/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/htsworkflow/settings.py b/htsworkflow/settings/base.py similarity index 67% rename from htsworkflow/settings.py rename to htsworkflow/settings/base.py index f030a95..97518e0 100644 --- a/htsworkflow/settings.py +++ b/htsworkflow/settings/base.py @@ -9,34 +9,32 @@ https://docs.djangoproject.com/en/1.6/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) -import os +from os.path import abspath, basename, dirname, join, normpath import sys +import logging -DJANGO_ROOT = os.path.dirname(__file__) -PROJECT_ROOT = os.path.dirname(DJANGO_ROOT) +from htsworkflow.util import config_helper -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ +DJANGO_ROOT = dirname(dirname(abspath(__file__))) +PROJECT_ROOT = dirname(DJANGO_ROOT) -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'c=5&609$7)bm_u+3$2bi=ida$*a)c1(cp_0siua7uyww!1qfg_' +INI_OPTIONS = config_helper.HTSWConfig() -DEFAULT_API_KEY = 'n7HsXGHIi0vp9j5u4TIRJyqAlXYc4wrH' +SECRET_KEY = INI_OPTIONS.setdefaultsecret('frontend', 'secret_key') +DEFAULT_API_KEY = INI_OPTIONS.setdefaultsecret('frontend', 'api') # SECURITY WARNING: don't run with debug turned on in production! # Override in settings_local -DEBUG = True +DEBUG = False -TEMPLATE_DEBUG = True - -ALLOWED_HOSTS = ['jumpgate.caltech.edu'] +TEMPLATE_DEBUG = False # Application definition AUTHENTICATION_BACKENDS = ( 'samples.auth_backend.HTSUserModelBackend', ) CUSTOM_USER_MODEL = 'samples.HTSUser' -INSTALLED_APPS = ( +INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -51,20 +49,20 @@ INSTALLED_APPS = ( 'bcmagic', 'inventory', 'labels', -) +] -MIDDLEWARE_CLASSES = ( +MIDDLEWARE_CLASSES = [ 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', -) +] -TEMPLATE_DIRS = ( - os.path.join(DJANGO_ROOT, 'templates'), -) +TEMPLATE_DIRS = [ + join(DJANGO_ROOT, 'templates'), +] ROOT_URLCONF = 'htsworkflow.urls' @@ -73,13 +71,6 @@ WSGI_APPLICATION = 'wsgi.application' # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(PROJECT_ROOT, 'fctracker.db'), - } -} - EMAIL_HOST = 'localhost' # Internationalization @@ -87,7 +78,7 @@ EMAIL_HOST = 'localhost' LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE='America/Los_Angeles' USE_I18N = True @@ -95,31 +86,23 @@ USE_L10N = True USE_TZ = True -TIME_ZONE='America/Los_Angeles' - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATICFILES_DIRS = ( - os.path.join(DJANGO_ROOT, 'static'), + join(DJANGO_ROOT, 'static'), ) STATIC_URL = '/static/' - -##### +### # Application specific settings DEFAULT_PM = 5 # How often to recheck the result archive RESCAN_DELAY=1 -# Update this in settings_local to point to your flowcell result directory -RESULT_HOME_DIR = os.path.join(PROJECT_ROOT, 'test', 'result', 'flowcells') # configure who is sending email and who should get BCCs of announcments NOTIFICATION_SENDER = "noreply@example.com" NOTIFICATION_BCC=[] -try: - # allow local customizations - from settings_local import * -except ImportError as e: - pass +# Update this in settings_local to point to your flowcell result directory +RESULT_HOME_DIR = join(PROJECT_ROOT, 'test', 'result', 'flowcells') diff --git a/htsworkflow/settings/local.py b/htsworkflow/settings/local.py new file mode 100644 index 0000000..dd5c3f7 --- /dev/null +++ b/htsworkflow/settings/local.py @@ -0,0 +1,26 @@ + +from .base import * + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'simple': { + 'format': '%(module)s %(levelname)s %(message)s', + } + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'simple', + 'level': 'DEBUG', + 'stream': 'ext://sys.stdout' + } + }, + 'loggers': { + 'htsworkflow': { + 'handlers': ['console'], + 'level': 'ERROR', + } + } +} diff --git a/htsworkflow/settings/myrada.py b/htsworkflow/settings/myrada.py new file mode 100644 index 0000000..aad83a8 --- /dev/null +++ b/htsworkflow/settings/myrada.py @@ -0,0 +1,27 @@ +# configure debugging +import os +from .local import * + +DEBUG=True +TEMPLATE_DEBUG = True + +INTERNAL_IPS = ('127.0.0.1',) + +MIDDLEWARE_CLASSES.extend([ + #'debug_toolbar.middleware.DebugToolbarMiddleware', +]) + +DATABASES = { + #'default': { + #'ENGINE': 'django.db.backends.sqlite3', + #'NAME': os.path.join(settings.BASE_DIR, '..', 'fctracker.db'), + #} + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'HOST': 'felcat.caltech.edu', + 'USER': 'diane', + 'NAME': 'htsworkflow-django1.7', + } + +} + diff --git a/htsworkflow/settings/production.py b/htsworkflow/settings/production.py new file mode 100644 index 0000000..449ff32 --- /dev/null +++ b/htsworkflow/settings/production.py @@ -0,0 +1,10 @@ +from .base import * + +ALLOWED_HOSTS = ['jumpgate.caltech.edu'] + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(PROJECT_ROOT, 'fctracker.db'), + } +} diff --git a/htsworkflow/util/config_helper.py b/htsworkflow/util/config_helper.py new file mode 100644 index 0000000..162580f --- /dev/null +++ b/htsworkflow/util/config_helper.py @@ -0,0 +1,43 @@ +"""Helper functions to manage ini file settings. +""" +import logging +import os +import ConfigParser + +from htsworkflow.util.api import make_django_secret_key + +LOGGER = logging.getLogger(__name__) + +class HTSWConfig(ConfigParser.SafeConfigParser): + '''Customization of SafeConfigParser that can open and save itself. + ''' + def __init__(self, path=[os.path.expanduser("~/.htsworkflow.ini"), + '/etc/htsworkflow.ini',]): + # ConfigParser isn't a new-style class? lame + # super(ConfigParser.SafeConfigParser, self).__init__() + ConfigParser.SafeConfigParser.__init__(self) + read_path = self.read(path) + if len(read_path) > 0: + self.filename = read_path[0] + else: + self.filename = path[0] + + def setdefaultsecret(self, section, key, length=216): + '''return current secret key, creating a new key if needed + ''' + if not self.has_section(section): + self.add_section(section) + + if not self.has_option(section, key): + secret = make_django_secret_key(length) + self.set(section, key, secret) + self.save() + return self.get(section, key) + + def save(self): + try: + ini_stream = open(self.filename, 'w') + self.write(ini_stream) + ini_stream.close() + except IOError, e: + LOGGER.info("Error saving setting: %s" % (str(e))) diff --git a/manage.py b/manage.py index 89759ca..0515cd1 100755 --- a/manage.py +++ b/manage.py @@ -3,7 +3,7 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "htsworkflow.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "htsworkflow.settings.local") from django.core.management import execute_from_command_line -- 2.30.2