From f5ce303053c5560455572e39e1dbe6e5e7bf4c15 Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Thu, 7 Sep 2017 16:00:09 -0400 Subject: Add in functionality for policy notification Change-Id: I4ef4bc9c35266814f226cbad13c55d11901c8e79 Issue-Id: DCAEGEN2-97 Signed-off-by: Michael Hwang --- python-dockering/dockering/core.py | 33 +++++++++++++++++++++++++++++++-- python-dockering/setup.py | 2 +- python-dockering/tests/test_core.py | 21 +++++++++++++++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/python-dockering/dockering/core.py b/python-dockering/dockering/core.py index dcd5908..9ef1ae3 100644 --- a/python-dockering/dockering/core.py +++ b/python-dockering/dockering/core.py @@ -25,8 +25,6 @@ from dockering import config_building as cb from dockering import utils -# TODO: Replace default for logins to source it from Consul..perhaps - def create_client(hostname, port, reauth=False, logins=[]): """Create Docker client @@ -134,3 +132,34 @@ def remove_image(client, image_name): # Failure to remove image is not classified as terrible..for now return False + +def build_policy_update_cmd(script_path, use_sh=True, msg_type="policy", **kwargs): + """Build command to execute for policy update""" + data = json.dumps(kwargs or {}) + + if use_sh: + return ['/bin/sh', script_path, msg_type, data] + else: + return [script_path, msg_type, data] + +def notify_for_policy_update(client, container_id, cmd): + """Notify Docker container that policy update occurred + + Notify the Docker container by doing Docker exec of passed-in command + + Args: + ----- + container_id: (string) + cmd: (list) of strings each entry being part of the command + """ + try: + result = client.exec_create(container=container_id, + cmd=cmd) + result = client.exec_start(exec_id=result['Id']) + + utils.logger.info("Pass to docker exec {0} {1} {2}".format( + container_id, cmd, result)) + + return result + except Exception as e: + raise DockerError(e) diff --git a/python-dockering/setup.py b/python-dockering/setup.py index 1c51ab9..418403e 100644 --- a/python-dockering/setup.py +++ b/python-dockering/setup.py @@ -23,7 +23,7 @@ from setuptools import setup setup( name='python-dockering', description='Library used to manage Docker containers in DCAE', - version="1.2.0", + version="1.3.0", author="Michael Hwang", email="dcae@lists.openecomp.org", packages=['dockering'], diff --git a/python-dockering/tests/test_core.py b/python-dockering/tests/test_core.py index e99dbd4..f66f88c 100644 --- a/python-dockering/tests/test_core.py +++ b/python-dockering/tests/test_core.py @@ -25,6 +25,7 @@ from dockering import core as doc from dockering.exceptions import DockerError, DockerConnectionError +@pytest.mark.skip(reason="Need to automatically setup Docker engine and maybe Consul") def test_create_client(): # Bad - Could not connect to docker engine @@ -33,8 +34,8 @@ def test_create_client(): # TODO: Does pytest provide an env file? -CONSUL_HOST = os.environ["CONSUL_HOST"] -EXTERNAL_IP = os.environ["EXTERNAL_IP"] +CONSUL_HOST = os.environ.get("CONSUL_HOST") +EXTERNAL_IP = os.environ.get("EXTERNAL_IP") @pytest.mark.skip(reason="Need to automatically setup Docker engine and maybe Consul") def test_create_container(): @@ -69,3 +70,19 @@ def test_create_container(): doc.stop_then_remove_container(client, scn) except: print("Container removal failed") + + +def test_build_policy_update_cmd(): + assert ["/bin/sh", "/bin/foo", "policy", "{}"] == doc.build_policy_update_cmd("/bin/foo") + assert ["/bin/foo", "policy", "{}"] == doc.build_policy_update_cmd("/bin/foo", use_sh=False) + + kwargs = { "bar": "baz" } + + assert ["/bin/foo", "policy", "{\"bar\": \"baz\"}"] == doc.build_policy_update_cmd( + "/bin/foo", use_sh=False, **kwargs) + + assert ["/bin/foo", "policy", "{\"application_config\": {\"key\": \"hello world\"}}"] \ + == doc.build_policy_update_cmd("/bin/foo", use_sh=False, + application_config={"key": "hello world"}) + + -- cgit 1.2.3-korg