]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/networkinterfaces.py
Make sure an interface is down (in the bond case, its slaves are also
[mirror_ifupdown2.git] / ifupdown / networkinterfaces.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6 # networkInterfaces --
7 # ifupdown network interfaces file parser
8 #
9
10 import collections
11 import logging
12 import glob
13 import re
14 import os
15 import copy
16 from utils import utils
17 from iface import *
18 from template import templateEngine
19
20 whitespaces = '\n\t\r '
21
22 class networkInterfaces():
23 """ debian ifupdown /etc/network/interfaces file parser """
24
25 hotplugs = {}
26 auto_ifaces = []
27 callbacks = {}
28 auto_all = False
29
30 _addrfams = {'inet' : ['static', 'manual', 'loopback', 'dhcp', 'dhcp6'],
31 'inet6' : ['static', 'manual', 'loopback', 'dhcp', 'dhcp6']}
32
33 def __init__(self, interfacesfile='/etc/network/interfaces',
34 interfacesfileiobuf=None, interfacesfileformat='native',
35 template_engine=None, template_lookuppath=None):
36 """This member function initializes the networkinterfaces parser object.
37
38 Kwargs:
39 **interfacesfile** (str): path to the interfaces file (default is /etc/network/interfaces)
40
41 **interfacesfileiobuf** (object): interfaces file io stream
42
43 **interfacesfileformat** (str): format of interfaces file (choices are 'native' and 'json'. 'native' being the default)
44
45 **template_engine** (str): template engine name
46
47 **template_lookuppath** (str): template lookup path
48
49 Raises:
50 AttributeError, KeyError """
51
52 self.logger = logging.getLogger('ifupdown.' +
53 self.__class__.__name__)
54 self.callbacks = {'iface_found' : None,
55 'validateifaceattr' : None,
56 'validateifaceobj' : None}
57 self.allow_classes = {}
58 self.interfacesfile = interfacesfile
59 self.interfacesfileiobuf = interfacesfileiobuf
60 self.interfacesfileformat = interfacesfileformat
61 self._filestack = [self.interfacesfile]
62 self._template_engine = templateEngine(template_engine,
63 template_lookuppath)
64 self._currentfile_has_template = False
65 self._ws_split_regex = re.compile(r'[\s\t]\s*')
66
67 @property
68 def _currentfile(self):
69 try:
70 return self._filestack[-1]
71 except:
72 return self.interfacesfile
73
74 def _parse_error(self, filename, lineno, msg):
75 if lineno == -1 or self._currentfile_has_template:
76 self.logger.error('%s: %s' %(filename, msg))
77 else:
78 self.logger.error('%s: line%d: %s' %(filename, lineno, msg))
79
80 def _parse_warn(self, filename, lineno, msg):
81 if lineno == -1 or self._currentfile_has_template:
82 self.logger.warn('%s: %s' %(filename, msg))
83 else:
84 self.logger.warn('%s: line%d: %s' %(filename, lineno, msg))
85
86 def _validate_addr_family(self, ifaceobj, lineno=-1):
87 if ifaceobj.addr_family:
88 if not self._addrfams.get(ifaceobj.addr_family):
89 self._parse_error(self._currentfile, lineno,
90 'iface %s: unsupported address family \'%s\''
91 %(ifaceobj.name, ifaceobj.addr_family))
92 ifaceobj.addr_family = None
93 ifaceobj.addr_method = None
94 return
95 if ifaceobj.addr_method:
96 if (ifaceobj.addr_method not in
97 self._addrfams.get(ifaceobj.addr_family)):
98 self._parse_error(self._currentfile, lineno,
99 'iface %s: unsupported address method \'%s\''
100 %(ifaceobj.name, ifaceobj.addr_method))
101 else:
102 ifaceobj.addr_method = 'static'
103
104 def subscribe(self, callback_name, callback_func):
105 """This member function registers callback functions.
106
107 Args:
108 **callback_name** (str): callback function name (supported names: 'iface_found', 'validateifaceattr', 'validateifaceobj')
109
110 **callback_func** (function pointer): callback function pointer
111
112 Warns on error
113 """
114
115 if callback_name not in self.callbacks.keys():
116 print 'warning: invalid callback ' + callback_name
117 return -1
118
119 self.callbacks[callback_name] = callback_func
120
121 def ignore_line(self, line):
122 l = line.strip(whitespaces)
123 if not l or l[0] == '#':
124 return 1
125 return 0
126
127 def process_allow(self, lines, cur_idx, lineno):
128 allow_line = lines[cur_idx]
129
130 words = re.split(self._ws_split_regex, allow_line)
131 if len(words) <= 1:
132 raise Exception('invalid allow line \'%s\' at line %d'
133 %(allow_line, lineno))
134
135 allow_class = words[0].split('-')[1]
136 ifacenames = words[1:]
137
138 if self.allow_classes.get(allow_class):
139 for i in ifacenames:
140 self.allow_classes[allow_class].append(i)
141 else:
142 self.allow_classes[allow_class] = ifacenames
143 return 0
144
145 def process_source(self, lines, cur_idx, lineno):
146 # Support regex
147 self.logger.debug('processing sourced line ..\'%s\'' %lines[cur_idx])
148 sourced_file = re.split(self._ws_split_regex, lines[cur_idx], 2)[1]
149 if sourced_file:
150 filenames = glob.glob(sourced_file)
151 if not filenames:
152 self._parse_warn(self._currentfile, lineno,
153 'cannot find source file %s' %sourced_file)
154 return 0
155 for f in filenames:
156 self.read_file(f)
157 else:
158 self._parse_error(self._currentfile, lineno,
159 'unable to read source line')
160 return 0
161
162 def process_auto(self, lines, cur_idx, lineno):
163 auto_ifaces = re.split(self._ws_split_regex, lines[cur_idx])[1:]
164 if not auto_ifaces:
165 self._parse_error(self._currentfile, lineno,
166 'invalid auto line \'%s\''%lines[cur_idx])
167 return 0
168 for a in auto_ifaces:
169 if a == 'all':
170 self.auto_all = True
171 break
172 r = utils.parse_iface_range(a)
173 if r:
174 for i in range(r[1], r[2]):
175 self.auto_ifaces.append('%s-%d' %(r[0], i))
176 self.auto_ifaces.append(a)
177 return 0
178
179 def _add_to_iface_config(self, ifacename, iface_config, attrname,
180 attrval, lineno):
181 newattrname = attrname.replace("_", "-")
182 try:
183 if not self.callbacks.get('validateifaceattr')(newattrname,
184 attrval):
185 self._parse_error(self._currentfile, lineno,
186 'iface %s: unsupported keyword (%s)'
187 %(ifacename, attrname))
188 return
189 except:
190 pass
191 attrvallist = iface_config.get(newattrname, [])
192 if newattrname in ['scope', 'netmask', 'broadcast', 'preferred-lifetime']:
193 # For attributes that are related and that can have multiple
194 # entries, store them at the same index as their parent attribute.
195 # The example of such attributes is 'address' and its related
196 # attributes. since the related attributes can be optional,
197 # we add null string '' in places where they are optional.
198 # XXX: this introduces awareness of attribute names in
199 # this class which is a violation.
200
201 # get the index corresponding to the 'address'
202 addrlist = iface_config.get('address')
203 if addrlist:
204 # find the index of last address element
205 for i in range(0, len(addrlist) - len(attrvallist) -1):
206 attrvallist.append('')
207 attrvallist.append(attrval)
208 iface_config[newattrname] = attrvallist
209 elif not attrvallist:
210 iface_config[newattrname] = [attrval]
211 else:
212 iface_config[newattrname].append(attrval)
213
214 def parse_iface(self, lines, cur_idx, lineno, ifaceobj):
215 lines_consumed = 0
216 line_idx = cur_idx
217
218 iface_line = lines[cur_idx].strip(whitespaces)
219 iface_attrs = re.split(self._ws_split_regex, iface_line)
220 ifacename = iface_attrs[1]
221
222 if utils.check_ifname_size_invalid(ifacename):
223 self._parse_warn(self._currentfile, lineno,
224 '%s: interface name too long' %ifacename)
225
226 # in cases where mako is unable to render the template
227 # or incorrectly renders it due to user template
228 # errors, we maybe left with interface names with
229 # mako variables in them. There is no easy way to
230 # recognize and warn about these. In the below check
231 # we try to warn the user of such cases by looking for
232 # variable patterns ('$') in interface names.
233 if '$' in ifacename:
234 self._parse_warn(self._currentfile, lineno,
235 '%s: unexpected characters in interface name' %ifacename)
236
237 ifaceobj.raw_config.append(iface_line)
238 iface_config = collections.OrderedDict()
239 for line_idx in range(cur_idx + 1, len(lines)):
240 l = lines[line_idx].strip(whitespaces)
241 if self.ignore_line(l) == 1:
242 continue
243 attrs = re.split(self._ws_split_regex, l, 1)
244 if self._is_keyword(attrs[0]):
245 line_idx -= 1
246 break
247 # if not a keyword, every line must have at least a key and value
248 if len(attrs) < 2:
249 self._parse_error(self._currentfile, line_idx,
250 'iface %s: invalid syntax \'%s\'' %(ifacename, l))
251 continue
252 ifaceobj.raw_config.append(l)
253 attrname = attrs[0]
254 # preprocess vars (XXX: only preprocesses $IFACE for now)
255 attrval = re.sub(r'\$IFACE', ifacename, attrs[1])
256 self._add_to_iface_config(ifacename, iface_config, attrname,
257 attrval, line_idx+1)
258 lines_consumed = line_idx - cur_idx
259
260 # Create iface object
261 if ifacename.find(':') != -1:
262 ifaceobj.name = ifacename.split(':')[0]
263 else:
264 ifaceobj.name = ifacename
265
266 ifaceobj.config = iface_config
267 ifaceobj.generate_env()
268
269 try:
270 ifaceobj.addr_family = iface_attrs[2]
271 ifaceobj.addr_method = iface_attrs[3]
272 except IndexError:
273 # ignore
274 pass
275 self._validate_addr_family(ifaceobj, lineno)
276
277 if self.auto_all or (ifaceobj.name in self.auto_ifaces):
278 ifaceobj.auto = True
279
280 classes = self.get_allow_classes_for_iface(ifaceobj.name)
281 if classes:
282 [ifaceobj.set_class(c) for c in classes]
283
284 return lines_consumed # Return next index
285
286 def process_iface(self, lines, cur_idx, lineno):
287 ifaceobj = iface()
288 lines_consumed = self.parse_iface(lines, cur_idx, lineno, ifaceobj)
289
290 range_val = utils.parse_iface_range(ifaceobj.name)
291 if range_val:
292 for v in range(range_val[1], range_val[2]):
293 ifaceobj_new = copy.deepcopy(ifaceobj)
294 ifaceobj_new.realname = '%s' %ifaceobj.name
295 ifaceobj_new.name = '%s%d' %(range_val[0], v)
296 ifaceobj_new.flags = iface.IFACERANGE_ENTRY
297 if v == range_val[1]:
298 ifaceobj_new.flags |= iface.IFACERANGE_START
299 self.callbacks.get('iface_found')(ifaceobj_new)
300 else:
301 self.callbacks.get('iface_found')(ifaceobj)
302
303 return lines_consumed # Return next index
304
305 def process_vlan(self, lines, cur_idx, lineno):
306 ifaceobj = iface()
307 lines_consumed = self.parse_iface(lines, cur_idx, lineno, ifaceobj)
308
309 range_val = utils.parse_iface_range(ifaceobj.name)
310 if range_val:
311 for v in range(range_val[1], range_val[2]):
312 ifaceobj_new = copy.deepcopy(ifaceobj)
313 ifaceobj_new.realname = '%s' %ifaceobj.name
314 ifaceobj_new.name = '%s%d' %(range_val[0], v)
315 ifaceobj_new.type = ifaceType.BRIDGE_VLAN
316 ifaceobj_new.flags = iface.IFACERANGE_ENTRY
317 if v == range_val[1]:
318 ifaceobj_new.flags |= iface.IFACERANGE_START
319 self.callbacks.get('iface_found')(ifaceobj_new)
320 else:
321 ifaceobj.type = ifaceType.BRIDGE_VLAN
322 self.callbacks.get('iface_found')(ifaceobj)
323
324 return lines_consumed # Return next index
325
326 network_elems = { 'source' : process_source,
327 'allow' : process_allow,
328 'auto' : process_auto,
329 'iface' : process_iface,
330 'vlan' : process_vlan}
331
332 def _is_keyword(self, str):
333 # The additional split here is for allow- keyword
334 tmp_str = str.split('-')[0]
335 if tmp_str in self.network_elems.keys():
336 return 1
337 return 0
338
339 def _get_keyword_func(self, str):
340 tmp_str = str.split('-')[0]
341 return self.network_elems.get(tmp_str)
342
343 def get_allow_classes_for_iface(self, ifacename):
344 classes = []
345 for class_name, ifacenames in self.allow_classes.items():
346 if ifacename in ifacenames:
347 classes.append(class_name)
348 return classes
349
350 def process_interfaces(self, filedata):
351
352 # process line continuations
353 filedata = ' '.join(d.strip() for d in filedata.split('\\'))
354
355 line_idx = 0
356 lines_consumed = 0
357 raw_config = filedata.split('\n')
358 lines = [l.strip(whitespaces) for l in raw_config]
359 while (line_idx < len(lines)):
360 if self.ignore_line(lines[line_idx]):
361 line_idx += 1
362 continue
363 words = re.split(self._ws_split_regex, lines[line_idx])
364 if not words:
365 line_idx += 1
366 continue
367 # Check if first element is a supported keyword
368 if self._is_keyword(words[0]):
369 keyword_func = self._get_keyword_func(words[0])
370 lines_consumed = keyword_func(self, lines, line_idx, line_idx+1)
371 line_idx += lines_consumed
372 else:
373 self._parse_error(self._currentfile, line_idx + 1,
374 'error processing line \'%s\'' %lines[line_idx])
375 line_idx += 1
376 return 0
377
378 def read_filedata(self, filedata):
379 self._currentfile_has_template = False
380 # run through template engine
381 try:
382 rendered_filedata = self._template_engine.render(filedata)
383 if rendered_filedata is filedata:
384 self._currentfile_has_template = False
385 else:
386 self._currentfile_has_template = True
387 except Exception, e:
388 self._parse_error(self._currentfile, -1,
389 'failed to render template (%s). ' %str(e) +
390 'Continue without template rendering ...')
391 rendered_filedata = None
392 pass
393 if rendered_filedata:
394 self.process_interfaces(rendered_filedata)
395 else:
396 self.process_interfaces(filedata)
397
398 def read_file(self, filename, fileiobuf=None):
399 if fileiobuf:
400 self.read_filedata(fileiobuf)
401 return
402 self._filestack.append(filename)
403 self.logger.info('processing interfaces file %s' %filename)
404 f = open(filename)
405 filedata = f.read()
406 f.close()
407 self.read_filedata(filedata)
408 self._filestack.pop()
409
410 def read_file_json(self, filename, fileiobuf=None):
411 if fileiobuf:
412 ifacedicts = json.loads(fileiobuf, encoding="utf-8")
413 #object_hook=ifaceJsonDecoder.json_object_hook)
414 elif filename:
415 self.logger.info('processing interfaces file %s' %filename)
416 fp = open(filename)
417 ifacedicts = json.load(fp)
418 #object_hook=ifaceJsonDecoder.json_object_hook)
419
420 # we need to handle both lists and non lists formats (e.g. {{}})
421 if not isinstance(ifacedicts,list):
422 ifacedicts = [ifacedicts]
423
424 for ifacedict in ifacedicts:
425 ifaceobj = ifaceJsonDecoder.json_to_ifaceobj(ifacedict)
426 if ifaceobj:
427 self._validate_addr_family(ifaceobj)
428 self.callbacks.get('validateifaceobj')(ifaceobj)
429 self.callbacks.get('iface_found')(ifaceobj)
430
431 def load(self):
432 """ This member function loads the networkinterfaces file.
433
434 Assumes networkinterfaces parser object is initialized with the
435 parser arguments
436 """
437 if self.interfacesfile == None:
438 self.logger.warn('no network interfaces file defined in ifupdown2.conf')
439 return
440
441 if self.interfacesfileformat == 'json':
442 return self.read_file_json(self.interfacesfile,
443 self.interfacesfileiobuf)
444 return self.read_file(self.interfacesfile,
445 self.interfacesfileiobuf)