001bbee4f23cac58d07d416805ddd3c7eda62ced
[pyplnet.git] / plnet.py
1 #!/usr/bin/python /usr/bin/plcsh
2
3 import os
4 import socket
5 import time
6 import tempfile
7 import errno
8 import struct
9
10 import sioc
11 import modprobe
12
13 global version
14 version = 4.3
15
16 def InitInterfaces(logger, plc, data, root="", files_only=False, program="NodeManager"):
17     global version
18
19     sysconfig = "%s/etc/sysconfig/network-scripts" % root
20     try:
21         os.makedirs(sysconfig)
22     except OSError, e:
23         if e.errno != errno.EEXIST:
24             raise e
25
26     # query running network interfaces
27     devs = sioc.gifconf()
28     ips = dict(zip(devs.values(), devs.keys()))
29     macs = {}
30     for dev in devs:
31         macs[sioc.gifhwaddr(dev).lower()] = dev
32
33     devices_map = {}
34     device_id = 1
35     hostname = data.get('hostname',socket.gethostname())
36     gateway = None
37     # assume data['interfaces'] contains this node's Interfaces
38     # can cope with 4.3 ('networks') or 5.0 ('interfaces')
39     try:
40         interfaces = data['interfaces']
41     except:
42         interfaces = data['networks']
43     failedToGetSettings = False
44
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
56
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') )
60     interfaces.reverse()
61
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
67         # on IP address.
68         hwaddr=interface['mac']
69         if hwaddr <> None: hwaddr=hwaddr.lower()
70         if hwaddr in macs:
71             orig_ifname = macs[hwaddr]
72         elif interface['ip'] in ips:
73             orig_ifname = ips[interface['ip']]
74         else:
75             orig_ifname = None
76
77         if orig_ifname:
78             logger.verbose('net:InitInterfaces orig_ifname = %s' % orig_ifname)
79
80         details = prepDetails(interface, hostname)
81
82         if 'interface_tag_ids' in interface:
83             version = 4.3
84             interface_tag_ids = "interface_tag_ids"
85             interface_tag_id = "interface_tag_id"
86             name_key = "tagname"
87         else:
88             version = 4.2
89             interface_tag_ids = "nodenetwork_setting_ids"
90             interface_tag_id = "nodenetwork_setting_id"
91             name_key = "name"
92
93         if len(interface[interface_tag_ids]) > 0:
94             try:
95                 if version == 4.3:
96                     settings = plc.GetInterfaceTags({interface_tag_id:interface[interface_tag_ids]})
97                 else:
98                     settings = plc.GetNodeNetworkSettings({interface_tag_id:interface[interface_tag_ids]})
99             except:
100                 logger.log("net:InitInterfaces FATAL: failed call GetInterfaceTags({'interface_tag_id':{%s})"% \
101                            interface[interface_tag_ids])
102                 failedToGetSettings = True
103                 continue # on to the next interface
104
105             for setting in settings:
106                 settingname = setting[name_key].upper()
107                 if settingname in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
108                     details[settingname]=setting['value']
109                 # wireless settings
110                 elif settingname in \
111                         [  "MODE", "ESSID", "NW", "FREQ", "CHANNEL", "SENS", "RATE",
112                            "KEY", "KEY1", "KEY2", "KEY3", "KEY4", "SECURITYMODE", 
113                            "IWCONFIG", "IWPRIV" ] :
114                     details [settingname] = setting['value']
115                     details ['TYPE']='Wireless'
116                 # Bridge setting
117                 elif settingname in [ 'BRIDGE' ]:
118                     details['BRIDGE'] = setting['value']
119                 else:
120                     logger.log("net:InitInterfaces WARNING: ignored setting named %s"%setting[name_key])
121
122         # support aliases to interfaces either by name or HWADDR
123         if 'ALIAS' in details:
124             if 'HWADDR' in details:
125                 hwaddr = details['HWADDR'].lower()
126                 del details['HWADDR']
127                 if hwaddr in macs:
128                     hwifname = macs[hwaddr]
129                     if ('IFNAME' in details) and details['IFNAME'] <> hwifname:
130                         logger.log("net:InitInterfaces WARNING: alias ifname (%s) and hwaddr ifname (%s) do not match"%\
131                                        (details['IFNAME'],hwifname))
132                         details['IFNAME'] = hwifname
133                 else:
134                     logger.log('net:InitInterfaces WARNING: mac addr %s for alias not found' %(hwaddr))
135
136             if 'IFNAME' in details:
137                 # stupid RH /etc/sysconfig/network-scripts/ifup-aliases:new_interface()
138                 # checks if the "$DEVNUM" only consists of '^[0-9A-Za-z_]*$'. Need to make
139                 # our aliases compliant.
140                 parts = details['ALIAS'].split('_')
141                 isValid=True
142                 for part in parts:
143                     isValid=isValid and part.isalnum()
144
145                 if isValid:
146                     devices_map["%s:%s" % (details['IFNAME'],details['ALIAS'])] = details 
147                 else:
148                     logger.log("net:InitInterfaces WARNING: interface alias (%s) not a valid string for RH ifup-aliases"% details['ALIAS'])
149             else:
150                 logger.log("net:InitInterfaces WARNING: interface alias (%s) not matched to an interface"% details['ALIAS'])
151             device_id -= 1
152         elif 'BRIDGE' in details:
153             #The bridge inherits the mac of the first attached interface.
154             if 'IFNAME' in details:
155                 ifname = details['IFNAME']
156                 device_id -= 1
157             elif orig_ifname:
158                 ifname = orig_ifname
159                 device_id -= 1
160             if 'PRIMARY' in details: del details['PRIMARY']
161             logger.log('net:InitInterfaces: Bridge detected. Adding %s to devices_map' % ifname)
162             devices_map[ifname] = details
163             bridgeName = details['BRIDGE']
164
165             logger.log('net:InitInterfaces: Adding bridge %s' % bridgeName)
166             bridgeDetails = prepDetails(interface)
167             bridgeDetails['TYPE']   = 'Bridge'
168             devices_map[bridgeName] = bridgeDetails
169         else:
170             if 'IFNAME' in details:
171                 ifname = details['IFNAME']
172                 device_id -= 1
173             elif orig_ifname:
174                 ifname = orig_ifname
175                 device_id -= 1
176             else:
177                 while True:
178                     ifname="eth%d" % (device_id-1)
179                     if ifname not in devices_map:
180                         break
181                     device_id += 1
182                 if os.path.exists("%s/ifcfg-%s"%(sysconfig,ifname)):
183                     logger.log("net:InitInterfaces WARNING: possibly blowing away %s configuration"%ifname)
184             devices_map[ifname] = details
185         device_id += 1 
186     logger.log('net:InitInterfaces: Device map: %r' % devices_map)
187     m = modprobe.Modprobe()
188     try:
189         m.input("%s/etc/modprobe.conf" % root)
190     except:
191         pass
192     for (dev, details) in devices_map.iteritems():
193         # get the driver string "moduleName option1=a option2=b"
194         driver=details.get('DRIVER','')
195         if driver <> '':
196             driver=driver.split()
197             kernelmodule=driver[0]
198             m.aliasset(dev,kernelmodule)
199             options=" ".join(driver[1:])
200             if options <> '':
201                 m.optionsset(dev,options)
202     m.output("%s/etc/modprobe.conf" % root, program)
203
204     # clean up after any ifcfg-$dev script that's no longer listed as
205     # part of the Interfaces associated with this node
206
207     # list all network-scripts
208     files = os.listdir(sysconfig)
209
210     # filter out the ifcfg-* files
211     ifcfgs=[]
212     for f in files:
213         if f.find("ifcfg-") == 0:
214             ifcfgs.append(f)
215
216     # remove loopback (lo) from ifcfgs list
217     lo = "ifcfg-lo"
218     if lo in ifcfgs: ifcfgs.remove(lo)
219
220     # remove known devices from ifcfgs list
221     for (dev, details) in devices_map.iteritems():
222         ifcfg = 'ifcfg-'+dev
223         if ifcfg in ifcfgs: ifcfgs.remove(ifcfg)
224
225     # delete the remaining ifcfgs from 
226     deletedSomething = False
227
228     if not failedToGetSettings:
229         for ifcfg in ifcfgs:
230             dev = ifcfg[len('ifcfg-'):]
231             path = "%s/ifcfg-%s" % (sysconfig,dev)
232             if not files_only:
233                 logger.verbose("net:InitInterfaces removing %s %s"%(dev,path))
234                 os.system("/sbin/ifdown %s" % dev)
235             deletedSomething=True
236             os.unlink(path)
237
238     # wait a bit for the one or more ifdowns to have taken effect
239     if deletedSomething:
240         time.sleep(2)
241
242     # Write network configuration file
243     networkconf = file("%s/etc/sysconfig/network" % root, "w")
244     networkconf.write("NETWORKING=yes\nHOSTNAME=%s\n" % hostname)
245     if gateway is not None:
246         networkconf.write("GATEWAY=%s\n" % gateway)
247     networkconf.close()
248
249     # Process ifcfg-$dev changes / additions
250     newdevs = []
251     table = 10
252     for (dev, details) in devices_map.iteritems():
253         (fd, tmpnam) = tempfile.mkstemp(dir=sysconfig)
254         f = os.fdopen(fd, "w")
255         f.write("# Autogenerated by pyplnet... do not edit!\n")
256         if 'DRIVER' in details:
257             f.write("# using %s driver for device %s\n" % (details['DRIVER'],dev))
258         f.write('DEVICE=%s\n' % dev)
259         
260         # print the configuration values
261         for (key, val) in details.iteritems():
262             if key not in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER','GATEWAY'):
263                 f.write('%s="%s"\n' % (key, val))
264
265         # print the configuration specific option values (if any)
266         if 'CFGOPTIONS' in details:
267             cfgoptions = details['CFGOPTIONS']
268             f.write('#CFGOPTIONS are %s\n' % cfgoptions)
269             for cfgoption in cfgoptions.split():
270                 key,val = cfgoption.split('=')
271                 key=key.strip()
272                 key=key.upper()
273                 val=val.strip()
274                 f.write('%s="%s"\n' % (key,val))
275         f.close()
276
277         # compare whether two files are the same
278         def comparefiles(a,b):
279             try:
280                 logger.verbose("net:InitInterfaces comparing %s with %s" % (a,b))
281                 if not os.path.exists(a): return False
282                 fb = open(a)
283                 buf_a = fb.read()
284                 fb.close()
285
286                 if not os.path.exists(b): return False
287                 fb = open(b)
288                 buf_b = fb.read()
289                 fb.close()
290
291                 return buf_a == buf_b
292             except IOError, e:
293                 return False
294
295         src_route_changed = False
296         if ('PRIMARY' not in details and 'GATEWAY' in details and
297             details['GATEWAY'] != ''):
298             table += 1
299             (fd, rule_tmpnam) = tempfile.mkstemp(dir=sysconfig)
300             os.write(fd, "from %s lookup %d\n" % (details['IPADDR'], table))
301             os.close(fd)
302             rule_dest = "%s/rule-%s" % (sysconfig, dev)
303             if not comparefiles(rule_tmpnam, rule_dest):
304                 os.rename(rule_tmpnam, rule_dest)
305                 os.chmod(rule_dest, 0644)
306                 src_route_changed = True
307             else:
308                 os.unlink(rule_tmpnam)
309             (fd, route_tmpnam) = tempfile.mkstemp(dir=sysconfig)
310             netmask = struct.unpack("I", socket.inet_aton(details['NETMASK']))[0]
311             ip = struct.unpack("I", socket.inet_aton(details['IPADDR']))[0]
312             network = socket.inet_ntoa(struct.pack("I", (ip & netmask)))
313             netmask = socket.ntohl(netmask)
314             i = 0
315             while (netmask & (1 << i)) == 0:
316                 i += 1
317             prefix = 32 - i
318             os.write(fd, "%s/%d dev %s table %d\n" % (network, prefix, dev, table))
319             os.write(fd, "default via %s dev %s table %d\n" % (details['GATEWAY'], dev, table))
320             os.close(fd)
321             route_dest = "%s/route-%s" % (sysconfig, dev)
322             if not comparefiles(route_tmpnam, route_dest):
323                 os.rename(route_tmpnam, route_dest)
324                 os.chmod(route_dest, 0644)
325                 src_route_changed = True
326             else:
327                 os.unlink(route_tmpnam)
328
329         path = "%s/ifcfg-%s" % (sysconfig,dev)
330         if not os.path.exists(path):
331             logger.verbose('net:InitInterfaces adding configuration for %s' % dev)
332             # add ifcfg-$dev configuration file
333             os.rename(tmpnam,path)
334             os.chmod(path,0644)
335             newdevs.append(dev)
336             
337         elif not comparefiles(tmpnam,path) or src_route_changed:
338             logger.verbose('net:InitInterfaces Configuration change for %s' % dev)
339             if not files_only:
340                 logger.verbose('net:InitInterfaces ifdown %s' % dev)
341                 # invoke ifdown for the old configuration
342                 os.system("/sbin/ifdown %s" % dev)
343                 # wait a few secs for ifdown to complete
344                 time.sleep(2)
345
346             logger.log('replacing configuration for %s' % dev)
347             # replace ifcfg-$dev configuration file
348             os.rename(tmpnam,path)
349             os.chmod(path,0644)
350             newdevs.append(dev)
351         else:
352             # tmpnam & path are identical
353             os.unlink(tmpnam)
354
355     for dev in newdevs:
356         cfgvariables = {}
357         fb = file("%s/ifcfg-%s"%(sysconfig,dev),"r")
358         for line in fb.readlines():
359             parts = line.split()
360             if parts[0][0]=="#":continue
361             if parts[0].find('='):
362                 name,value = parts[0].split('=')
363                 # clean up name & value
364                 name = name.strip()
365                 value = value.strip()
366                 value = value.strip("'")
367                 value = value.strip('"')
368                 cfgvariables[name]=value
369         fb.close()
370
371         def getvar(name):
372             if name in cfgvariables:
373                 value=cfgvariables[name]
374                 value = value.lower()
375                 return value
376             return ''
377
378         # skip over device configs with ONBOOT=no
379         if getvar("ONBOOT") == 'no': continue
380
381         # don't bring up slave devices, the network scripts will
382         # handle those correctly
383         if getvar("SLAVE") == 'yes': continue
384
385         if not files_only:
386             logger.verbose('net:InitInterfaces bringing up %s' % dev)
387             os.system("/sbin/ifup %s" % dev)
388
389 ##
390 # Prepare the interface details.
391 #
392 def prepDetails(interface, hostname=''):
393     details = {}
394     details['ONBOOT']  = 'yes'
395     details['USERCTL'] = 'no'
396     if interface['mac']:
397         details['HWADDR'] = interface['mac']
398     if interface['is_primary']:
399         details['PRIMARY'] = 'yes'
400
401     if interface['method'] == "static":
402         details['BOOTPROTO'] = "static"
403         details['IPADDR']    = interface['ip']
404         details['NETMASK']   = interface['netmask']
405         details['GATEWAY']   = interface['gateway']
406         if interface['is_primary']:
407             gateway = interface['gateway']
408             if interface['dns1']:
409                 details['DNS1'] = interface['dns1']
410             if interface['dns2']:
411                 details['DNS2'] = interface['dns2']
412
413     elif interface['method'] == "dhcp":
414         details['BOOTPROTO'] = "dhcp"
415         details['PERSISTENT_DHCLIENT'] = "yes"
416         if interface['hostname']:
417             details['DHCP_HOSTNAME'] = interface['hostname']
418         else:
419             details['DHCP_HOSTNAME'] = hostname
420         if not interface['is_primary']:
421             details['DHCLIENTARGS'] = "-R subnet-mask"
422
423     return details
424
425 if __name__ == "__main__":
426     import optparse
427     import sys
428
429     parser = optparse.OptionParser(usage="plnet [-v] [-f] [-p <program>] -r root node_id")
430     parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
431     parser.add_option("-r", "--root", action="store", type="string",
432                       dest="root", default=None)
433     parser.add_option("-f", "--files-only", action="store_true",
434                       dest="files_only")
435     parser.add_option("-p", "--program", action="store", type="string",
436                       dest="program", default="plnet")
437     (options, args) = parser.parse_args()
438     options.root = ''
439     options.verbose = True
440     if len(args) != 1 or options.root is None:
441         print sys.argv
442         print >>sys.stderr, "Missing root or node_id"
443         parser.print_help()
444         sys.exit(1)
445
446     node = shell.GetNodes({'node_id': [int(args[0])]})
447     try:
448         interfaces = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
449     except AttributeError:
450         interfaces = shell.GetNodeNetworks({'nodenetwork_id':node[0]['nodenetwork_ids']})
451         version = 4.2
452
453
454     data = {'hostname': node[0]['hostname'], 'interfaces': interfaces}
455     class logger:
456         def __init__(self, verbose):
457             self.verbosity = verbose
458         def log(self, msg, loglevel=2):
459             if self.verbosity:
460                 print msg
461         def verbose(self, msg):
462             self.log(msg, 1)
463     l = logger(options.verbose)
464     InitInterfaces(l, shell, data, options.root, options.files_only)