Skip to content
Permalink
Browse files

Switch to environment variables for application config

Discontinue sourcing of medley.conf from application root and
/etc. Apply environment variables prefixed with "MEDLEY." instead.

Also clean up some leftover references to the cache directory, which
is no longer used.
  • Loading branch information
lovett committed Dec 12, 2019
1 parent dba6f56 commit a0bb0e2361b8fd6bc91cc4e24a5fd8656aaa84c5
Showing with 42 additions and 32 deletions.
  1. +0 −1 .gitignore
  2. +0 −1 .rsync-filter
  3. +6 −1 Makefile
  4. +6 −1 ansible/roles/application/templates/medley.service.j2
  5. +30 −28 medley.py
@@ -21,7 +21,6 @@ ansible/*.hosts
ansible/host_vars
ansible/install.retry
venv
cache
node_modules
.pytest_cache
.mypy_cache
@@ -6,7 +6,6 @@
- /.vagrant
- /.cache
- /.git
- /cache
- /coverage
- /.coverage
- /testing
@@ -85,7 +85,12 @@ setup: dummy

# Run a local development webserver
#
serve: dummy
serve: export MEDLEY.engine.autoreload.on=True
serve: export MEDLEY.memorize_checksums=False
serve: export MEDLEY.request.show_tracebacks=True
serve: export MEDLEY.cache_static_assets=True
serve: export MEDLEY.use_service_workers=True
serve:
python medley.py


@@ -7,9 +7,14 @@ Type=notify
NotifyAccess=all
User=medley
Group=medley
Environment=MEDLEY_NOTIFY_SYSTEMD_AT_STARTUP=1
Environment=MEDLEY.notify_systemd_at_startup=1
Environment=MEDLEY.database_dir=/var/medley/db
WorkingDirectory={{ project_root }}
ExecStart={{ project_root }}/venv/bin/python medley.py

[Install]
WantedBy=multi-user.target

# Local Variables:
# mode: conf
# End:
@@ -65,7 +65,6 @@ def setup() -> None:
# These are reasonable values for a production environment.
cherrypy.config.update({
"app_root": app_root,
"cache_dir": "./cache",
"cache_static_assets": True,
"database_dir": "./db",
"engine.autoreload.on": False,
@@ -90,33 +89,36 @@ def setup() -> None:
"tools.gzip.on": True,
})

# Overrides to the default configuration are sourced from
# one or more external files.
config_candidates = (
"/etc/medley.conf",
os.path.join(server_root, "medley.conf")
)

for path in config_candidates:
if os.path.isfile(path):
cherrypy.config.update(path)
cherrypy.log(f"Configuration overrides loaded from {path}")

# Directory creation
# Configuration overrides
#
# Accept any environment variable that starts with "MEDLEY."
environment_config = {
key[7:]: os.getenv(key)
for key in os.environ
if key.startswith("MEDLEY")
}

for key, value in environment_config.items():
if value == "True":
environment_config[key] = True
if value == "False":
environment_config[key] = False
if value.isnumeric():
environment_config[key] = int(value)

if environment_config:
cherrypy.config.update(environment_config)

# Database Directory
#
# Filesystem paths specified in the configuration are expected to
# already exist. Try to create them if they don't.
for key in ("database_dir", "cache_dir"):
value = cherrypy.config.get(key)

try:
os.mkdir(value)
except FileExistsError:
pass
except PermissionError:
raise SystemExit(
f"Unable to create {key} directory at {value}"
)
# Databases will be created automatically, but the directory they
# reside in needs to exist.
try:
os.mkdir(cherrypy.config.get("database_dir"))
except FileExistsError:
pass
except PermissionError:
raise SystemExit("No permission to create database directory")

# Mount the apps
for app in os.listdir(app_root):
@@ -229,7 +231,7 @@ def setup() -> None:
if __name__ == '__main__':
setup()

if os.environ.get("MEDLEY_NOTIFY_SYSTEMD_AT_STARTUP"):
if cherrypy.config.get("notify_systemd_at_startup"):
sdnotify.SystemdNotifier().notify("READY=1")

cherrypy.engine.publish("server:ready")

0 comments on commit a0bb0e2

Please sign in to comment.
You can’t perform that action at this time.