-
Notifications
You must be signed in to change notification settings - Fork 10
/
jenkins.py
62 lines (53 loc) · 2.14 KB
/
jenkins.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
import httplib
import socket
import base64
class JenkinsUtils(object):
def __init__(self, jenkinsURL, connectionType, username, password):
self.jenkinsURL = jenkinsURL
self.connectionType = connectionType
self.username = username
self.password = password
# setup authentication header
base64string = base64.encodestring('%s:%s' % (self.username, self.password)).replace('\n', '')
self.basicAuth = "Basic " + base64string
def post_jenkins_job(self, configXML, jobName):
if not self.job_running(jobName):
if self.job_exists(jobName):
response = self.update_job(jobName, configXML)
else:
response = self.create_job(jobName, configXML)
return response
def job_exists(self, jobName):
connection = self.create_connection()
existsURL = "/job/" + jobName + "/config.xml"
headers = {"Authorization": self.basicAuth}
connection.request("GET", existsURL,"", headers)
response = connection.getresponse()
if response.status == 200:
return True
else:
return False
# Fill out this method and force the post_jenkins_job method to only
# create the job if job_running is false
def job_running(self, jobName):
return False
def create_job(self, jobName, configXML):
connection = self.create_connection()
createURL = "/createItem?name=" + jobName
headers = {"Content-Type" : "text/xml", "Accept": "text/plain", "Authorization": self.basicAuth}
connection.request("POST", createURL, configXML, headers)
response = connection.getresponse()
return response
def update_job(self, jobName, configXML):
connection = self.create_connection()
updateURL = "/job/" + jobName + "/config.xml"
headers = {"Content-Type" : "text/xml", "Accept": "text/plain", "Authorization": self.basicAuth}
connection.request("POST", updateURL, configXML, headers)
response = connection.getresponse()
return response
def create_connection(self):
if self.connectionType != 'http':
connection = httplib.HTTPSConnection(self.jenkinsURL, timeout=30)
else:
connection = httplib.HTTPConnection(self.jenkinsURL, timeout=30)
return connection