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
|
import json
import urllib
import logging
import urlparse
from jenkinsapi import config
from jenkinsapi.job import Job
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.exceptions import UnknownJob, JenkinsAPIException
log = logging.getLogger(__name__)
class Jenkins(JenkinsBase):
"""
Represents a jenkins environment.
"""
def __init__(self, baseurl, username=None, password=None, requester=None):
"""
: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)
JenkinsBase.__init__(self, baseurl)
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
def get_jobs(self):
"""
Fetch all the build-names on this Jenkins server.
"""
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
"""
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[jobname]
def has_job(self, jobname):
"""
Does a job by the name specified exist
:param jobname: string
:return: boolean
"""
return jobname in self
def create_job(self, jobname, config):
"""
Create a job
:param jobname: name of new job, str
:param config: configuration of new job, xml
:return: new Job obj
"""
if self.has_job(jobname):
return self[jobname]
params = {'name': jobname}
if isinstance(config, unicode):
config = str(config)
self.requester.post_xml_and_confirm_status(self.get_create_url(), data=config, params=params)
self.poll()
if not self.has_job(jobname):
raise JenkinsAPIException('Cannot create job %s' % jobname)
return self[jobname]
def copy_job(self, jobname, newjobname):
"""
Copy a job
:param jobname: name of a exist job, str
:param newjobname: name of new job, str
:return: new Job obj
"""
params = {'name': newjobname,
'mode': 'copy',
'from': jobname}
self.requester.post_and_confirm_status(
self.get_create_url(),
params=params,
data='')
self.poll()
return self[newjobname]
def build_job(self, jobname, params={}):
"""
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)
return
def delete_job(self, jobname):
"""
Delete a job by name
:param jobname: name of a exist job, str
:return: new jenkins_obj
"""
delete_job_url = self[jobname].get_delete_url()
self.requester.post_and_confirm_status(
delete_job_url,
data='some random bytes...'
)
self.poll()
return self
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
"""
params = {'newName': newjobname}
rename_job_url = self[jobname].get_rename_url()
self.requester.post_and_confirm_status(
rename_job_url, params=params, data='')
self.poll()
return self[newjobname]
def iterkeys(self):
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 __getitem__(self, jobname):
"""
Get a job by name
:param jobname: name of job, str
:return: Job obj
"""
for info in self._data["jobs"]:
if info["name"] == jobname:
return Job(info["url"], info["name"], jenkins_obj=self)
raise UnknownJob(jobname)
def __contains__(self, jobname):
"""
Does a job by the name specified exist
:param jobname: string
:return: boolean
"""
return jobname in self.get_jobs_list()
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' % urllib.quote(nodename))
return url
def get_queue_url(self):
url = urlparse.urljoin(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)
self.requester.get_and_confirm_status(url)
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" % urllib.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):
# This only ever needs to work on the base object
return '%s/pluginManager/api/python?depth=1' % self.baseurl
def get_plugins(self):
url = self.get_plugins_url()
return Plugins(url, self)
def has_plugin(self, plugin_name):
return plugin_name in self.get_plugins()
|