Skip to content

Instantly share code, notes, and snippets.

@raidzero
Created June 28, 2016 17:02
Show Gist options
  • Save raidzero/3ba71e0da46d9f761ca186703b2983bf to your computer and use it in GitHub Desktop.
Save raidzero/3ba71e0da46d9f761ca186703b2983bf to your computer and use it in GitHub Desktop.
get list of running & queued builds on a teamcity server
#!/usr/bin/env python
import base64
import os
import sys
import urllib2
import xml.etree.ElementTree
def openUrl(urlStr, user, passwd):
req = urllib2.Request(urlStr)
authStr = base64.encodestring('%s:%s' % (user, passwd)).strip()
req.add_header('Authorization', 'Basic %s' % authStr)
return urllib2.urlopen(req)
def printQueuedBuildInfo(urlStr, user, passwd):
infoRes = openUrl(urlStr, USER, PASS)
infoXml = xml.etree.ElementTree.parse(infoRes).getroot()
version = ''
projectName = ''
buildName = ''
for info in infoXml:
infoTag = info.tag
infoData = info.attrib
if infoTag == 'properties':
for property in infoXml.iter('property'):
if property.attrib['name'] == 'branch_version': # this is specific to these builds
version = property.attrib['value']
if len(infoData) > 0 and 'name' in infoData and 'projectName' in infoData:
projectName = infoData['projectName']
buildName = infoData['name']
print projectName + ' - ' + buildName + ' build #' + version + '.[PROJECTED_BUILD_NUMBER_HERE]'
def printRunningBuildInfo(urlStr, user, passwd, number):
infoRes = openUrl(urlStr, USER, PASS)
infoXml = xml.etree.ElementTree.parse(infoRes).getroot()
for info in infoXml:
infoData = info.attrib
if info.tag == 'buildType':
print infoData['projectName'] + ' - ' + infoData['name'] + ' build #' + number
CONFIG_FILE = "/Users/%s/.config/teamcity" % os.environ['USER']
SERVER_KEY = "SERVER_URL"
USER_KEY = "USER"
PASS_KEY = "PASS"
CONFIG_DELIMITER = ":::"
CONFIG_PARAMS = {}
if not os.path.isfile(CONFIG_FILE):
print "~/.config/teamcity is missing"
sys.exit(1)
# parse config
with open(CONFIG_FILE, 'r') as f:
DATA = f.readlines()
for LINE in DATA:
LINE = LINE.strip()
LINEDATA = LINE.split(CONFIG_DELIMITER)
CONFIG_PARAMS[LINEDATA[0]] = LINEDATA[1]
URL = CONFIG_PARAMS[SERVER_KEY]
USER = CONFIG_PARAMS[USER_KEY]
PASS = CONFIG_PARAMS[PASS_KEY]
QUEUED_PATH = '/httpAuth/app/rest/buildQueue'
RUNNING_PATH = '/httpAuth/app/rest/builds?locator=running:true'
# open connections for running & queued builds
runningResult = openUrl(URL + RUNNING_PATH, USER, PASS)
queuedResult = openUrl(URL + QUEUED_PATH, USER, PASS)
# get parsed xml for both
runningXml = xml.etree.ElementTree.parse(runningResult).getroot()
queuedXml = xml.etree.ElementTree.parse(queuedResult).getroot()
numRunning = len(runningXml)
numQueued = len(queuedXml)
if numRunning > 0:
print "%d Running build(s):" % numRunning
for child in runningXml:
runningData = child.attrib
printRunningBuildInfo(URL + runningData['href'], USER, PASS, runningData['number'])
else:
print "No running builds."
if numQueued > 0:
print "%d Queued build(s):" % numQueued
for child in queuedXml:
data = child.attrib
if data['href'] != None:
printQueuedBuildInfo(URL + data['href'], USER, PASS)
else:
print "No queued builds."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment