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