blob: 043fb93c9d9ed4a291eae64e6431050f410098f1 [file] [log] [blame]
Luca Prete8137ffc2016-08-16 11:53:45 -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
Luca Prete15fb3192016-08-16 14:57:04 -070012path = os.path.join( '/home', 'mininet', 'mininet','util', 'vm' )
Luca Prete8137ffc2016-08-16 11:53:45 -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
Luca Prete15fb3192016-08-16 14:57:04 -070026 url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/onos-tutorial-1.1;f=vm-setup.sh'
Luca Prete8137ffc2016-08-16 11:53:45 -070027 vm.sendline( ' wget -O vm-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
28 vm.expect( prompt, timeout=20 )
29 vm.sendline( 'bash vm-setup.sh' )
30 vm.expect( prompt, timeout=3600 )
31
Luca Prete15fb3192016-08-16 14:57:04 -070032 url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/onos-tutorial-1.1;f=onos-setup.sh'
Luca Prete8137ffc2016-08-16 11:53:45 -070033 vm.sendline( ' wget -O onos-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
34 vm.expect( prompt, timeout=20 )
35 vm.sendline( 'bash onos-setup.sh' )
36 vm.expect(prompt, timeout=3600)
37
Luca Prete8137ffc2016-08-16 11:53:45 -070038
39def vmdk2size( vmdk ):
40 "Return virtual disk size (in bytes) of vmdk image"
41 output = check_output( [ 'file', vmdk ] )
42 assert 'disk image' in output
43 return os.stat( vmdk )[ ST_SIZE ]
44
Luca Prete15fb3192016-08-16 14:57:04 -070045def buildONOS( image, out, flavor='trusty64server', installMemory=2048, ovfMemory=4096 ):
Luca Prete8137ffc2016-08-16 11:53:45 -070046 if not image:
47 # build VM from flavor
48 #build( flavor )
49 pass
50
51 bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out )
52
53 size = vmdk2size( out + '.vmdk' ) # get vmdk size
54 ovfname = out
55 osname, osid = OVFOSNameID( flavor )
56 print osname, osid
57
58 ovf = generateOVF( name=ovfname, osname=osname, osid=osid,
Luca Prete15fb3192016-08-16 14:57:04 -070059 diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2,
60 vmname='ONOS-Tutorial', vminfo='ONOS Tutorial VM' )
Luca Prete8137ffc2016-08-16 11:53:45 -070061 log( '* Generated OVF descriptor file', ovf )
62
63if __name__ == '__main__':
Luca Prete15fb3192016-08-16 14:57:04 -070064 parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' )
Luca Prete8137ffc2016-08-16 11:53:45 -070065 parser.add_argument( '-i', '--image', metavar='image', required=True,
66 help='Build from an existing VM image' )
Luca Prete15fb3192016-08-16 14:57:04 -070067 parser.add_argument( '-f', '--flavor', default='trusty64server',
68 help='VM flavor to build (e.g. trusty64server)' )
Luca Prete8137ffc2016-08-16 11:53:45 -070069 parser.add_argument( 'scripts', nargs='*',
Luca Prete15fb3192016-08-16 14:57:04 -070070 help='VM flavor(s) to build (e.g. trusty64server)' )
Luca Prete8137ffc2016-08-16 11:53:45 -070071 parser.add_argument( '-z', '--zip', action='store_true',
72 help='archive .ovf and .vmdk into .zip file' )
73 parser.add_argument( '-o', '--out', required=True,
74 help='output file for test image (vmdk)' )
75 args = parser.parse_args()
76
77 print args
78
79 buildONOS( args.image, args.out )
80
81 if args.zip:
82 log( '* Generating .zip file' )
83 run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) )