From dd17aadfae0711670071a75024ebb8a5c0c568e7 Mon Sep 17 00:00:00 2001 From: Christophe Mehay Date: Sun, 4 Dec 2016 21:11:10 +0100 Subject: [PATCH] Expends user and group in environment --- .pre-commit-config.yaml | 1 - docs/config.rst | 2 ++ pyentrypoint/config.py | 18 ++++++++++++++++++ pyentrypoint/entrypoint.py | 1 + setup.py | 2 +- tests/commons.py | 7 +++++++ tests/configs/base.yml | 3 +++ tests/main_test.py | 6 ++++++ tests/pyentrypoint_test.py | 37 +++++++++++++++++++++++-------------- tests/reloader_test.py | 6 ++++++ 10 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 tests/commons.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 005225d..f830571 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,6 @@ - id: flake8 args: - --exclude=__init__.py,docs/conf.py - - id: name-tests-test - id: autopep8-wrapper - id: requirements-txt-fixer - id: trailing-whitespace diff --git a/docs/config.rst b/docs/config.rst index e391158..9d28131 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -137,6 +137,8 @@ Affect only command handled. **Note**: Dockerfile USER value by default. +Can be expended from environment in ``ENTRYPOINT_USER`` and ``ENTRYPOINT_GROUP``. + config_files ^^^^^^^^^^^^ diff --git a/pyentrypoint/config.py b/pyentrypoint/config.py index 31f54cb..e6c7340 100644 --- a/pyentrypoint/config.py +++ b/pyentrypoint/config.py @@ -81,6 +81,24 @@ class ConfigMeta(object): pass self._config[key] = val + def set_to_env(self): + self.log.debug('Add conf to environment') + for attr in [a for a in dir(self) if not a.startswith('_')]: + setup = getattr(self, attr) + env = 'ENTRYPOINT_{attr}'.format(attr=attr.upper()) + if type(setup) is bool and setup: + self.log.debug('set env {env} with "true"'.format( + env=env + )) + os.environ[env] = 'true' + if type(setup) is int or type(setup) is str: + if env not in os.environ: + self.log.debug('set env {env} with "{val}"'.format( + env=env, + val=str(setup), + )) + os.environ[env] = str(setup) + class Config(ConfigMeta): diff --git a/pyentrypoint/entrypoint.py b/pyentrypoint/entrypoint.py index accd56c..d77e368 100644 --- a/pyentrypoint/entrypoint.py +++ b/pyentrypoint/entrypoint.py @@ -142,6 +142,7 @@ def main(argv): if not entry.is_handled and not entry.should_config: entry.log.warning("Running command without config") entry.launch() + entry.config.set_to_env() entry.log.debug("Starting config") entry.run_pre_conf_cmds() entry.apply_conf() diff --git a/setup.py b/setup.py index eb530e3..a8eb91e 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import setup # Thanks Sam and Max -__version__ = '0.4.5' +__version__ = '0.4.6' if __name__ == '__main__': setup( diff --git a/tests/commons.py b/tests/commons.py new file mode 100644 index 0000000..8f12731 --- /dev/null +++ b/tests/commons.py @@ -0,0 +1,7 @@ +import os + + +def clean_env(): + for key in os.environ.copy().keys(): + if key.startswith('ENTRYPOINT_'): + del os.environ[key] diff --git a/tests/configs/base.yml b/tests/configs/base.yml index e27c0b4..0e153a1 100644 --- a/tests/configs/base.yml +++ b/tests/configs/base.yml @@ -31,6 +31,9 @@ pre_conf_commands: post_conf_commands: - echo TEST2 > /tmp/OKOK - echo TEST3 > /tmp/OKOKOK + - echo ${ENTRYPOINT_USER} > /tmp/user + - echo ${ENTRYPOINT_GROUP} > /tmp/group + - echo ${ENTRYPOINT_DEBUG} > /tmp/debug - echo "INFO IS DISPLAYED\nON TWO LINES" - echo "WARNING IS DISPLAYED" 1>&2 diff --git a/tests/main_test.py b/tests/main_test.py index ef9d731..f6a1607 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -5,9 +5,15 @@ from __future__ import unicode_literals import os from multiprocessing import Process +from commons import clean_env + from pyentrypoint.entrypoint import main +def teardown_function(function): + clean_env() + + class ProxyMain(object): def __init__(self, args, env): diff --git a/tests/pyentrypoint_test.py b/tests/pyentrypoint_test.py index 58c4c3c..9f8d974 100644 --- a/tests/pyentrypoint_test.py +++ b/tests/pyentrypoint_test.py @@ -7,6 +7,7 @@ import os from multiprocessing import Process import pytest +from commons import clean_env from yaml import load from yaml import Loader @@ -21,6 +22,10 @@ LINKS = [ ] +def teardown_function(function): + clean_env() + + def test_all_links(): links = DockerLinks() all_links = links.links() @@ -153,28 +158,32 @@ def test_templates(): def test_conf_commands(): + entry = Entrypoint(conf='configs/base.yml') + checks = [ + ('/tmp/OK', 'TEST'), + ('/tmp/OKOK', 'TEST2'), + ('/tmp/OKOKOK', 'TEST3'), + ('/tmp/OKOKOKOK', 'TEST4'), + ('/tmp/OKOKOKOKOK', 'TEST5'), + ('/tmp/user', '1000'), + ('/tmp/group', '1000'), + ('/tmp/debug', 'true'), + ] + os.environ['ENTRYPOINT_PRECONF_COMMAND'] = 'echo TEST4 > /tmp/OKOKOKOK' os.environ['ENTRYPOINT_POSTCONF_COMMAND'] = 'echo TEST5 > /tmp/OKOKOKOKOK' + entry.config.set_to_env() entry.run_pre_conf_cmds() entry.run_post_conf_cmds() - with open('/tmp/OK') as f: - assert f.readline().startswith('TEST') - - with open('/tmp/OKOK') as f: - assert f.readline().startswith('TEST2') - - with open('/tmp/OKOKOK') as f: - assert f.readline().startswith('TEST3') - - with open('/tmp/OKOKOKOK') as f: - assert f.readline().startswith('TEST4') - - with open('/tmp/OKOKOKOKOK') as f: - assert f.readline().startswith('TEST5') + for filename, value in checks: + with open(filename) as f: + line = f.readline() + print(line) + assert line.startswith(value) def test_command(): diff --git a/tests/reloader_test.py b/tests/reloader_test.py index 311a2d5..9ff49e1 100644 --- a/tests/reloader_test.py +++ b/tests/reloader_test.py @@ -20,6 +20,12 @@ from signal import SIGHUP from time import sleep +from commons import clean_env + + +def teardown_function(function): + clean_env() + def _reloader_check(conf, command): entry = Entrypoint(conf=conf)