Fun With Deploy Studio and Python

While thinking about my previous post , I wrote a quick example on how to interact with another plist formatted transactional system: Deploy Studio. Today we were running into an issue with using some post renaming scripts that DeployStudio did not know about. So ideally I just wanted to make sure that as names of machines changed , deploy studio was notified for an subseqent restores that took place. To accomplish this I took a look at the Post Methods aviable in DP and was able to get the whole proof of concept built without calling any external commands. I am going to probobly add quite a bit to this (such as pushing ARD fields ) but here is my proof of concept.

#!/usr/bin/python
# Created by zack -at- wallcity.org
# Grabs the systems hostname and updates the record in DeployStudios database.
import sys # The exits string
import plistlib # The property list library
import urllib,urllib2,base64 # Our URL handlers
from uuid import getnode as get_mac
from socket import gethostname
# Get our Mac Address
mac = get_mac()
username = "deploystudioadmin" # The Deploy Studio username
password = "f00b4rb4z" # The Deploy Studio password
server = "some.server.com" # The DP instance to communicate with
port = "60443" # The DP server
progID = "https://" # The start of the URL
mac = "%X" % get_mac() # Convert DecHex and produce "001f6bf8de5e"
mac = mac.lower() # Ensure lower
#macCol = [mac[i:i+2] for i in range(0,len(mac),2)]
#macCol = ':'.join(mac)
request = urllib2.Request(progID + server + ":" + port + "/computers/get/entry?id=" + mac )
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
httpResult = urllib2.urlopen(request)
deployStudioXML = httpResult.read()
print "Found Mac Address: " + mac
print "Found HostName: " + gethostname()
plist = plistlib.readPlistFromString(deployStudioXML)
# Set the values
for key in plist:
plist[key]['cn'] = gethostname()
plist[key]['dstudio-hostname'] = gethostname()
rootXMLPlist = plistlib.writePlistToString(plist[key])
print rootXMLPlist
submitRequest = urllib2.Request(progID + server + ":" + port + "/computers/set/entry?id=" + mac, rootXMLPlist, {'Content-type': 'text/xml'})
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
submitRequest.add_header("Authorization", "Basic %s" % base64string)
submitResult = urllib2.urlopen(submitRequest)
submitResultXML = submitResult.read()
sys.exit(0)