Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 1 | from __future__ import print_function |
Dmitry Tyzhnenko | b39de05 | 2019-03-21 17:05:07 +0200 | [diff] [blame] | 2 | import datetime |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 3 | import time |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 4 | |
| 5 | import jenkins |
Dennis Dmitriev | 27a9679 | 2018-07-30 07:52:03 +0300 | [diff] [blame] | 6 | import json |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 7 | import requests |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 8 | |
| 9 | from devops.helpers import helpers |
| 10 | |
Dmitry Tyzhnenko | 80ce020 | 2019-02-07 13:27:19 +0200 | [diff] [blame] | 11 | from requests.exceptions import ConnectionError |
| 12 | |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 13 | |
| 14 | class JenkinsClient(object): |
| 15 | |
Dennis Dmitriev | 27a9679 | 2018-07-30 07:52:03 +0300 | [diff] [blame] | 16 | def __init__(self, host=None, username='admin', password='r00tme'): |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 17 | host = host or 'http://172.16.44.33:8081' |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 18 | self.__client = jenkins.Jenkins( |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 19 | host, |
| 20 | username=username, |
| 21 | password=password) |
obutenko | ca85840 | 2019-07-04 18:31:39 +0300 | [diff] [blame] | 22 | self.__client._session.verify = False |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 23 | |
| 24 | def jobs(self): |
| 25 | return self.__client.get_jobs() |
| 26 | |
| 27 | def find_jobs(self, name): |
| 28 | return filter(lambda x: name in x['fullname'], self.jobs()) |
| 29 | |
| 30 | def job_info(self, name): |
| 31 | return self.__client.get_job_info(name) |
| 32 | |
| 33 | def list_builds(self, name): |
| 34 | return self.job_info(name).get('builds') |
| 35 | |
| 36 | def build_info(self, name, build_id): |
| 37 | return self.__client.get_build_info(name, build_id) |
| 38 | |
| 39 | def job_params(self, name): |
| 40 | job = self.job_info(name) |
| 41 | job_params = next( |
| 42 | p for p in job['property'] if |
| 43 | 'hudson.model.ParametersDefinitionProperty' == p['_class']) |
| 44 | job_params = job_params['parameterDefinitions'] |
| 45 | return job_params |
| 46 | |
| 47 | def make_defults_params(self, name): |
| 48 | job_params = self.job_params(name) |
| 49 | def_params = dict( |
| 50 | [(j['name'], j['defaultParameterValue']['value']) |
| 51 | for j in job_params]) |
| 52 | return def_params |
| 53 | |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 54 | def run_build(self, name, params=None, timeout=600, verbose=False): |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 55 | params = params or self.make_defults_params(name) |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 56 | num = self.__client.build_job(name, params) |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 57 | time.sleep(2) # wait while job is started |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 58 | |
Dmitry Tyzhnenko | b39de05 | 2019-03-21 17:05:07 +0200 | [diff] [blame] | 59 | def is_build_queued(): |
| 60 | try: |
| 61 | item = self.__client.get_queue_item(num) |
| 62 | ts = item['inQueueSince'] / 1000 |
| 63 | since_time = datetime.datetime.fromtimestamp(ts) |
| 64 | print("Build in the queue since {}".format(since_time)) |
| 65 | return True |
| 66 | except jenkins.JenkinsException: |
| 67 | if verbose: |
| 68 | print("Build have not been queued {} yet".format(num)) |
| 69 | |
| 70 | helpers.wait( |
| 71 | is_build_queued, |
| 72 | timeout=timeout, |
| 73 | interval=30, |
| 74 | timeout_msg='Timeout waiting to queue the build ' |
| 75 | 'for {} job'.format(name)) |
| 76 | |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 77 | def is_blocked(): |
| 78 | queued = self.__client.get_queue_item(num) |
| 79 | status = not queued['blocked'] |
| 80 | if not status and verbose: |
| 81 | print("pending the job [{}] : {}".format(name, queued['why'])) |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 82 | return (status and |
| 83 | 'executable' in (queued or {}) and |
| 84 | 'number' in (queued['executable'] or {})) |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 85 | |
| 86 | helpers.wait( |
| 87 | is_blocked, |
| 88 | timeout=timeout, |
| 89 | interval=30, |
| 90 | timeout_msg='Timeout waiting to run the job [{}]'.format(name)) |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 91 | build_id = self.__client.get_queue_item(num)['executable']['number'] |
Dmitry Tyzhnenko | b39de05 | 2019-03-21 17:05:07 +0200 | [diff] [blame] | 92 | |
| 93 | def is_build_started(): |
| 94 | try: |
| 95 | build = self.__client.get_build_info(name, build_id) |
| 96 | ts = float(build['timestamp']) / 1000 |
| 97 | start_time = datetime.datetime.fromtimestamp(ts) |
| 98 | print("the build {} in {} have started at {} UTC".format( |
| 99 | build_id, name, start_time)) |
| 100 | return True |
| 101 | except jenkins.JenkinsException: |
| 102 | if verbose: |
| 103 | print("the build {} in {} have not strated yet".format( |
| 104 | build_id, name)) |
| 105 | helpers.wait( |
| 106 | is_build_started, |
| 107 | timeout=timeout, |
| 108 | interval=30, |
| 109 | timeout_msg='Timeout waiting to run build of ' |
| 110 | 'the job [{}]'.format(name)) |
| 111 | |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 112 | return name, build_id |
| 113 | |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 114 | def wait_end_of_build(self, name, build_id, timeout=600, interval=5, |
| 115 | verbose=False, job_output_prefix=''): |
| 116 | '''Wait until the specified build is finished |
| 117 | |
| 118 | :param name: ``str``, job name |
| 119 | :param build_id: ``int``, build id |
| 120 | :param timeout: ``int``, timeout waiting the job, sec |
| 121 | :param interval: ``int``, interval of polling the job result, sec |
| 122 | :param verbose: ``bool``, print the job console updates during waiting |
| 123 | :param job_output_prefix: ``str``, print the prefix for each console |
| 124 | output line, with the pre-defined |
| 125 | substitution keys: |
| 126 | - '{name}' : the current job name |
| 127 | - '{build_id}' : the current build-id |
| 128 | - '{time}' : the current time |
| 129 | :returns: requests object with headers and console output, ``obj`` |
| 130 | ''' |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 131 | start = [0] |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 132 | time_str = time.strftime("%H:%M:%S") |
| 133 | prefix = "\n" + job_output_prefix.format(job_name=name, |
| 134 | build_number=build_id, |
| 135 | time=time_str) |
| 136 | if verbose: |
| 137 | print(prefix, end='') |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 138 | |
| 139 | def building(): |
Dmitry Tyzhnenko | 80ce020 | 2019-02-07 13:27:19 +0200 | [diff] [blame] | 140 | try: |
| 141 | status = not self.build_info(name, build_id)['building'] |
| 142 | except ConnectionError: |
| 143 | status = False |
| 144 | |
Dennis Dmitriev | a5bd165 | 2018-05-31 20:57:19 +0300 | [diff] [blame] | 145 | if verbose: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 146 | time_str = time.strftime("%H:%M:%S") |
| 147 | prefix = "\n" + job_output_prefix.format( |
| 148 | job_name=name, build_number=build_id, time=time_str) |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 149 | res = self.get_progressive_build_output(name, |
| 150 | build_id, |
| 151 | start=start[0]) |
| 152 | if 'X-Text-Size' in res.headers: |
| 153 | text_size = int(res.headers['X-Text-Size']) |
| 154 | if start[0] < text_size: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 155 | text = res.content.decode('utf-8', |
| 156 | errors='backslashreplace') |
| 157 | print(text.replace("\n", prefix), end='') |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 158 | start[0] = text_size |
| 159 | return status |
Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame] | 160 | |
| 161 | helpers.wait( |
| 162 | building, |
| 163 | timeout=timeout, |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 164 | interval=interval, |
Dennis Dmitriev | 13e804b | 2018-10-09 19:25:14 +0300 | [diff] [blame] | 165 | timeout_msg=('Timeout waiting the job {0}:{1} in {2} sec.' |
| 166 | .format(name, build_id, timeout))) |
Dmitry Tyzhnenko | b610afd | 2018-02-19 15:43:45 +0200 | [diff] [blame] | 167 | |
| 168 | def get_build_output(self, name, build_id): |
| 169 | return self.__client.get_build_console_output(name, build_id) |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 170 | |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 171 | def get_progressive_build_output(self, name, build_id, start=0): |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 172 | '''Get build console text. |
| 173 | |
| 174 | :param name: Job name, ``str`` |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 175 | :param build_id: Build id, ``int`` |
| 176 | :param start: Start offset, ``int`` |
Dennis Dmitriev | 6111511 | 2018-05-31 06:48:43 +0300 | [diff] [blame] | 177 | :returns: requests object with headers and console output, ``obj`` |
| 178 | ''' |
| 179 | folder_url, short_name = self.__client._get_job_folder(name) |
| 180 | |
| 181 | PROGRESSIVE_CONSOLE_OUTPUT = ( |
| 182 | '%(folder_url)sjob/%(short_name)s/%(build_id)d/' |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 183 | 'logText/progressiveText?start=%(start)d') |
| 184 | req = requests.Request( |
| 185 | 'GET', |
| 186 | self.__client._build_url(PROGRESSIVE_CONSOLE_OUTPUT, locals())) |
| 187 | return(self.__client.jenkins_request(req)) |
Dennis Dmitriev | 27a9679 | 2018-07-30 07:52:03 +0300 | [diff] [blame] | 188 | |
| 189 | def get_workflow(self, name, build_id, enode=None, mode='describe'): |
| 190 | '''Get workflow results from pipeline job |
| 191 | |
| 192 | :param name: job name |
| 193 | :param build_id: str, build number or 'lastBuild' |
| 194 | :param enode: int, execution node in the workflow |
| 195 | :param mode: the stage or execution node description if 'describe', |
| 196 | the execution node log if 'log' |
| 197 | ''' |
| 198 | folder_url, short_name = self.__client._get_job_folder(name) |
| 199 | |
| 200 | if enode: |
| 201 | WORKFLOW_DESCRIPTION = ( |
| 202 | '%(folder_url)sjob/%(short_name)s/%(build_id)s/' |
| 203 | 'execution/node/%(enode)d/wfapi/%(mode)s') |
| 204 | else: |
| 205 | WORKFLOW_DESCRIPTION = ( |
| 206 | '%(folder_url)sjob/%(short_name)s/%(build_id)s/wfapi/%(mode)s') |
| 207 | req = requests.Request( |
| 208 | 'GET', |
| 209 | self.__client._build_url(WORKFLOW_DESCRIPTION, locals())) |
| 210 | response = self.__client.jenkins_open(req) |
| 211 | return json.loads(response) |
Dennis Dmitriev | 8565c34 | 2019-02-11 23:45:03 +0200 | [diff] [blame] | 212 | |
| 213 | def get_artifact(self, name, build_id, artifact_path, destination_name): |
| 214 | '''Wait until the specified build is finished |
| 215 | |
| 216 | :param name: ``str``, job name |
| 217 | :param build_id: ``str``, build id or "lastBuild" |
| 218 | :param artifact_path: ``str``, path and filename of the artifact |
| 219 | relative to the job URL |
| 220 | :param artifact_path: ``str``, destination path and filename |
| 221 | on the local filesystem where to save |
| 222 | the artifact content |
| 223 | :returns: requests object with headers and console output, ``obj`` |
| 224 | ''' |
| 225 | folder_url, short_name = self.__client._get_job_folder(name) |
| 226 | |
| 227 | DOWNLOAD_URL = ('%(folder_url)sjob/%(short_name)s/%(build_id)s/' |
| 228 | 'artifact/%(artifact_path)s') |
| 229 | req = requests.Request( |
| 230 | 'GET', |
| 231 | self.__client._build_url(DOWNLOAD_URL, locals())) |
| 232 | |
| 233 | response = self.__client.jenkins_request(req) |
| 234 | return response.content |