-
Notifications
You must be signed in to change notification settings - Fork 1
/
jenkins_api_functions.py
135 lines (106 loc) · 4.38 KB
/
jenkins_api_functions.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
# coding=utf-8
"""
Helper functions to access Jenkins using jenkins library
"""
from datetime import datetime
import multiprocessing as mp
import jenkins
def __get_builds_list(jenkins_reference, job_name):
"""
Get builds list (for the job_name)
:param job_name: Job Name, ``str``
:param jenkins_reference: Jenkins handler, ``jenkins.Jenkins``
:return: Build List, ``list``
"""
builds = []
try:
job_info = jenkins_reference.get_job_info(job_name)
if job_info is not None:
builds = [d['number'] for d in job_info['builds']]
except KeyError as exception:
print u'Erro ao obter job_info: {0} - {1}'.format(job_name, exception.message)
return builds
def __get_build_info(jenkins_url,
jenkins_username,
jenkins_password,
job_name,
build_number):
"""
Get build info ( for the params job_name and build_number )
:param jenkins_url: Jenkins server url, ``str``
:param jenkins_username: Username to access Jenkins server or None, ``str``
:param jenkins_password: Password to access Jenkins server or None, ``str``
:param job_name: Job Name, ``str``
:param build_number: Build Number, ``int``
:return: dictionary of build information, ``dict``
"""
build_info = None
try:
# Tem que iniciar uma nova instancia pois esse metodo
# está rodando em modo paralelo
ref = jenkins.Jenkins(jenkins_url,
jenkins_username,
jenkins_password)
build_info = ref.get_build_info(job_name,
build_number)
except Exception as exception:
print u"Erro ao obter build_info: {0} - {1}".format(job_name,
unicode(exception.message, errors='ignore'))
return build_info
def __convert_build_info(job_name, build_number, build_info):
"""
Convert build info in a dictionary like the example bellow:
dict(job_name=job_name,
build_number=build_number,
build_date=build_date,
build_date_month=build_date.month,
build_date_year=build_date.year,
build_result=build_result)
:param job_name: Job Name, ``str``
:param build_number: Build Number, ``int``
:param build_info: Build info dictionary, ``dict``
:return: Build info, ``dict``
"""
build_timestamp = build_info['timestamp']
build_result = build_info['result']
build_date = datetime.fromtimestamp(build_timestamp / 1000.0)
return dict(job_name=job_name,
build_number=build_number,
build_date=build_date,
build_date_month=build_date.month,
build_date_year=build_date.year,
build_result=build_result)
def get_jobs_details(jenkins_url,
jenkins_user=None,
jenkins_password=None):
"""
Get jobs details - Return all jobs details from the informed server
:param jenkins_url: Jenkins server url, ``str``
:param jenkins_user: Jenkins user name, ``str``
:param jenkins_password: Jenkins password, ``str``
:return: Job details, ``dict``
"""
jenkins_reference = jenkins.Jenkins(jenkins_url,
jenkins_user,
jenkins_password)
jobs = [job['name'] for job in jenkins_reference.get_jobs()]
dados_jobs = []
for job in jobs:
print job
builds = __get_builds_list(jenkins_reference, job)
print 'builds:', len(builds)
pool = mp.Pool(processes=mp.cpu_count())
results = [pool.apply_async(__get_build_info,
args=(jenkins_url,
jenkins_user,
jenkins_password,
job,
build_number,))
for build_number in builds]
for result in results:
build_info = result.get()
if build_info is not None:
dados_jobs.append(__convert_build_info(job,
0,
build_info))
return dados_jobs