Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 1 | from __future__ import print_function |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 2 | import time |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 3 | |
| 4 | import jenkins |
Dennis Dmitriev | 27a9679 | 2018-07-30 07:52:03 +0300 | [diff] [blame] | 5 | import json |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 6 | import requests |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 7 | |
| 8 | from devops.helpers import helpers |
| 9 | |
| 10 | |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 11 | class JenkinsWrapper(jenkins.Jenkins): |
| 12 | """Workaround for the bug: |
| 13 | https://bugs.launchpad.net/python-jenkins/+bug/1775047 |
| 14 | """ |
| 15 | def _response_handler(self, response): |
| 16 | '''Handle response objects''' |
| 17 | |
| 18 | # raise exceptions if occurred |
| 19 | response.raise_for_status() |
| 20 | |
| 21 | headers = response.headers |
| 22 | if (headers.get('content-length') is None and |
| 23 | headers.get('transfer-encoding') is None and |
| 24 | (response.status_code == 201 and |
| 25 | headers.get('location') is None) and |
| 26 | (response.content is None or len(response.content) <= 0)): |
| 27 | # response body should only exist if one of these is provided |
| 28 | raise jenkins.EmptyResponseException( |
| 29 | "Error communicating with server[%s]: " |
| 30 | "empty response" % self.server) |
| 31 | |
| 32 | # Response objects will automatically return unicode encoded |
| 33 | # when accessing .text property |
| 34 | return response |
| 35 | |
| 36 | |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 37 | class JenkinsClient(object): |
| 38 | |
Dennis Dmitriev | 27a9679 | 2018-07-30 07:52:03 +0300 | [diff] [blame] | 39 | def __init__(self, host=None, username='admin', password='r00tme'): |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 40 | host = host or 'http://172.16.44.33:8081' |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 41 | # self.__client = jenkins.Jenkins( |
| 42 | self.__client = JenkinsWrapper( |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 43 | host, |
| 44 | username=username, |
| 45 | password=password) |
| 46 | |
| 47 | def jobs(self): |
| 48 | return self.__client.get_jobs() |
| 49 | |
| 50 | def find_jobs(self, name): |
| 51 | return filter(lambda x: name in x['fullname'], self.jobs()) |
| 52 | |
| 53 | def job_info(self, name): |
| 54 | return self.__client.get_job_info(name) |
| 55 | |
| 56 | def list_builds(self, name): |
| 57 | return self.job_info(name).get('builds') |
| 58 | |
| 59 | def build_info(self, name, build_id): |
| 60 | return self.__client.get_build_info(name, build_id) |
| 61 | |
| 62 | def job_params(self, name): |
| 63 | job = self.job_info(name) |
| 64 | job_params = next( |
| 65 | p for p in job['property'] if |
| 66 | 'hudson.model.ParametersDefinitionProperty' == p['_class']) |
| 67 | job_params = job_params['parameterDefinitions'] |
| 68 | return job_params |
| 69 | |
| 70 | def make_defults_params(self, name): |
| 71 | job_params = self.job_params(name) |
| 72 | def_params = dict( |
| 73 | [(j['name'], j['defaultParameterValue']['value']) |
| 74 | for j in job_params]) |
| 75 | return def_params |
| 76 | |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 77 | def run_build(self, name, params=None, timeout=600, verbose=False): |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 78 | params = params or self.make_defults_params(name) |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 79 | num = self.__client.build_job(name, params) |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 80 | time.sleep(2) # wait while job is started |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 81 | |
| 82 | def is_blocked(): |
| 83 | queued = self.__client.get_queue_item(num) |
| 84 | status = not queued['blocked'] |
| 85 | if not status and verbose: |
| 86 | print("pending the job [{}] : {}".format(name, queued['why'])) |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 87 | return (status and |
| 88 | 'executable' in (queued or {}) and |
| 89 | 'number' in (queued['executable'] or {})) |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 90 | |
| 91 | helpers.wait( |
| 92 | is_blocked, |
| 93 | timeout=timeout, |
| 94 | interval=30, |
| 95 | timeout_msg='Timeout waiting to run the job [{}]'.format(name)) |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 96 | build_id = self.__client.get_queue_item(num)['executable']['number'] |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 97 | return name, build_id |
| 98 | |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 99 | def wait_end_of_build(self, name, build_id, timeout=600, interval=5, |
| 100 | verbose=False, job_output_prefix=''): |
| 101 | '''Wait until the specified build is finished |
| 102 | |
| 103 | :param name: ``str``, job name |
| 104 | :param build_id: ``int``, build id |
| 105 | :param timeout: ``int``, timeout waiting the job, sec |
| 106 | :param interval: ``int``, interval of polling the job result, sec |
| 107 | :param verbose: ``bool``, print the job console updates during waiting |
| 108 | :param job_output_prefix: ``str``, print the prefix for each console |
| 109 | output line, with the pre-defined |
| 110 | substitution keys: |
| 111 | - '{name}' : the current job name |
| 112 | - '{build_id}' : the current build-id |
| 113 | - '{time}' : the current time |
| 114 | :returns: requests object with headers and console output, ``obj`` |
| 115 | ''' |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 116 | start = [0] |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 117 | time_str = time.strftime("%H:%M:%S") |
| 118 | prefix = "\n" + job_output_prefix.format(job_name=name, |
| 119 | build_number=build_id, |
| 120 | time=time_str) |
| 121 | if verbose: |
| 122 | print(prefix, end='') |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 123 | |
| 124 | def building(): |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 125 | status = not self.build_info(name, build_id)['building'] |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 126 | if verbose: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 127 | time_str = time.strftime("%H:%M:%S") |
| 128 | prefix = "\n" + job_output_prefix.format( |
| 129 | job_name=name, build_number=build_id, time=time_str) |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 130 | res = self.get_progressive_build_output(name, |
| 131 | build_id, |
| 132 | start=start[0]) |
| 133 | if 'X-Text-Size' in res.headers: |
| 134 | text_size = int(res.headers['X-Text-Size']) |
| 135 | if start[0] < text_size: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 136 | text = res.content.decode('utf-8', |
| 137 | errors='backslashreplace') |
| 138 | print(text.replace("\n", prefix), end='') |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 139 | start[0] = text_size |
| 140 | return status |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 141 | |
| 142 | helpers.wait( |
| 143 | building, |
| 144 | timeout=timeout, |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 145 | interval=interval, |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 146 | timeout_msg='Timeout waiting, job {0} are not finished "{1}" build' |
| 147 | ' still'.format(name, build_id)) |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 148 | |
| 149 | def get_build_output(self, name, build_id): |
| 150 | return self.__client.get_build_console_output(name, build_id) |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 151 | |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 152 | def get_progressive_build_output(self, name, build_id, start=0): |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 153 | '''Get build console text. |
| 154 | |
| 155 | :param name: Job name, ``str`` |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 156 | :param build_id: Build id, ``int`` |
| 157 | :param start: Start offset, ``int`` |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 158 | :returns: requests object with headers and console output, ``obj`` |
| 159 | ''' |
| 160 | folder_url, short_name = self.__client._get_job_folder(name) |
| 161 | |
| 162 | PROGRESSIVE_CONSOLE_OUTPUT = ( |
| 163 | '%(folder_url)sjob/%(short_name)s/%(build_id)d/' |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 164 | 'logText/progressiveText?start=%(start)d') |
| 165 | req = requests.Request( |
| 166 | 'GET', |
| 167 | self.__client._build_url(PROGRESSIVE_CONSOLE_OUTPUT, locals())) |
| 168 | return(self.__client.jenkins_request(req)) |
Dennis Dmitriev | 27a9679 | 2018-07-30 07:52:03 +0300 | [diff] [blame] | 169 | |
| 170 | def get_workflow(self, name, build_id, enode=None, mode='describe'): |
| 171 | '''Get workflow results from pipeline job |
| 172 | |
| 173 | :param name: job name |
| 174 | :param build_id: str, build number or 'lastBuild' |
| 175 | :param enode: int, execution node in the workflow |
| 176 | :param mode: the stage or execution node description if 'describe', |
| 177 | the execution node log if 'log' |
| 178 | ''' |
| 179 | folder_url, short_name = self.__client._get_job_folder(name) |
| 180 | |
| 181 | if enode: |
| 182 | WORKFLOW_DESCRIPTION = ( |
| 183 | '%(folder_url)sjob/%(short_name)s/%(build_id)s/' |
| 184 | 'execution/node/%(enode)d/wfapi/%(mode)s') |
| 185 | else: |
| 186 | WORKFLOW_DESCRIPTION = ( |
| 187 | '%(folder_url)sjob/%(short_name)s/%(build_id)s/wfapi/%(mode)s') |
| 188 | req = requests.Request( |
| 189 | 'GET', |
| 190 | self.__client._build_url(WORKFLOW_DESCRIPTION, locals())) |
| 191 | response = self.__client.jenkins_open(req) |
| 192 | return json.loads(response) |