-
Notifications
You must be signed in to change notification settings - Fork 3
/
launch.py
executable file
·432 lines (345 loc) · 11.7 KB
/
launch.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import atexit
import os
from time import sleep
import click
import jinja2
import requests
from termcolor import colored
JENKINS_URL = None
AUTH = None
@click.group()
@click.option(
'-j',
'--jenkins',
'jenkins_url',
envvar='JENKDO_URL',
required=True,
help='Jenkins URL. Can be set via env var "JENKDO_URL"'
)
@click.option(
'-u',
'--user',
'jenkins_user',
envvar='JENKDO_USER',
required=True,
help='User on jenkins to execute as. Can be set via env var "JENKDO_USER"'
)
@click.option(
'-p',
'--password',
'jenkins_password',
envvar='JENKDO_PASSWORD',
required=True,
prompt=True,
hide_input=True,
help='Jenkins password or token. Can be set via env var "JENKDO_PASSWORD"'
)
def cli(jenkins_url, jenkins_user, jenkins_password):
"""
Script for launch things on jenkins
Invocation without commands do nothing
"""
globals()['JENKINS_URL'] = jenkins_url
globals()['AUTH'] = requests.auth.HTTPBasicAuth(jenkins_user, jenkins_password)
pass
@cli.command()
@click.argument(
'jenkinsfile',
type=click.File('r'),
required=True
)
@click.argument(
'templatefile',
type=click.File('r'),
required=True,
default=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'template_job.xml')
)
@click.option(
'-k',
'--keep',
'keep',
is_flag=True,
show_default=False,
help='Don\'t delete a job when build ended'
)
@click.option(
'-v',
'--verbose',
'verbose',
is_flag=True,
show_default=False,
help='Print [Pipeline] rows in console output'
)
@click.option(
'-y',
'--yes',
'yes',
is_flag=True,
show_default=False,
help='Automatically agree with validation result'
)
@click.option(
'-f',
'--force',
'force',
is_flag=True,
show_default=False,
help='Delete a job if it\'s already exists'
)
def build(jenkinsfile, templatefile, keep, verbose, yes, force):
'''
Execute passed file as build on Jenkins
This script will create pipeline task from TEMPLATE_FILE with
JENKINS_FILE as pipeline script
JENKINS_FILE - File with pipeline. Should end on '.groovy' or '.jenkinsfile'
TEMPLATE_FILE - File with job config. Should be '.xml'.
See template_job.xml here.
Or get it from just created pipeline task via
'GET %JENKINS_URL%/job/%PIPELINE_TASK%/config.xml'.
Should contain '{{ jenkinsfile | forceescape() }}' in <script> section
'''
job = JenkinsJob(jenkinsfile, templatefile)
job.validate()
if not yes:
msg = '> Press Enter to continue or Ctrl+C to exit'
input(colored(msg, 'green'))
job.create(force)
if not keep:
atexit.register(job.delete)
job.start()
atexit.register(job.stop)
if job.watch_stream(verbose) == 0:
atexit.unregister(job.stop)
@cli.command()
@click.argument(
'script',
type=click.File('r'),
required=True
)
def script(script):
'''
Execute passed file as script on Jenkins
SCRIPT - File with script. Should end on '.groovy'.
'''
script_content = script.read()
script.close()
data = {
'script': script_content,
}
url = '{}/scriptText'.format(JENKINS_URL)
res = requests.post(url, data=data, auth=AUTH)
if res.status_code != 200:
msg = "> Script execution failed with code {}".format(res.status_code)
print(colored(msg,'red'))
msg = "> Response:"
print(colored(msg,'red'))
print(res.text)
exit(1)
print(res.text)
exit(0)
class JenkinsJob:
def __init__(self, jenkinsfile, template):
self.name = (
os.path.basename(jenkinsfile.name)
.replace('.groovy', '')
.replace('.jenkinsfile', ''))
self.jenkinsfile_content = jenkinsfile.read()
jenkinsfile.close()
self.template_content = template.read()
template.close()
self.build_number = None
self.build_url = None
self.queue_url = None
def validate(self):
'''Validate pipeline part of this taks on server'''
msg = '> Validating...'
print(colored(msg, 'green'))
url = '{}/pipeline-model-converter/validate'.format(JENKINS_URL)
data = {'jenkinsfile': self.jenkinsfile_content}
res = requests.post(
url,
auth=AUTH,
data=data)
msg = '> Validation response:'
print(colored(msg, 'green'))
print(res.text, end='')
def create(self, force):
'''Create this taks on server'''
if force:
url = ('{}/job/debug/job/{}/config.xml'
.format(JENKINS_URL, self.name))
res = requests.get(url, auth=AUTH)
if res.status_code != 404:
self.delete()
template = jinja2.Template(self.template_content)
filled_template = template.render(
jenkinsfile=self.jenkinsfile_content)
headers = {'Content-Type': 'text/xml'}
params = {'name': self.name}
url = '{}/job/debug/createItem'.format(JENKINS_URL)
res = requests.post(
url,
headers=headers,
data=filled_template.encode('utf-8'),
params=params,
auth=AUTH
)
if res.status_code != 200:
msg = "> Unable to create '{}' job".format(self.name)
print(colored(msg,'red'))
log_filename = self._log('create', res.text)
msg = "> See '{}' for additional info".format(log_filename)
print(colored(msg, 'red'))
exit(1)
msg = "> Job '{}' created".format(self.name)
print(colored(msg, 'green'))
def delete(self):
'''Delete this taks on server'''
url = '{}/job/debug/job/{}/doDelete'.format(JENKINS_URL, self.name)
res = requests.post(url, auth=AUTH)
if res.status_code != 200:
msg = "> Unable to delete '{}' job".format(self.name)
print(colored(msg, 'red'))
log_filename = self._log('delete', res.text)
msg = "> See '{}' for additional info".format(log_filename)
print(colored(msg, 'red'))
exit(1)
msg = "> Job '{}' deleted".format(self.name)
print(colored(msg, 'green'))
def start(self):
'''Start this taks on server'''
msg = '> Starting...'
print(colored(msg, 'green'))
url = '{}/job/debug/job/{}/build'.format(JENKINS_URL, self.name)
res = requests.post(url, auth=AUTH)
if res.status_code != 201:
msg = '> Something goes wrong'
print(colored(msg, 'red'))
print(res.text)
self.queue_url = res.headers['location']
self._set_build_number()
def _set_build_number(self):
if self.queue_url is None:
raise RuntimeError('No queue url - build was not scheduled')
waiting = True
carriage_return_printed = False
waiting_time = 0
while waiting:
url = '{}{}'.format(self.queue_url, 'api/json')
res = requests.get(url)
if res.json().get('executable') is None:
reason = 'Unknown'
if res.json().get('why') is not None:
reason = res.json().get('why')
if waiting_time >= 9:
# TODO: Добавить время ожидания
msg = ('\r> Waiting in queue for {} seconds. Reason: {}'
.format(int(waiting_time / 10), reason))
print(colored(msg, 'yellow'), end='', flush=True)
carriage_return_printed = True
sleep(0.1)
waiting_time += 1
continue
if carriage_return_printed:
print('')
msg = "> Building '{}' started".format(self.name)
print(colored(msg, 'green'))
waiting = False
self.build_number = res.json().get('executable').get('number')
self.build_url = res.json().get('executable').get('url')
self.queue_url = None
def _get_queue_number(self):
if self.queue_url is None:
return None
return os.path.basename(os.path.normpath(self.queue_url))
# TODO: Проверки status_code
def stop(self):
'''Stop this task on server'''
if self._get_queue_number() is not None:
res = requests.post(
'{}/queue/cancelItem'.format(JENKINS_URL),
data={'id': int(self._get_queue_number())},
auth=AUTH
)
msg = ("> '{}' stopped from queue, HTTP code: {}"
.format(self.name, res.status_code))
print(colored(msg, 'green'))
return
if self.build_url is not None:
res = requests.post(
'{}/stop'.format(self.build_url),
auth=AUTH
)
msg = ("> '{}' abort request sended. Waiting for stop..."
.format(self.name))
print(colored(msg, 'green'))
url = '{}/api/json'.format(self.build_url)
stopped = False
while not stopped:
sleep(0.1)
res = requests.get(url, auth=AUTH)
if not res.json().get('building'):
stopped = True
msg = ("> '{}' stopped"
.format(self.name))
print(colored(msg, 'green'))
return
msg = "> Building '{}' job already stopped".format(self.name)
print(colored(msg, 'green'))
def watch_stream(self, verbose):
start_at = 0
stream_open = True
check_job_status = 0
console = requests.session()
console_url = '{}{}'.format(self.build_url, 'logText/progressiveText')
status_url = '{}{}'.format(self.build_url, 'api/json')
print(colored('> Attempting to get console output:', 'green'))
print('')
while stream_open:
res = requests.get(status_url, auth=AUTH)
build_going = res.json().get('building')
if not build_going:
stream_open = False
res = console.post(console_url,
data={'start': start_at},
auth=AUTH)
content_length = int(res.headers.get('Content-Length', -1))
# Прикольное состояние, когда билд уже не в queue,
# но ещё не начался
if res.status_code == 404:
continue
if res.status_code != 200:
msg = '> Something goes wrong'
print(colored(msg, 'red'))
print(res.content)
print(res.headers)
if content_length == 0:
sleep(0.1)
continue
for line in res.text.splitlines():
if verbose:
print(line)
continue
if '[Pipeline]' in line:
continue
print(line)
start_at = int(res.headers.get('X-Text-Size'))
sleep(0.1)
res = requests.get(status_url, auth=AUTH)
build_result = res.json().get('result')
print('')
msg = '> Build ended with result: {}'.format(build_result)
print(colored(msg, 'green'))
return 0
def _log(self, log_descripton, log_content):
if not os.path.isdir('./logs'):
os.mkdir('./logs')
filename = './logs/{}_{}_log.html'.format(self.name, log_descripton)
log_file = open(filename, 'w')
log_file.write(log_content)
log_file.close()
return filename
if __name__ == '__main__':
cli()