Release version 1.13.7
[sdc.git] / sdc-os-chef / kubernetes / sdc / templates / configmaps / sdc-check-job-completion.yaml
1 ---
2 kind: ConfigMap
3 apiVersion: v1
4 metadata:
5   name: sdc-check-job-completion
6   namespace: onap-sdc
7 data:
8   sdc_check_job_completion.py: |
9     #!/usr/bin/python
10     from __future__ import print_function
11     import time, argparse, logging, sys, os
12     import kubernetes.client
13     from kubernetes import client, config
14     from pprint import pprint
15     
16     #extract env variables.
17     namespace = os.environ['NAMESPACE']
18     cert = os.environ['CERT']
19     host = os.environ['KUBERNETES_SERVICE_HOST']
20     token_path = os.environ['TOKEN']
21     
22     with open(token_path, 'r') as token_file:
23         token = token_file.read().replace('\n', '')
24     
25     client.configuration.api_key['authorization'] = token
26     client.configuration.api_key_prefix['authorization'] = 'Bearer'
27     client.configuration.host = "https://" + str(host)
28     client.configuration.ssl_ca_cert = cert
29     
30     api_instance = client.BatchV1Api()
31     
32     #setup logging
33     log = logging.getLogger(__name__)
34     handler = logging.StreamHandler(sys.stdout)
35     handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
36     handler.setLevel(logging.INFO)
37     log.addHandler(handler)
38     log.setLevel(logging.INFO)
39     
40     
41     def is_ready(job_name):
42         log.info( "[INFO] Checking if " + job_name + "  is completed")
43         pretty = True
44         job_status = False
45
46         try:
47             api_response = api_instance.read_namespaced_job_status(job_name, namespace, pretty=pretty)
48         except Exception as e:
49           print("Exception when calling BatchV1Api->read_namespaced_job_status: %s\n" % e)
50
51         pprint(api_response)
52         if api_response.status.succeeded == 1:
53           job_status_type = api_response.status.conditions[0].type
54           if job_status_type == "Complete":
55             job_status = True
56             
57         print("[DBG] jobStatus: " + unicode(job_status))
58         return job_status
59
60
61     def main(args):
62         for job_name in args:
63             timeout = time.time() + 60 * 10
64             while True:
65                 ready = is_ready(job_name)
66                 if ready is True :
67                     break
68                 elif time.time() > timeout:
69                     log.warning( "timed out waiting for '" + job_name + "' to be ready")
70                     exit(1)
71                 else:
72                     time.sleep(5)
73     
74     
75     if __name__ == "__main__":
76         parser = argparse.ArgumentParser(description='Process some names.')
77         parser.add_argument('--job-name', action='append', required=True, help='A container name')
78         args = parser.parse_args()
79         arg_dict = vars(args)
80     
81         for arg in arg_dict.itervalues():
82             main(arg)