Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2.0 #45

Merged
merged 3 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build-deb/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
sanji-bundle-route (1.2.0) unstable; urgency=low

* feat: Support `status` and `wan`.

-- Aeluin Chen <aeluin.chen@moxa.com> Wed, 05 Apr 2017 16:00:35 +0800

sanji-bundle-route (1.1.0) unstable; urgency=low

* feat: Get interface via shell script command.
Expand Down
2 changes: 1 addition & 1 deletion bundle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "route",
"version": "1.1.0",
"version": "1.2.0",
"author": "Aeluin Chen",
"email": "aeluin.chen@moxa.com",
"description": "Handles the routing table",
Expand Down
43 changes: 24 additions & 19 deletions route.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def init(self, *args, **kwargs):
if self.bundle_env == "debug": # pragma: no cover
self._path_root = "%s/tests" % self._path_root

self.interfaces = []
self.interfaces = {}
try:
self.load(self._path_root)
except:
Expand Down Expand Up @@ -123,7 +123,10 @@ def list_interfaces(self):
inet_ip = [inet["ip"]
for inet in iface_info["inet"]
if "" != inet["ip"]]
if len(inet_ip):
if len(inet_ip) and \
(iface in self.interfaces and
self.interfaces[iface]["status"] is True and
self.interfaces[iface]["wan"] is True):
data.append(iface)
return data

Expand All @@ -141,6 +144,8 @@ def get_default(self):
else:
return default

default["wan"] = True
default["status"] = True
default["gateway"] = gw[0]
default["interface"] = gw[1]
return default
Expand Down Expand Up @@ -198,6 +203,8 @@ def _try_update_default(self, routes):
"""
ifaces = self.list_interfaces()
if not ifaces:
# FIXME: keep or clean?
# self.update_default({})
raise IPRouteError("Interfaces should be UP.")

default = {}
Expand All @@ -210,13 +217,11 @@ def _try_update_default(self, routes):
return

# find gateway by interface
for iface in self.interfaces:
if iface["interface"] == default["interface"]:
default = iface
break
default.update(self.interfaces[default["interface"]])

current = self.get_default()
if current != default:
if current.get("interface", "") != default.get("interface", "") or \
current.get("gateway", "") != default.get("gateway", ""):
self.update_default(default)

def try_update_default(self, routes):
Expand All @@ -226,7 +231,7 @@ def try_update_default(self, routes):
except IPRouteError as e:
_logger.debug(e)

def update_router(self, interface):
def update_router(self, iface):
"""
Save the interface name with its gateway and update the default
gateway if needed.
Expand All @@ -237,18 +242,18 @@ def update_router(self, interface):
Args:
interface: dict format with interface "name" and/or "gateway".
"""
if "status" not in iface:
iface["status"] = True
if "wan" not in iface:
iface["wan"] = True

# update the router information
for iface in self.interfaces:
if iface["interface"] == interface["name"]:
if "gateway" in interface:
iface["gateway"] = interface["gateway"]
break
else:
iface = {}
iface["interface"] = interface["name"]
if "gateway" in interface:
iface["gateway"] = interface["gateway"]
self.interfaces.append(iface)
if iface["name"] not in self.interfaces:
self.interfaces[iface["name"]] = {}
self.interfaces[iface["name"]]["status"] = iface["status"]
self.interfaces[iface["name"]]["wan"] = iface["wan"]
if "gateway" in iface:
self.interfaces[iface["name"]]["gateway"] = iface["gateway"]

# check if the default gateway need to be modified
self.try_update_default(self._routes)
Expand Down
Loading