blob: 087c98ccdc7891e5f736afad9073b26d91d97cac [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 Hart53433b52015-03-26 17:23:34 -070028 url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/new-onos-tutorial;f=vm-setup-lxc.sh'
Jonathan Harta09e5352015-03-26 17:30:36 -070029 vm.sendline( ' wget -O vm-setup-lxc.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 )
Jonathan Hart53433b52015-03-26 17:23:34 -070031 vm.sendline( 'bash vm-setup-lxc.sh' )
alshabibfed22522014-12-09 00:49:59 -080032 vm.expect( prompt, timeout=3600 )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070033
Jonathan Hart7b6cfef2015-03-27 10:09:38 -070034
Jonathan Hartc3f5a142015-03-30 16:42:37 -070035 #vm.sendline('sudo su - mininet')
36 #vm.expect(prompt, timeout=20)
37
Jonathan Hart7b6cfef2015-03-27 10:09:38 -070038 #vm.sendline( 'git clone https://gerrit.onosproject.org/onos' )
39 #vm.expect( prompt, timeout=30 )
40 #vm.sendline( 'cd onos && git checkout %s && cd -' % onos_version )
41 #vm.expect( prompt, timeout=1200 )
alshabibff061ee2014-11-17 15:57:10 -080042
43
Jonathan Hart0defebb2015-03-27 10:21:19 -070044 #vm.sendline('ssh 127.0.0.1')
45 #vm.expect('.*connecting (yes/no)?')
46 #vm.sendline('yes')
47 #vm.expect(prompt, timeout=30)
48 #vm.sendline('exit')
alshabibbca6bf22014-11-15 16:38:45 -080049
Jonathan Hart0defebb2015-03-27 10:21:19 -070050 #vm.expect( prompt, timeout=1200 )
alshabibcc130252014-11-17 10:19:35 -080051
Jonathan Hartc3f5a142015-03-30 16:42:37 -070052 #url = 'https://gerrit.onosproject.org/gitweb?p=onos-vm.git;a=blob_plain;hb=refs/heads/onos-tutorial;f=onos-setup-lxc.sh'
53 #vm.sendline( ' wget -O onos-setup-lxc.sh "%s" | bash' % url ) # space prefix is used to avoid entry in history
54 #vm.expect( prompt, timeout=20 )
55 #vm.sendline( 'bash onos-setup-lxc.sh' )
56 #vm.expect(prompt, timeout=3600)
57 #vm.sendline('exit')
58 #vm.expect(prompt, timeout=20)
Jonathan Hart7b6cfef2015-03-27 10:09:38 -070059
alshabib22fd2312014-11-18 18:33:34 -080060
alshabibbca6bf22014-11-15 16:38:45 -080061
Brian O'Connor37f0cc72014-06-06 20:03:05 -070062def vmdk2size( vmdk ):
63 "Return virtual disk size (in bytes) of vmdk image"
64 output = check_output( [ 'file', vmdk ] )
65 assert 'disk image' in output
66 return os.stat( vmdk )[ ST_SIZE ]
67
alshabib0ecf4c42014-12-11 15:11:44 -080068def buildONOS( image, out, flavor='trusty64server', installMemory=2048, ovfMemory=4096 ):
Brian O'Connor37f0cc72014-06-06 20:03:05 -070069 if not image:
70 # build VM from flavor
71 #build( flavor )
72 pass
73
74 bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out )
75
76 size = vmdk2size( out + '.vmdk' ) # get vmdk size
77 ovfname = out
78 osname, osid = OVFOSNameID( flavor )
79 print osname, osid
80
81 ovf = generateOVF( name=ovfname, osname=osname, osid=osid,
alshabibd2b24562014-11-18 19:24:00 -080082 diskname=out+'.vmdk', disksize=size, mem=ovfMemory, cpus=2,
83 vmname='ONOS-Tutorial', vminfo='ONOS Tutorial VM' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070084 log( '* Generated OVF descriptor file', ovf )
85
86if __name__ == '__main__':
Brian O'Connor3ca84022014-08-11 19:46:54 -070087 parser = argparse.ArgumentParser( description='Mininet Tutorial VM build script' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070088 parser.add_argument( '-i', '--image', metavar='image', required=True,
89 help='Build from an existing VM image' )
Brian O'Connor3ca84022014-08-11 19:46:54 -070090 parser.add_argument( '-f', '--flavor', default='trusty64server',
91 help='VM flavor to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070092 parser.add_argument( 'scripts', nargs='*',
Brian O'Connor3ca84022014-08-11 19:46:54 -070093 help='VM flavor(s) to build (e.g. trusty64server)' )
Brian O'Connor37f0cc72014-06-06 20:03:05 -070094 parser.add_argument( '-z', '--zip', action='store_true',
95 help='archive .ovf and .vmdk into .zip file' )
96 parser.add_argument( '-o', '--out', required=True,
97 help='output file for test image (vmdk)' )
98 args = parser.parse_args()
99
100 print args
101
102 buildONOS( args.image, args.out )
103
104 if args.zip:
105 log( '* Generating .zip file' )
106 run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) )