Setting tag pyplnet-4.3-7
[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     for (dev, details) in devices_map.iteritems():
260         (fd, tmpnam) = tempfile.mkstemp(dir=sysconfig)
261         f = os.fdopen(fd, "w")
262         f.write("# Autogenerated by pyplnet... do not edit!\n")
263         if 'DRIVER' in details:
264             f.write("# using %s driver for device %s\n" % (details['DRIVER'],dev))
265         f.write('DEVICE=%s\n' % dev)
266         
267         # print the configuration values
268         for (key, val) in details.iteritems():
269             if key not in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER','GATEWAY'):
270                 f.write('%s=%s\n' % (key, val))
271
272         # print the configuration specific option values (if any)
273         if 'CFGOPTIONS' in details:
274             cfgoptions = details['CFGOPTIONS']
275             f.write('#CFGOPTIONS are %s\n' % cfgoptions)
276             for cfgoption in cfgoptions.split():
277                 key,val = cfgoption.split('=')
278                 key=key.strip()
279                 key=key.upper()
280                 val=val.strip()
281                 f.write('%s="%s"\n' % (key,val))
282         f.close()
283
284         # compare whether two files are the same
285         def comparefiles(a,b):
286             try:
287                 logger.verbose("net:InitInterfaces comparing %s with %s" % (a,b))
288                 if not os.path.exists(a): return False
289                 fb = open(a)
290                 buf_a = fb.read()
291                 fb.close()
292
293                 if not os.path.exists(b): return False
294                 fb = open(b)
295                 buf_b = fb.read()
296                 fb.close()
297
298                 return buf_a == buf_b
299             except IOError, e:
300                 return False
301
302         src_route_changed = False
303         if ('PRIMARY' not in details and 'GATEWAY' in details and
304             details['GATEWAY'] != ''):
305             i = len(dev) - 1
306             while dev[i - 1].isdigit():
307                 i -= 1
308             table = 10 + int(dev[i:])
309             (fd, rule_tmpnam) = tempfile.mkstemp(dir=sysconfig)
310             os.write(fd, "from %s lookup %d\n" % (details['IPADDR'], table))
311             os.close(fd)
312             rule_dest = "%s/rule-%s" % (sysconfig, dev)
313             if not comparefiles(rule_tmpnam, rule_dest):
314                 os.rename(rule_tmpnam, rule_dest)
315                 os.chmod(rule_dest, 0644)
316                 src_route_changed = True
317             else:
318                 os.unlink(rule_tmpnam)
319             (fd, route_tmpnam) = tempfile.mkstemp(dir=sysconfig)
320             netmask = struct.unpack("I", socket.inet_aton(details['NETMASK']))[0]
321             ip = struct.unpack("I", socket.inet_aton(details['IPADDR']))[0]
322             network = socket.inet_ntoa(struct.pack("I", (ip & netmask)))
323             netmask = socket.ntohl(netmask)
324             i = 0
325             while (netmask & (1 << i)) == 0:
326                 i += 1
327             prefix = 32 - i
328             os.write(fd, "%s/%d dev %s table %d\n" % (network, prefix, dev, table))
329             os.write(fd, "default via %s dev %s table %d\n" % (details['GATEWAY'], dev, table))
330             os.close(fd)
331             route_dest = "%s/route-%s" % (sysconfig, dev)
332             if not comparefiles(route_tmpnam, route_dest):
333                 os.rename(route_tmpnam, route_dest)
334                 os.chmod(route_dest, 0644)
335                 src_route_changed = True
336             else:
337                 os.unlink(route_tmpnam)
338
339         path = "%s/ifcfg-%s" % (sysconfig,dev)
340         if not os.path.exists(path):
341             logger.verbose('net:InitInterfaces adding configuration for %s' % dev)
342             # add ifcfg-$dev configuration file
343             os.rename(tmpnam,path)
344             os.chmod(path,0644)
345             newdevs.append(dev)
346             
347         elif not comparefiles(tmpnam,path) or src_route_changed:
348             logger.verbose('net:InitInterfaces Configuration change for %s' % dev)
349             if not files_only:
350                 logger.verbose('net:InitInterfaces ifdown %s' % dev)
351                 # invoke ifdown for the old configuration
352                 os.system("/sbin/ifdown %s" % dev)
353                 # wait a few secs for ifdown to complete
354                 time.sleep(2)
355
356             logger.log('replacing configuration for %s' % dev)
357             # replace ifcfg-$dev configuration file
358             os.rename(tmpnam,path)
359             os.chmod(path,0644)
360             newdevs.append(dev)
361         else:
362             # tmpnam & path are identical
363             os.unlink(tmpnam)
364
365     for dev in newdevs:
366         cfgvariables = {}
367         fb = file("%s/ifcfg-%s"%(sysconfig,dev),"r")
368         for line in fb.readlines():
369             parts = line.split()
370             if parts[0][0]=="#":continue
371             if parts[0].find('='):
372                 name,value = parts[0].split('=')
373                 # clean up name & value
374                 name = name.strip()
375                 value = value.strip()
376                 value = value.strip("'")
377                 value = value.strip('"')
378                 cfgvariables[name]=value
379         fb.close()
380
381         def getvar(name):
382             if name in cfgvariables:
383                 value=cfgvariables[name]
384                 value = value.lower()
385                 return value
386             return ''
387
388         # skip over device configs with ONBOOT=no
389         if getvar("ONBOOT") == 'no': continue
390
391         # don't bring up slave devices, the network scripts will
392         # handle those correctly
393         if getvar("SLAVE") == 'yes': continue
394
395         if not files_only:
396             logger.verbose('net:InitInterfaces bringing up %s' % dev)
397             os.system("/sbin/ifup %s" % dev)
398
399 if __name__ == "__main__":
400     import optparse
401     import sys
402
403     parser = optparse.OptionParser(usage="plnet [-v] [-f] [-p <program>] -r root node_id")
404     parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
405     parser.add_option("-r", "--root", action="store", type="string",
406                       dest="root", default=None)
407     parser.add_option("-f", "--files-only", action="store_true",
408                       dest="files_only")
409     parser.add_option("-p", "--program", action="store", type="string",
410                       dest="program", default="plnet")
411     (options, args) = parser.parse_args()
412     if len(args) != 1 or options.root is None:
413         print sys.argv
414         print >>sys.stderr, "Missing root or node_id"
415         parser.print_help()
416         sys.exit(1)
417
418     node = shell.GetNodes({'node_id': [int(args[0])]})
419     try:
420         interfaces = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
421     except AttributeError:
422         interfaces = shell.GetNodeNetworks({'nodenetwork_id':node[0]['nodenetwork_ids']})
423         version = 4.2
424
425
426     data = {'hostname': node[0]['hostname'], 'interfaces': interfaces}
427     class logger:
428         def __init__(self, verbose):
429             self.verbosity = verbose
430         def log(self, msg, loglevel=2):
431             if self.verbosity:
432                 print msg
433         def verbose(self, msg):
434             self.log(msg, 1)
435     l = logger(options.verbose)
436     InitInterfaces(l, shell, data, options.root, options.files_only)