Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -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 |
| 12 | path = os.path.join( '~', 'mininet', 'util', 'vm' ) |
| 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 installONOS( vm, prompt=Prompt ): |
| 24 | # start with sendline |
| 25 | #TODO consider resizing the HDD |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 26 | url = 'https://gerrit.onlab.us/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/mininet-tutorial;f=vm-setup.sh' |
Brian O'Connor | 5b18ef5 | 2014-08-05 02:25:53 -0700 | [diff] [blame] | 27 | vm.sendline( 'wget -O vm-setup.sh "%s" | bash' % url ) |
| 28 | vm.expect( prompt, timeout=20 ) |
| 29 | vm.sendline( 'bash vm-setup.sh' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 30 | # end with expect prompt |
Brian O'Connor | 10d941e | 2014-06-12 19:55:09 -0700 | [diff] [blame] | 31 | vm.expect( prompt, timeout=1200 ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 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 | |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 39 | def buildONOS( image, out, flavor='trusty64server', installMemory=1024, ovfMemory=1024 ): |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 40 | if not image: |
| 41 | # build VM from flavor |
| 42 | #build( flavor ) |
| 43 | pass |
| 44 | |
| 45 | bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out ) |
| 46 | |
| 47 | size = vmdk2size( out + '.vmdk' ) # get vmdk size |
| 48 | ovfname = out |
| 49 | osname, osid = OVFOSNameID( flavor ) |
| 50 | print osname, osid |
| 51 | |
| 52 | ovf = generateOVF( name=ovfname, osname=osname, osid=osid, |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 53 | diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=1, |
| 54 | vmname='Mininet-Tutorial', vminfo='Mininet Tutorial VM' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 55 | log( '* Generated OVF descriptor file', ovf ) |
| 56 | |
| 57 | if __name__ == '__main__': |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 58 | parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 59 | parser.add_argument( '-i', '--image', metavar='image', required=True, |
| 60 | help='Build from an existing VM image' ) |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 61 | parser.add_argument( '-f', '--flavor', default='trusty64server', |
| 62 | help='VM flavor to build (e.g. trusty64server)' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 63 | parser.add_argument( 'scripts', nargs='*', |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 64 | help='VM flavor(s) to build (e.g. trusty64server)' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 65 | parser.add_argument( '-z', '--zip', action='store_true', |
| 66 | help='archive .ovf and .vmdk into .zip file' ) |
| 67 | parser.add_argument( '-o', '--out', required=True, |
| 68 | help='output file for test image (vmdk)' ) |
| 69 | args = parser.parse_args() |
| 70 | |
| 71 | print args |
| 72 | |
| 73 | buildONOS( args.image, args.out ) |
| 74 | |
| 75 | if args.zip: |
| 76 | log( '* Generating .zip file' ) |
| 77 | run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) ) |