Dmitry Tyzhnenko | 5a5d8da | 2017-12-14 14:14:42 +0200 | [diff] [blame^] | 1 | import time |
| 2 | |
| 3 | import jenkins |
| 4 | |
| 5 | from devops.helpers import helpers |
| 6 | |
| 7 | |
| 8 | class JenkinsClient(object): |
| 9 | |
| 10 | def __init__(self, host=None, username=None, password=None): |
| 11 | host = host or 'http://172.16.44.33:8081' |
| 12 | username = username or 'admin' |
| 13 | password = password or 'r00tme' |
| 14 | self.__client = jenkins.Jenkins( |
| 15 | host, |
| 16 | username=username, |
| 17 | password=password) |
| 18 | |
| 19 | def jobs(self): |
| 20 | return self.__client.get_jobs() |
| 21 | |
| 22 | def find_jobs(self, name): |
| 23 | return filter(lambda x: name in x['fullname'], self.jobs()) |
| 24 | |
| 25 | def job_info(self, name): |
| 26 | return self.__client.get_job_info(name) |
| 27 | |
| 28 | def list_builds(self, name): |
| 29 | return self.job_info(name).get('builds') |
| 30 | |
| 31 | def build_info(self, name, build_id): |
| 32 | return self.__client.get_build_info(name, build_id) |
| 33 | |
| 34 | def job_params(self, name): |
| 35 | job = self.job_info(name) |
| 36 | job_params = next( |
| 37 | p for p in job['property'] if |
| 38 | 'hudson.model.ParametersDefinitionProperty' == p['_class']) |
| 39 | job_params = job_params['parameterDefinitions'] |
| 40 | return job_params |
| 41 | |
| 42 | def make_defults_params(self, name): |
| 43 | job_params = self.job_params(name) |
| 44 | def_params = dict( |
| 45 | [(j['name'], j['defaultParameterValue']['value']) |
| 46 | for j in job_params]) |
| 47 | return def_params |
| 48 | |
| 49 | def run_build(self, name, params=None): |
| 50 | params = params or self.make_defults_params(name) |
| 51 | self.__client.build_job(name, params) |
| 52 | time.sleep(10) # wait while jobs started: |
| 53 | build_id = self.job_info(name)['lastBuild']['number'] |
| 54 | return name, build_id |
| 55 | |
| 56 | def wait_end_of_build(self, name, build_id, timeout=600): |
| 57 | |
| 58 | def building(): |
| 59 | return not self.build_info(name, build_id)['building'] |
| 60 | |
| 61 | helpers.wait( |
| 62 | building, |
| 63 | timeout=timeout, |
| 64 | timeout_msg='Timeout waiting, job {0} are not finished "{1}" build' |
| 65 | ' still'.format(name, build_id)) |