Update git submodules
[oom.git] / kubernetes / readiness / src / main / scripts / job_complete.py
1 #!/usr/bin/env python
2 import getopt
3 import logging
4 import os
5 import sys
6 import time
7 import random
8
9 from kubernetes import client
10
11 # extract env variables.
12 namespace = os.environ['NAMESPACE']
13 cert = os.environ['CERT']
14 host = os.environ['KUBERNETES_SERVICE_HOST']
15 token_path = os.environ['TOKEN']
16
17 with open(token_path, 'r') as token_file:
18     token = token_file.read().replace('\n', '')
19
20 # setup logging
21 log = logging.getLogger(__name__)
22 handler = logging.StreamHandler(sys.stdout)
23 formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
24 handler.setFormatter(formatter)
25 handler.setLevel(logging.INFO)
26 log.addHandler(handler)
27 log.setLevel(logging.INFO)
28
29 configuration = client.Configuration()
30 configuration.host = "https://" + host
31 configuration.ssl_ca_cert = cert
32 configuration.api_key['authorization'] = token
33 configuration.api_key_prefix['authorization'] = 'Bearer'
34 batchV1Api = client.BatchV1Api(client.ApiClient(configuration))
35
36
37 def is_job_complete(job_name):
38     complete = False
39     log.info("Checking if " + job_name + "  is complete")
40     response = ""
41     try:
42         response = batchV1Api.read_namespaced_job_status(job_name, namespace)
43         if response.status.succeeded == 1:
44             job_status_type = response.status.conditions[0].type
45             if job_status_type == "Complete":
46                 complete = True
47             else:
48                 log.info(job_name + " is not complete")
49         else:
50             log.info(job_name + " has not succeeded yet")
51         return complete
52     except Exception as e:
53         log.error("Exception when calling read_namespaced_job_status: %s\n" % e)
54
55
56 DEF_TIMEOUT = 10
57 DESCRIPTION = "Kubernetes container job complete check utility"
58 USAGE = "Usage: job_complete.py [-t <timeout>] -j <job_name> " \
59         "[-j <job_name> ...]\n" \
60         "where\n" \
61         "<timeout> - wait for container job complete timeout in min, " \
62         "default is " + str(DEF_TIMEOUT) + "\n" \
63         "<job_name> - name of the job to wait for\n"
64
65
66 def main(argv):
67     # args are a list of job names
68     job_names = []
69     timeout = DEF_TIMEOUT
70     try:
71         opts, args = getopt.getopt(argv, "hj:t:", ["job-name=",
72                                                    "timeout=",
73                                                    "help"])
74         for opt, arg in opts:
75             if opt in ("-h", "--help"):
76                 print("%s\n\n%s" % (DESCRIPTION, USAGE))
77                 sys.exit()
78             elif opt in ("-j", "--job-name"):
79                 job_names.append(arg)
80             elif opt in ("-t", "--timeout"):
81                 timeout = float(arg)
82     except (getopt.GetoptError, ValueError) as e:
83         print("Error parsing input parameters: %s\n" % e)
84         print(USAGE)
85         sys.exit(2)
86     if job_names.__len__() == 0:
87         print("Missing required input parameter(s)\n")
88         print(USAGE)
89         sys.exit(2)
90
91     for job_name in job_names:
92         timeout = time.time() + timeout * 60
93         while True:
94             complete = is_job_complete(job_name)
95             if complete is True:
96                 break
97             elif time.time() > timeout:
98                 log.warning("timed out waiting for '" + job_name +
99                             "' to be completed")
100                 exit(1)
101             else:
102                 # spread in time potentially parallel execution in multiple
103                 # containers
104                 time.sleep(random.randint(5, 11))
105
106
107 if __name__ == "__main__":
108     main(sys.argv[1:])