blob: 8c9939f12d5633b13de12d79b808ef4ce3258007 [file] [log] [blame]
Dennis Dmitriev61115112018-05-31 06:48:43 +03001from __future__ import print_function
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +02002import time
3
4import jenkins
Dennis Dmitriev61115112018-05-31 06:48:43 +03005import requests
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +02006
7from devops.helpers import helpers
8
9
10class JenkinsClient(object):
11
12 def __init__(self, host=None, username=None, password=None):
13 host = host or 'http://172.16.44.33:8081'
14 username = username or 'admin'
15 password = password or 'r00tme'
16 self.__client = jenkins.Jenkins(
17 host,
18 username=username,
19 password=password)
20
21 def jobs(self):
22 return self.__client.get_jobs()
23
24 def find_jobs(self, name):
25 return filter(lambda x: name in x['fullname'], self.jobs())
26
27 def job_info(self, name):
28 return self.__client.get_job_info(name)
29
30 def list_builds(self, name):
31 return self.job_info(name).get('builds')
32
33 def build_info(self, name, build_id):
34 return self.__client.get_build_info(name, build_id)
35
36 def job_params(self, name):
37 job = self.job_info(name)
38 job_params = next(
39 p for p in job['property'] if
40 'hudson.model.ParametersDefinitionProperty' == p['_class'])
41 job_params = job_params['parameterDefinitions']
42 return job_params
43
44 def make_defults_params(self, name):
45 job_params = self.job_params(name)
46 def_params = dict(
47 [(j['name'], j['defaultParameterValue']['value'])
48 for j in job_params])
49 return def_params
50
51 def run_build(self, name, params=None):
52 params = params or self.make_defults_params(name)
53 self.__client.build_job(name, params)
54 time.sleep(10) # wait while jobs started:
55 build_id = self.job_info(name)['lastBuild']['number']
56 return name, build_id
57
Dennis Dmitriev61115112018-05-31 06:48:43 +030058 def wait_end_of_build(self, name, build_id, timeout=600,
59 print_job_output=False):
60 start = [0]
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +020061
62 def building():
Dennis Dmitriev61115112018-05-31 06:48:43 +030063 status = not self.build_info(name, build_id)['building']
64 if print_job_output:
65 res = self.get_progressive_build_output(name,
66 build_id,
67 start=start[0])
68 if 'X-Text-Size' in res.headers:
69 text_size = int(res.headers['X-Text-Size'])
70 if start[0] < text_size:
71 print(res.content, end='')
72 start[0] = text_size
73 return status
Dmitry Tyzhnenko5a5d8da2017-12-14 14:14:42 +020074
75 helpers.wait(
76 building,
77 timeout=timeout,
78 timeout_msg='Timeout waiting, job {0} are not finished "{1}" build'
79 ' still'.format(name, build_id))
Dmitry Tyzhnenkob610afd2018-02-19 15:43:45 +020080
81 def get_build_output(self, name, build_id):
82 return self.__client.get_build_console_output(name, build_id)
Dennis Dmitriev61115112018-05-31 06:48:43 +030083
84 def get_progressive_build_output(self, name, build_id, start=0,
85 raise_on_err=False):
86 '''Get build console text.
87
88 :param name: Job name, ``str``
89 :param name: Build id, ``int``
90 :param name: Start offset, ``int``
91 :returns: requests object with headers and console output, ``obj``
92 '''
93 folder_url, short_name = self.__client._get_job_folder(name)
94
95 PROGRESSIVE_CONSOLE_OUTPUT = (
96 '%(folder_url)sjob/%(short_name)s/%(build_id)d/'
97 'logText/progressiveHtml?start=%(start)d')
98 url = self.__client._build_url(PROGRESSIVE_CONSOLE_OUTPUT, locals())
99 return(requests.get(url))