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
|
#!/usr/bin/python
"""Helper to mass-edit jobs in jenkins.
"""
###############################################################################
# Copyright (c) 2011 Linaro
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
###############################################################################
import base64
from contextlib import nested
import json
import os
import sys
from tempfile import NamedTemporaryFile
import urllib2
import optparse
from lxml.etree import fromstring, tostring
optparser = optparse.OptionParser(usage="%prog <mangle script>")
optparser.add_option("--user",
help="Jenkins username")
optparser.add_option("--passwd-file", metavar="FILE",
help="File holding Jenkins password")
optparser.add_option("--really", action="store_true",
help="Actually perform changes")
optparser.add_option("--limit", type="int", default=-1,
help="Change at most LIMIT jobs")
optparser.add_option("--file",
help="Process a file instead of all jobs on a remote server")
options, args = optparser.parse_args(sys.argv[1:])
if len(args) != 1:
optparser.error("Wrong number of arguments")
d = {}
execfile(args[0], d, d)
mangler = d['mangle']
password = None
if options.passwd_file:
password = open(options.passwd_file).read().strip()
auth_headers = {
'Authorization': 'Basic %s' % (
base64.encodestring('%s:%s' % (options.user, password))[:-1],),
}
def _authJenkins(jenkins_path, data=None, extra_headers=None):
"""Make an authenticated request to jenkins.
@param jenkins_path: The path on the Jenkins instance to make the request
to.
@param data: Data to include in the request (if this is not None the
request will be a POST).
@param extra_headers: A dictionary of extra headers that will passed in
addition to Authorization.
@raises urllib2.HTTPError: If the response is not a HTTP 200.
@returns: the body of the response.
"""
headers = auth_headers.copy()
if extra_headers:
headers.update(extra_headers)
req = urllib2.Request(
'http://localhost:9090/jenkins/' + jenkins_path, data, headers)
resp = urllib2.urlopen(req)
return resp.read()
def getJobConfig(job_name):
return _authJenkins('job/' + job_name + '/config.xml')
def postConfig(url, configXml):
_authJenkins(url, configXml, {'Content-Type': 'text/xml'})
def render_xml(tree):
text = tostring(tree, xml_declaration=True, encoding='UTF-8')
line1, rest = text.split("\n", 1)
line1 = line1.replace("'", '"')
return line1 + rest
def show_diff(old, new):
with nested(NamedTemporaryFile(), NamedTemporaryFile()) as (a, b):
a.write(old)
b.write(new)
a.flush(); b.flush()
os.system('diff -u %s %s' % (a.name, b.name))
print
def process_remote_jenkins():
jobs = json.load(urllib2.urlopen('http://localhost:9090/jenkins/api/json?tree=jobs[name]'))
names = [job['name'] for job in jobs['jobs']]
names = [name for name in names if name == 'blank' or '_' in name]
limit = options.limit
for name in names:
if limit == 0:
break
limit -= 1
print "Processing:" + name
sys.stdout.flush()
text = getJobConfig(name)
tree = fromstring(text)
mangler(tree)
new_text = render_xml(tree)
if not options.really:
show_diff(text, new_text)
else:
if type(new_text) == type(u""):
new_text = new_text.encode("utf8")
postConfig(str('job/' + name + '/config.xml'), new_text)
def main():
if options.file:
text = open(options.file).read()
tree = fromstring(text)
mangler(tree)
new_text = render_xml(tree)
show_diff(text, new_text)
else:
process_remote_jenkins()
if __name__ == "__main__":
main()
|