fix wrong gre tunnel deleted when topology changes
[nodemanager.git] / plugins / privatebridge.py
1 #!/usr/bin/python
2
3 """ Private Bridge configurator.  """
4
5 import httplib
6 import os
7 import select
8 import shutil
9 import subprocess
10 import time
11 import tools
12
13 from threading import Thread
14 import logger
15 import tools
16
17 priority = 9
18
19 def start():
20     logger.log('private bridge plugin starting up...')
21
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
28     try:
29         child = subprocess.Popen(command, bufsize=1,
30                                  stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
31
32         stdout = ""
33         while 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)
37             # is process over ?
38             returncode=child.poll()
39             # yes
40             if returncode != None:
41                 stdout = stdout + child.stdout.read()
42                 # child is done and return 0
43                 if returncode == 0:
44                     logger.log("log_call:end command (%s) completed" % message)
45                     if stdout != "":
46                         logger.log("log_call:stdout: %s" % stdout)
47                     return (returncode, stdout)
48                 # child has failed
49                 else:
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:
54                 child.terminate()
55                 logger.log("log_call:end terminating command (%s) - exceeded timeout %d s"%(message,timeout))
56                 return (-2, None)
57                 break
58     except:
59         logger.log_exc("failed to run command %s" % message)
60
61     return (-1, None)
62
63 def ovs_vsctl(args):
64     return log_call_read(["ovs-vsctl"] + args)
65
66 def ovs_listbridge():
67     (returncode, stdout) = ovs_vsctl(["list-br"])
68     if (returncode != 0):
69         raise OvsException()
70     return stdout.split()
71
72 def ovs_addbridge(name):
73     (returncode, stdout) = ovs_vsctl(["add-br",name])
74     if (returncode != 0):
75         raise OvsException()
76
77 def ovs_listports(name):
78     (returncode, stdout) = ovs_vsctl(["list-ports", name])
79     if (returncode != 0):
80         raise OvsException()
81     return stdout.split()
82
83 def ovs_addport(name, portname, type, remoteip, key):
84     args = ["add-port", name, portname, "--", "set", "interface", portname, "type="+type]
85     if remoteip:
86         args = args + ["options:remote_ip=" + remoteip]
87     if key:
88         args = args + ["options:key=" + str(key)]
89
90     (returncode, stdout) = ovs_vsctl(args)
91     if (returncode != 0):
92         raise OvsException()
93
94 def ovs_delport(name, portname):
95     (returncode, stdout) = ovs_vsctl(["del-port",name,portname])
96     if (returncode != 0):
97         raise OvsException()
98
99 def ensure_slicebridge_created(name, addr):
100     bridges = ovs_listbridge()
101     logger.log("privatebridge: current bridges = " + ",".join(bridges))
102     if name in bridges:
103         return
104
105     ovs_addbridge(name)
106
107     logger.log_call(["ifconfig", name, addr, "netmask", "255.0.0.0"])
108
109 def ensure_slicebridge_neighbors(name, sliver_id, neighbors):
110     ports = ovs_listports(name)
111
112     want_ports = []
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)
117
118         want_ports.append(portname)
119
120         if not portname in ports:
121             ovs_addport(name, portname, "gre", neighbor_ip, sliver_id)
122
123     for portname in ports:
124         if portname.startswith("gre") and (portname not in want_ports):
125             ovs_delport(name, portname)
126
127 def configure_slicebridge(sliver, attributes):
128     sliver_name = sliver['name']
129     sliver_id = sliver['slice_id']
130
131     slice_bridge_name = attributes["slice_bridge_name"]
132
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)
136         return
137
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)
141         return
142
143     slice_bridge_neighbors = [x.strip() for x in slice_bridge_neighbors.split(",")]
144
145     ensure_slicebridge_created(slice_bridge_name, slice_bridge_addr)
146     ensure_slicebridge_neighbors(slice_bridge_name, sliver_id, slice_bridge_neighbors)
147
148 def GetSlivers(data, conf = None, plc = None):
149     node_id = tools.node_id()
150
151     if 'slivers' not in data:
152         logger.log_missing_data("privatebridge.GetSlivers",'slivers')
153         return
154
155     for sliver in data['slivers']:
156         sliver_name = sliver['name']
157
158         # build a dict of attributes, because it's more convenient
159         attributes={}
160         for attribute in sliver['attributes']:
161             attributes[attribute['tagname']] = attribute['value']
162
163         if attributes.get('slice_bridge_name',None):
164             configure_slicebridge(sliver, attributes)
165
166