Skip to content

Commit

Permalink
Merge pull request #30 from lwindg/develop
Browse files Browse the repository at this point in the history
Update dhclient to non-blocking mode
  • Loading branch information
imZack committed Feb 25, 2016
2 parents 66d215d + 891c0b0 commit 2bd43dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
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 (0.10.5-1) unstable; urgency=low

* Change dhcp client setting to non-blocking mode.

-- Aeluin Chen <aeluin.chen@moxa.com> Wed, 24 Feb 2016 16:36:44 +0800

sanji-bundle-route (0.10.4-1) unstable; urgency=low

* Update policy for checking link status.
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": "0.10.4",
"version": "0.10.5",
"author": "Aeluin Chen",
"email": "aeluin.chen@moxa.com",
"description": "Handle the routing table",
Expand Down
56 changes: 26 additions & 30 deletions ip/addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def interfaces():
ifaces = [x for x in ifaces if not
(x.startswith("lo") or x.startswith("mon."))]
return ifaces
except Exception, e:
except Exception as e:
_logger.info("Cannot get interfaces: %s" % e)
raise e

Expand Down Expand Up @@ -109,24 +109,36 @@ def ifupdown(iface, up):
ValueError
"""
if not up:
try:
output = sh.awk(
sh.grep(
sh.grep(sh.ps("ax"), iface, _timeout=5),
"dhclient", _timeout=5),
"{print $1}")
dhclients = output().split()
for dhclient in dhclients:
sh.kill(dhclient)
except:
pass
dhclient(iface, False)
try:
sh.ip("link", "set", iface, "up" if up else "down")
except:
raise ValueError("Cannot update the link status for \"%s\"."
% iface)


def dhclient(iface, enable, script=None):
# Disable the dhcp client and flush interface
try:
dhclients = sh.awk(
sh.grep(
sh.grep(sh.ps("ax"), iface, _timeout=5),
"dhclient", _timeout=5),
"{print $1}")
dhclients = dhclients.split()
for dhclient in dhclients:
sh.kill(dhclient)
except Exception as e:
_logger.info("Failed to stop dhclient: %s" % e)
pass

if enable:
if script:
sh.dhclient("-nw", "-sf", script, iface, _bg=True)
else:
sh.dhclient("-nw", iface, _bg=True)


def ifconfig(iface, dhcpc, ip="", netmask="24", gateway="", script=None):
"""Set the interface to static IP or dynamic IP (by dhcpclient).
Expand All @@ -150,31 +162,15 @@ def ifconfig(iface, dhcpc, ip="", netmask="24", gateway="", script=None):
raise ValueError("Unknown error for \"%s\"." % iface)

# Disable the dhcp client and flush interface
try:
dhclients = sh.awk(
sh.grep(
sh.grep(sh.ps("ax"), iface, _timeout=5),
"dhclient", _timeout=5),
"{print $1}")
dhclients = dhclients.split()
if 1 == len(dhclients):
sh.dhclient("-x", iface)
elif len(dhclients) > 1:
for dhclient in dhclients:
sh.kill(dhclient)
except:
pass
dhclient(iface, False)

try:
sh.ip("-4", "addr", "flush", "label", iface)
except:
raise ValueError("Unknown error for \"%s\"." % iface)

if dhcpc:
if script:
sh.dhclient("-sf", script, iface)
else:
sh.dhclient(iface)
dhclient(iface, True, script)
else:
if ip:
net = ipcalc.Network("%s/%s" % (ip, netmask))
Expand Down

0 comments on commit 2bd43dc

Please sign in to comment.