]>
Commit | Line | Data |
---|---|---|
bfc1fef6 AS |
1 | # https://github.com/dmakhno/travis_after_all/tree/b7172bca92d6dcfcec2a0eacdf14eb5f3a1ad627 |
2 | ||
3 | # The MIT License (MIT) | |
4 | # | |
5 | # Copyright (c) 2014 Dmytro Makhno | |
6 | # | |
7 | # Permission is hereby granted, free of charge, to any person obtaining a copy of | |
8 | # this software and associated documentation files (the "Software"), to deal in | |
9 | # the Software without restriction, including without limitation the rights to | |
10 | # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
11 | # the Software, and to permit persons to whom the Software is furnished to do so, | |
12 | # subject to the following conditions: | |
13 | # | |
14 | # The above copyright notice and this permission notice shall be included in all | |
15 | # copies or substantial portions of the Software. | |
16 | # | |
17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
19 | # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
20 | # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
21 | # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
23 | ||
24 | import os | |
25 | import json | |
26 | import time | |
27 | import logging | |
28 | ||
29 | try: | |
30 | import urllib.request as urllib2 | |
31 | except ImportError: | |
32 | import urllib2 | |
33 | ||
34 | log = logging.getLogger("travis.leader") | |
35 | log.addHandler(logging.StreamHandler()) | |
36 | log.setLevel(logging.INFO) | |
37 | ||
38 | TRAVIS_JOB_NUMBER = 'TRAVIS_JOB_NUMBER' | |
39 | TRAVIS_BUILD_ID = 'TRAVIS_BUILD_ID' | |
40 | POLLING_INTERVAL = 'LEADER_POLLING_INTERVAL' | |
41 | ||
42 | build_id = os.getenv(TRAVIS_BUILD_ID) | |
43 | polling_interval = int(os.getenv(POLLING_INTERVAL, '5')) | |
44 | ||
45 | #assume, first job is the leader | |
46 | is_leader = lambda job_number: job_number.endswith('.1') | |
47 | ||
48 | if not os.getenv(TRAVIS_JOB_NUMBER): | |
49 | # seems even for builds with only one job, this won't get here | |
50 | log.fatal("Don't use defining leader for build without matrix") | |
51 | exit(1) | |
52 | elif is_leader(os.getenv(TRAVIS_JOB_NUMBER)): | |
53 | log.info("This is a leader") | |
54 | else: | |
55 | #since python is subprocess, env variables are exported back via file | |
56 | with open(".to_export_back", "w") as export_var: | |
57 | export_var.write("BUILD_MINION=YES") | |
58 | log.info("This is a minion") | |
59 | exit(0) | |
60 | ||
61 | ||
62 | class MatrixElement(object): | |
63 | def __init__(self, json_raw): | |
64 | self.is_finished = json_raw['finished_at'] is not None | |
65 | self.is_succeeded = json_raw['result'] == 0 | |
66 | self.number = json_raw['number'] | |
67 | self.is_leader = is_leader(self.number) | |
68 | ||
69 | ||
70 | def matrix_snapshot(): | |
71 | """ | |
72 | :return: Matrix List | |
73 | """ | |
74 | response = urllib2.build_opener().open("https://api.travis-ci.org/builds/{0}".format(build_id)).read() | |
75 | raw_json = json.loads(response) | |
76 | matrix_without_leader = [MatrixElement(element) for element in raw_json["matrix"]] | |
77 | return matrix_without_leader | |
78 | ||
79 | ||
80 | def wait_others_to_finish(): | |
81 | def others_finished(): | |
82 | """ | |
83 | Dumps others to finish | |
84 | Leader cannot finish, it is working now | |
85 | :return: tuple(True or False, List of not finished jobs) | |
86 | """ | |
87 | snapshot = matrix_snapshot() | |
88 | finished = [el.is_finished for el in snapshot if not el.is_leader] | |
89 | return reduce(lambda a, b: a and b, finished), [el.number for el in snapshot if | |
90 | not el.is_leader and not el.is_finished] | |
91 | ||
92 | while True: | |
93 | finished, waiting_list = others_finished() | |
94 | if finished: break | |
95 | log.info("Leader waits for minions {0}...".format(waiting_list)) # just in case do not get "silence timeout" | |
96 | time.sleep(polling_interval) | |
97 | ||
98 | ||
99 | try: | |
100 | wait_others_to_finish() | |
101 | ||
102 | final_snapshot = matrix_snapshot() | |
103 | log.info("Final Results: {0}".format([(e.number, e.is_succeeded) for e in final_snapshot])) | |
104 | ||
105 | BUILD_AGGREGATE_STATUS = 'BUILD_AGGREGATE_STATUS' | |
106 | others_snapshot = [el for el in final_snapshot if not el.is_leader] | |
107 | if reduce(lambda a, b: a and b, [e.is_succeeded for e in others_snapshot]): | |
108 | os.environ[BUILD_AGGREGATE_STATUS] = "others_succeeded" | |
109 | elif reduce(lambda a, b: a and b, [not e.is_succeeded for e in others_snapshot]): | |
110 | log.error("Others Failed") | |
111 | os.environ[BUILD_AGGREGATE_STATUS] = "others_failed" | |
112 | else: | |
113 | log.warn("Others Unknown") | |
114 | os.environ[BUILD_AGGREGATE_STATUS] = "unknown" | |
115 | #since python is subprocess, env variables are exported back via file | |
116 | with open(".to_export_back", "w") as export_var: | |
117 | export_var.write("BUILD_LEADER=YES {0}={1}".format(BUILD_AGGREGATE_STATUS, os.environ[BUILD_AGGREGATE_STATUS])) | |
118 | ||
119 | except Exception as e: | |
120 | log.fatal(e) |