blob: 043fb93c9d9ed4a291eae64e6431050f410098f1 [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
Jonathan Hart8b3b1832015-03-31 10:02:22 -070021from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run
Brian O'Connor37f0cc72014-06-06 20:03:05 -070022
23def installONOS( vm, prompt=Prompt ):
24 # start with sendline
25 #TODO consider resizing the HDD
Jonathan Hartfa911cc2015-04-02 23:20:13 -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'
Jonathan Hart445aa5c2015-04-02 16:34:22 -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 )
Jonathan Hart445aa5c2015-04-02 16:34:22 -070029 vm.sendline( 'bash vm-setup.sh' )
alshabibfed22522014-12-09 00:49:59 -080030 vm.expect( prompt, timeout=3600 )
alshabibcc130252014-11-17 10:19:35 -080031
Jonathan Hartfa911cc2015-04-02 23:20:13 -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'
Jonathan Hart445aa5c2015-04-02 16:34:22 -070033 vm.sendline( ' wget -O onos-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
Jonathan Hartb1e22112015-03-31 16:42:03 -070034 vm.expect( prompt, timeout=20 )
Jonathan Hart445aa5c2015-04-02 16:34:22 -070035 vm.sendline( 'bash onos-setup.sh' )
Jonathan Hartb1e22112015-03-31 16:42:03 -070036 vm.expect(prompt, timeout=3600)
Jonathan Hart1cc5a1a2015-03-31 16:54:11 -070037
alshabibbca6bf22014-11-15 16:38:45 -080038
Brian O'Connor37f0cc72014-06-06 20:03:05 -070039def 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
alshabib0ecf4c42014-12-11 15:11:44 -080045def buildONOS( image, out, flavor='trusty64server', installMemory=2048, ovfMemory=4096 ):
Brian O'Connor37f0cc72014-06-06 20:03:05 -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,
alshabibd2b24562014-11-18 19:24:00 -080059 diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2,
60 vmname='ONOS-Tutorial', vminfo='ONOS Tutorial VM' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070061 log( '* Generated OVF descriptor file', ovf )
62
63if __name__ == '__main__':
Brian O'Connor3ca84022014-08-11 19:46:54 -070064 parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070065 parser.add_argument( '-i', '--image', metavar='image', required=True,
66 help='Build from an existing VM image' )
Brian O'Connor3ca84022014-08-11 19:46:54 -070067 parser.add_argument( '-f', '--flavor', default='trusty64server',
68 help='VM flavor to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070069 parser.add_argument( 'scripts', nargs='*',
Brian O'Connor3ca84022014-08-11 19:46:54 -070070 help='VM flavor(s) to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -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
Brian O'Connor37f0cc72014-06-06 20:03:05 -070078
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' ) )