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