2
0
mirror of https://github.com/cmehay/pyentrypoint synced 2024-10-30 15:21:11 +00:00

Merge pull request #8 from cmehay/disable

Add env variable to diable service

v 0.4.3
This commit is contained in:
Christophe Mehay 2016-11-23 12:44:31 +01:00 committed by GitHub
commit aca34177e5
6 changed files with 37 additions and 3 deletions

View File

@ -251,6 +251,7 @@ Some setups can be overridden using environment variables.
- `ENTRYPOINT_DISABLE_RELOAD` disable reload system even if it is enabled in `entrypoint-config.yml`. - `ENTRYPOINT_DISABLE_RELOAD` disable reload system even if it is enabled in `entrypoint-config.yml`.
- `ENTRYPOINT_USER` overrides `user` in config. - `ENTRYPOINT_USER` overrides `user` in config.
- `ENTRYPOINT_GROUP` overrides `group` in config. - `ENTRYPOINT_GROUP` overrides `group` in config.
- `ENTRYPOINT_DISABLE_SERVICE` exits container with 0 before doing anything. Useful to disable container using environement.

View File

@ -18,3 +18,4 @@ Some setups can be overridden using environment variables in the container.
in ``entrypoint-config.yml``. in ``entrypoint-config.yml``.
- ``ENTRYPOINT_USER`` overrides ``user`` in config. - ``ENTRYPOINT_USER`` overrides ``user`` in config.
- ``ENTRYPOINT_GROUP`` overrides ``group`` in config. - ``ENTRYPOINT_GROUP`` overrides ``group`` in config.
- ``ENTRYPOINT_DISABLE_SERVICE`` exits container with 0 before doing anything. Useful to disable container using environement.

View File

@ -53,6 +53,11 @@ class Entrypoint(object):
"""Is command handled by entrypoint""" """Is command handled by entrypoint"""
return self.config.command.is_handled return self.config.command.is_handled
@property
def is_disabled(self):
"""Return if service is disabled using environment"""
return 'ENTRYPOINT_DISABLE_SERVICE' in os.environ
@property @property
def should_config(self): def should_config(self):
"""Check environment to tell if config should apply anyway""" """Check environment to tell if config should apply anyway"""
@ -63,6 +68,15 @@ class Entrypoint(object):
"""Check if command output should be displayed using logging or not""" """Check if command output should be displayed using logging or not"""
return 'ENTRYPOINT_RAW' in os.environ return 'ENTRYPOINT_RAW' in os.environ
def exit_if_disabled(self):
"""Exist 0 if service is disabled"""
if not self.is_disabled:
return
self.log.warning("Service is disabled by 'ENTRYPOINT_DISABLE_SERVICE' "
"environement variable... exiting with 0")
exit(0)
def apply_conf(self): def apply_conf(self):
"""Apply config to template files""" """Apply config to template files"""
env = Environment(loader=FileSystemLoader('/')) env = Environment(loader=FileSystemLoader('/'))
@ -120,6 +134,7 @@ def main(argv):
argv.pop(0) argv.pop(0)
entry = Entrypoint(args=argv) entry = Entrypoint(args=argv)
try: try:
entry.exit_if_disabled()
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()

View File

@ -1,5 +1,3 @@
PyYAML==3.11
Pygments==2.0.2
argparse==1.4.0 argparse==1.4.0
aspy.yaml==0.2.1 aspy.yaml==0.2.1
blessings==1.6 blessings==1.6
@ -16,6 +14,8 @@ jsonschema==2.5.1
nodeenv==0.13.6 nodeenv==0.13.6
ordereddict==1.1 ordereddict==1.1
pre-commit==0.7.5 pre-commit==0.7.5
Pygments==2.0.2
PyYAML==3.11
requests==2.7.0 requests==2.7.0
six==1.10.0 six==1.10.0
texttable==0.8.4 texttable==0.8.4

View File

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

View File

@ -6,6 +6,7 @@ import fnmatch
import os import os
from multiprocessing import Process from multiprocessing import Process
import pytest
from yaml import load from yaml import load
from yaml import Loader from yaml import Loader
@ -268,6 +269,8 @@ def test_user_env():
assert entry.config.user == 1009 assert entry.config.user == 1009
del os.environ['ENTRYPOINT_USER']
def test_group_env(): def test_group_env():
os.environ['ENTRYPOINT_GROUP'] = '100' os.environ['ENTRYPOINT_GROUP'] = '100'
@ -279,3 +282,17 @@ def test_group_env():
entry = Entrypoint(conf='configs/base.yml') entry = Entrypoint(conf='configs/base.yml')
assert entry.config.group == 1010 assert entry.config.group == 1010
del os.environ['ENTRYPOINT_GROUP']
def test_disabled_service():
os.environ['ENTRYPOINT_DISABLE_SERVICE'] = 'true'
entry = Entrypoint(conf='configs/base.yml')
assert entry.is_disabled
with pytest.raises(SystemExit):
entry.exit_if_disabled()
del os.environ['ENTRYPOINT_DISABLE_SERVICE']