blob: e419ab604e0cfaf59507724771ed8e3f7560faa6 [file] [log] [blame]
Hariharasubramanian Rcd387972016-01-20 07:19:41 -06001#!/usr/bin/env python
2
Hariharasubramanian R308cffc2016-03-03 09:35:16 -06003from subprocess import call, Popen, PIPE
Edward A. James75757c02016-07-15 09:11:18 -05004from IPy import IP
Hariharasubramanian Rcd387972016-01-20 07:19:41 -06005import sys
6import subprocess
7import dbus
8import string
Hariharasubramanian R308cffc2016-03-03 09:35:16 -06009import socket
10import re
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060011import os
12import fcntl
13import glib
14import gobject
15import dbus.service
16import dbus.mainloop.glib
Vishwanatha Subbanna92a5d1e2016-08-30 21:23:32 +053017from ConfigParser import SafeConfigParser
18import glob
Dinesh Chinarie8d42112017-06-18 23:28:32 -050019import obmc.mapper
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060020
Ratan Gupta8613b872016-10-07 17:15:58 +053021#MAC address mask for locally administered.
22MAC_LOCAL_ADMIN_MASK = 0x20000000000
23BROADCAST_MAC = 0xFFFFFFFFFFFF
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060024DBUS_NAME = 'org.openbmc.NetworkManager'
25OBJ_NAME = '/org/openbmc/NetworkManager/Interface'
Dinesh Chinarie8d42112017-06-18 23:28:32 -050026INV_INTF_NAME = 'xyz.openbmc_project.Inventory.Item.NetworkInterface'
27INVENTORY_ROOT = '/xyz/openbmc_project/inventory'
28MAC_PROPERTY = 'MACAddress'
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060029
30network_providers = {
Dinesh Chinarie8d42112017-06-18 23:28:32 -050031 'networkd' : {
Adriana Kobylak88c733b2016-02-03 16:46:58 -060032 'bus_name' : 'org.freedesktop.network1',
33 'ip_object_name' : '/org/freedesktop/network1/network/default',
34 'hw_object_name' : '/org/freedesktop/network1/link/_31',
Hariharasubramanian R3a224e72016-02-10 12:32:08 -060035 'ip_if_name' : 'org.freedesktop.network1.Network',
36 'hw_if_name' : 'org.freedesktop.network1.Link',
Adriana Kobylak88c733b2016-02-03 16:46:58 -060037 'method' : 'org.freedesktop.network1.Network.SetAddr'
38 },
39 'NetworkManager' : {
40 'bus_name' : 'org.freedesktop.NetworkManager',
41 'ip_object_name' : '/org/freedesktop/NetworkManager',
42 'hw_object_name' : '/org/freedesktop/NetworkManager',
Hariharasubramanian R3a224e72016-02-10 12:32:08 -060043 'ip_if_name' : 'org.freedesktop.NetworkManager',
44 'hw_if_name' : 'org.freedesktop.NetworkManager',
Adriana Kobylak88c733b2016-02-03 16:46:58 -060045 'method' : 'org.freedesktop.NetworkManager' # FIXME:
46 },
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060047}
48
49def getPrefixLen(mask):
Adriana Kobylak88c733b2016-02-03 16:46:58 -060050 prefixLen = sum([bin(int(x)).count('1') for x in mask.split('.')])
51 return prefixLen
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060052
Vishwanatha Subbanna92a5d1e2016-08-30 21:23:32 +053053# Enable / Disable the UseDHCP setting in .network file
54def modifyNetConfig(confFile, usentp):
55 parser = SafeConfigParser()
56 parser.optionxform = str
57 parser.read(confFile)
58 sections = parser.sections()
59
60 if "Match" not in sections:
61 raise NameError, "[Match] section not found"
62
63 interface = parser.get('Match', 'Name')
64 if interface == '':
65 raise NameError, "Invalid interface"
66
67 if "DHCP" not in sections:
68 parser.add_section("DHCP")
69 if usentp.lower() == "yes":
70 parser.set('DHCP', 'UseNTP', "true")
71 elif usentp.lower() == "no":
72 parser.set('DHCP', 'UseNTP', "false")
73
74 print "Updating" + confFile + '\n'
75 with open(confFile, 'wb') as configfile:
76 parser.write(configfile)
77
78 rc = call(["ip", "addr", "flush", interface])
79 rc = call(["systemctl", "restart", "systemd-networkd.service"])
80 rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"])
81 return rc
82
Ratan Gupta8613b872016-10-07 17:15:58 +053083
Ratan Gupta8613b872016-10-07 17:15:58 +053084# Get Mac address from the eeprom
85def get_mac_from_eeprom():
86 bus = dbus.SystemBus()
Dinesh Chinarie8d42112017-06-18 23:28:32 -050087 mapper = obmc.mapper.Mapper(bus)
Ratan Gupta8613b872016-10-07 17:15:58 +053088
Dinesh Chinarie8d42112017-06-18 23:28:32 -050089 # Get the inventory subtree, limited
90 # to objects that implement NetworkInterface.
91 for path, info in \
92 mapper.get_subtree(
93 path=INVENTORY_ROOT,
94 interfaces=[INV_INTF_NAME]).iteritems():
95 # Find a NetworkInterface with 'bmc' in the path.
96 if 'bmc' not in path:
97 continue
98
99 # Only expecting a single service to implement
100 # NetworkInterface. Get the service connection
101 # from the mapper response
102 conn = info.keys()[0]
103
104 # Get the inventory object implementing NetworkInterface.
105 obj = bus.get_object(conn, path)
106
107 # Get the MAC address
108 mproxy = obj.get_dbus_method('Get', dbus.PROPERTIES_IFACE)
109 return mproxy(INV_INTF_NAME, MAC_PROPERTY)
Ratan Gupta8613b872016-10-07 17:15:58 +0530110
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600111class IfAddr ():
112 def __init__ (self, family, scope, flags, prefixlen, addr, gw):
113 self.family = family
114 self.scope = scope
115 self.flags = flags
116 self.prefixlen = prefixlen
117 self.addr = addr
118 self.gw = gw
119
120class NetMan (dbus.service.Object):
121 def __init__(self, bus, name):
122 self.bus = bus
123 self.name = name
124 dbus.service.Object.__init__(self,bus,name)
125
126 def setNetworkProvider(self, provider):
127 self.provider = provider
128
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600129 def _isvaliddev(self, device):
130 devices = os.listdir ("/sys/class/net")
131 if not device in devices : return False
132 else: return True
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600133
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600134 def _ishwdev (self, device):
135 f = open ("/sys/class/net/"+device+"/type")
136 type = f.read()
137 return False if (int(type) == 772) else True
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600138
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600139 def _isvalidmask (self, mask):
140 for x in mask.split('.'):
141 try:
142 y = int(x)
143 except:
144 return False
145 if y > 255: return False
146 return mask.count('.') == 3
147
Ratan Gupta8613b872016-10-07 17:15:58 +0530148 def validatemac(self, mac):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600149 macre = '([a-fA-F0-9]{2}[:|\-]?){6}'
Ratan Gupta8613b872016-10-07 17:15:58 +0530150 if re.compile(macre).search(mac) is None:
151 raise ValueError("Malformed MAC address")
152
153 # Don't allow Broadcast or global unique mac
154 int_mac = int(mac.replace(":", ""), 16)
155 if not (int_mac ^ BROADCAST_MAC):
156 raise ValueError("Given Mac is BroadCast Mac Address")
157
158 if not int_mac & MAC_LOCAL_ADMIN_MASK:
Dinesh Chinarie8d42112017-06-18 23:28:32 -0500159 eep_mac = get_mac_from_eeprom()
160 if eep_mac:
161 int_eep_mac = int(eep_mac, 16)
162 if int_eep_mac != int_mac:
163 raise ValueError("Given MAC address is neither a local Admin type \
Ratan Gupta8613b872016-10-07 17:15:58 +0530164 nor is same as in eeprom")
165
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600166
Edward A. James75757c02016-07-15 09:11:18 -0500167 def _isvalidipv4(self, ipstr, netmask):
168 ip_parts = ipstr.split(".")
169 if len(ip_parts) != 4:
170 return "Malformed"
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600171
Edward A. James75757c02016-07-15 09:11:18 -0500172 first, second, third, fourth = [int(part) for part in ip_parts]
173 if first == 0 and second == 0 and third == 0 and fourth == 0:
174 return "Invalid" # "this" network disallowed
175 if first == 169 and second == 254:
176 return "Link Local"
177 if first >= 224:
178 return "Invalid" # class D multicast and class E disallowed
179 if first == 192 and second == 88 and third == 99:
180 return "Invalid" # ipv6 relay
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600181
Edward A. James75757c02016-07-15 09:11:18 -0500182 # check validity against netmask
183 if netmask != '0':
184 ip_bin = (first << 24) + (second << 16) + (third << 8) + fourth
185 mask_parts = netmask.split(".")
186 if len(mask_parts) == 4: # long form netmask
187 mask_bin = (int(mask_parts[0]) << 24) + (int(mask_parts[1]) << 16) + (int(mask_parts[2]) << 8) + int(mask_parts[3])
188 elif netmask.count(".") == 0: # short form netmask
189 mask_bin = 0xffffffff ^ (1 << 32 - int(netmask)) - 1
190 else:
191 return "Malformed" # bad netmask
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600192
Edward A. James75757c02016-07-15 09:11:18 -0500193 if ip_bin & ~mask_bin == 0:
194 return "Invalid" # disallowed by this netmask
195 if ip_bin | mask_bin == 0xFFFFFFFF:
196 return "Invalid" # disallowed by this netmask
197
198 return "Valid"
199
200
201 def _isvalidip(self, ipaddr, netmask = '0'):
202 try:
203 ip = IP(ipaddr)
204 except ValueError:
205 return "Malformed"
206
207 ipstr = ip.strNormal(0)
208 ipstr_masked = ip.strNormal(2)
209 if ipstr_masked.count("/") != 0 and netmask == '0':
210 netmask = ipstr_masked.split("/")[1]
211
212 if ip.version() == 4: # additional checks for ipv4
213 return self._isvalidipv4(ipstr, netmask)
214 # TODO: check ipv6 openbmc/openbmc#496
215
216 return "Valid"
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600217
218 def _getAddr (self, target, device):
219 netprov = network_providers [self.provider]
220 bus_name = netprov ['bus_name']
221
222 if (target == "ip"):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600223 ipaddr = ""
224 defgw = ""
225 prefixlen = "0"
226
227 proc = subprocess.Popen(["ip", "addr", "show", "dev", device], stdout=PIPE)
228 procout = proc.communicate()
229 if procout:
230 ipout = procout[0].splitlines()[2].strip()
231 ipaddr,prefixlen = ipout.split ()[1].split("/")
232
233 proc = subprocess.Popen(["ip", "route", "show", "dev", device, "default", "0.0.0.0/0"], stdout=PIPE)
234 procout = proc.communicate()
235 if procout[0]:
236 ipout = procout[0].splitlines()[0].strip()
237 defgw = ipout.split ()[2]
238
239 return 2, int(prefixlen), ipaddr, defgw
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600240
241 if (target == "mac"):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600242 proc = subprocess.Popen(["ip", "link", "show", "dev", device], stdout=PIPE)
243 ipout = proc.communicate()[0].splitlines()[1].strip()
244 mac = ipout.split ()[1]
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600245 return mac
246
Dave Cobbleybac20c72017-11-06 10:35:25 -0800247 def _checkNetworkForRequiredFields(self, device):
248 parser = SafeConfigParser()
249 parser.optionxform = str
250 parser.read("/etc/systemd/network/00-bmc-" + device + ".network")
251 sections = parser.sections()
252 if "Match" not in sections:
253 parser.add_section("Match")
254 parser.set("Match", "Name", device)
255
256 if "Network" not in sections:
257 parser.add_section("Network")
258
259 if "Link" not in sections:
260 parser.add_section("Link")
261 mac = self._getAddr("mac", device)
262 try:
263 self.validatemac(mac)
264 parser.set("Link", MAC_PROPERTY, mac)
265 except ValueError as e:
266 print("System MAC Address invalid:" + e)
267
268 return parser
269
270 def _upDownNetworkService(self, device, mac=None):
271 print "Restarting networkd service..."
272 subprocess.check_call(["ip", "link", "set", "dev", device, "down"])
273 if mac is not None:
274 subprocess.check_call(["fw_setenv", "ethaddr", mac])
275 subprocess.check_call(
276 ["ip", "link", "set", "dev", device, "address", mac])
277
278 subprocess.check_call(
279 ["systemctl", "restart", "systemd-networkd.service"])
280
281 return 0
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600282
Vishwanatha Subbanna92a5d1e2016-08-30 21:23:32 +0530283 @dbus.service.method(DBUS_NAME, "sas", "x")
284 def SetNtpServer (self, device, ntpservers):
285 if not self._isvaliddev (device) : raise ValueError, "Invalid Device"
286
287 # Convert the array into space separated value string
288 ntp_ip = " ".join(ntpservers)
289 if not ntp_ip : raise ValueError, "Invalid Data"
290
291 confFile = "/etc/systemd/network/00-bmc-" + device + ".network"
292
293 parser = SafeConfigParser()
294 parser.optionxform = str
295 parser.read(confFile)
296 sections = parser.sections()
297 if "Match" not in sections:
298 raise NameError, "[Match] section not found"
299
300 interface = parser.get('Match', 'Name')
301 if interface != device:
302 raise ValueError, "Device [" + device + "] Not Configured"
303
304 if "Network" not in sections:
305 raise NameError, "[Network] section not found"
306
307 parser.set('Network', 'NTP', ntp_ip)
308 print "Updating " + confFile + '\n'
309 with open(confFile, 'wb') as configfile:
310 parser.write(configfile)
311 rc = call(["ip", "addr", "flush", device])
312 rc = call(["systemctl", "restart", "systemd-networkd.service"])
313 rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"])
314 return rc
315
316 @dbus.service.method(DBUS_NAME, "s", "x")
317 def UpdateUseNtpField (self, usentp):
318 filelist = glob.glob("/etc/systemd/network/*.network")
319 for configfile in filelist:
320 modifyNetConfig(configfile,usentp)
321 return 0
322
Hariharasubramanian R3a224e72016-02-10 12:32:08 -0600323 @dbus.service.method(DBUS_NAME, "s", "x")
Dave Cobbleybac20c72017-11-06 10:35:25 -0800324 def EnableDHCP(self, device):
325 if not self._isvaliddev(device):
326 raise ValueError("Invalid Device")
327 parser = self._checkNetworkForRequiredFields(device)
328 parser.set("Network", "DHCP", "yes")
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600329
Dave Cobbleybac20c72017-11-06 10:35:25 -0800330 if "DHCP" not in parser.sections():
331 parser.add_section("DHCP")
332 parser.set("DHCP", "ClientIdentifier", "mac")
Hariharasubramanian R3a224e72016-02-10 12:32:08 -0600333
Dave Cobbleybac20c72017-11-06 10:35:25 -0800334 # Write to config file
335 with open("/etc/systemd/network/00-bmc-" + device + ".network", 'w') \
336 as configfile:
337 parser.write(configfile)
Hariharasubramanian R3a224e72016-02-10 12:32:08 -0600338
Dave Cobbleybac20c72017-11-06 10:35:25 -0800339 return (subprocess.check_call(
340 ["systemctl", "restart", "systemd-networkd.service"]))
Hariharasubramanian R3a224e72016-02-10 12:32:08 -0600341
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600342 @dbus.service.method(DBUS_NAME, "ssss", "x")
Dave Cobbleybac20c72017-11-06 10:35:25 -0800343 def SetAddress4(self, device, ipaddr, netmask, gateway):
344 if not self._isvaliddev(device): raise ValueError, "Invalid Device"
345 if not self._isvalidmask(netmask): raise ValueError, "Invalid Mask"
346 prefixLen = getPrefixLen(netmask)
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600347 if prefixLen == 0: raise ValueError, "Invalid Mask"
Dave Cobbleybac20c72017-11-06 10:35:25 -0800348 valid = self._isvalidip(ipaddr, netmask)
Edward A. James75757c02016-07-15 09:11:18 -0500349 if valid != "Valid": raise ValueError, valid + " IP Address"
Dave Cobbleybac20c72017-11-06 10:35:25 -0800350 valid = self._isvalidip(gateway)
Edward A. James75757c02016-07-15 09:11:18 -0500351 if valid != "Valid": raise ValueError, valid + " IP Address"
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600352
Dave Cobbleybac20c72017-11-06 10:35:25 -0800353 parser = self._checkNetworkForRequiredFields(device)
354 parser.set("Network", "DHCP", "no")
355 parser.set("Network", "Address", '{}/{}'.format(ipaddr, prefixLen))
356 parser.set("Network", "Gateway", gateway)
357 with open("/etc/systemd/network/00-bmc-" + device + ".network", 'w') \
358 as configfile:
359 parser.write(configfile)
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600360
Dave Cobbleybac20c72017-11-06 10:35:25 -0800361 return (subprocess.check_call(
362 ["systemctl", "restart", "systemd-networkd.service"]))
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600363
Hariharasubramanian Rcc28a6f2016-04-29 12:25:12 -0500364 @dbus.service.method(DBUS_NAME, "s", "s")
365 def GetAddressType (self, device):
366 if not self._isvaliddev (device) : raise ValueError, "Invalid Device"
367
368 confFile = "/etc/systemd/network/00-bmc-" + device + ".network"
369 if not os.path.exists(confFile):
370 print "Config file (%s) not found !" % confFile
371 netprov = network_providers [self.provider]
372 bus_name = netprov ['bus_name']
373 obj_name = netprov ['ip_object_name']
374 o = self.bus.get_object(bus_name, obj_name, introspect=False)
375 i = dbus.Interface(o, 'org.freedesktop.DBus.Properties')
376 f = i.Get (netprov ['ip_if_name'], "SourcePath")
377 print "Using default networkd config file (%s)" % f
378 confFile = f
379
Dave Cobbleybac20c72017-11-06 10:35:25 -0800380 parser = self._checkNetworkForRequiredFields(device)
381
382 if parser.has_option("Network", "DHCP") is True:
383 mode = parser.get("Network", "DHCP")
384 else:
385 return "Unknown"
386
387 setting = {
388 'yes': 'DHCP',
389 'true': 'DHCP',
390 'no': 'STATIC'
391 }
392 setting.get(mode.lower(), "Unknown")
393
394 return setting
Hariharasubramanian Rcc28a6f2016-04-29 12:25:12 -0500395
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600396 #family, prefixlen, ip, defgw
397 @dbus.service.method(DBUS_NAME, "s", "iyss")
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600398 def GetAddress4 (self, device):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600399 if not self._isvaliddev (device) : raise ValueError, "Invalid Device"
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600400 return self._getAddr ("ip", device)
401
402 @dbus.service.method(DBUS_NAME, "s", "s")
403 def GetHwAddress (self, device):
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600404 if not self._isvaliddev (device) : raise ValueError, "Invalid Device"
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600405 return self._getAddr ("mac", device)
406
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600407 @dbus.service.method(DBUS_NAME, "ss", "i")
408 def SetHwAddress (self, device, mac):
409 if not self._isvaliddev (device) : raise ValueError, "Invalid Device"
410 if not self._ishwdev (device) : raise ValueError, "Not a Hardware Device"
Ratan Gupta8613b872016-10-07 17:15:58 +0530411
412 self.validatemac(mac)
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600413
Dave Cobbleybac20c72017-11-06 10:35:25 -0800414 parser = self._checkNetworkForRequiredFields(device)
415 parser.set("Link", MAC_PROPERTY, mac)
416 with open("/etc/systemd/network/00-bmc-" + device + ".network", 'w') as configfile:
417 parser.write(configfile)
418
Adriana Kobylak88c733b2016-02-03 16:46:58 -0600419 rc = subprocess.call(["fw_setenv", "ethaddr", mac])
Hariharasubramanian R308cffc2016-03-03 09:35:16 -0600420
421 print("Restarting networkd service...")
422 rc = call(["ip", "link", "set", "dev", device, "down"])
423 rc = call(["ip", "link", "set", "dev", device, "address", mac])
424 rc = call(["ip", "link", "set", "dev", device, "up"])
425
426 rc = call(["systemctl", "restart", "systemd-networkd.service"])
Adriana Kobylak88c733b2016-02-03 16:46:58 -0600427 return rc
428
vishwa5c912c82016-03-24 07:59:28 -0500429 #string of nameservers
430 @dbus.service.method(DBUS_NAME,"s", "s")
431 def SetNameServers (self, nameservers):
432 dns_entry = nameservers.split()
433 fail_msg = ''
434 dhcp_auto = False
435 file_opened = False
436 if len(dns_entry) > 0:
437 for dns in dns_entry:
Edward A. James75757c02016-07-15 09:11:18 -0500438 valid = self._isvalidip (dns)
439 if valid != "Valid":
vishwa5c912c82016-03-24 07:59:28 -0500440 if dns == "DHCP_AUTO=":
441 #This DNS is supplied by DHCP.
442 dhcp_auto = True
443 else:
Edward A. James75757c02016-07-15 09:11:18 -0500444 print valid + " DNS Address [" + dns + "]"
vishwa5c912c82016-03-24 07:59:28 -0500445 fail_msg = fail_msg + '[' + dns + ']'
446 else:
447 #Only over write on a first valid input
448 if file_opened == False:
449 resolv_conf = open("/etc/resolv.conf",'w')
450 file_opened = True
451 if dhcp_auto == True:
452 resolv_conf.write("### Generated automatically via DHCP ###\n")
453 else:
454 resolv_conf.write("### Generated manually via dbus settings ###\n")
455 dns_ip = 'nameserver ' + dns + '\n'
456 resolv_conf.write(dns_ip)
457 if file_opened == True:
458 resolv_conf.close()
459 else:
460 raise ValueError, "Invalid DNS entry"
461 if len(fail_msg) > 0:
462 return 'Failures encountered processing' + fail_msg
463 else:
464 return "DNS entries updated Successfully"
465
Dave Cobbleybac20c72017-11-06 10:35:25 -0800466 @dbus.service.method(DBUS_NAME, "s", "x")
467 def SetHostname(self, hostname):
468 subprocess.check_call(["hostnamectl", "set-hostname", hostname])
469 subprocess.check_call(
470 ["systemctl", "restart", "systemd-networkd.service"])
471 return 0
472
473 @dbus.service.method(DBUS_NAME, "", "s")
474 def GetHostname(self):
475 value = subprocess.check_output("hostname")
476 return value.rstrip()
477
478 @dbus.service.method(DBUS_NAME, "ss", "x")
479 def SetGateway(self, device, gateway):
480 if not self._isvaliddev(device):
481 raise ValueError("Invalid Device")
482 valid = self._isvalidip(gateway)
483 if valid != "Valid":
484 raise ValueError(valid + " IP Address")
485 parser = self._checkNetworkForRequiredFields(device)
486 if "no" not in parser.get("Network", "DHCP"):
487 raise EnvironmentError("DHCP is on")
488 parser.set("Network", "Gateway", gateway)
489 with open("/etc/systemd/network/00-bmc-" + device + ".network", 'w') \
490 as configfile:
491 parser.write(configfile)
492 return (subprocess.check_call(
493 ["systemctl", "restart", "systemd-networkd.service"]))
494
495 @dbus.service.method(DBUS_NAME, "s", "s")
496 def GetGateway(self, device):
497 if not self._isvaliddev(device):
498 raise ValueError("Invalid Device")
499 return self._getAddr("ip", device)[3]
500
501
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600502def main():
503 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
504 bus = dbus.SystemBus()
505 name = dbus.service.BusName(DBUS_NAME, bus)
506 obj = NetMan (bus, OBJ_NAME)
507 obj.setNetworkProvider ("networkd")
508 mainloop = gobject.MainLoop()
509 print("Started")
510 mainloop.run()
511
512if __name__ == '__main__':
Adriana Kobylake150bfd2016-02-01 21:47:34 -0600513 sys.exit(main())