blob: af67cb223bc8563114a3920bab0b06b97799909b [file] [log] [blame]
Brian O'Connor37f0cc72014-06-06 20:03:05 -07001#!/usr/bin/python
2
3import os
4import sys
5import re
6import argparse
7from stat import ST_MODE, ST_SIZE
8from 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
alshabib9a044c12014-11-17 10:36:36 -080012path = os.path.join( '/home', 'mininet', 'mininet','util', 'vm' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070013path = os.path.expanduser( path )
14if os.path.isdir( path ):
15 sys.path.append( path )
16else:
17 print 'Cannot find Mininet build script. Exiting...'
18 sys.exit( 1 )
19
20# Import required functions from the Mininet build script
21from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run
22
23def installONOS( vm, prompt=Prompt ):
24 # start with sendline
25 #TODO consider resizing the HDD
alshabib858bbcc2014-11-17 10:55:56 -080026 url = 'https://gerrit.onosproject.org/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/onos-tutorial;f=vm-setup.sh'
Brian O'Connor4c074f42014-08-12 13:39:34 -070027 vm.sendline( ' wget -O vm-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
Brian O'Connor5b18ef52014-08-05 02:25:53 -070028 vm.expect( prompt, timeout=20 )
29 vm.sendline( 'bash vm-setup.sh' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070030 # end with expect prompt
Brian O'Connor10d941e2014-06-12 19:55:09 -070031 vm.expect( prompt, timeout=1200 )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070032
alshabib3a13cfb2014-11-17 19:23:04 -080033
34 vm.sendline('sudo su - tutorial1')
35 vm.expect(prompt, timeout=20)
alshabib858bbcc2014-11-17 10:55:56 -080036 vm.sendline( 'git clone https://gerrit.onosproject.org/onos-next/' )
37 vm.expect("Username for 'https://gerrit.onosproject.org':", timeout=30)
alshabibbca6bf22014-11-15 16:38:45 -080038 vm.sendline('vm')
alshabib858bbcc2014-11-17 10:55:56 -080039 vm.expect("Password for 'https://vm@gerrit.onosproject.org':", timeout=30)
alshabib0dd4f392014-11-17 10:46:39 -080040 vm.sendline(os.environ['ONOSPASSWORD'])
alshabibbca6bf22014-11-15 16:38:45 -080041
42 vm.expect( prompt, timeout=1200 )
alshabibff061ee2014-11-17 15:57:10 -080043
44
alshabib295ac802014-11-17 14:05:01 -080045 vm.expect(prompt, timeout=20)
alshabibbca6bf22014-11-15 16:38:45 -080046 vm.sendline('ssh 127.0.0.1')
47 vm.expect('.*connecting (yes/no)?')
48 vm.sendline('yes')
49 vm.sendline('exit')
50
51 vm.expect( prompt, timeout=1200 )
alshabibcc130252014-11-17 10:19:35 -080052
alshabib858bbcc2014-11-17 10:55:56 -080053 url = 'https://gerrit.onosproject.org/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/onos-tutorial;f=onos-setup.sh'
alshabibcc130252014-11-17 10:19:35 -080054 vm.sendline( ' wget -O onos-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
55 vm.expect( prompt, timeout=20 )
alshabibbca6bf22014-11-15 16:38:45 -080056 vm.sendline( 'bash onos-setup.sh' )
57
58
59
Brian O'Connor37f0cc72014-06-06 20:03:05 -070060def vmdk2size( vmdk ):
61 "Return virtual disk size (in bytes) of vmdk image"
62 output = check_output( [ 'file', vmdk ] )
63 assert 'disk image' in output
64 return os.stat( vmdk )[ ST_SIZE ]
65
Brian O'Connor3ca84022014-08-11 19:46:54 -070066def buildONOS( image, out, flavor='trusty64server', installMemory=1024, ovfMemory=1024 ):
Brian O'Connor37f0cc72014-06-06 20:03:05 -070067 if not image:
68 # build VM from flavor
69 #build( flavor )
70 pass
71
72 bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out )
73
74 size = vmdk2size( out + '.vmdk' ) # get vmdk size
75 ovfname = out
76 osname, osid = OVFOSNameID( flavor )
77 print osname, osid
78
79 ovf = generateOVF( name=ovfname, osname=osname, osid=osid,
Brian O'Connor3ca84022014-08-11 19:46:54 -070080 diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=1,
81 vmname='Mininet-Tutorial', vminfo='Mininet Tutorial VM' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070082 log( '* Generated OVF descriptor file', ovf )
83
84if __name__ == '__main__':
Brian O'Connor3ca84022014-08-11 19:46:54 -070085 parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070086 parser.add_argument( '-i', '--image', metavar='image', required=True,
87 help='Build from an existing VM image' )
Brian O'Connor3ca84022014-08-11 19:46:54 -070088 parser.add_argument( '-f', '--flavor', default='trusty64server',
89 help='VM flavor to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070090 parser.add_argument( 'scripts', nargs='*',
Brian O'Connor3ca84022014-08-11 19:46:54 -070091 help='VM flavor(s) to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070092 parser.add_argument( '-z', '--zip', action='store_true',
93 help='archive .ovf and .vmdk into .zip file' )
94 parser.add_argument( '-o', '--out', required=True,
95 help='output file for test image (vmdk)' )
96 args = parser.parse_args()
97
98 print args
99
100 buildONOS( args.image, args.out )
101
102 if args.zip:
103 log( '* Generating .zip file' )
104 run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) )