-
Notifications
You must be signed in to change notification settings - Fork 16
/
ci-jobs
executable file
·204 lines (147 loc) · 5.96 KB
/
ci-jobs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
# pylint: disable=invalid-name,missing-docstring
import argparse
import getpass
import subprocess
import sys
from time import sleep
import jenkins
def parse_pass_lines(lines):
for line in lines:
if not line.startswith('#') and ': ' in line:
yield line.split(': ', 1)
def get_pass(name):
try:
output = subprocess.check_output(['gopass', 'show', name], universal_newlines=True)
except subprocess.CalledProcessError:
return None, {}
lines = output.splitlines()
if not lines:
return None, {}
password = lines[0]
extras = dict(parse_pass_lines(lines[1:]))
return password, extras
def get_connection(url='https://ci.theforeman.org', username=None,
password=None, pass_name='theforeman/jenkins-token'):
if not password or not username:
pass_password, pass_extras = get_pass(pass_name)
if not password:
password = pass_password
if not username:
try:
username = pass_extras['Username']
except KeyError:
username = getpass.getuser()
return jenkins.Jenkins(url, username=username, password=password)
def get_last_build_number(server, job):
job_info = server.get_job_info(job)
return job_info['lastBuild']['number'] if job_info['lastBuild'] else -1
def build_job(job, parameters=None, wait=True):
server = get_connection()
# Get the last build so we can poll later
build_number_before = get_last_build_number(server, job)
server.build_job(job, parameters)
while True:
last_build_number = get_last_build_number(server, job)
if last_build_number > build_number_before:
break
sleep(1)
if wait:
_poll(server, job, last_build_number)
else:
build_info = server.get_build_info(job, last_build_number)
print(build_info['url'])
def _poll(server, job, build_number):
build_info = server.get_build_info(job, build_number)
print(build_info['url'])
while build_info['building']:
try:
sleep(1)
build_info = server.get_build_info(job, build_number)
except KeyboardInterrupt:
raise SystemExit('Aborted')
print(build_info['result'])
return build_info['result']
def ping(args): # pylint: disable=unused-argument
server = get_connection()
user = server.get_whoami()
version = server.get_version()
print(f'Hello {user["fullName"]} from Jenkins {version}')
def poll(args):
server = get_connection()
job = args.job
if args.build_number:
build_number = args.build_number
else:
build_number = get_last_build_number(server, job)
_poll(server, job, build_number)
def release(args):
try:
major, minor, _ = args.version.split('.', 2)
except ValueError:
raise argparse.ArgumentTypeError('Pass in the full version like 1.24.0-RC1')
if args.type == 'packages':
job = 'release_packages'
else:
job = 'tarballs-release'
parameters = {
'version': args.version,
'major_version': f'{major}.{minor}',
}
build_job(job, parameters, args.wait)
def pipeline(args):
try:
major, minor = args.version.split('.', 2)[:2]
except ValueError:
msg = 'Pass in the version like 2.4.0-RC1 or 2.4, depending on the pipeline'
raise argparse.ArgumentTypeError(msg)
parameters = None
if args.type == 'foreman':
job = f'{args.type}-{major}.{minor}-release-pipeline'
parameters = {
'expected_version': args.version,
}
elif args.type in ('katello', 'candlepin', 'pulpcore'):
job = f'{args.type}-{major}.{minor}-rpm-pipeline'
else:
job = f'foreman-{args.type}-{major}.{minor}-rpm-pipeline'
build_job(job, parameters, args.wait)
def green(args):
job = args.job
print(f'Checking job {job}')
server = get_connection()
last_build_number = server.get_job_info(job)['lastCompletedBuild']['number']
result = _poll(server, job, last_build_number)
if result != 'SUCCESS':
sys.exit(1)
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
ping_parser = subparsers.add_parser('ping', help='Ping the Jenkins server')
ping_parser.set_defaults(func=ping)
poll_parser = subparsers.add_parser('poll', help='Poll a running job till completion')
poll_parser.set_defaults(func=poll)
poll_parser.add_argument('job', help='The job to poll')
poll_parser.add_argument('build_number', help='The specific job number to poll', nargs='?')
release_parser = subparsers.add_parser('release', help='Run a release job')
release_parser.set_defaults(func=release)
release_parser.add_argument('type', choices=('tarballs', 'packages'))
release_parser.add_argument('version', help='The full version like 1.24.0-RC1')
release_parser.add_argument('--no-wait', action='store_false', dest='wait',
help="Don't wait for the job to complete and exit immediately")
pipeline_parser = subparsers.add_parser('pipeline', help='Run a pipeline')
pipeline_parser.set_defaults(func=pipeline)
pipeline_parser.add_argument('type', choices=('foreman', 'client', 'plugins', 'katello', 'candlepin', 'pulpcore'))
pipeline_parser.add_argument('version', help='The full version like 1.24.0-RC1')
pipeline_parser.add_argument('--no-wait', action='store_false', dest='wait',
help="Don't wait for the job to complete and exit immediately")
job_green_parser = subparsers.add_parser('green', help='Verify a job is green')
job_green_parser.set_defaults(func=green)
job_green_parser.add_argument('job', help='The job to poll')
args = parser.parse_args()
# Python 3.7 added required=True to add_subparsers but 3.6 is still common
if not args.func:
parser.exit(2, parser.format_help())
args.func(args)
if __name__ == '__main__':
main()