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