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 |
alshabib | 9a044c1 | 2014-11-17 10:36:36 -0800 | [diff] [blame] | 12 | path = os.path.join( '/home', 'mininet', 'mininet','util', 'vm' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -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 installONOS( vm, prompt=Prompt ): |
| 24 | # start with sendline |
| 25 | #TODO consider resizing the HDD |
alshabib | 858bbcc | 2014-11-17 10:55:56 -0800 | [diff] [blame] | 26 | url = 'https://gerrit.onosproject.org/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/onos-tutorial;f=vm-setup.sh' |
Brian O'Connor | 4c074f4 | 2014-08-12 13:39:34 -0700 | [diff] [blame] | 27 | vm.sendline( ' wget -O vm-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history |
Brian O'Connor | 5b18ef5 | 2014-08-05 02:25:53 -0700 | [diff] [blame] | 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 | |
alshabib | 858bbcc | 2014-11-17 10:55:56 -0800 | [diff] [blame] | 33 | vm.sendline( 'git clone https://gerrit.onosproject.org/onos-next/' ) |
| 34 | vm.expect("Username for 'https://gerrit.onosproject.org':", timeout=30) |
alshabib | bca6bf2 | 2014-11-15 16:38:45 -0800 | [diff] [blame] | 35 | vm.sendline('vm') |
alshabib | 858bbcc | 2014-11-17 10:55:56 -0800 | [diff] [blame] | 36 | vm.expect("Password for 'https://vm@gerrit.onosproject.org':", timeout=30) |
alshabib | 0dd4f39 | 2014-11-17 10:46:39 -0800 | [diff] [blame] | 37 | vm.sendline(os.environ['ONOSPASSWORD']) |
alshabib | bca6bf2 | 2014-11-15 16:38:45 -0800 | [diff] [blame] | 38 | |
| 39 | vm.expect( prompt, timeout=1200 ) |
alshabib | ff061ee | 2014-11-17 15:57:10 -0800 | [diff] [blame] | 40 | |
| 41 | |
alshabib | 295ac80 | 2014-11-17 14:05:01 -0800 | [diff] [blame] | 42 | vm.sendline('sudo su - tutorial1') |
| 43 | vm.expect(prompt, timeout=20) |
alshabib | bca6bf2 | 2014-11-15 16:38:45 -0800 | [diff] [blame] | 44 | vm.sendline('ssh 127.0.0.1') |
| 45 | vm.expect('.*connecting (yes/no)?') |
| 46 | vm.sendline('yes') |
| 47 | vm.sendline('exit') |
| 48 | |
| 49 | vm.expect( prompt, timeout=1200 ) |
alshabib | cc13025 | 2014-11-17 10:19:35 -0800 | [diff] [blame] | 50 | |
alshabib | 858bbcc | 2014-11-17 10:55:56 -0800 | [diff] [blame] | 51 | url = 'https://gerrit.onosproject.org/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/onos-tutorial;f=onos-setup.sh' |
alshabib | cc13025 | 2014-11-17 10:19:35 -0800 | [diff] [blame] | 52 | vm.sendline( ' wget -O onos-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history |
| 53 | vm.expect( prompt, timeout=20 ) |
alshabib | bca6bf2 | 2014-11-15 16:38:45 -0800 | [diff] [blame] | 54 | vm.sendline( 'bash onos-setup.sh' ) |
| 55 | |
| 56 | |
| 57 | |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 58 | def vmdk2size( vmdk ): |
| 59 | "Return virtual disk size (in bytes) of vmdk image" |
| 60 | output = check_output( [ 'file', vmdk ] ) |
| 61 | assert 'disk image' in output |
| 62 | return os.stat( vmdk )[ ST_SIZE ] |
| 63 | |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 64 | def buildONOS( image, out, flavor='trusty64server', installMemory=1024, ovfMemory=1024 ): |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 65 | if not image: |
| 66 | # build VM from flavor |
| 67 | #build( flavor ) |
| 68 | pass |
| 69 | |
| 70 | bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out ) |
| 71 | |
| 72 | size = vmdk2size( out + '.vmdk' ) # get vmdk size |
| 73 | ovfname = out |
| 74 | osname, osid = OVFOSNameID( flavor ) |
| 75 | print osname, osid |
| 76 | |
| 77 | ovf = generateOVF( name=ovfname, osname=osname, osid=osid, |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 78 | diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=1, |
| 79 | vmname='Mininet-Tutorial', vminfo='Mininet Tutorial VM' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 80 | log( '* Generated OVF descriptor file', ovf ) |
| 81 | |
| 82 | if __name__ == '__main__': |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 83 | parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 84 | parser.add_argument( '-i', '--image', metavar='image', required=True, |
| 85 | help='Build from an existing VM image' ) |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 86 | parser.add_argument( '-f', '--flavor', default='trusty64server', |
| 87 | help='VM flavor to build (e.g. trusty64server)' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 88 | parser.add_argument( 'scripts', nargs='*', |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 89 | help='VM flavor(s) to build (e.g. trusty64server)' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 90 | parser.add_argument( '-z', '--zip', action='store_true', |
| 91 | help='archive .ovf and .vmdk into .zip file' ) |
| 92 | parser.add_argument( '-o', '--out', required=True, |
| 93 | help='output file for test image (vmdk)' ) |
| 94 | args = parser.parse_args() |
| 95 | |
| 96 | print args |
| 97 | |
| 98 | buildONOS( args.image, args.out ) |
| 99 | |
| 100 | if args.zip: |
| 101 | log( '* Generating .zip file' ) |
| 102 | run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) ) |