-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathCsStaticRoutes.py
More file actions
executable file
·77 lines (67 loc) · 3.34 KB
/
CsStaticRoutes.py
File metadata and controls
executable file
·77 lines (67 loc) · 3.34 KB
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
#!/usr/bin/python
# -- coding: utf-8 --
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from . import CsHelper
from .CsDatabag import CsDataBag
from .CsRoute import CsRoute
class CsStaticRoutes(CsDataBag):
def process(self):
logging.debug("Processing CsStaticRoutes file ==> %s" % self.dbag)
for item in self.dbag:
if item == "id":
continue
self.__update(self.dbag[item])
def __update(self, route):
network = route['network']
gateway = route['gateway']
if route['revoke']:
# Delete from main table
command = "ip route del %s via %s" % (network, gateway)
CsHelper.execute(command)
# Delete from PBR table if applicable
device = CsHelper.find_device_for_gateway(self.config, gateway)
if device:
cs_route = CsRoute()
table_name = cs_route.get_tablename(device)
command = "ip route del %s via %s table %s" % (network, gateway, table_name)
CsHelper.execute(command)
logging.info("Deleted static route %s via %s from PBR table %s" % (network, gateway, table_name))
else:
# Add to main table (existing logic)
command = "ip route show | grep %s | awk '{print $1, $3}'" % network
result = CsHelper.execute(command)
if not result:
route_command = "ip route add %s via %s" % (network, gateway)
CsHelper.execute(route_command)
logging.info("Added static route %s via %s to main table" % (network, gateway))
# Add to PBR table if applicable
device = CsHelper.find_device_for_gateway(self.config, gateway)
if device:
cs_route = CsRoute()
table_name = cs_route.get_tablename(device)
# Check if route already exists in the PBR table
check_command = "ip route show table %s | grep %s | awk '{print $1, $3}'" % (table_name, network)
result = CsHelper.execute(check_command)
if not result:
# Add route to the interface-specific table
route_command = "ip route add %s via %s dev %s table %s" % (network, gateway, device, table_name)
CsHelper.execute(route_command)
logging.info("Added static route %s via %s to PBR table %s" % (network, gateway, table_name))
else:
logging.info("Static route %s via %s added to main table only (no matching interface found for PBR table)" % (network, gateway))