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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
"""
Module for jenkinsapi Jenkins object
"""
import json
try:
import urlparse
from urllib import quote as urlquote, urlencode
except ImportError:
# Python3
import urllib.parse as urlparse
from urllib.parse import quote as urlquote, urlencode
import logging
from jenkinsapi import config
from jenkinsapi.executors import Executors
from jenkinsapi.job import Job
from jenkinsapi.jobs import Jobs
from jenkinsapi.node import Node
from jenkinsapi.view import View
from jenkinsapi.nodes import Nodes
from jenkinsapi.plugins import Plugins
from jenkinsapi.views import Views
from jenkinsapi.queue import Queue
from jenkinsapi.fingerprint import Fingerprint
from jenkinsapi.jenkinsbase import JenkinsBase
from jenkinsapi.utils.requester import Requester
from jenkinsapi.custom_exceptions import UnknownJob, PostRequired
log = logging.getLogger(__name__)
class Jenkins(JenkinsBase):
"""
Represents a jenkins environment.
"""
def __init__(self, baseurl, username=None, password=None, requester=None, lazy=False):
"""
:param baseurl: baseurl for jenkins instance including port, str
:param username: username for jenkins auth, str
:param password: password for jenkins auth, str
:return: a Jenkins obj
"""
self.username = username
self.password = password
self.requester = requester or Requester(username, password, baseurl=baseurl)
self.lazy = lazy
JenkinsBase.__init__(self, baseurl, poll=not lazy)
def _poll_if_needed(self):
if self.lazy and self._data is None:
self.poll()
def _clone(self):
return Jenkins(self.baseurl, username=self.username,
password=self.password, requester=self.requester)
def base_server_url(self):
if config.JENKINS_API in self.baseurl:
return self.baseurl[:-(len(config.JENKINS_API))]
else:
return self.baseurl
def validate_fingerprint(self, id_):
obj_fingerprint = Fingerprint(self.baseurl, id_, jenkins_obj=self)
obj_fingerprint.validate()
log.info(msg="Jenkins says %s is valid" % id_)
# def reload(self):
# '''Try and reload the configuration from disk'''
# self.requester.get_url("%(baseurl)s/reload" % self.__dict__)
def get_artifact_data(self, id_):
obj_fingerprint = Fingerprint(self.baseurl, id_, jenkins_obj=self)
obj_fingerprint.validate()
return obj_fingerprint.get_info()
def validate_fingerprint_for_build(self, digest, filename, job, build):
obj_fingerprint = Fingerprint(self.baseurl, digest, jenkins_obj=self)
return obj_fingerprint.validate_for_build(filename, job, build)
def get_jenkins_obj(self):
return self
def get_jenkins_obj_from_url(self, url):
return Jenkins(url, self.username, self.password, self.requester)
def get_create_url(self):
# This only ever needs to work on the base object
return '%s/createItem' % self.baseurl
def get_nodes_url(self):
# This only ever needs to work on the base object
return '%s/computer' % self.baseurl
@property
def jobs(self):
return Jobs(self)
def get_jobs(self):
"""
Fetch all the build-names on this Jenkins server.
"""
self._poll_if_needed()
for info in self._data["jobs"]:
yield info["name"], \
Job(info["url"], info["name"], jenkins_obj=self)
def get_jobs_info(self):
"""
Get the jobs information
:return url, name
"""
self._poll_if_needed()
for info in self._data["jobs"]:
yield info["url"], info["name"]
def get_job(self, jobname):
"""
Get a job by name
:param jobname: name of the job, str
:return: Job obj
"""
return self.jobs[jobname]
def has_job(self, jobname):
"""
Does a job by the name specified exist
:param jobname: string
:return: boolean
"""
return jobname in self.jobs
def create_job(self, jobname, config_):
"""
Create a job
alternatively you can create job using Jobs object:
self.jobs['job_name'] = config
:param jobname: name of new job, str
:param config: configuration of new job, xml
:return: new Job obj
"""
return self.jobs.create(jobname, config_)
def copy_job(self, jobname, newjobname):
return self.jobs.copy(jobname, newjobname)
def build_job(self, jobname, params=None):
"""
Invoke a build by job name
:param jobname: name of exist job, str
:param params: the job params, dict
:return: none
"""
self[jobname].invoke(build_params=params or {})
def delete_job(self, jobname):
"""
Delete a job by name
:param jobname: name of a exist job, str
:return: new jenkins_obj
"""
del self.jobs[jobname]
def rename_job(self, jobname, newjobname):
"""
Rename a job
:param jobname: name of a exist job, str
:param newjobname: name of new job, str
:return: new Job obj
"""
return self.jobs.rename(jobname, newjobname)
def iterkeys(self):
self._poll_if_needed()
for info in self._data["jobs"]:
yield info["name"]
def iteritems(self):
"""
:param return: An iterator of pairs.
Each pair will be (job name, Job object)
"""
return self.get_jobs()
def items(self):
"""
:param return: A list of pairs. Each pair will be (job name, Job object)
"""
return list(self.get_jobs())
def keys(self):
return [a for a in self.iterkeys()]
# This is a function alias we retain for historical compatibility
get_jobs_list = keys
def __str__(self):
return "Jenkins server at %s" % self.baseurl
@property
def views(self):
return Views(self)
def get_view_by_url(self, str_view_url):
# for nested view
str_view_name = str_view_url.split('/view/')[-1].replace('/', '')
return View(str_view_url, str_view_name, jenkins_obj=self)
def delete_view_by_url(self, str_url):
url = "%s/doDelete" % str_url
self.requester.post_and_confirm_status(url, data='')
self.poll()
return self
def __getitem__(self, jobname):
"""
Get a job by name
:param jobname: name of job, str
:return: Job obj
"""
self._poll_if_needed()
for info in self._data["jobs"]:
if info["name"] == jobname:
return Job(info["url"], info["name"], jenkins_obj=self)
raise UnknownJob(jobname)
def __len__(self):
self._poll_if_needed()
return len(self._data["jobs"])
def __contains__(self, jobname):
"""
Does a job by the name specified exist
:param jobname: string
:return: boolean
"""
return jobname in self.jobs
def __delitem__(self, job_name):
del self.jobs[job_name]
def get_node(self, nodename):
"""Get a node object for a specific node"""
return self.get_nodes()[nodename]
def get_node_url(self, nodename=""):
"""Return the url for nodes"""
url = urlparse.urljoin(self.base_server_url(), 'computer/%s' % urlquote(nodename))
return url
def get_queue_url(self):
url = "%s/%s" % (self.base_server_url(), 'queue')
return url
def get_queue(self):
queue_url = self.get_queue_url()
return Queue(queue_url, self)
def get_nodes(self):
url = self.get_nodes_url()
return Nodes(url, self)
def has_node(self, nodename):
"""
Does a node by the name specified exist
:param nodename: string, hostname
:return: boolean
"""
self.poll()
return nodename in self.get_nodes()
def delete_node(self, nodename):
"""
Remove a node from the managed slave list
Please note that you cannot remove the master node
:param nodename: string holding a hostname
:return: None
"""
assert self.has_node(nodename), "This node: %s is not registered as a slave" % nodename
assert nodename != "master", "you cannot delete the master node"
url = "%s/doDelete" % self.get_node_url(nodename)
try:
self.requester.get_and_confirm_status(url)
except PostRequired:
# Latest Jenkins requires POST here. GET kept for compatibility
self.requester.post_and_confirm_status(url, data={})
def create_node(self, name, num_executors=2, node_description=None,
remote_fs='/var/lib/jenkins', labels=None, exclusive=False):
"""
Create a new slave node by name.
:param name: fqdn of slave, str
:param num_executors: number of executors, int
:param node_description: a freetext field describing the node
:param remote_fs: jenkins path, str
:param labels: labels to associate with slave, str
:param exclusive: tied to specific job, boolean
:return: node obj
"""
NODE_TYPE = 'hudson.slaves.DumbSlave$DescriptorImpl'
MODE = 'NORMAL'
if self.has_node(name):
return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self)
if exclusive:
MODE = 'EXCLUSIVE'
params = {
'name': name,
'type': NODE_TYPE,
'json': json.dumps({
'name': name,
'nodeDescription': node_description,
'numExecutors': num_executors,
'remoteFS': remote_fs,
'labelString': labels,
'mode': MODE,
'type': NODE_TYPE,
'retentionStrategy': {'stapler-class': 'hudson.slaves.RetentionStrategy$Always'},
'nodeProperties': {'stapler-class-bag': 'true'},
'launcher': {'stapler-class': 'hudson.slaves.JNLPLauncher'}
})
}
url = self.get_node_url() + "doCreateItem?%s" % urlencode(params)
self.requester.get_and_confirm_status(url)
return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self)
def get_plugins_url(self, depth):
# This only ever needs to work on the base object
return '%s/pluginManager/api/python?depth=%i' % (self.baseurl, depth)
def get_plugins(self, depth=1):
url = self.get_plugins_url(depth=depth)
return Plugins(url, self)
def has_plugin(self, plugin_name):
return plugin_name in self.get_plugins()
def get_executors(self, nodename):
url = '%s/computer/%s' % (self.baseurl, nodename)
return Executors(url, nodename, self)
def get_master_data(self):
url = '%s/computer/api/python' % self.baseurl
return self.get_data(url)
@property
def version(self):
"""
Return version number of Jenkins
"""
response = self.requester.get_and_confirm_status(self.baseurl)
version_key = 'X-Jenkins'
return response.headers.get(version_key, '0.0')
|