MCPcopy Create free account
hub / github.com/apache/cloudstack / CsRoute

Class CsRoute

systemvm/debian/opt/cloud/bin/cs/CsRoute.py:22–200  ·  view source on GitHub ↗

Manage routes

Source from the content-addressed store, hash-verified

20
21
22class CsRoute:
23
24 """ Manage routes """
25
26 def __init__(self):
27 self.table_prefix = "Table_"
28
29 def get_tablename(self, name):
30 return self.table_prefix + name
31
32 def add_table(self, devicename):
33 tablenumber = 100 + int(devicename[3:])
34 tablename = self.get_tablename(devicename)
35 str = "%s %s" % (tablenumber, tablename)
36 filename = "/etc/iproute2/rt_tables"
37 logging.info("Adding route table: " + str + " to " + filename + " if not present ")
38 if not CsHelper.definedinfile(filename, str):
39 CsHelper.execute("sudo echo " + str + " >> /etc/iproute2/rt_tables")
40 # remove "from all table tablename" if exists, else it will interfer with
41 # routing of unintended traffic
42 if self.findRule("from all lookup " + tablename):
43 CsHelper.execute("sudo ip rule delete from all table " + tablename)
44
45 def flush_table(self, tablename):
46 CsHelper.execute("ip route flush table %s" % (tablename))
47 CsHelper.execute("ip route flush cache")
48
49 def add_route(self, dev, address):
50 """ Wrapper method that adds table name and device to route statement """
51 # ip route add dev eth1 table Table_eth1 10.0.2.0/24
52 table = self.get_tablename(dev)
53
54 if not table or not address:
55 empty_param = "table" if not table else "address"
56 logging.info("Empty parameter received %s while trying to add route, skipping" % empty_param)
57 else:
58 logging.info("Adding route: dev " + dev + " table: " +
59 table + " network: " + address + " if not present")
60 cmd = "default via %s table %s proto static" % (address, table)
61 self.set_route(cmd)
62
63 def add_network_route(self, dev, address):
64 """ Wrapper method that adds table name and device to route statement """
65 # ip route add dev eth1 table Table_eth1 10.0.2.0/24
66 table = self.get_tablename(dev)
67
68 if not table or not address:
69 empty_param = "table" if not table else "address"
70 logging.info("Empty parameter received %s while trying to add network route, skipping" % empty_param)
71 else:
72 logging.info("Adding route: dev " + dev + " table: " +
73 table + " network: " + address + " if not present")
74 cmd = "throw %s table %s proto static" % (address, table)
75 self.set_route(cmd)
76
77 def set_route(self, cmd, method="add"):
78 """ Add a route if it is not already defined """
79 found = False

Callers 9

mainFunction · 0.90
post_configureMethod · 0.90
post_config_changeMethod · 0.90
set_primaryMethod · 0.90
__updateMethod · 0.90
test_initMethod · 0.90
test_add_defaultrouteMethod · 0.90
test_get_tablenameMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_initMethod · 0.72
test_add_defaultrouteMethod · 0.72
test_get_tablenameMethod · 0.72