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 | |
Jonathan Hart | 4a7ae2b | 2014-12-04 23:12:43 -0800 | [diff] [blame] | 20 | onos_version='1.0.0' |
| 21 | |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 22 | # Import required functions from the Mininet build script |
Jonathan Hart | 8b3b183 | 2015-03-31 10:02:22 -0700 | [diff] [blame] | 23 | from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 24 | |
| 25 | def installONOS( vm, prompt=Prompt ): |
| 26 | # start with sendline |
| 27 | #TODO consider resizing the HDD |
Jonathan Hart | 53433b5 | 2015-03-26 17:23:34 -0700 | [diff] [blame] | 28 | url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/new-onos-tutorial;f=vm-setup-lxc.sh' |
Jonathan Hart | a09e535 | 2015-03-26 17:30:36 -0700 | [diff] [blame] | 29 | vm.sendline( ' wget -O vm-setup-lxc.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] | 30 | vm.expect( prompt, timeout=20 ) |
Jonathan Hart | 53433b5 | 2015-03-26 17:23:34 -0700 | [diff] [blame] | 31 | vm.sendline( 'bash vm-setup-lxc.sh' ) |
alshabib | fed2252 | 2014-12-09 00:49:59 -0800 | [diff] [blame] | 32 | vm.expect( prompt, timeout=3600 ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 33 | |
Jonathan Hart | 7b6cfef | 2015-03-27 10:09:38 -0700 | [diff] [blame] | 34 | |
Jonathan Hart | a6348c4 | 2015-03-31 18:38:44 -0700 | [diff] [blame^] | 35 | #vm.sendline('sudo su - mininet') |
| 36 | #vm.expect(prompt, timeout=20) |
Jonathan Hart | c3f5a14 | 2015-03-30 16:42:37 -0700 | [diff] [blame] | 37 | |
Jonathan Hart | 7b6cfef | 2015-03-27 10:09:38 -0700 | [diff] [blame] | 38 | #vm.sendline( 'git clone https://gerrit.onosproject.org/onos' ) |
| 39 | #vm.expect( prompt, timeout=30 ) |
| 40 | #vm.sendline( 'cd onos && git checkout %s && cd -' % onos_version ) |
| 41 | #vm.expect( prompt, timeout=1200 ) |
alshabib | ff061ee | 2014-11-17 15:57:10 -0800 | [diff] [blame] | 42 | |
| 43 | |
Jonathan Hart | 0defebb | 2015-03-27 10:21:19 -0700 | [diff] [blame] | 44 | #vm.sendline('ssh 127.0.0.1') |
| 45 | #vm.expect('.*connecting (yes/no)?') |
| 46 | #vm.sendline('yes') |
| 47 | #vm.expect(prompt, timeout=30) |
| 48 | #vm.sendline('exit') |
alshabib | bca6bf2 | 2014-11-15 16:38:45 -0800 | [diff] [blame] | 49 | |
Jonathan Hart | 0defebb | 2015-03-27 10:21:19 -0700 | [diff] [blame] | 50 | #vm.expect( prompt, timeout=1200 ) |
alshabib | cc13025 | 2014-11-17 10:19:35 -0800 | [diff] [blame] | 51 | |
Jonathan Hart | 1cc5a1a | 2015-03-31 16:54:11 -0700 | [diff] [blame] | 52 | url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/new-onos-tutorial;f=onos-setup-lxc.sh' |
Jonathan Hart | b1e2211 | 2015-03-31 16:42:03 -0700 | [diff] [blame] | 53 | vm.sendline( ' wget -O onos-setup-lxc.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history |
| 54 | vm.expect( prompt, timeout=20 ) |
| 55 | vm.sendline( 'bash onos-setup-lxc.sh' ) |
| 56 | vm.expect(prompt, timeout=3600) |
Jonathan Hart | 1cc5a1a | 2015-03-31 16:54:11 -0700 | [diff] [blame] | 57 | |
| 58 | |
Jonathan Hart | a6348c4 | 2015-03-31 18:38:44 -0700 | [diff] [blame^] | 59 | #vm.sendline('exit') |
| 60 | #vm.expect(prompt, timeout=20) |
Jonathan Hart | 7b6cfef | 2015-03-27 10:09:38 -0700 | [diff] [blame] | 61 | |
alshabib | 22fd231 | 2014-11-18 18:33:34 -0800 | [diff] [blame] | 62 | |
alshabib | bca6bf2 | 2014-11-15 16:38:45 -0800 | [diff] [blame] | 63 | |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 64 | def vmdk2size( vmdk ): |
| 65 | "Return virtual disk size (in bytes) of vmdk image" |
| 66 | output = check_output( [ 'file', vmdk ] ) |
| 67 | assert 'disk image' in output |
| 68 | return os.stat( vmdk )[ ST_SIZE ] |
| 69 | |
alshabib | 0ecf4c4 | 2014-12-11 15:11:44 -0800 | [diff] [blame] | 70 | def buildONOS( image, out, flavor='trusty64server', installMemory=2048, ovfMemory=4096 ): |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 71 | if not image: |
| 72 | # build VM from flavor |
| 73 | #build( flavor ) |
| 74 | pass |
| 75 | |
| 76 | bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out ) |
| 77 | |
| 78 | size = vmdk2size( out + '.vmdk' ) # get vmdk size |
| 79 | ovfname = out |
| 80 | osname, osid = OVFOSNameID( flavor ) |
| 81 | print osname, osid |
| 82 | |
| 83 | ovf = generateOVF( name=ovfname, osname=osname, osid=osid, |
alshabib | d2b2456 | 2014-11-18 19:24:00 -0800 | [diff] [blame] | 84 | diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2, |
| 85 | vmname='ONOS-Tutorial', vminfo='ONOS Tutorial VM' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 86 | log( '* Generated OVF descriptor file', ovf ) |
| 87 | |
| 88 | if __name__ == '__main__': |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 89 | parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 90 | parser.add_argument( '-i', '--image', metavar='image', required=True, |
| 91 | help='Build from an existing VM image' ) |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 92 | parser.add_argument( '-f', '--flavor', default='trusty64server', |
| 93 | help='VM flavor to build (e.g. trusty64server)' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 94 | parser.add_argument( 'scripts', nargs='*', |
Brian O'Connor | 3ca8402 | 2014-08-11 19:46:54 -0700 | [diff] [blame] | 95 | help='VM flavor(s) to build (e.g. trusty64server)' ) |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 96 | parser.add_argument( '-z', '--zip', action='store_true', |
| 97 | help='archive .ovf and .vmdk into .zip file' ) |
| 98 | parser.add_argument( '-o', '--out', required=True, |
| 99 | help='output file for test image (vmdk)' ) |
| 100 | args = parser.parse_args() |
| 101 | |
| 102 | print args |
Brian O'Connor | 37f0cc7 | 2014-06-06 20:03:05 -0700 | [diff] [blame] | 103 | |
| 104 | buildONOS( args.image, args.out ) |
| 105 | |
| 106 | if args.zip: |
| 107 | log( '* Generating .zip file' ) |
| 108 | run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) ) |