diff options
author | Michael Hudson <michael.hudson@linaro.org> | 2011-03-18 15:56:10 +1300 |
---|---|---|
committer | Michael Hudson <michael.hudson@linaro.org> | 2011-03-18 15:56:10 +1300 |
commit | e50c4ca30bb42a90d4bca9c7cfc2fa4ccb6ab1e3 (patch) | |
tree | fdb285d9f84bfb76f4479c4a6f35bcfe461bfa66 /control | |
parent | 60a3baf8dfe6ba6bf3793375c52da038d6a39714 (diff) |
start of a tool to help maintaining the jobs on the control node
Diffstat (limited to 'control')
-rwxr-xr-x | control/mangle-jobs | 65 | ||||
-rw-r--r-- | control/test-mangle | 6 |
2 files changed, 71 insertions, 0 deletions
diff --git a/control/mangle-jobs b/control/mangle-jobs new file mode 100755 index 0000000..e74429d --- /dev/null +++ b/control/mangle-jobs @@ -0,0 +1,65 @@ +#!/usr/bin/python + +import base64 +from contextlib import nested +import json +import os +import sys +from tempfile import NamedTemporaryFile +import urllib2 + + +from lxml.etree import fromstring, tostring + +username = sys.argv[1] +password = open(sys.argv[2]).read().strip() +d = {} +execfile(sys.argv[3], d, d) +mangler = d['mangle'] + +auth_headers = { + 'Authorization': 'Basic %s' % ( + base64.encodestring('%s:%s' % (username, 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'}) + + +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] +for name in names: + print name + text = getJobConfig(name) + tree = fromstring(text) + mangler(tree) + with nested(NamedTemporaryFile(), NamedTemporaryFile()) as (a, b): + a.write(text) + b.write(tostring(tree)) + a.flush(); b.flush() + os.system('xmldiff %s %s' % (a.name, b.name)) + diff --git a/control/test-mangle b/control/test-mangle new file mode 100644 index 0000000..5f90802 --- /dev/null +++ b/control/test-mangle @@ -0,0 +1,6 @@ + +def mangle(tree): + tags = tree.xpath('/project/disabled') + if not tags: return + tag = tags[0] + tag.text = 'true' |