Policy Reconfiguration, Component Spec, Help text
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / util / docker_util.py
index b59688b..3e29f5c 100644 (file)
@@ -1,7 +1,7 @@
 # ============LICENSE_START=======================================================
 # org.onap.dcae
 # ================================================================================
-# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
 # ================================================================================
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@ import dockering as doc
 from dcae_cli.util.logger import get_logger
 from dcae_cli.util.exc import DcaeException
 
-
 dlog = get_logger('Docker')
 
 _reg_img = 'gliderlabs/registrator:latest'
@@ -74,30 +73,19 @@ def build_envs(profile, docker_config, instance_name):
 # TODO: Consolidate these two docker client methods. Need ability to invoke local
 # vs remote Docker engine
 
-def get_docker_client(profile):
-    base_url = "tcp://{0}".format(profile.docker_host)
+def get_docker_client(profile, logins=[]):
+    hostname, port = profile.docker_host.split(":")
     try:
-        client = docker.Client(base_url=base_url)
+        client = doc.create_client(hostname, port, logins=logins)
         client.ping()
         return client
     except:
         raise DockerError('Could not connect to the Docker daemon. Is it running?')
 
-def _get_docker_client(client_funcs=(docker.Client, docker.from_env)):
-    '''Returns a docker client object'''
-    for func in client_funcs:
-        try:
-            client = func(version='auto')
-            client.ping()
-            return client
-        except:
-            continue
-    raise DockerError('Could not connect to the Docker daemon. Is it running?')
-
 
 def image_exists(image):
     '''Returns True if the image exists locally'''
-    client = _get_docker_client()
+    client = docker.from_env(version="auto")
     return True if client.images(image) else False
 
 
@@ -162,7 +150,8 @@ def _run_registrator(client, external_ip=None):
 # High level calls
 #
 
-def deploy_component(profile, image, instance_name, docker_config, should_wait=False):
+def deploy_component(profile, image, instance_name, docker_config, should_wait=False,
+        logins=[]):
     """Deploy Docker component
 
     This calls runs a Docker container detached.  The assumption is that the Docker
@@ -170,6 +159,13 @@ def deploy_component(profile, image, instance_name, docker_config, should_wait=F
 
     TODO: Split out the wait functionality
 
+    Args
+    ----
+    logins (list): List of objects where the objects are each a docker login of
+    the form:
+
+        {"registry": .., "username":.., "password":.. }
+
     Returns
     -------
     Dict that is the result from a Docker inspect call
@@ -188,9 +184,10 @@ def deploy_component(profile, image, instance_name, docker_config, should_wait=F
         raise DockerConstructionError("Could not resolve the docker hostname:{0}".format(dh))
 
     envs = build_envs(profile, docker_config, instance_name)
-    client = get_docker_client(profile)
+    client = get_docker_client(profile, logins=logins)
 
     config = doc.create_container_config(client, image, envs, hcp)
+
     return _run_container(client, config, name=instance_name, wait=should_wait)
 
 
@@ -213,3 +210,17 @@ def undeploy_component(client, image, instance_name):
     except Exception as e:
         dlog.error("Error while undeploying Docker container/image: {0}".format(e))
         return False
+
+def reconfigure(client, instance_name, command):
+    """  Execute the Reconfig script in the Docker container  """
+
+    #  'command' has 3 parts in a list (1 Command and 2 ARGs)
+    exec_Id = client.exec_create(container=instance_name, cmd=command)
+
+    exec_start_resp = client.exec_start(exec_Id, stream=True)
+
+    #  Using a 'single' generator response to solve issue of 'start_exec' returning control after 6 minutes
+    for response in exec_start_resp:
+        dlog.info("Reconfig Script execution response: {:}".format(response))
+        exec_start_resp.close()
+        break