Merge pull request #11 from cmehay/environment

Expends user and group in environment
dockerfiles v0.4.6
Christophe Mehay 8 years ago committed by GitHub
commit 4b888c3461

@ -9,7 +9,6 @@
- id: flake8 - id: flake8
args: args:
- --exclude=__init__.py,docs/conf.py - --exclude=__init__.py,docs/conf.py
- id: name-tests-test
- id: autopep8-wrapper - id: autopep8-wrapper
- id: requirements-txt-fixer - id: requirements-txt-fixer
- id: trailing-whitespace - id: trailing-whitespace

@ -137,6 +137,8 @@ Affect only command handled.
**Note**: Dockerfile USER value by default. **Note**: Dockerfile USER value by default.
Can be expended from environment in ``ENTRYPOINT_USER`` and ``ENTRYPOINT_GROUP``.
config_files config_files
^^^^^^^^^^^^ ^^^^^^^^^^^^

@ -81,6 +81,24 @@ class ConfigMeta(object):
pass pass
self._config[key] = val 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): class Config(ConfigMeta):

@ -142,6 +142,7 @@ def main(argv):
if not entry.is_handled and not entry.should_config: if not entry.is_handled and not entry.should_config:
entry.log.warning("Running command without config") entry.log.warning("Running command without config")
entry.launch() entry.launch()
entry.config.set_to_env()
entry.log.debug("Starting config") entry.log.debug("Starting config")
entry.run_pre_conf_cmds() entry.run_pre_conf_cmds()
entry.apply_conf() entry.apply_conf()

@ -5,7 +5,7 @@ from setuptools import setup
# Thanks Sam and Max # Thanks Sam and Max
__version__ = '0.4.5' __version__ = '0.4.6'
if __name__ == '__main__': if __name__ == '__main__':
setup( setup(

@ -0,0 +1,7 @@
import os
def clean_env():
for key in os.environ.copy().keys():
if key.startswith('ENTRYPOINT_'):
del os.environ[key]

@ -31,6 +31,9 @@ pre_conf_commands:
post_conf_commands: post_conf_commands:
- echo TEST2 > /tmp/OKOK - echo TEST2 > /tmp/OKOK
- echo TEST3 > /tmp/OKOKOK - 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 "INFO IS DISPLAYED\nON TWO LINES"
- echo "WARNING IS DISPLAYED" 1>&2 - echo "WARNING IS DISPLAYED" 1>&2

@ -5,9 +5,15 @@ from __future__ import unicode_literals
import os import os
from multiprocessing import Process from multiprocessing import Process
from commons import clean_env
from pyentrypoint.entrypoint import main from pyentrypoint.entrypoint import main
def teardown_function(function):
clean_env()
class ProxyMain(object): class ProxyMain(object):
def __init__(self, args, env): def __init__(self, args, env):

@ -7,6 +7,7 @@ import os
from multiprocessing import Process from multiprocessing import Process
import pytest import pytest
from commons import clean_env
from yaml import load from yaml import load
from yaml import Loader from yaml import Loader
@ -21,6 +22,10 @@ LINKS = [
] ]
def teardown_function(function):
clean_env()
def test_all_links(): def test_all_links():
links = DockerLinks() links = DockerLinks()
all_links = links.links() all_links = links.links()
@ -153,28 +158,32 @@ def test_templates():
def test_conf_commands(): def test_conf_commands():
entry = Entrypoint(conf='configs/base.yml') 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_PRECONF_COMMAND'] = 'echo TEST4 > /tmp/OKOKOKOK'
os.environ['ENTRYPOINT_POSTCONF_COMMAND'] = 'echo TEST5 > /tmp/OKOKOKOKOK' os.environ['ENTRYPOINT_POSTCONF_COMMAND'] = 'echo TEST5 > /tmp/OKOKOKOKOK'
entry.config.set_to_env()
entry.run_pre_conf_cmds() entry.run_pre_conf_cmds()
entry.run_post_conf_cmds() entry.run_post_conf_cmds()
with open('/tmp/OK') as f: for filename, value in checks:
assert f.readline().startswith('TEST') with open(filename) as f:
line = f.readline()
with open('/tmp/OKOK') as f: print(line)
assert f.readline().startswith('TEST2') assert line.startswith(value)
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')
def test_command(): def test_command():

@ -20,6 +20,12 @@ from signal import SIGHUP
from time import sleep from time import sleep
from commons import clean_env
def teardown_function(function):
clean_env()
def _reloader_check(conf, command): def _reloader_check(conf, command):
entry = Entrypoint(conf=conf) entry = Entrypoint(conf=conf)

Loading…
Cancel
Save