blob: f601648f2514fc7f5ab0abb881a67c111897d180 [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
Jonathan Hart4a7ae2b2014-12-04 23:12:43 -080020onos_version='1.0.0'
21
Brian O'Connor37f0cc72014-06-06 20:03:05 -070022# Import required functions from the Mininet build script
23from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run
24
25def installONOS( vm, prompt=Prompt ):
26 # start with sendline
27 #TODO consider resizing the HDD
Jonathan Hart449f4462014-12-04 15:37:46 -080028 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 -070029 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 -070030 vm.expect( prompt, timeout=20 )
31 vm.sendline( 'bash vm-setup.sh' )
Brian O'Connor10d941e2014-06-12 19:55:09 -070032 vm.expect( prompt, timeout=1200 )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070033
alshabib3a13cfb2014-11-17 19:23:04 -080034
35 vm.sendline('sudo su - tutorial1')
36 vm.expect(prompt, timeout=20)
alshabibea2b4492014-12-03 00:08:23 -080037 vm.sendline( 'git clone https://gerrit.onosproject.org/onos' )
Jonathan Hart4a7ae2b2014-12-04 23:12:43 -080038 vm.expect( prompt, timeout=30 )
39 vm.sendline( 'cd onos' )
40 vm.expect( prompt, timeout=30 )
41 vm.sendline( 'git checkout %s' % onos_version )
42
alshabibbca6bf22014-11-15 16:38:45 -080043
44 vm.expect( prompt, timeout=1200 )
alshabibff061ee2014-11-17 15:57:10 -080045
46
alshabibbca6bf22014-11-15 16:38:45 -080047 vm.sendline('ssh 127.0.0.1')
48 vm.expect('.*connecting (yes/no)?')
49 vm.sendline('yes')
alshabibf86bfb32014-11-17 22:30:26 -080050 vm.expect(prompt, timeout=30)
alshabibbca6bf22014-11-15 16:38:45 -080051 vm.sendline('exit')
52
53 vm.expect( prompt, timeout=1200 )
alshabibcc130252014-11-17 10:19:35 -080054
Jonathan Hart449f4462014-12-04 15:37:46 -080055 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 -080056 vm.sendline( ' wget -O onos-setup.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
57 vm.expect( prompt, timeout=20 )
alshabibbca6bf22014-11-15 16:38:45 -080058 vm.sendline( 'bash onos-setup.sh' )
alshabib411144e2014-11-18 10:56:15 -080059 vm.expect(prompt, timeout=1200)
alshabib71ec25a2014-11-18 18:59:46 -080060 vm.sendline('exit')
61 vm.expect(prompt, timeout=20)
62
alshabib22fd2312014-11-18 18:33:34 -080063
alshabibbca6bf22014-11-15 16:38:45 -080064
Brian O'Connor37f0cc72014-06-06 20:03:05 -070065def vmdk2size( vmdk ):
66 "Return virtual disk size (in bytes) of vmdk image"
67 output = check_output( [ 'file', vmdk ] )
68 assert 'disk image' in output
69 return os.stat( vmdk )[ ST_SIZE ]
70
alshabibda71cc92014-11-18 16:39:41 -080071def buildONOS( image, out, flavor='trusty64server', installMemory=2048, ovfMemory=2048 ):
Brian O'Connor37f0cc72014-06-06 20:03:05 -070072 if not image:
73 # build VM from flavor
74 #build( flavor )
75 pass
76
77 bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out )
78
79 size = vmdk2size( out + '.vmdk' ) # get vmdk size
80 ovfname = out
81 osname, osid = OVFOSNameID( flavor )
82 print osname, osid
83
84 ovf = generateOVF( name=ovfname, osname=osname, osid=osid,
alshabibd2b24562014-11-18 19:24:00 -080085 diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2,
86 vmname='ONOS-Tutorial', vminfo='ONOS Tutorial VM' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070087 log( '* Generated OVF descriptor file', ovf )
88
89if __name__ == '__main__':
Brian O'Connor3ca84022014-08-11 19:46:54 -070090 parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070091 parser.add_argument( '-i', '--image', metavar='image', required=True,
92 help='Build from an existing VM image' )
Brian O'Connor3ca84022014-08-11 19:46:54 -070093 parser.add_argument( '-f', '--flavor', default='trusty64server',
94 help='VM flavor to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070095 parser.add_argument( 'scripts', nargs='*',
Brian O'Connor3ca84022014-08-11 19:46:54 -070096 help='VM flavor(s) to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070097 parser.add_argument( '-z', '--zip', action='store_true',
98 help='archive .ovf and .vmdk into .zip file' )
99 parser.add_argument( '-o', '--out', required=True,
100 help='output file for test image (vmdk)' )
101 args = parser.parse_args()
102
103 print args
104
105 buildONOS( args.image, args.out )
106
107 if args.zip:
108 log( '* Generating .zip file' )
109 run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) )