|
| 1 | +# Copied from: |
| 2 | +# https://github.com/dmakhno/travis_after_all |
| 3 | +# commit b7172bca9 |
| 4 | +import os |
| 5 | +import json |
| 6 | +import time |
| 7 | +import logging |
| 8 | + |
| 9 | +try: |
| 10 | + import urllib.request as urllib2 |
| 11 | +except ImportError: |
| 12 | + import urllib2 |
| 13 | + |
| 14 | +log = logging.getLogger("travis.leader") |
| 15 | +log.addHandler(logging.StreamHandler()) |
| 16 | +log.setLevel(logging.INFO) |
| 17 | + |
| 18 | +TRAVIS_JOB_NUMBER = 'TRAVIS_JOB_NUMBER' |
| 19 | +TRAVIS_BUILD_ID = 'TRAVIS_BUILD_ID' |
| 20 | +POLLING_INTERVAL = 'LEADER_POLLING_INTERVAL' |
| 21 | + |
| 22 | +build_id = os.getenv(TRAVIS_BUILD_ID) |
| 23 | +polling_interval = int(os.getenv(POLLING_INTERVAL, '5')) |
| 24 | + |
| 25 | +#assume, first job is the leader |
| 26 | +is_leader = lambda job_number: job_number.endswith('.1') |
| 27 | + |
| 28 | +if not os.getenv(TRAVIS_JOB_NUMBER): |
| 29 | + # seems even for builds with only one job, this won't get here |
| 30 | + log.fatal("Don't use defining leader for build without matrix") |
| 31 | + exit(1) |
| 32 | +elif is_leader(os.getenv(TRAVIS_JOB_NUMBER)): |
| 33 | + log.info("This is a leader") |
| 34 | +else: |
| 35 | + #since python is subprocess, env variables are exported back via file |
| 36 | + with open(".to_export_back", "w") as export_var: |
| 37 | + export_var.write("BUILD_MINION=YES") |
| 38 | + log.info("This is a minion") |
| 39 | + exit(0) |
| 40 | + |
| 41 | + |
| 42 | +class MatrixElement(object): |
| 43 | + def __init__(self, json_raw): |
| 44 | + self.is_finished = json_raw['finished_at'] is not None |
| 45 | + self.is_succeeded = json_raw['result'] == 0 |
| 46 | + self.number = json_raw['number'] |
| 47 | + self.is_leader = is_leader(self.number) |
| 48 | + |
| 49 | + |
| 50 | +def matrix_snapshot(): |
| 51 | + """ |
| 52 | + :return: Matrix List |
| 53 | + """ |
| 54 | + response = urllib2.build_opener().open("https://api.travis-ci.org/builds/{0}".format(build_id)).read() |
| 55 | + raw_json = json.loads(response) |
| 56 | + matrix_without_leader = [MatrixElement(element) for element in raw_json["matrix"]] |
| 57 | + return matrix_without_leader |
| 58 | + |
| 59 | + |
| 60 | +def wait_others_to_finish(): |
| 61 | + def others_finished(): |
| 62 | + """ |
| 63 | + Dumps others to finish |
| 64 | + Leader cannot finish, it is working now |
| 65 | + :return: tuple(True or False, List of not finished jobs) |
| 66 | + """ |
| 67 | + snapshot = matrix_snapshot() |
| 68 | + finished = [el.is_finished for el in snapshot if not el.is_leader] |
| 69 | + return reduce(lambda a, b: a and b, finished), [el.number for el in snapshot if |
| 70 | + not el.is_leader and not el.is_finished] |
| 71 | + |
| 72 | + while True: |
| 73 | + finished, waiting_list = others_finished() |
| 74 | + if finished: break |
| 75 | + log.info("Leader waits for minions {0}...".format(waiting_list)) # just in case do not get "silence timeout" |
| 76 | + time.sleep(polling_interval) |
| 77 | + |
| 78 | + |
| 79 | +try: |
| 80 | + wait_others_to_finish() |
| 81 | + |
| 82 | + final_snapshot = matrix_snapshot() |
| 83 | + log.info("Final Results: {0}".format([(e.number, e.is_succeeded) for e in final_snapshot])) |
| 84 | + |
| 85 | + BUILD_AGGREGATE_STATUS = 'BUILD_AGGREGATE_STATUS' |
| 86 | + others_snapshot = [el for el in final_snapshot if not el.is_leader] |
| 87 | + if reduce(lambda a, b: a and b, [e.is_succeeded for e in others_snapshot]): |
| 88 | + os.environ[BUILD_AGGREGATE_STATUS] = "others_succeeded" |
| 89 | + elif reduce(lambda a, b: a and b, [not e.is_succeeded for e in others_snapshot]): |
| 90 | + log.error("Others Failed") |
| 91 | + os.environ[BUILD_AGGREGATE_STATUS] = "others_failed" |
| 92 | + else: |
| 93 | + log.warn("Others Unknown") |
| 94 | + os.environ[BUILD_AGGREGATE_STATUS] = "unknown" |
| 95 | + #since python is subprocess, env variables are exported back via file |
| 96 | + with open(".to_export_back", "w") as export_var: |
| 97 | + export_var.write("BUILD_LEADER=YES {0}={1}".format(BUILD_AGGREGATE_STATUS, os.environ[BUILD_AGGREGATE_STATUS])) |
| 98 | + |
| 99 | +except Exception as e: |
| 100 | + log.fatal(e) |
0 commit comments