Cloudify support for OOM

Issue-ID: OOM-106
Change-Id: Ie0a37ef378fd1907825da181c81502c6fbe9134c
Signed-off-by: Marek Wolczanski <marek.wolczanski@cloudify.co>
diff --git a/cloudify-onap/docker-custom-readiness/Dockerfile b/cloudify-onap/docker-custom-readiness/Dockerfile
new file mode 100644
index 0000000..d42456d
--- /dev/null
+++ b/cloudify-onap/docker-custom-readiness/Dockerfile
@@ -0,0 +1,21 @@
+from ubuntu:16.04
+
+ENV no_proxy "localhost,127.0.0.1,.cluster.local,$KUBERNETES_SERVICE_HOST"
+# Setup Corporate proxy
+ENV https_proxy ${HTTPS_PROXY}
+ENV http_proxy ${HTTP_PROXY}
+
+# Additional packages
+RUN apt-get update
+RUN apt-get install -y apt-utils git wget curl dnsutils python python-pip jq net-tools coreutils vim
+
+RUN pip install requests pyyaml kubernetes==1.0.2
+
+
+ENV CERT="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
+ENV TOKEN="/var/run/secrets/kubernetes.io/serviceaccount/token"
+
+COPY ready.py /root/ready.py
+RUN chmod a+x /root/ready.py
+#ENTRYPOINT /root/ready.py
+
diff --git a/cloudify-onap/docker-custom-readiness/ready.py b/cloudify-onap/docker-custom-readiness/ready.py
new file mode 100644
index 0000000..22b24d3
--- /dev/null
+++ b/cloudify-onap/docker-custom-readiness/ready.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python
+#from kubernetes import client, config
+import kubernetes
+import time, argparse, logging, sys, os, base64
+import yaml
+
+#setup logging
+log = logging.getLogger(__name__)
+handler = logging.StreamHandler(sys.stdout)
+handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
+handler.setLevel(logging.DEBUG)
+log.addHandler(handler)
+log.setLevel(logging.DEBUG)
+
+
+def is_ready(container_name):
+    log.info( "Checking if " + container_name + "  is ready")
+
+    kubernetes.config.kube_config.KubeConfigLoader(config_dict=get_k8s_config_env()).load_and_set()
+    client = kubernetes.client
+    namespace = get_namespace_env()
+    v1 = client.CoreV1Api()
+
+    ready = False
+
+    try:
+        response = v1.list_namespaced_pod(namespace=namespace, watch=False)
+        for i in response.items:
+            for s in i.status.container_statuses:
+                if s.name == container_name:
+                    log.debug ( "response %s" % response )
+                    ready = s.ready
+                    if not ready:
+                        log.info( container_name + " is not ready.")
+                    else:
+                        log.info( container_name + " is ready!")
+                else:
+                    continue
+        return ready
+    except Exception as e:
+        log.error("Exception when calling list_namespaced_pod: %s\n" % e)
+
+
+def get_k8s_config_env():
+    try:
+        k8s_config_env = os.environ.get("K8S_CONFIG_B64")
+        decoded = base64.b64decode(k8s_config_env)
+        return yaml.load(decoded)
+    except KeyError as ke:
+        raise Exception("K8S_CONFIG_B64 variable is not set.")
+
+
+def get_namespace_env():
+    try:
+        namespace_env = os.environ.get("NAMESPACE")
+        return namespace_env
+    except KeyError as ke:
+        raise Exception("NAMESPACE variable is not set.")
+
+
+def main(args):#from kubernetes import client, config
+
+    # args are a list of container names
+    for container_name in args:
+        # 5 min, TODO: make configurable
+        timeout = time.time() + 60 * 10
+        while True:
+            ready = is_ready(container_name)
+            if ready is True:
+                break
+            elif time.time() > timeout:
+                log.warning( "timed out waiting for '" + container_name + "' to be ready")
+                exit(1)
+            else:
+                time.sleep(5)
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description='Process some names.')
+    parser.add_argument('--container-name', action='append', required=True, help='A container name')
+    args = parser.parse_args()
+    arg_dict = vars(args)
+
+    for arg in arg_dict.itervalues():
+        main(arg)