Setting tag pyplnet-4.3-6
[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
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 = {}
81         details['ONBOOT']='yes'
82         details['USERCTL']='no'
83         if interface['mac']:
84             details['HWADDR'] = interface['mac']
85         if interface['is_primary']:
86             details['PRIMARY']='yes'
87
88         if interface['method'] == "static":
89             details['BOOTPROTO'] = "static"
90             details['IPADDR'] = interface['ip']
91             details['NETMASK'] = interface['netmask']
92             if interface['is_primary']:
93                 gateway = interface['gateway']
94                 if interface['dns1']:
95                     details['DNS1'] = interface['dns1']
96                 if interface['dns2']:
97                     details['DNS2'] = interface['dns2']
98
99         elif interface['method'] == "dhcp":
100             details['BOOTPROTO'] = "dhcp"
101             details['PERSISTENT_DHCLIENT'] = "yes"
102             if interface['hostname']:
103                 details['DHCP_HOSTNAME'] = interface['hostname']
104             else:
105                 details['DHCP_HOSTNAME'] = hostname 
106             if not interface['is_primary']:
107                 details['DHCLIENTARGS'] = "-R subnet-mask"
108
109         try:
110             plc.GetInterfaceTags()
111             version = 4.3
112         except AttributeError:
113             version = 4.2
114
115         if version == 4.3:
116             interface_tag_ids = "interface_tag_ids"
117             interface_tag_id = "interface_tag_id"
118         else:
119             interface_tag_ids = "nodenetwork_setting_ids"
120             interface_tag_id = "nodenetwork_setting_id"
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                 # to explicitly set interface name
136                 name_key = "name"
137                 if version == 4.3:
138                     name_key = "tagname"
139                     
140                 settingname = setting[name_key].upper()
141                 if settingname in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
142                     details[settingname]=setting['value']
143                 # wireless settings
144                 elif settingname in \
145                         [  "MODE", "ESSID", "NW", "FREQ", "CHANNEL", "SENS", "RATE",
146                            "KEY", "KEY1", "KEY2", "KEY3", "KEY4", "SECURITYMODE", 
147                            "IWCONFIG", "IWPRIV" ] :
148                     details [settingname] = setting['value']
149                     details ['TYPE']='Wireless'
150                 else:
151                     logger.log("net:InitInterfaces WARNING: ignored setting named %s"%setting[name_key])
152
153         # support aliases to interfaces either by name or HWADDR
154         if 'ALIAS' in details:
155             if 'HWADDR' in details:
156                 hwaddr = details['HWADDR'].lower()
157                 del details['HWADDR']
158                 if hwaddr in macs:
159                     hwifname = macs[hwaddr]
160                     if ('IFNAME' in details) and details['IFNAME'] <> hwifname:
161                         logger.log("net:InitInterfaces WARNING: alias ifname (%s) and hwaddr ifname (%s) do not match"%\
162                                        (details['IFNAME'],hwifname))
163                         details['IFNAME'] = hwifname
164                 else:
165                     logger.log('net:InitInterfaces WARNING: mac addr %s for alias not found' %(hwaddr))
166
167             if 'IFNAME' in details:
168                 # stupid RH /etc/sysconfig/network-scripts/ifup-aliases:new_interface()
169                 # checks if the "$DEVNUM" only consists of '^[0-9A-Za-z_]*$'. Need to make
170                 # our aliases compliant.
171                 parts = details['ALIAS'].split('_')
172                 isValid=True
173                 for part in parts:
174                     isValid=isValid and part.isalnum()
175
176                 if isValid:
177                     devices_map["%s:%s" % (details['IFNAME'],details['ALIAS'])] = details 
178                 else:
179                     logger.log("net:InitInterfaces WARNING: interface alias (%s) not a valid string for RH ifup-aliases"% details['ALIAS'])
180             else:
181                 logger.log("net:InitInterfaces WARNING: interface alias (%s) not matched to an interface"% details['ALIAS'])
182             device_id -= 1
183         else:
184             if ('IFNAME' not in details) and not orig_ifname:
185                 ifname="eth%d" % (device_id-1)
186                 # should check if $ifname is an eth already defines
187                 if os.path.exists("%s/ifcfg-%s"%(sysconfig,ifname)):
188                     logger.log("net:InitInterfaces WARNING: possibly blowing away %s configuration"%ifname)
189             else:
190                 if ('IFNAME' not in details) and orig_ifname:
191                     ifname = orig_ifname
192                 else:
193                     ifname = details['IFNAME']
194                 device_id -= 1
195             devices_map[ifname] = details
196                 
197     m = modprobe.Modprobe()
198     try:
199         m.input("%s/etc/modprobe.conf" % root)
200     except:
201         pass
202     for (dev, details) in devices_map.iteritems():
203         # get the driver string "moduleName option1=a option2=b"
204         driver=details.get('DRIVER','')
205         if driver <> '':
206             driver=driver.split()
207             kernelmodule=driver[0]
208             m.aliasset(dev,kernelmodule)
209             options=" ".join(driver[1:])
210             if options <> '':
211                 m.optionsset(dev,options)
212     m.output("%s/etc/modprobe.conf" % root, program)
213
214     # clean up after any ifcfg-$dev script that's no longer listed as
215     # part of the Interfaces associated with this node
216
217     # list all network-scripts
218     files = os.listdir(sysconfig)
219
220     # filter out the ifcfg-* files
221     ifcfgs=[]
222     for f in files:
223         if f.find("ifcfg-") == 0:
224             ifcfgs.append(f)
225
226     # remove loopback (lo) from ifcfgs list
227     lo = "ifcfg-lo"
228     if lo in ifcfgs: ifcfgs.remove(lo)
229
230     # remove known devices from icfgs list
231     for (dev, details) in devices_map.iteritems():
232         ifcfg = 'ifcfg-'+dev
233         if ifcfg in ifcfgs: ifcfgs.remove(ifcfg)
234
235     # delete the remaining ifcfgs from 
236     deletedSomething = False
237
238     if not failedToGetSettings:
239         for ifcfg in ifcfgs:
240             dev = ifcfg[len('ifcfg-'):]
241             path = "%s/ifcfg-%s" % (sysconfig,dev)
242             if not files_only:
243                 logger.verbose("net:InitInterfaces removing %s %s"%(dev,path))
244                 os.system("/sbin/ifdown %s" % dev)
245             deletedSomething=True
246             os.unlink(path)
247
248     # wait a bit for the one or more ifdowns to have taken effect
249     if deletedSomething:
250         time.sleep(2)
251
252     # Write network configuration file
253     networkconf = file("%s/etc/sysconfig/network" % root, "w")
254     networkconf.write("NETWORKING=yes\nHOSTNAME=%s\n" % hostname)
255     if gateway is not None:
256         networkconf.write("GATEWAY=%s\n" % gateway)
257     networkconf.close()
258
259     # Process ifcfg-$dev changes / additions
260     newdevs = []
261     for (dev, details) in devices_map.iteritems():
262         (fd, tmpnam) = tempfile.mkstemp(dir=sysconfig)
263         f = os.fdopen(fd, "w")
264         f.write("# Autogenerated by pyplnet... do not edit!\n")
265         if 'DRIVER' in details:
266             f.write("# using %s driver for device %s\n" % (details['DRIVER'],dev))
267         f.write('DEVICE=%s\n' % dev)
268         
269         # print the configuration values
270         for (key, val) in details.iteritems():
271             if key not in ('IFNAME','ALIAS','CFGOPTIONS','DRIVER'):
272                 f.write('%s=%s\n' % (key, val))
273
274         # print the configuration specific option values (if any)
275         if 'CFGOPTIONS' in details:
276             cfgoptions = details['CFGOPTIONS']
277             f.write('#CFGOPTIONS are %s\n' % cfgoptions)
278             for cfgoption in cfgoptions.split():
279                 key,val = cfgoption.split('=')
280                 key=key.strip()
281                 key=key.upper()
282                 val=val.strip()
283                 f.write('%s="%s"\n' % (key,val))
284         f.close()
285
286         # compare whether two files are the same
287         def comparefiles(a,b):
288             try:
289                 logger.verbose("net:InitInterfaces comparing %s with %s" % (a,b))
290                 if not os.path.exists(a): return False
291                 fb = open(a)
292                 buf_a = fb.read()
293                 fb.close()
294
295                 if not os.path.exists(b): return False
296                 fb = open(b)
297                 buf_b = fb.read()
298                 fb.close()
299
300                 return buf_a == buf_b
301             except IOError, e:
302                 return False
303
304         path = "%s/ifcfg-%s" % (sysconfig,dev)
305         if not os.path.exists(path):
306             logger.verbose('net:InitInterfaces adding configuration for %s' % dev)
307             # add ifcfg-$dev configuration file
308             os.rename(tmpnam,path)
309             os.chmod(path,0644)
310             newdevs.append(dev)
311             
312         elif not comparefiles(tmpnam,path):
313             logger.verbose('net:InitInterfaces Configuration change for %s' % dev)
314             if not files_only:
315                 logger.verbose('net:InitInterfaces ifdown %s' % dev)
316                 # invoke ifdown for the old configuration
317                 os.system("/sbin/ifdown %s" % dev)
318                 # wait a few secs for ifdown to complete
319                 time.sleep(2)
320
321             logger.log('replacing configuration for %s' % dev)
322             # replace ifcfg-$dev configuration file
323             os.rename(tmpnam,path)
324             os.chmod(path,0644)
325             newdevs.append(dev)
326         else:
327             # tmpnam & path are identical
328             os.unlink(tmpnam)
329
330     for dev in newdevs:
331         cfgvariables = {}
332         fb = file("%s/ifcfg-%s"%(sysconfig,dev),"r")
333         for line in fb.readlines():
334             parts = line.split()
335             if parts[0][0]=="#":continue
336             if parts[0].find('='):
337                 name,value = parts[0].split('=')
338                 # clean up name & value
339                 name = name.strip()
340                 value = value.strip()
341                 value = value.strip("'")
342                 value = value.strip('"')
343                 cfgvariables[name]=value
344         fb.close()
345
346         def getvar(name):
347             if name in cfgvariables:
348                 value=cfgvariables[name]
349                 value = value.lower()
350                 return value
351             return ''
352
353         # skip over device configs with ONBOOT=no
354         if getvar("ONBOOT") == 'no': continue
355
356         # don't bring up slave devices, the network scripts will
357         # handle those correctly
358         if getvar("SLAVE") == 'yes': continue
359
360         if not files_only:
361             logger.verbose('net:InitInterfaces bringing up %s' % dev)
362             os.system("/sbin/ifup %s" % dev)
363
364 if __name__ == "__main__":
365     import optparse
366     import sys
367
368     parser = optparse.OptionParser(usage="plnet [-v] [-f] [-p <program>] -r root node_id")
369     parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
370     parser.add_option("-r", "--root", action="store", type="string",
371                       dest="root", default=None)
372     parser.add_option("-f", "--files-only", action="store_true",
373                       dest="files_only")
374     parser.add_option("-p", "--program", action="store", type="string",
375                       dest="program", default="plnet")
376     (options, args) = parser.parse_args()
377     if len(args) != 1 or options.root is None:
378         print >>sys.stderr, "Missing root or node_id"
379         parser.print_help()
380         sys.exit(1)
381
382     node = shell.GetNodes({'node_id': [int(args[0])]})
383     try:
384         interfaces = shell.GetInterfaces({'interface_id': node[0]['interface_ids']})
385     except AttributeError:
386         interfaces = shell.GetNodeNetworks({'nodenetwork_id':node[0]['nodenetwork_ids']})
387         version = 4.2
388
389
390     data = {'hostname': node[0]['hostname'], 'interfaces': interfaces}
391     class logger:
392         def __init__(self, verbose):
393             self.verbosity = verbose
394         def log(self, msg, loglevel=2):
395             if self.verbosity:
396                 print msg
397         def verbose(self, msg):
398             self.log(msg, 1)
399     l = logger(options.verbose)
400     InitInterfaces(l, shell, data, options.root, options.files_only)