5 name: sdc-check-job-completion
8 sdc_check_job_completion.py: |
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
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']
22 with open(token_path, 'r') as token_file:
23 token = token_file.read().replace('\n', '')
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
30 api_instance = client.BatchV1Api()
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)
41 def is_ready(job_name):
42 log.info( "[INFO] Checking if " + job_name + " is completed")
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)
52 if api_response.status.succeeded == 1:
53 job_status_type = api_response.status.conditions[0].type
54 if job_status_type == "Complete":
57 print("[DBG] jobStatus: " + unicode(job_status))
63 timeout = time.time() + 60 * 10
65 ready = is_ready(job_name)
68 elif time.time() > timeout:
69 log.warning( "timed out waiting for '" + job_name + "' to be ready")
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()
81 for arg in arg_dict.itervalues():