vaibhavjayas | 5ca5465 | 2018-07-31 09:23:16 +0000 | [diff] [blame] | 1 | # Copyright © 2018 Amdocs, Bell Canada |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Sonsino, Ofir (os0695) | 2dda01d | 2018-05-30 18:41:14 +0300 | [diff] [blame] | 15 | apiVersion: v1 |
| 16 | kind: ConfigMap |
| 17 | metadata: |
| 18 | name: {{ include "common.fullname" . }}-check-job-completion |
| 19 | namespace: {{ include "common.namespace" . }} |
| 20 | data: |
| 21 | vid_check_job_completion.py: | |
| 22 | #!/usr/bin/python |
| 23 | from __future__ import print_function |
| 24 | import time, argparse, logging, sys, os |
| 25 | import kubernetes.client |
| 26 | from kubernetes import client, config |
| 27 | from pprint import pprint |
| 28 | |
| 29 | #extract env variables. |
| 30 | namespace = os.environ['NAMESPACE'] |
| 31 | cert = os.environ['CERT'] |
| 32 | host = os.environ['KUBERNETES_SERVICE_HOST'] |
| 33 | token_path = os.environ['TOKEN'] |
| 34 | |
| 35 | with open(token_path, 'r') as token_file: |
| 36 | token = token_file.read().replace('\n', '') |
| 37 | |
| 38 | client.configuration.api_key['authorization'] = token |
| 39 | client.configuration.api_key_prefix['authorization'] = 'Bearer' |
| 40 | client.configuration.host = "https://" + str(host) |
| 41 | client.configuration.ssl_ca_cert = cert |
| 42 | |
| 43 | api_instance = client.BatchV1Api() |
| 44 | |
| 45 | #setup logging |
| 46 | log = logging.getLogger(__name__) |
| 47 | handler = logging.StreamHandler(sys.stdout) |
| 48 | handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) |
| 49 | handler.setLevel(logging.INFO) |
| 50 | log.addHandler(handler) |
| 51 | log.setLevel(logging.INFO) |
| 52 | |
| 53 | |
| 54 | def is_ready(job_name): |
| 55 | log.info( "[INFO] Checking if " + job_name + " is completed") |
| 56 | pretty = True |
| 57 | job_status = False |
| 58 | |
| 59 | try: |
| 60 | api_response = api_instance.read_namespaced_job_status(job_name, namespace, pretty=pretty) |
| 61 | except Exception as e: |
| 62 | print("Exception when calling BatchV1Api->read_namespaced_job_status: %s\n" % e) |
| 63 | |
| 64 | pprint(api_response) |
| 65 | if api_response.status.succeeded == 1: |
| 66 | job_status_type = api_response.status.conditions[0].type |
| 67 | if job_status_type == "Complete": |
| 68 | job_status = True |
| 69 | |
| 70 | print("[DBG] jobStatus: " + unicode(job_status)) |
| 71 | return job_status |
| 72 | |
| 73 | |
| 74 | def main(args): |
| 75 | for job_name in args: |
| 76 | timeout = time.time() + 60 * 10 |
| 77 | while True: |
| 78 | ready = is_ready(job_name) |
| 79 | if ready is True : |
| 80 | break |
| 81 | elif time.time() > timeout: |
| 82 | log.warning( "timed out waiting for '" + job_name + "' to be ready") |
| 83 | exit(1) |
| 84 | else: |
| 85 | time.sleep(5) |
| 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | parser = argparse.ArgumentParser(description='Process some names.') |
| 90 | parser.add_argument('--job-name', action='append', required=True, help='A container name') |
| 91 | args = parser.parse_args() |
| 92 | arg_dict = vars(args) |
| 93 | |
| 94 | for arg in arg_dict.itervalues(): |
| 95 | main(arg) |
| 96 | |
| 97 | |