So I needed to get the universally-unique identifier (UUID) and host for an Oracle VM guest for scripting purposes. There were only two ways I could see to do it:
- Scrape it from the HTML from the Web console
- Oracle publishes a ovm-utils package that can get it via command line
The second one seemed like a no-brainer: I could just do something like:
ovm_vmcontrol -h $OVM_HOST -p $OVM_PASS -u $OVM_USER -c list
And that should work. Only I looked at the output for 'list', which (sanitized) looks like this:
Oracle VM VM Control utility 0.5.2.
Connected.
Command : list
VM name : 'ldapsrv04'
uuid : '0004fb00000600005d0b19b16c0f734e'
server : 'oravm02'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'opsproxy'
uuid : '0004fb000006000013b1a11720ca7d33'
server : 'oravm01'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'webserver3'
uuid : '0004fb000006000065aee90bd6a3d3f4'
server : 'oravm03'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'buildserver1'
uuid : '0004fb00000600001a3895d7840a826b'
server : 'oravm01'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'webcache1'
uuid : '0004fb000006000021311662d6f8da6d'
server : 'oravm03'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'printserv1'
uuid : '0004fb0000060000836f02774f653329'
server : 'oravm02'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'monitor1'
uuid : '0004fb0000060000e08f7a106ef21558'
server : 'oravm02'
pool : 'unix_pool_0'
status : 'Running'
VM name : 'oldwebserver1'
uuid : '0004fb00000600002c6c5b37626efdff'
server : 'unassigned'
pool : 'unknown'
status : 'Stopped'
VM name : 'win2kr8_test'
uuid : '0004fb0000060000c7228ffd68fb762a'
server : 'unassigned'
pool : 'unknown'
status : 'Stopped'
listvm completed.
I thought, "Oh, gods. That's going to be a MESS to parse out with a shell script. I'll have to do some combination of grep, sed, and awk. Fsck. Might as well just do something in Perl or Python."
Then I thought, "Python....that output is almost like JSON..." and then I thought of Patrick Maupin's RSON, which I already remembered him talking about on Armed and Dangerous. After quickly reviewing the Google Code page for RSON, I thought to myself, "that's like almost valid RSON!"
so I took and added a hash mark on the non-parseable lines of the output with a few quick regexp replaces in Python, took out all the single quotes, and parsing this crap boiled down to one import and one line of code:
from rson import loads
output=check_output(['ovm_vmcontrol',
'-u', os.environ['OVM_USER'],
'-p', os.environ['OVM_PASS'],
'-h', os.environ['OVM_HOST'],
'-c', 'list'])
data=comment_non_parseable(output)
p=re.compile('VM name')
data=p.sub('VM_name', data)
p=re.compile("'")
data=p.sub('',data)
vm=loads(data)
print(str(vm['VM_name'][guestname]['server']))
Done! Minutes to write, lots of uses.
Thanks, Patrick!