| #!/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 installONOS( vm, 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;f=vm-setup.sh' |
| vm.sendline( ' wget -O vm-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history |
| vm.expect( prompt, timeout=20 ) |
| vm.sendline( 'bash vm-setup.sh' ) |
| vm.expect( prompt, timeout=1200 ) |
| |
| |
| vm.sendline('sudo su - tutorial1') |
| vm.expect(prompt, timeout=20) |
| vm.sendline( 'git clone https://gerrit.onosproject.org/onos-next/' ) |
| vm.expect("Username for 'https://gerrit.onosproject.org':", timeout=30) |
| vm.sendline('vm') |
| vm.expect("Password for 'https://vm@gerrit.onosproject.org':", timeout=30) |
| vm.sendline(os.environ['ONOSPASSWORD']) |
| |
| vm.expect( prompt, timeout=1200 ) |
| |
| |
| vm.sendline('ssh 127.0.0.1') |
| vm.expect('.*connecting (yes/no)?') |
| vm.sendline('yes') |
| vm.expect(prompt, timeout=30) |
| vm.sendline('exit') |
| |
| vm.expect( prompt, timeout=1200 ) |
| |
| url = 'https://gerrit.onosproject.org/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/onos-tutorial;f=onos-setup.sh' |
| vm.sendline( ' wget -O onos-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history |
| vm.expect( prompt, timeout=20 ) |
| vm.sendline( 'bash onos-setup.sh' ) |
| vm.expect(prompt, timeout=1200) |
| |
| |
| 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 buildONOS( image, out, flavor='trusty64server', installMemory=2048, ovfMemory=2048 ): |
| if not image: |
| # build VM from flavor |
| #build( flavor ) |
| pass |
| |
| bootAndRun( image, runFunction=installONOS, 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=1, |
| vmname='Mininet-Tutorial', vminfo='Mininet Tutorial VM' ) |
| log( '* Generated OVF descriptor file', ovf ) |
| |
| if __name__ == '__main__': |
| parser = argparse.ArgumentParser( description='Mininet Tutorial 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', |
| 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)' ) |
| args = parser.parse_args() |
| |
| print args |
| |
| buildONOS( args.image, args.out ) |
| |
| if args.zip: |
| log( '* Generating .zip file' ) |
| run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) ) |