From 9dabf010517660eac761f0f3f52ab38ec5e5054e Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Thu, 5 Dec 2019 00:38:09 +0100 Subject: [PATCH] Allow overriding container env vars --- restic_compose_backup/config.py | 3 +++ restic_compose_backup/containers.py | 13 ++++++++++++- restic_compose_backup/log.py | 4 ---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/restic_compose_backup/config.py b/restic_compose_backup/config.py index 0adf4b2..10d6033 100644 --- a/restic_compose_backup/config.py +++ b/restic_compose_backup/config.py @@ -9,6 +9,9 @@ class Config: self.password = os.environ.get('RESTIC_REPOSITORY') self.docker_base_url = os.environ.get('DOCKER_BASE_URL') or "unix://tmp/docker.sock" + # Log + self.log_level = os.environ.get('LOG_LEVEL') + # forget / keep self.keep_daily = os.environ.get('KEEP_DAILY') or "7" self.keep_weekly = os.environ.get('KEEP_WEEKLY') or "4" diff --git a/restic_compose_backup/containers.py b/restic_compose_backup/containers.py index af2652a..490e807 100644 --- a/restic_compose_backup/containers.py +++ b/restic_compose_backup/containers.py @@ -63,7 +63,7 @@ class Container: @property def environment(self) -> list: """All configured env vars for the container as a list""" - return self.get_config('Env', default=[]) + return self.get_config('Env') def get_config_env(self, name) -> str: """Get a config environment variable by name""" @@ -71,6 +71,17 @@ class Container: data = {i[0:i.find('=')]: i[i.find('=')+1:] for i in self.environment} return data.get(name) + def set_config_env(self, name, value): + """Set an environment variable""" + env = self.environment + new_value = f'{name}={value}' + for i, entry in enumerate(env): + if f'{name}=' in entry: + env[i] = new_value + break + else: + env.append(new_value) + @property def volumes(self) -> dict: """ diff --git a/restic_compose_backup/log.py b/restic_compose_backup/log.py index 5e1b9eb..8e6de70 100644 --- a/restic_compose_backup/log.py +++ b/restic_compose_backup/log.py @@ -15,10 +15,6 @@ LOG_LEVELS = { def setup(level: str = 'warning'): """Set up logging""" - # Get log level from env if not set directly - if level is None: - level = os.environ.get('LOG_LEVEL') - level = level or "" level = LOG_LEVELS.get(level.lower(), DEFAULT_LOG_LEVEL) logger.setLevel(level)