1 #!/usr/bin/python /usr/bin/plcsh
16 def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeManager"):
19 sysconfig = "%s/etc/sysconfig/network-scripts" % root
21 os.makedirs(sysconfig)
23 if e.errno != errno.EEXIST:
26 # query running network interfaces
28 ips = dict(zip(devs.values(), devs.keys()))
31 macs[sioc.gifhwaddr(dev).lower()] = dev
35 hostname = data.get('hostname',socket.gethostname())
37 # assume data['interfaces'] contains this node's Interfaces
38 # can cope with 4.3 ('networks') or 5.0 ('interfaces')
40 interfaces = data['interfaces']
42 interfaces = data['networks']
43 failedToGetSettings = False
45 # NOTE: GetInterfaces/NodeNetworks does not necessarily order the interfaces
46 # returned. Because 'interface'is decremented as each interface is processed,
47 # by the time is_primary=True (primary) interface is reached, the device
48 # "eth%s" % interface, is not eth0. But, something like eth-4, or eth-12.
49 # This code sorts the interfaces, placing is_primary=True interfaces first.
50 # There is a lot of room for improvement to how this
51 # script handles interfaces and how it chooses the primary interface.
52 def compare_by (fieldname):
53 def compare_two_dicts (a, b):
54 return cmp(a[fieldname], b[fieldname])
55 return compare_two_dicts
57 # NOTE: by sorting on 'is_primary' and then reversing (since False is sorted
58 # before True) all 'is_primary' interfaces are at the beginning of the list.
59 interfaces.sort( compare_by('is_primary') )
62 for interface in interfaces:
63 logger.verbose('net:InitInterfaces interface %d: %r'%(device_id,interface))
64 logger.verbose('net:InitInterfaces macs = %r' % macs)
65 logger.verbose('net:InitInterfaces ips = %r' % ips)
66 # Get interface name preferably from MAC address, falling back
68 hwaddr=interface['mac']
69 if hwaddr <> None: hwaddr=hwaddr.lower()
71 orig_ifname = macs[hwaddr]
72 elif interface['ip'] in ips:
73 orig_ifname = ips[interface['ip']]
78 logger.verbose('net:InitInterfaces orig_ifname = %s' % orig_ifname)
81 details['ONBOOT']='yes'
82 details['USERCTL']='no'
84 details['HWADDR'] = interface['mac']
85 if interface['is_primary']:
86 details['PRIMARY']='yes'
88 if interface['method'] == "static":
89 details['BOOTPROTO'] = "static"
90 details['IPADDR'] = interface['ip']
91 details['NETMASK'] = interface['netmask']
92 if interface['is_primary']:
93 gateway = interface['gateway']
95 details['DNS1'] = interface['dns1']
97 details['DNS2'] = interface['dns2']
99 elif interface['method'] == "dhcp":
100 details['BOOTPROTO'] = "dhcp"
101 details['PERSISTENT_DHCLIENT'] = "yes"
102 if interface['hostname']:
103 details['DHCP_HOSTNAME'] = interface['hostname']
105 details['DHCP_HOSTNAME'] = hostname
106 if not interface['is_primary']:
107 details['DHCLIENTARGS'] = "-R subnet-mask"
109 if 'interface_tag_ids' in interface:
111 interface_tag_ids = "interface_tag_ids"
112 interface_tag_id = "interface_tag_id"
116 interface_tag_ids = "nodenetwork_setting_ids"
117 interface_tag_id = "nodenetwork_setting_id"
120 if len(interface[interface_tag_ids]) > 0:
123 settings = plc.GetInterfaceTags({interface_tag_id:interface[interface_tag_ids]})
125 settings = plc.GetNodeNetworkSettings({interface_tag_id:interface[interface_tag_ids]})
127 logger.log("net:InitInterfaces FATAL: failed call GetInterfaceTags({'interface_tag_id':{%s})"% \
128 interface[interface_tag_ids])
129 failedToGetSettings = True
130 continue # on to the next interface
132 for setting in settings:
133 settingname = setting[name_key].upper()
134 if settingname in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
135 details[settingname]=setting['value']
137 elif settingname in \
138 [ "MODE", "ESSID", "NW", "FREQ", "CHANNEL", "SENS", "RATE",
139 "KEY", "KEY1", "KEY2", "KEY3", "KEY4", "SECURITYMODE",
140 "IWCONFIG", "IWPRIV" ] :
141 details [settingname] = setting['value']
142 details ['TYPE']='Wireless'
144 logger.log("net:InitInterfaces WARNING: ignored setting named %s"%setting[name_key])
146 # support aliases to interfaces either by name or HWADDR
147 if 'ALIAS' in details:
148 if 'HWADDR' in details:
149 hwaddr = details['HWADDR'].lower()
150 del details['HWADDR']
152 hwifname = macs[hwaddr]
153 if ('IFNAME' in details) and details['IFNAME'] <> hwifname:
154 logger.log("net:InitInterfaces WARNING: alias ifname (%s) and hwaddr ifname (%s) do not match"%\
155 (details['IFNAME'],hwifname))
156 details['IFNAME'] = hwifname
158 logger.log('net:InitInterfaces WARNING: mac addr %s for alias not found' %(hwaddr))
160 if 'IFNAME' in details:
161 # stupid RH /etc/sysconfig/network-scripts/ifup-aliases:new_interface()
162 # checks if the "$DEVNUM" only consists of '^[0-9A-Za-z_]*$'. Need to make
163 # our aliases compliant.
164 parts = details['ALIAS'].split('_')
167 isValid=isValid and part.isalnum()
170 devices_map["%s:%s" % (details['IFNAME'],details['ALIAS'])] = details
172 logger.log("net:InitInterfaces WARNING: interface alias (%s) not a valid string for RH ifup-aliases"% details['ALIAS'])
174 logger.log("net:InitInterfaces WARNING: interface alias (%s) not matched to an interface"% details['ALIAS'])
177 if 'IFNAME' in details:
178 ifname = details['IFNAME']
185 ifname="eth%d" % (device_id-1)
186 if ifname not in devices_map:
189 if os.path.exists("%s/ifcfg-%s"%(sysconfig,ifname)):
190 logger.log("net:InitInterfaces WARNING: possibly blowing away %s configuration"%ifname)
191 devices_map[ifname] = details
193 m = modprobe.Modprobe()
195 m.input("%s/etc/modprobe.conf" % root)
198 for (dev, details) in devices_map.iteritems():
199 # get the driver string "moduleName option1=a option2=b"
200 driver=details.get('DRIVER','')
202 driver=driver.split()
203 kernelmodule=driver[0]
204 m.aliasset(dev,kernelmodule)
205 options=" ".join(driver[1:])
207 m.optionsset(dev,options)
208 m.output("%s/etc/modprobe.conf" % root, program)
210 # clean up after any ifcfg-$dev script that's no longer listed as
211 # part of the Interfaces associated with this node
213 # list all network-scripts
214 files = os.listdir(sysconfig)
216 # filter out the ifcfg-* files
219 if f.find("ifcfg-") == 0:
222 # remove loopback (lo) from ifcfgs list
224 if lo in ifcfgs: ifcfgs.remove(lo)
226 # remove known devices from icfgs list
227 for (dev, details) in devices_map.iteritems():
229 if ifcfg in ifcfgs: ifcfgs.remove(ifcfg)
231 # delete the remaining ifcfgs from
232 deletedSomething = False
234 if not failedToGetSettings:
236 dev = ifcfg[len('ifcfg-'):]
237 path = "%s/ifcfg-%s" % (sysconfig,dev)
239 logger.verbose("net:InitInterfaces removing %s %s"%(dev,path))
240 os.system("/sbin/ifdown %s" % dev)
241 deletedSomething=True
244 # wait a bit for the one or more ifdowns to have taken effect
248 # Write network configuration file
249 networkconf = file("%s/etc/sysconfig/network" % root, "w")
250 networkconf.write("NETWORKING=yes\nHOSTNAME=%s\n" % hostname)
251 if gateway is not None:
252 networkconf.write("GATEWAY=%s\n" % gateway)
255 # Process ifcfg-$dev changes / additions
257 for (dev, details) in devices_map.iteritems():
258 (fd, tmpnam) = tempfile.mkstemp(dir=sysconfig)
259 f = os.fdopen(fd, "w")
260 f.write("# Autogenerated by pyplnet... do not edit!\n")
261 if 'DRIVER' in details:
262 f.write("# using %s driver for device %s\n" % (details['DRIVER'],dev))
263 f.write('DEVICE=%s\n' % dev)
265 # print the configuration values
266 for (key, val) in details.iteritems():
267 if key not in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
268 f.write('%s=%s\n' % (key, val))
270 # print the configuration specific option values (if any)
271 if 'CFGOPTIONS' in details:
272 cfgoptions = details['CFGOPTIONS']
273 f.write('#CFGOPTIONS are %s\n' % cfgoptions)
274 for cfgoption in cfgoptions.split():
275 key,val = cfgoption.split('=')
279 f.write('%s="%s"\n' % (key,val))
282 # compare whether two files are the same
283 def comparefiles(a,b):
285 logger.verbose("net:InitInterfaces comparing %s with %s" % (a,b))
286 if not os.path.exists(a): return False
291 if not os.path.exists(b): return False
296 return buf_a == buf_b
300 path = "%s/ifcfg-%s" % (sysconfig,dev)
301 if not os.path.exists(path):
302 logger.verbose('net:InitInterfaces adding configuration for %s' % dev)
303 # add ifcfg-$dev configuration file
304 os.rename(tmpnam,path)
308 elif not comparefiles(tmpnam,path):
309 logger.verbose('net:InitInterfaces Configuration change for %s' % dev)
311 logger.verbose('net:InitInterfaces ifdown %s' % dev)
312 # invoke ifdown for the old configuration
313 os.system("/sbin/ifdown %s" % dev)
314 # wait a few secs for ifdown to complete
317 logger.log('replacing configuration for %s' % dev)
318 # replace ifcfg-$dev configuration file
319 os.rename(tmpnam,path)
323 # tmpnam & path are identical
328 fb = file("%s/ifcfg-%s"%(sysconfig,dev),"r")
329 for line in fb.readlines():
331 if parts[0][0]=="#":continue
332 if parts[0].find('='):
333 name,value = parts[0].split('=')
334 # clean up name & value
336 value = value.strip()
337 value = value.strip("'")
338 value = value.strip('"')
339 cfgvariables[name]=value
343 if name in cfgvariables:
344 value=cfgvariables[name]
345 value = value.lower()
349 # skip over device configs with ONBOOT=no
350 if getvar("ONBOOT") == 'no': continue
352 # don't bring up slave devices, the network scripts will
353 # handle those correctly
354 if getvar("SLAVE") == 'yes': continue
357 logger.verbose('net:InitInterfaces bringing up %s' % dev)
358 os.system("/sbin/ifup %s" % dev)
360 if __name__ == "__main__":
364 parser = optparse.OptionParser(usage="plnet [-v] [-f] [-p <program>] -r root node_id")
365 parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
366 parser.add_option("-r", "--root", action="store", type="string",
367 dest="root", default=None)
368 parser.add_option("-f", "--files-only", action="store_true",
370 parser.add_option("-p", "--program", action="store", type="string",
371 dest="program", default="plnet")
372 (options, args) = parser.parse_args()
373 if len(args) != 1 or options.root is None:
374 print >>sys.stderr, "Missing root or node_id"
378 node = shell.GetNodes({'node_id': [int(args[0])]})
380 interfaces = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
381 except AttributeError:
382 interfaces = shell.GetNodeNetworks({'nodenetwork_id':node[0]['nodenetwork_ids']})
386 data = {'hostname': node[0]['hostname'], 'interfaces': interfaces}
388 def __init__(self, verbose):
389 self.verbosity = verbose
390 def log(self, msg, loglevel=2):
393 def verbose(self, msg):
395 l = logger(options.verbose)
396 InitInterfaces(l, shell, data, options.root, options.files_only)