3 """ Private Bridge configurator. """
13 from threading import Thread
20 logger.log('private bridge plugin starting up...')
22 def log_call_read(command,timeout=logger.default_timeout_minutes*60,poll=1):
23 message=" ".join(command)
24 logger.log("log_call: running command %s" % message)
25 logger.verbose("log_call: timeout=%r s" % timeout)
26 logger.verbose("log_call: poll=%r s" % poll)
27 trigger=time.time()+timeout
29 child = subprocess.Popen(command, bufsize=1,
30 stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
34 # see if anything can be read within the poll interval
35 (r,w,x)=select.select([child.stdout],[],[],poll)
36 if r: stdout = stdout + child.stdout.read(1)
38 returncode=child.poll()
40 if returncode != None:
41 stdout = stdout + child.stdout.read()
42 # child is done and return 0
44 logger.log("log_call:end command (%s) completed" % message)
46 logger.log("log_call:stdout: %s" % stdout)
47 return (returncode, stdout)
50 log("log_call:end command (%s) returned with code %d" %(message,returncode))
51 return (returncode, stdout)
52 # no : still within timeout ?
53 if time.time() >= trigger:
55 logger.log("log_call:end terminating command (%s) - exceeded timeout %d s"%(message,timeout))
59 logger.log_exc("failed to run command %s" % message)
64 return log_call_read(["ovs-vsctl"] + args)
67 (returncode, stdout) = ovs_vsctl(["list-br"])
72 def ovs_addbridge(name):
73 (returncode, stdout) = ovs_vsctl(["add-br",name])
77 def ovs_listports(name):
78 (returncode, stdout) = ovs_vsctl(["list-ports", name])
83 def ovs_addport(name, portname, type, remoteip, key):
84 args = ["add-port", name, portname, "--", "set", "interface", portname, "type="+type]
86 args = args + ["options:remote_ip=" + remoteip]
88 args = args + ["options:key=" + str(key)]
90 (returncode, stdout) = ovs_vsctl(args)
94 def ovs_delport(name, portname):
95 (returncode, stdout) = ovs_vsctl(["del-port",name,portname])
99 def ensure_slicebridge_created(name, addr):
100 bridges = ovs_listbridge()
101 logger.log("privatebridge: current bridges = " + ",".join(bridges))
107 logger.log_call(["ifconfig", name, addr, "netmask", "255.0.0.0"])
109 def ensure_slicebridge_neighbors(name, sliver_id, neighbors):
110 ports = ovs_listports(name)
113 for neighbor in neighbors:
114 (neighbor_node_id, neighbor_ip) = neighbor.split("/")
115 neighbor_node_id = int(neighbor_node_id)
116 portname = "gre%d-%d" % (sliver_id, neighbor_node_id)
118 want_ports.append(portname)
120 if not portname in ports:
121 ovs_addport(name, portname, "gre", neighbor_ip, sliver_id)
124 if port.startswith("gre") and (port not in want_ports):
125 ovs_delport(name, portname)
127 def configure_slicebridge(sliver, attributes):
128 sliver_name = sliver['name']
129 sliver_id = sliver['slice_id']
131 slice_bridge_name = attributes["slice_bridge_name"]
133 slice_bridge_addr = attributes.get("slice_bridge_addr", None)
134 if not slice_bridge_addr:
135 logger.log("privatebridge: no slice_bridge_addr for %s" % sliver_name)
138 slice_bridge_neighbors = attributes.get("slice_bridge_neighbors", None)
139 if not slice_bridge_neighbors:
140 logger.log("privatebridge: no slice_bridge_neighbors for %s" % sliver_name)
143 slice_bridge_neighbors = [x.strip() for x in slice_bridge_neighbors.split(",")]
145 ensure_slicebridge_created(slice_bridge_name, slice_bridge_addr)
146 ensure_slicebridge_neighbors(slice_bridge_name, sliver_id, slice_bridge_neighbors)
148 def GetSlivers(data, conf = None, plc = None):
149 node_id = tools.node_id()
151 if 'slivers' not in data:
152 logger.log_missing_data("privatebridge.GetSlivers",'slivers')
155 for sliver in data['slivers']:
156 sliver_name = sliver['name']
158 # build a dict of attributes, because it's more convenient
160 for attribute in sliver['attributes']:
161 attributes[attribute['tagname']] = attribute['value']
163 if attributes.get('slice_bridge_name',None):
164 configure_slicebridge(sliver, attributes)