X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetstack%2Fopenstack%2Fdriver.py;h=15ea3f5124c3bdc541b61580c2fe672e659a9bf1;hb=44187013d70d0ed5362f90baa237223c12c9324e;hp=b9faa975a98c3a95b63eed5c43a3012f64c512a1;hpb=ac3e014b8c3c2fe436d25514d92e81537e9587de;p=plstackapi.git diff --git a/planetstack/openstack/driver.py b/planetstack/openstack/driver.py index b9faa97..15ea3f5 100644 --- a/planetstack/openstack/driver.py +++ b/planetstack/openstack/driver.py @@ -239,7 +239,15 @@ class OpenStackDriver: self.delete_external_route(subnet) return 1 - def add_external_route(self, subnet): + def get_external_routes(self): + status, output = commands.getstatusoutput('route') + routes = output.split('\n')[3:] + return routes + + def add_external_route(self, subnet, routes=[]): + if not routes: + routes = self.get_external_routes() + ports = self.shell.quantum.list_ports()['ports'] gw_ip = subnet['gateway_ip'] @@ -256,14 +264,23 @@ class OpenStackDriver: gw_port = port router_id = gw_port['device_id'] router = self.shell.quantum.show_router(router_id)['router'] - ext_net = router['external_gateway_info']['network_id'] - for port in ports: - if port['device_id'] == router_id and port['network_id'] == ext_net: - ip_address = port['fixed_ips'][0]['ip_address'] + if router and router.get('external_gateway_info'): + ext_net = router['external_gateway_info']['network_id'] + for port in ports: + if port['device_id'] == router_id and port['network_id'] == ext_net: + ip_address = port['fixed_ips'][0]['ip_address'] if ip_address: - cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address) - commands.getstatusoutput(cmd) + # check if external route already exists + route_exists = False + if routes: + for route in routes: + if subnet['cidr'] in route and ip_address in route: + route_exists = True + if not route_exists: + cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address) + s, o = commands.getstatusoutput(cmd) + #print cmd, "\n", s, o return 1