forked from alagalah/gbpsfc-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfrastructure_launch.py
executable file
·150 lines (134 loc) · 5.31 KB
/
infrastructure_launch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import socket
import os
import re
import time
import sys
import ipaddr
import commands
from subprocess import call
from subprocess import check_output
from infrastructure_config import *
def addController(sw, ip):
call(['ovs-vsctl', 'set-controller', sw, 'tcp:%s:6653' % ip ])
def addManager(ip):
cmd="ovs-vsctl set-manager tcp:%s:6640" % ip
listcmd=cmd.split()
print check_output(listcmd)
def addSwitch(name, dpid=None):
call(['ovs-vsctl', 'add-br', name]) #Add bridge
if dpid:
if len(dpid) < 16: #DPID must be 16-bytes in later versions of OVS
filler='0000000000000000'
dpid=filler[:len(filler)-len(dpid)]+dpid
elif len(dpid) > 16:
print 'DPID: %s is too long' % dpid
sys.exit(3)
call(['ovs-vsctl','set','bridge', name,'other-config:datapath-id=%s'%dpid])
def addHost(net, switch, name, ip, mac):
containerID=launchContainer()
def setOFVersion(sw, version='OpenFlow13,OpenFlow12,OpenFlow10'):
call(['ovs-vsctl', 'set', 'bridge', sw, 'protocols={}'.format(version)])
def addTunnel(sw, sourceIp=None):
ifaceName = '{}-vxlan-0'.format(sw)
cmd = ['ovs-vsctl', 'add-port', sw, ifaceName,
'--', 'set', 'Interface', ifaceName,
'type=vxlan',
'options:remote_ip=flow',
'options:key=flow']
# if sourceIp is not None:
# cmd.append('options:source_ip={}'.format(sourceIp))
call(cmd)
def addGpeTunnel(sw, sourceIp=None):
ifaceName = '{}-vxlangpe-0'.format(sw)
cmd = ['ovs-vsctl', 'add-port', sw, ifaceName,
'--', 'set', 'Interface', ifaceName,
'type=vxlan',
'options:remote_ip=flow',
'options:dst_port=6633',
'options:nshc1=flow',
'options:nshc2=flow',
'options:nshc3=flow',
'options:nshc4=flow',
'options:nsp=flow',
'options:nsi=flow',
'options:key=flow']
# if sourceIp is not None:
# cmd.append('options:source_ip={}'.format(sourceIp))
call(cmd)
def launchContainer(host,containerImage):
containerID= check_output(['docker','run','-d','--net=none','--name=%s'%host['name'],'-h',host['name'],'-t', '-i','--privileged=True',containerImage,'/bin/bash']) #docker run -d --net=none --name={name} -h {name} -t -i {image} /bin/bash
#print "created container:", containerID[:-1]
return containerID[:-1] #Remove extraneous \n from output of above
def connectContainerToSwitch(sw,host,containerID,of_port):
hostIP=host['ip']
mac=host['mac']
nw = ipaddr.IPv4Network(hostIP)
broadcast = "{}".format(nw.broadcast)
router = "{}".format(nw.network + 1)
cmd=['/vagrant/ovswork.sh',sw,containerID,hostIP,broadcast,router,mac,of_port,host['name']]
if host.has_key('vlan'):
cmd.append(host['vlan'])
call(cmd)
def doCmd(cmd):
listcmd=cmd.split()
print check_output(listcmd)
def launch(switches, hosts, contIP='127.0.0.1'):
for sw in switches:
addManager(contIP)
ports=0
first_host=True
for host in hosts:
if host['switch'] == sw['name']:
if first_host:
dpid=sw['dpid']
addSwitch(sw['name'],sw['dpid'])
setOFVersion(sw['name'])
addController(sw['name'], contIP)
addGpeTunnel(sw['name'])
addTunnel(sw['name'])
first_host=False
containerImage=defaultContainerImage #from Config
if host.has_key('container_image'): #from Config
containerImage=host['container_image']
containerID=launchContainer(host,containerImage)
ports+=1
connectContainerToSwitch(sw['name'],host,containerID,str(ports))
host['port-name']='vethl-'+host['name']
print "Created container: %s with IP: %s. Connect using 'docker attach %s', disconnect with ctrl-p-q." % (host['name'],host['ip'],host['name'])
if __name__ == "__main__" :
# print "Cleaning environment..."
# doCmd('/vagrant/clean.sh')
sw_index=int(socket.gethostname().split("gbpsfc",1)[1])-1
if sw_index in range(0,len(switches)+1):
controller=os.environ.get('ODL')
sw_type = switches[sw_index]['type']
sw_name = switches[sw_index]['name']
if sw_type == 'gbp':
print "*****************************"
print "Configuring %s as a GBP node." % sw_name
print "*****************************"
print
launch([switches[sw_index]],hosts,controller)
print "*****************************"
print "OVS status:"
print "-----------"
print
doCmd('ovs-vsctl show')
print
print "Docker containers:"
print "------------------"
doCmd('docker ps')
print "*****************************"
elif sw_type == 'sff':
print "*****************************"
print "Configuring %s as an SFF." % sw_name
print "*****************************"
doCmd('sudo ovs-vsctl set-manager tcp:%s:6640' % controller)
print
elif sw_type == 'sf':
print "*****************************"
print "Configuring %s as an SF, running /home/vagrant/sf-config.sh locally." % sw_name
print "*****************************"
doCmd('sudo /home/vagrant/sf-config.sh')
#addGpeTunnel(switches[sw_index]['name'])