| #!/usr/bin/python |
| |
| import os |
| import sys |
| import re |
| import argparse |
| from stat import ST_MODE, ST_SIZE |
| from subprocess import check_output |
| |
| # Add the Mininet build script directory to the path, so we can import it |
| #TODO do a better job finding Mininet |
| path = os.path.join( '/home', 'mininet', 'mininet','util', 'vm' ) |
| path = os.path.expanduser( path ) |
| if os.path.isdir( path ): |
| sys.path.append( path ) |
| else: |
| print 'Cannot find Mininet build script. Exiting...' |
| sys.exit( 1 ) |
| |
| # Import required functions from the Mininet build script |
| from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run |
| |
| def install( vm, scriptFileName='install-base.sh', prompt=Prompt ): |
| # start with sendline |
| #TODO consider resizing the HDD |
| url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/onos-tutorial-1.1;f=%s' % scriptFileName |
| vm.sendline( ' wget -O %s "%s" | bash' % (scriptFileName, url) ) # space prefix is used to avoid entry in history |
| vm.expect( prompt, timeout=20 ) |
| vm.sendline( 'bash %s' % scriptFileName ) |
| vm.expect( prompt, timeout=3600 ) |
| |
| |
| def vmdk2size( vmdk ): |
| "Return virtual disk size (in bytes) of vmdk image" |
| output = check_output( [ 'file', vmdk ] ) |
| assert 'disk image' in output |
| return os.stat( vmdk )[ ST_SIZE ] |
| |
| def buildOnosVm( image, out, scriptFileName, vmname='ONOS', vminfo='ONOS VM', |
| flavor='utopic64server', installMemory=4096, ovfMemory=4096 ): |
| |
| bootAndRun( image, runFunction=install, scriptFileName=scriptFileName, memory=installMemory, outputFile=out ) |
| |
| size = vmdk2size( out + '.vmdk' ) # get vmdk size |
| ovfname = out |
| osname, osid = OVFOSNameID( flavor ) |
| print osname, osid |
| |
| ovf = generateOVF( name=ovfname, osname=osname, osid=osid, |
| diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2, |
| vmname=vmname, vminfo=vminfo ) |
| log( '* Generated OVF descriptor file', ovf ) |
| |
| if __name__ == '__main__': |
| parser = argparse.ArgumentParser( description='ONOS VM build script' ) |
| parser.add_argument( '-i', '--image', metavar='image', required=True, |
| help='Build from an existing VM image' ) |
| parser.add_argument( '-f', '--flavor', default='trusty64server', required=True, |
| help='VM flavor to build (e.g. trusty64server)' ) |
| parser.add_argument( 'scripts', nargs='*', |
| help='VM flavor(s) to build (e.g. trusty64server)' ) |
| parser.add_argument( '-z', '--zip', action='store_true', |
| help='archive .ovf and .vmdk into .zip file' ) |
| parser.add_argument( '-o', '--out', required=True, |
| help='output file for test image (vmdk)' ) |
| parser.add_argument( '-s', '--script', required=True, |
| help='Install script filename' ) |
| parser.add_argument( '--vmname', default='ONOS', |
| help='Name of the generated VM image') |
| parser.add_argument( '--vminfo', default='ONOS VM', |
| help='Short description of the VM') |
| args = parser.parse_args() |
| |
| print args |
| |
| buildOnosVm( args.image, args.out, args.script, vmname=args.vmname, vminfo=args.vminfo, flavor=args.flavor) |
| |
| if args.zip: |
| log( '* Generating .zip file' ) |
| run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) ) |