blob: 64b1578bc0a5734ddac286b0315a0c2fae8b4c07 [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 Prete32a1fdb2016-08-16 15:00:41 -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 install( vm, scriptFileName='install-base.sh', prompt=Prompt ):
24 # start with sendline
25 #TODO consider resizing the HDD
26 url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/onos-tutorial-1.6;f=%s' % scriptFileName
27 vm.sendline( ' wget -O %s "%s" | bash' % (scriptFileName, url) ) # space prefix is used to avoid entry in history
28 vm.expect( prompt, timeout=20 )
29 vm.sendline( 'bash %s' % scriptFileName )
30 vm.expect( prompt, timeout=3600 )
31
32
33def vmdk2size( vmdk ):
34 "Return virtual disk size (in bytes) of vmdk image"
35 output = check_output( [ 'file', vmdk ] )
36 assert 'disk image' in output
37 return os.stat( vmdk )[ ST_SIZE ]
38
39def buildOnosVm( image, out, scriptFileName, vmname='ONOS', vminfo='ONOS VM',
Luca Pretef9fb74a2016-08-16 23:35:22 -070040 flavor='xenial64server', installMemory=4096, cpuCores=4, ovfMemory=4096 ):
Luca Prete8137ffc2016-08-16 11:53:45 -070041
42 bootAndRun( image, runFunction=install, scriptFileName=scriptFileName, memory=installMemory, cpuCores=cpuCores, outputFile=out )
43
44 size = vmdk2size( out + '.vmdk' ) # get vmdk size
45 ovfname = out
46 osname, osid = OVFOSNameID( flavor )
47 print osname, osid
48
49 ovf = generateOVF( name=ovfname, osname=osname, osid=osid,
50 diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2,
51 vmname=vmname, vminfo=vminfo )
52 log( '* Generated OVF descriptor file', ovf )
53
54if __name__ == '__main__':
55 parser = argparse.ArgumentParser( description='ONOS VM build script' )
56 parser.add_argument( '-i', '--image', metavar='image', required=True,
57 help='Build from an existing VM image' )
Luca Pretef9fb74a2016-08-16 23:35:22 -070058 parser.add_argument( '-f', '--flavor', default='xenial64server', required=True,
59 help='VM flavor to build (e.g. xenial64server)' )
Luca Prete8137ffc2016-08-16 11:53:45 -070060 parser.add_argument( 'scripts', nargs='*',
Luca Pretef9fb74a2016-08-16 23:35:22 -070061 help='VM flavor(s) to build (e.g. xenial64server)' )
Luca Prete8137ffc2016-08-16 11:53:45 -070062 parser.add_argument( '-z', '--zip', action='store_true',
63 help='archive .ovf and .vmdk into .zip file' )
64 parser.add_argument( '-o', '--out', required=True,
65 help='output file for test image (vmdk)' )
66 parser.add_argument( '-s', '--script', required=True,
67 help='Install script filename' )
68 parser.add_argument( '--vmname', default='ONOS',
69 help='Name of the generated VM image')
70 parser.add_argument( '--vminfo', default='ONOS VM',
71 help='Short description of the VM')
72 args = parser.parse_args()
73
74 print args
75
76 buildOnosVm( args.image, args.out, args.script, vmname=args.vmname, vminfo=args.vminfo, flavor=args.flavor)
77
78 if args.zip:
79 log( '* Generating .zip file' )
80 run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) )