Luca Prete | 8137ffc | 2016-08-16 11:53:45 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import re |
| 6 | import argparse |
| 7 | from stat import ST_MODE, ST_SIZE |
| 8 | from subprocess import check_output |
| 9 | |
| 10 | # Add the Mininet build script directory to the path, so we can import it |
| 11 | #TODO do a better job finding Mininet |
Luca Prete | 32a1fdb | 2016-08-16 15:00:41 -0700 | [diff] [blame^] | 12 | path = os.path.join( '/home', 'mininet', 'mininet','util', 'vm' ) |
Luca Prete | 8137ffc | 2016-08-16 11:53:45 -0700 | [diff] [blame] | 13 | path = os.path.expanduser( path ) |
| 14 | if os.path.isdir( path ): |
| 15 | sys.path.append( path ) |
| 16 | else: |
| 17 | print 'Cannot find Mininet build script. Exiting...' |
| 18 | sys.exit( 1 ) |
| 19 | |
| 20 | # Import required functions from the Mininet build script |
| 21 | from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run |
| 22 | |
| 23 | def install( vm, scriptFileName='install-base.sh', prompt=Prompt ): |
| 24 | # start with sendline |
| 25 | #TODO consider resizing the HDD |
| 26 | url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/onos-tutorial-1.6;f=%s' % scriptFileName |
| 27 | vm.sendline( ' wget -O %s "%s" | bash' % (scriptFileName, url) ) # space prefix is used to avoid entry in history |
| 28 | vm.expect( prompt, timeout=20 ) |
| 29 | vm.sendline( 'bash %s' % scriptFileName ) |
| 30 | vm.expect( prompt, timeout=3600 ) |
| 31 | |
| 32 | |
| 33 | def vmdk2size( vmdk ): |
| 34 | "Return virtual disk size (in bytes) of vmdk image" |
| 35 | output = check_output( [ 'file', vmdk ] ) |
| 36 | assert 'disk image' in output |
| 37 | return os.stat( vmdk )[ ST_SIZE ] |
| 38 | |
| 39 | def buildOnosVm( image, out, scriptFileName, vmname='ONOS', vminfo='ONOS VM', |
| 40 | flavor='utopic64server', installMemory=4096, cpuCores=4, ovfMemory=4096 ): |
| 41 | |
| 42 | bootAndRun( image, runFunction=install, scriptFileName=scriptFileName, memory=installMemory, cpuCores=cpuCores, outputFile=out ) |
| 43 | |
| 44 | size = vmdk2size( out + '.vmdk' ) # get vmdk size |
| 45 | ovfname = out |
| 46 | osname, osid = OVFOSNameID( flavor ) |
| 47 | print osname, osid |
| 48 | |
| 49 | ovf = generateOVF( name=ovfname, osname=osname, osid=osid, |
| 50 | diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2, |
| 51 | vmname=vmname, vminfo=vminfo ) |
| 52 | log( '* Generated OVF descriptor file', ovf ) |
| 53 | |
| 54 | if __name__ == '__main__': |
| 55 | parser = argparse.ArgumentParser( description='ONOS VM build script' ) |
| 56 | parser.add_argument( '-i', '--image', metavar='image', required=True, |
| 57 | help='Build from an existing VM image' ) |
| 58 | parser.add_argument( '-f', '--flavor', default='trusty64server', required=True, |
| 59 | help='VM flavor to build (e.g. trusty64server)' ) |
| 60 | parser.add_argument( 'scripts', nargs='*', |
| 61 | help='VM flavor(s) to build (e.g. trusty64server)' ) |
| 62 | parser.add_argument( '-z', '--zip', action='store_true', |
| 63 | help='archive .ovf and .vmdk into .zip file' ) |
| 64 | parser.add_argument( '-o', '--out', required=True, |
| 65 | help='output file for test image (vmdk)' ) |
| 66 | parser.add_argument( '-s', '--script', required=True, |
| 67 | help='Install script filename' ) |
| 68 | parser.add_argument( '--vmname', default='ONOS', |
| 69 | help='Name of the generated VM image') |
| 70 | parser.add_argument( '--vminfo', default='ONOS VM', |
| 71 | help='Short description of the VM') |
| 72 | args = parser.parse_args() |
| 73 | |
| 74 | print args |
| 75 | |
| 76 | buildOnosVm( args.image, args.out, args.script, vmname=args.vmname, vminfo=args.vminfo, flavor=args.flavor) |
| 77 | |
| 78 | if args.zip: |
| 79 | log( '* Generating .zip file' ) |
| 80 | run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) ) |