From ea72fac83c2fe182343b6bbd941d707b88d06e7a Mon Sep 17 00:00:00 2001 From: Christophe Mehay Date: Wed, 23 Nov 2016 12:37:10 +0100 Subject: [PATCH] Add env variable to diable service --- README.md | 1 + docs/extra.rst | 1 + pyentrypoint/entrypoint.py | 15 +++++++++++++++ requirements-dev.txt | 4 ++-- setup.py | 2 +- tests/pyentrypoint_test.py | 17 +++++++++++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index abc3990..cb7962f 100644 --- a/README.md +++ b/README.md @@ -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_USER` overrides `user` in config. - `ENTRYPOINT_GROUP` overrides `group` in config. +- `ENTRYPOINT_DISABLE_SERVICE` exits container with 0 before doing anything. Useful to disable container using environement. diff --git a/docs/extra.rst b/docs/extra.rst index 40b5657..213d5d5 100644 --- a/docs/extra.rst +++ b/docs/extra.rst @@ -18,3 +18,4 @@ Some setups can be overridden using environment variables in the container. in ``entrypoint-config.yml``. - ``ENTRYPOINT_USER`` overrides ``user`` in config. - ``ENTRYPOINT_GROUP`` overrides ``group`` in config. +- ``ENTRYPOINT_DISABLE_SERVICE`` exits container with 0 before doing anything. Useful to disable container using environement. diff --git a/pyentrypoint/entrypoint.py b/pyentrypoint/entrypoint.py index 226e412..561c6de 100644 --- a/pyentrypoint/entrypoint.py +++ b/pyentrypoint/entrypoint.py @@ -53,6 +53,11 @@ class Entrypoint(object): """Is command handled by entrypoint""" 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 def should_config(self): """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""" 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): """Apply config to template files""" env = Environment(loader=FileSystemLoader('/')) @@ -120,6 +134,7 @@ def main(argv): argv.pop(0) entry = Entrypoint(args=argv) try: + entry.exit_if_disabled() if not entry.is_handled and not entry.should_config: entry.log.warning("Running command without config") entry.launch() diff --git a/requirements-dev.txt b/requirements-dev.txt index 927c1c1..619fbf6 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,3 @@ -PyYAML==3.11 -Pygments==2.0.2 argparse==1.4.0 aspy.yaml==0.2.1 blessings==1.6 @@ -16,6 +14,8 @@ jsonschema==2.5.1 nodeenv==0.13.6 ordereddict==1.1 pre-commit==0.7.5 +Pygments==2.0.2 +PyYAML==3.11 requests==2.7.0 six==1.10.0 texttable==0.8.4 diff --git a/setup.py b/setup.py index 69ea988..6678754 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import setup # Thanks Sam and Max -__version__ = '0.4.2' +__version__ = '0.4.3' if __name__ == '__main__': setup( diff --git a/tests/pyentrypoint_test.py b/tests/pyentrypoint_test.py index 6a5db3e..db3c8f4 100644 --- a/tests/pyentrypoint_test.py +++ b/tests/pyentrypoint_test.py @@ -6,6 +6,7 @@ import fnmatch import os from multiprocessing import Process +import pytest from yaml import load from yaml import Loader @@ -268,6 +269,8 @@ def test_user_env(): assert entry.config.user == 1009 + del os.environ['ENTRYPOINT_USER'] + def test_group_env(): os.environ['ENTRYPOINT_GROUP'] = '100' @@ -279,3 +282,17 @@ def test_group_env(): entry = Entrypoint(conf='configs/base.yml') 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']