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