diff options
author | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2016-08-30 21:23:32 +0530 |
---|---|---|
committer | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2016-09-15 22:36:07 +0530 |
commit | 92a5d1ec6ac1fe77e515e62c64f3ea01497fb63a (patch) | |
tree | 64cd625bd3c2688d4bb5db8bf37bcbfe8dd3715a /netman.py | |
parent | 15d498e2568b6e104de75e7423caab0c9a487485 (diff) | |
download | phosphor-networkd-92a5d1ec6ac1fe77e515e62c64f3ea01497fb63a.tar.gz phosphor-networkd-92a5d1ec6ac1fe77e515e62c64f3ea01497fb63a.zip |
Add support to update NTP server list in network files
This provides following APIs:
SetNtpServer takes network device name and list of NTP server IP's or
hostnames and updates the corresponding .network files with NTP= under
[Network] section.
Example:
[Network]
NTP = 1.2.3.4 time.org
UpdateUseNtpField takes a string and updates all the available .network files
with either UseNtp=true or UseNtp=false depending on user input.
Example:
[DHCP]
UseNtp = false
Change-Id: Iead3e6e4cdaf7e12c855c8a7d8e7d57d7037eda2
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
Diffstat (limited to 'netman.py')
-rwxr-xr-x | netman.py | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -14,6 +14,8 @@ import glib import gobject import dbus.service import dbus.mainloop.glib +from ConfigParser import SafeConfigParser +import glob DBUS_NAME = 'org.openbmc.NetworkManager' OBJ_NAME = '/org/openbmc/NetworkManager/Interface' @@ -41,6 +43,36 @@ def getPrefixLen(mask): prefixLen = sum([bin(int(x)).count('1') for x in mask.split('.')]) return prefixLen +# Enable / Disable the UseDHCP setting in .network file +def modifyNetConfig(confFile, usentp): + parser = SafeConfigParser() + parser.optionxform = str + parser.read(confFile) + sections = parser.sections() + + if "Match" not in sections: + raise NameError, "[Match] section not found" + + interface = parser.get('Match', 'Name') + if interface == '': + raise NameError, "Invalid interface" + + if "DHCP" not in sections: + parser.add_section("DHCP") + if usentp.lower() == "yes": + parser.set('DHCP', 'UseNTP', "true") + elif usentp.lower() == "no": + parser.set('DHCP', 'UseNTP', "false") + + print "Updating" + confFile + '\n' + with open(confFile, 'wb') as configfile: + parser.write(configfile) + + rc = call(["ip", "addr", "flush", interface]) + rc = call(["systemctl", "restart", "systemd-networkd.service"]) + rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"]) + return rc + class IfAddr (): def __init__ (self, family, scope, flags, prefixlen, addr, gw): self.family = family @@ -167,6 +199,46 @@ class NetMan (dbus.service.Object): def test(self): print("TEST") + @dbus.service.method(DBUS_NAME, "sas", "x") + def SetNtpServer (self, device, ntpservers): + if not self._isvaliddev (device) : raise ValueError, "Invalid Device" + + # Convert the array into space separated value string + ntp_ip = " ".join(ntpservers) + if not ntp_ip : raise ValueError, "Invalid Data" + + confFile = "/etc/systemd/network/00-bmc-" + device + ".network" + + parser = SafeConfigParser() + parser.optionxform = str + parser.read(confFile) + sections = parser.sections() + if "Match" not in sections: + raise NameError, "[Match] section not found" + + interface = parser.get('Match', 'Name') + if interface != device: + raise ValueError, "Device [" + device + "] Not Configured" + + if "Network" not in sections: + raise NameError, "[Network] section not found" + + parser.set('Network', 'NTP', ntp_ip) + print "Updating " + confFile + '\n' + with open(confFile, 'wb') as configfile: + parser.write(configfile) + rc = call(["ip", "addr", "flush", device]) + rc = call(["systemctl", "restart", "systemd-networkd.service"]) + rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"]) + return rc + + @dbus.service.method(DBUS_NAME, "s", "x") + def UpdateUseNtpField (self, usentp): + filelist = glob.glob("/etc/systemd/network/*.network") + for configfile in filelist: + modifyNetConfig(configfile,usentp) + return 0 + @dbus.service.method(DBUS_NAME, "s", "x") def EnableDHCP (self, device): if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |