rajiv-v
10/4/2016 - 6:57 AM

Read VM Attributes from OVF file. Helps to quickly recreate VM in another Virtualization Platform. Usage : python ovfreader.py <path-to-ovf-

Read VM Attributes from OVF file. Helps to quickly recreate VM in another Virtualization Platform. Usage : python ovfreader.py <path-to-ovf-file> or python ovfreader.py (Will read first ovf file in current dir)

ovaFile=abc.ova
# List the contents
tar -tf $ovaFile 

# Extract OVF file
tar -xvf $ovaFile abc.ovf
#!/usr/bin/env python

"""
Read VM Attributes from OVF file.
Helps to quickly recreate VM in another Virtualization Platform.
Usage : python ovfreader.py <path-to-ovf-file> or
        python ovfreader.py (Will read first ovf file in current dir)
"""

__author__ = "Rajiv Vishwa"
__license__ = "GPL"
__version__ = "1.0"
__status__ = "Production"

import xmltodict
import sys
import glob

def sizeof_fmt(num, suffix='B'):
    for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
        if abs(num) < 1024.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1024.0
    return "%.1f%s%s" % (num, 'Yi', suffix)


try:
    ovfFile = str(sys.argv[1])
except:
    try:
        #print "[INFO] Reading OVF file in current directory "
        ovfFile = glob.glob('*.ovf')[0]
    except:
        print "[ERROR] Unable to find OVF file"
        print "Usage : python ovfreader.py <path-to-ovf-file>"
        exit()



print '-------------------------------------'
print 'Reading "{}" Attributes'.format(ovfFile)
print '-------------------------------------'
with open(ovfFile) as fd:
    doc = xmltodict.parse(fd.read())

vmName = doc['Envelope']['VirtualSystem']['Name']
diskSize = doc['Envelope']['DiskSection']['Disk']['@ovf:populatedSize']
networkType = doc['Envelope']['NetworkSection']['Network']['Description']
vSystem = doc['Envelope']['VirtualSystem']['VirtualHardwareSection']['Item']


print 'VM Name                  : {}'.format(vmName)
print 'Network Type             : {}'.format(networkType)
print 'Disk Size                : {}'.format(sizeof_fmt(int(diskSize)))

for count,items in enumerate(vSystem):
    if count == 0:
        print '{}   : {}'.format(items.items()[1][1],items.items()[2][1])
    if count == 1:
        print '{}              : {}'.format(items.items()[1][1],items.items()[2][1])
        break