RSON is awesomeness.

44 views
Skip to first unread message

Morgan Greywolf

unread,
Jul 4, 2012, 12:01:18 PM7/4/12
to rson-d...@googlegroups.com
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:
  1. Scrape it from the HTML from the Web console
  2. 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!





Patrick Maupin

unread,
Jul 4, 2012, 12:33:39 PM7/4/12
to rson-d...@googlegroups.com
On Wednesday, July 4, 2012 11:01:18 AM UTC-5, Morgan Greywolf wrote:
So I needed to get the universally-unique identifier (UUID) and host for an Oracle VM guest for scripting purposes...
 
Thanks, Patrick!

You're welcome!  Glad it helped.

FWIW, I think there might be two minor simplifications to your code (depending on the code around it):

1) Unlike JSON, RSON doesn't care if (what you think of as) an identifier has a space in it:

>>> from rson import loads
>>> loads('''
... foo bar : standard test
...   sammy davis : jr
... ''')
{u'foo bar': {u'standard test': {u'sammy davis': u'jr'}}}

2) Python strings have a built-in substitute method that is not nearly as powerful as the one in the re module, but is more than adequate for what you're doing here:

>>> 'This "is" a test'.replace('"', '')
'This is a test'


Reply all
Reply to author
Forward
0 new messages