6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2025-01-12 09:51:08 +00:00

Load configuration values from environment

Configuration values can be loaded from the system environment by adding
a "TUTOR_" prefix.

Environment values supersede values from the user configuration file, so
that we can set values from the command line with "KEY=VAL tutor config
save --silent" even when KEY is already present in the user
configuration file.
This commit is contained in:
Régis Behmo 2019-03-18 21:19:03 +01:00
parent 0c2e33f616
commit e3b10b72f2
6 changed files with 43 additions and 12 deletions

View File

@ -2,6 +2,7 @@
## Latest
- [Feature] Load configuration parameters from the system environment
- [Improvement] Automatic environment re-generation after re-configuration
- [Improvement] Error and interrupt handling in UI and web UI
- [Bugfix] Fix missing webui env directory

View File

@ -15,10 +15,16 @@ Alternatively, you can set each parameter from the command line::
tutor config save --silent --set PARAM1=VALUE1 --set PARAM2=VALUE2
Once the base configuration is created or updated, the environment is automatically re-generated. The environment is the set of all files required to manage an Open edX platform: Dockerfile, ``lms.env.json``, settings files... You can view the environment files in the ``env`` folder::
Or from the system environment::
export TUTOR_PARAM1=VALUE1
Once the base configuration is created or updated, the environment is automatically re-generated. The environment is the set of all files required to manage an Open edX platform: Dockerfile, ``lms.env.json``, settings files, etc. You can view the environment files in the ``env`` folder::
ls $(tutor config printroot)/env
With an up-to-date environment, Tutor is ready to launch an Open edX platform and perform usual operations. Below, we document some of the configuration parameters.
.. _docker_images:
``DOCKER_IMAGE_*`` Custom Docker images

View File

@ -8,6 +8,7 @@ from . import exceptions
from . import env
from . import fmt
from . import opts
from . import utils
from .__about__ import __version__
@ -24,7 +25,7 @@ def config():
@opts.key_value
def save(root, silent, set_):
config = {}
load_files(config, root)
load_current(config, root)
for k, v in set_:
config[k] = v
if not silent:
@ -47,7 +48,7 @@ def load(root):
exist.
"""
config = {}
load_files(config, root)
load_current(config, root)
should_update_env = False
if not os.path.exists(config_path(root)):
@ -72,15 +73,29 @@ def load(root):
return config
def load_files(config, root):
def load_current(config, root):
convert_json2yml(root)
load_base(config, root)
load_user(config, root)
load_env(config, root)
# Load base values
base = yaml.load(env.read("config.yml"))
def load_base(config, root):
base = yaml.load(env.read("config-base.yml"))
for k, v in base.items():
config[k] = v
# Load user file
def load_env(config, root):
base_config = yaml.load(env.read("config-base.yml"))
default_config = yaml.load(env.read("config-defaults.yml"))
keys = set(list(base_config.keys()) + list(default_config.keys()))
for k in keys:
env_var = "TUTOR_" + k
if env_var in os.environ:
print(k, os.environ[env_var])
config[k] = utils.parse_yaml_value(os.environ[env_var])
def load_user(config, root):
path = config_path(root)
if os.path.exists(path):
with open(path) as fi:

View File

@ -1,6 +1,9 @@
import appdirs
import click
from . import utils
root = click.option(
"-r", "--root",
envvar="TUTOR_ROOT",
@ -31,11 +34,7 @@ class YamlParamType(click.ParamType):
k, v = value.split("=")
except ValueError:
self.fail("'{}' is not of the form 'key=value'.".format(value), param, ctx)
if v.isdigit():
v = int(v)
elif v in ["true", "false"]:
v = (v == "true")
return (k, v)
return k, utils.parse_yaml_value(v)
key_value = click.option(
"-s", "--set", "set_", type=YamlParamType(), multiple=True, metavar="KEY=VAL",

View File

@ -11,6 +11,16 @@ from . import fmt
def random_string(length):
return "".join([random.choice(string.ascii_letters + string.digits) for _ in range(length)])
def parse_yaml_value(v):
"""
Parse a yaml-formatted string
"""
if v.isdigit():
v = int(v)
elif v in ["true", "false"]:
v = (v == "true")
return v
def docker_run(*command):
return docker("run", "--rm", "-it", *command)