From b0f2adb93186074dc0b5bd0b78ad62212d56847e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Sat, 15 Sep 2018 15:51:41 +0200 Subject: [PATCH] Read default settings from environment variables Moreover, ACTIVATE_* variables are now made permanent. It is no longer necessary to re-declare them at each call of `make configure`. --- Makefile | 2 +- configurator/bin/configure.py | 43 +++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 9a2cd98..8f9ed32 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ all: configure $(post_configure_targets) update migrate assets daemonize configure: build-configurator docker run --rm -it --volume="$(PWD)/config:/openedx/config" \ - -e USERID=$(USERID) -e SILENT=$(SILENT) -e ACTIVATE_HTTPS=$(ACTIVATE_HTTPS) -e ACTIVATE_XQUEUE=$(ACTIVATE_XQUEUE) \ + -e USERID=$(USERID) -e SILENT=$(SILENT) -e SETTING_ACTIVATE_HTTPS=$(ACTIVATE_HTTPS) -e SETTING_ACTIVATE_XQUEUE=$(ACTIVATE_XQUEUE) \ regis/openedx-configurator:hawthorn bash -c "./configure.py interactive && \ ./configure.py substitute ./config/openedx/templates/lms.env.json.templ ./config/openedx/lms.env.json && \ ./configure.py substitute ./config/openedx/templates/cms.env.json.templ ./config/openedx/cms.env.json && \ diff --git a/configurator/bin/configure.py b/configurator/bin/configure.py index bc902a7..2db4de3 100755 --- a/configurator/bin/configure.py +++ b/configurator/bin/configure.py @@ -16,6 +16,12 @@ import jinja2 class Configurator: def __init__(self, **default_overrides): + """ + Default values are read, in decreasing order of priority, from: + - SETTING_ environment variable + - Existing config file (in `default_overrides`) + - Value passed to add() + """ self.__values = OrderedDict() self.__default_values = default_overrides try: @@ -30,14 +36,29 @@ class Configurator: self.__input = None def add(self, name, question="", default=""): - default = self.__default_values.get(name, default) - value = default + default = self.default_value(name) or default message = question + " (default: \"{}\"): ".format(default) if question else None value = self.ask(message, default) self.set(name, value) return self + def add_bool(self, name, question="", default=False): + self.add(name, question=question, default=default) + value = self.get(name) + if value in [1, '1']: + return self.set(name, True) + if value in [0, '0']: + return self.set(name, False) + return self + + def default_value(self, name): + setting_name = 'SETTING_' + name.upper() + if setting_name in os.environ: + return os.environ[setting_name] + + return self.__default_values.get(name) + def ask(self, message, default): if self.__input and message: return self.__input(message) or default @@ -64,8 +85,6 @@ def main(): "This is good for debugging and automation, but " "probably not what you want" )) - parser_interactive.add_argument('--activate-https', action='store_true', default=False, help='Activate https feature flag') - parser_interactive.add_argument('--activate-xqueue', action='store_true', default=False, help='Activate xqueue feature flag') parser_interactive.set_defaults(func=interactive) parser_substitute = subparsers.add_parser('substitute') @@ -105,25 +124,25 @@ def interactive(args): ).add( 'MYSQL_USERNAME', "", 'openedx' ).add( - 'MYSQL_PASSWORD', "", random_string(8), + 'MYSQL_PASSWORD', "", random_string(8) ).add( 'MONGODB_DATABASE', "", 'openedx' ).add( 'XQUEUE_AUTH_USERNAME', "", 'lms' ).add( - 'XQUEUE_AUTH_PASSWORD', "", random_string(8), + 'XQUEUE_AUTH_PASSWORD', "", random_string(8) ).add( 'XQUEUE_MYSQL_DATABASE', "", 'xqueue', ).add( 'XQUEUE_MYSQL_USERNAME', "", 'xqueue', ).add( - 'XQUEUE_MYSQL_PASSWORD', "", random_string(8), + 'XQUEUE_MYSQL_PASSWORD', "", random_string(8) ).add( - 'XQUEUE_SECRET_KEY', "", random_string(24), - ).set( - 'ACTIVATE_HTTPS', bool(args.activate_https or os.environ.get('ACTIVATE_HTTPS')) - ).set( - 'ACTIVATE_XQUEUE', bool(args.activate_xqueue or os.environ.get('ACTIVATE_XQUEUE')) + 'XQUEUE_SECRET_KEY', "", random_string(24) + ).add_bool( + 'ACTIVATE_HTTPS', "", False + ).add_bool( + 'ACTIVATE_XQUEUE', "", False ) # Save values