-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait_for_tarball.py
executable file
·82 lines (71 loc) · 2.72 KB
/
wait_for_tarball.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
#
# Script to wait for a tarball to be generated in Jenkins
#
# Copyright 2013 Thierry Carrez <[email protected]>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import re
import requests
import time
def get_from_jenkins(job_url, tree=None):
api_job_url = job_url + "api/json"
if tree:
api_job_url += "?tree=%s" % tree
r = requests.get(api_job_url, verify=False)
if r.status_code != 200:
raise IOError("Return code %d from %s" % (r.status_code, api_job_url))
return r.json()
def find_job_url(sha=None, retries=60, wait=10):
retry = 0
shaexpr = (r'https://review\.openstack\.org/gitweb\?p=.*\.git;'
'a=commitdiff;h=(.*)$')
while retry < retries:
statusjson = requests.get('http://zuul.openstack.org/status.json')
status = statusjson.json()
for pipeline in status['pipelines']:
for queue in pipeline['change_queues']:
for head in queue['heads']:
for r in head:
for job in r['jobs']:
if job['name'].endswith('-tarball'):
c = re.match(shaexpr, r['url'])
if c and c.group(1).startswith(sha):
return job['url']
retry += 1
time.sleep(wait)
else:
raise IOError("timeout")
def wait_for_completion(job_url, retries=60, wait=10):
retry = 0
while retry < retries:
job_json = get_from_jenkins(job_url)
if not job_json['building']:
return job_json['artifacts'][0]['displayPath']
retry += 1
time.sleep(wait)
else:
raise IOError("timeout")
# Argument parsing
parser = argparse.ArgumentParser(description='Wait for a tarball to be built '
'on Jenkins.')
parser.add_argument('sha', help='commit that will generate the tarball')
args = parser.parse_args()
print "Looking for matching job..."
job_url = find_job_url(sha=args.sha)
print "Found at %s" % job_url
print "Waiting for job completion..."
tarball = wait_for_completion(job_url)
print "Tarball generated at %s" % tarball