]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/ifupdownmain.py
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
16 from statemanager
import *
17 from networkinterfaces
import *
19 from scheduler
import *
20 from collections
import deque
21 from collections
import OrderedDict
32 modules_dir
='/etc/network'
33 builtin_modules_dir
='/usr/share/ifupdownaddons'
35 # iface dictionary in the below format:
36 # { '<ifacename>' : [<ifaceobject1>, <ifaceobject2> ..] }
38 # { 'swp1' : [<ifaceobject1>, <ifaceobject2> ..] }
40 # Each ifaceobject corresponds to a configuration block for
42 ifaceobjdict
= OrderedDict()
45 # iface dictionary representing the curr running state of an iface
46 # in the below format:
47 # {'<ifacename>' : <ifaceobject>}
48 ifaceobjcurrdict
= OrderedDict()
50 # Dictionary representing operation, sub operation and modules
51 # for every sub operation
53 OrderedDict([('pre-up', OrderedDict({})),
54 ('up' , OrderedDict({})),
55 ('post-up' , OrderedDict({}))]),
57 OrderedDict([('query-checkcurr', OrderedDict({}))]),
60 OrderedDict([('query-running', OrderedDict({}))]),
63 OrderedDict([('pre-down', OrderedDict({})),
64 ('down' , OrderedDict({})),
65 ('post-down' , OrderedDict({}))])}
68 def __init__(self
, force
=False, dryrun
=False, nowait
=False,
69 perfmode
=False, nodepends
=False, njobs
=1,
70 format
='nwifaces', cache
=False):
71 self
.logger
= logging
.getLogger('ifupdown')
76 self
.PERFMODE
= perfmode
77 self
.NODEPENDS
= nodepends
79 self
._DELETE
_DEPENDENT
_IFACES
_WITH
_NOCONFIG
= True
81 self
.ifaces
= OrderedDict()
83 self
.pp
= pprint
.PrettyPrinter(indent
=4)
84 self
.load_modules_builtin(self
.builtin_modules_dir
)
85 self
.load_modules(self
.modules_dir
)
88 self
.statemanager
= stateManager()
89 self
.statemanager
.read_saved_state()
91 # XXX Maybe we should continue by ignoring old state
92 self
.logger
.warning('error reading state (%s)' %str
(e
))
95 def get_subops(self
, op
):
96 """ Returns sub-operation list """
97 return self
.operations
.get(op
).keys()
99 def compat_conv_op_to_mode(self
, op
):
100 """ Returns old op name to work with existing scripts """
108 def set_force(self
, force
):
109 """ Set force flag. """
111 self
.logger
.debug('setting force to true')
115 """ return force flag. """
118 def set_dryrun(self
, dryrun
):
120 self
.logger
.debug('setting dryrun to true')
123 def get_dryrun(self
):
129 def get_ifaceobjdict(self
):
130 return self
.ifaceobjdict
132 def set_ifaceobjdict(self
, ifaceobjdict
):
133 del self
.ifaceobjdict
134 self
.ifaceobjdict
= ifaceobjdict
136 def set_perfmode(self
, perfmode
):
138 self
.logger
.debug('setting perfmode to true')
139 self
.PERFMODE
= perfmode
141 def get_perfmode(self
):
144 def set_nowait(self
, nowait
):
146 self
.logger
.debug('setting dryrun to true')
149 def get_nowait(self
):
152 def set_njobs(self
, njobs
):
153 self
.logger
.debug('setting njobs to %d' %njobs
)
159 def get_nodepends(self
):
160 return self
.NODEPENDS
162 def set_nodepends(self
, nodepends
):
163 self
.logger
.debug('setting nodepends to true')
164 self
.NODEPENDS
= nodepends
166 def set_iface_state(self
, ifaceobj
, state
, status
):
167 ifaceobj
.set_state(state
)
168 ifaceobj
.set_status(status
)
169 self
.statemanager
.update_iface_state(ifaceobj
)
171 def get_iface_objs(self
, ifacename
):
172 return self
.ifaceobjdict
.get(ifacename
)
174 def get_iface_obj_first(self
, ifacename
):
175 ifaceobjs
= self
.get_iface_objs(ifacename
)
176 if ifaceobjs
is not None:
180 def get_iface_obj_last(self
, ifacename
):
181 return self
.ifaceobjdict
.get(ifacename
)[-1]
183 def create_ifaceobjcurr(self
, ifaceobj
):
184 ifacename
= ifaceobj
.get_name()
185 ifaceobjcurr
= self
.get_ifaceobjcurr(ifacename
)
186 if ifaceobjcurr
is not None:
189 ifaceobjcurr
= iface()
190 ifaceobjcurr
.set_name(ifacename
)
191 ifaceobjcurr
.set_dependents(ifaceobj
.get_dependents())
192 self
.ifaceobjcurrdict
[ifacename
] = ifaceobjcurr
196 def get_ifaceobjcurr(self
, ifacename
):
197 return self
.ifaceobjcurrdict
.get(ifacename
)
200 def get_ifaceobjrunning(self
, ifacename
):
201 return self
.ifaceobjrunningdict
.get(ifacename
)
203 def get_iface_status(self
, ifacename
):
204 ifaceobjs
= self
.get_iface_objs(ifacename
)
206 if i
.get_status() != ifaceStatus
.SUCCESS
:
207 return i
.get_status()
209 return ifaceStatus
.SUCCESS
211 def get_iface_refcnt(self
, ifacename
):
213 ifaceobjs
= self
.get_iface_objs(ifacename
)
215 if i
.get_refcnt() > max:
219 def create_n_save_ifaceobj(self
, ifacename
, increfcnt
=False):
220 """ creates and returns a fake vlan iface object.
221 This was added to support creation of simple vlan
222 devices without any user specified configuration.
225 ifaceobj
.set_name(ifacename
)
226 if increfcnt
== True:
227 ifaceobj
.inc_refcnt()
228 self
.ifaceobjdict
[ifacename
] = [ifaceobj
]
230 def is_vlan_device(self
, ifacename
):
231 """ Returns true if iface name is a vlan interface.
233 only supports vlan interfaces of the format <ifacename>.<vlanid>
236 if (re
.search(r
'\.', ifacename
, 0) is not None):
240 def preprocess_dependency_list(self
, dlist
, op
):
241 """ We go through the dependency list and
242 delete or add interfaces from the interfaces dict by
243 applying the following rules:
244 if flag _DELETE_DEPENDENT_IFACES_WITH_NOCONFIG is True:
245 we only consider devices whose configuration was
246 specified in the network interfaces file. We delete
247 any interface whose config was not specified except
248 for vlan devices. vlan devices get special treatment.
249 Even if they are not present they are created and added
251 elif flag _DELETE_DEPENDENT_IFACES_WITH_NOCONFIG is False:
252 we create objects for all dependent devices that are not
253 present in the ifacesdict
258 self
.logger
.debug('pre-processing dependency list: %s' %list(dlist
))
260 dilist
= self
.get_iface_objs(d
)
262 if (self
.is_vlan_device(d
) == True or
263 self
._DELETE
_DEPENDENT
_IFACES
_WITH
_NOCONFIG
== False):
264 create_list
.append(d
)
274 # create fake devices to all dependents that dont have config
275 map(lambda i
: self
.create_n_save_ifaceobj(i
, increfcnt
=True),
278 self
.logger
.debug('After Processing dependency list: %s'
281 def get_dependents(self
, ifaceobj
, op
):
282 """ Gets iface dependents by calling into respective modules """
285 self
.logger
.debug('%s: ' %ifaceobj
.get_name() + 'getting dependency')
287 # Get dependents for interface by querying respective modules
288 subopdict
= self
.operations
.get(op
)
289 for subop
, mdict
in subopdict
.items():
290 for mname
, mdata
in mdict
.items():
291 if mdata
.get('ftype') == 'pmodule':
292 module
= mdata
.get('module')
293 if op
== 'query-running':
295 'get_dependent_ifacenames_running') == False):
297 dlist
= module
.get_dependent_ifacenames_running(
301 'get_dependent_ifacenames') == False):
303 dlist
= module
.get_dependent_ifacenames(ifaceobj
,
304 self
.ifaceobjdict
.keys())
305 if dlist
is not None:
306 ifaceobj
.set_realdev_dependents(dlist
[:])
307 self
.logger
.debug('%s: ' %ifaceobj
.get_name() +
308 'got dependency list: %s' %str
(dlist
))
312 def generate_dependency_info(self
, ifacenames
, dependency_graph
, op
):
313 """ recursive function to generate iface dependency info """
315 self
.logger
.debug('generating dependency info for %s' %str
(ifacenames
))
317 iqueue
= deque(ifacenames
)
321 # Go through all modules and find dependent ifaces
323 ifaceobj
= self
.get_iface_obj_first(i
)
327 dlist
= ifaceobj
.get_dependents()
329 dlist
= self
.get_dependents(ifaceobj
, op
)
333 if dlist
is not None:
334 self
.preprocess_dependency_list(dlist
, op
)
335 ifaceobj
.set_dependents(dlist
)
336 [iqueue
.append(d
) for d
in dlist
]
338 if dependency_graph
.get(i
) is None:
339 dependency_graph
[i
] = dlist
341 def is_valid_state_transition(self
, ifname
, to_be_state
):
342 return self
.statemanager
.is_valid_state_transition(ifname
,
345 def save_iface(self
, ifaceobj
):
346 if self
.ifaceobjdict
.get(ifaceobj
.get_name()) is None:
347 self
.ifaceobjdict
[ifaceobj
.get_name()] = [ifaceobj
]
349 self
.ifaceobjdict
[ifaceobj
.get_name()].append(ifaceobj
)
351 def read_default_iface_config(self
):
352 """ Reads default network interface config /etc/network/interfaces. """
353 nifaces
= networkInterfaces()
354 nifaces
.subscribe('iface_found', self
.save_iface
)
357 def read_iface_config(self
):
358 return self
.read_default_iface_config()
360 def read_old_iface_config(self
):
361 """ Reads the saved iface config instead of default iface config. """
363 # Read it from the statemanager
364 self
.ifaceobjdict
= self
.statemanager
.get_ifaceobjdict()
367 def save_module(self
, mkind
, msubkind
, mname
, mftype
, module
):
368 """ saves a module into internal module dict for later use.
370 mtype - pre-up.d, post-up.d and so on
371 mftype - pmodule (python module), bashscript (bash script)
376 mmetadata
= self
.operations
[mkind
][msubkind
].get(mname
)
378 self
.logger
.warn('unsupported module type %s' %mname
)
381 if mmetadata
is None or mmetadata
.get('ftype') != 'pmodule':
383 mmetadata
['ftype'] = mftype
384 mmetadata
['module'] = module
385 self
.operations
[mkind
][msubkind
][mname
] = mmetadata
387 self
.logger
.debug('saved module %s' %mkind
+
388 ' %s' %mname
+ ' %s' %mftype
)
390 self
.logger
.info('ignoring module %s' %mkind
+ ' %s' %msubkind
+
391 ' %s' %mname
+ ' of type %s' %mftype
)
394 def load_modules_builtin(self
, modules_dir
):
395 """ load python modules from modules_dir
397 Default modules_dir is /usr/share/ifupdownmodules
401 self
.logger
.info('loading builtin modules from %s' %modules_dir
)
403 if not modules_dir
in sys
.path
:
404 sys
.path
.append(modules_dir
)
406 module_list
= os
.listdir(modules_dir
)
407 for module
in module_list
:
408 if re
.search('.*\.pyc', module
, 0) != None:
411 mname
, mext
= os
.path
.splitext(module
)
412 if mext
is not None and mext
== '.py':
413 self
.logger
.info('loading ' + modules_dir
+ '/' + module
)
415 m
= __import__(mname
)
416 mclass
= getattr(m
, mname
)
420 minstance
= mclass(force
=self
.get_force(),
421 dryrun
=self
.get_dryrun(),
422 nowait
=self
.get_nowait(),
423 perfmode
=self
.get_perfmode(),
424 cache
=self
.get_cache())
425 ops
= minstance
.get_ops()
427 if re
.search('query', op
) is not None:
428 self
.save_module(op
, op
, mname
, 'pmodule',
430 elif re
.search('up', op
) is not None:
431 self
.save_module('up', op
, mname
, 'pmodule',
434 self
.save_module('down', op
, mname
,
435 'pmodule', minstance
)
441 def load_modules(self
, modules_dir
):
442 """ loading user modules from /etc/network/.
444 Note that previously loaded python modules override modules found
445 under /etc/network if any
449 self
.logger
.info('loading user modules from %s' %modules_dir
)
450 for op
, subops
in self
.operations
.items():
451 if re
.search('query', op
) is not None:
454 for subop
in subops
.keys():
455 msubdir
= modules_dir
+ '/if-%s.d' %subop
456 self
.logger
.info('loading modules under %s ...' %msubdir
)
458 module_list
= os
.listdir(msubdir
)
459 for module
in module_list
:
460 if re
.search('.*\.pyc', module
, 0) != None:
463 mname
, mext
= os
.path
.splitext(module
)
464 if mext
is not None and mext
== '.py':
465 self
.logger
.debug('loading ' + msubdir
+ '/' + module
)
467 m
= imp
.load_source(module
,
468 msubdir
+ '/' + module
)
469 mclass
= getattr(m
, mname
)
473 self
.save_module(op
, subop
, mname
, 'pmodule',
476 self
.save_module(op
, subop
, mname
, 'script',
477 msubdir
+ '/' + module
)
481 #self.logger.debug('modules ...')
482 #self.pp.pprint(self.operations)
484 # For query, we add a special entry, basically use all 'up' modules
485 self
.operations
['query'] = self
.operations
.get('up')
488 def conv_iface_namelist_to_objlist(self
, intf_list
):
489 for intf
in intf_list
:
490 iface_obj
= self
.get_iface(intf
)
491 if iface_obj
== None:
492 raise ifupdownInvalidValue('no iface %s', intf
)
494 iface_objs
.append(iface_obj
)
499 def run_without_dependents(self
, op
, ifacenames
):
500 ifaceSched
= ifaceScheduler(force
=self
.FORCE
)
502 self
.logger
.debug('run_without_dependents for op %s' %op
+
503 ' for %s' %str
(ifacenames
))
505 if ifacenames
== None:
506 raise ifupdownInvalidValue('no interfaces found')
508 return ifaceSched
.run_iface_list(self
, ifacenames
, op
)
511 def run_with_dependents(self
, op
, ifacenames
):
512 dependency_graph
= {}
514 self
.logger
.debug('run_with_dependents for op %s'
515 %op
+ ' for %s' %str
(ifacenames
))
517 ifaceSched
= ifaceScheduler()
519 if ifacenames
is None:
520 ifacenames
= self
.ifaceobjdict
.keys()
522 # generate dependency graph of interfaces
523 self
.generate_dependency_info(ifacenames
, dependency_graph
, op
)
525 if self
.logger
.isEnabledFor(logging
.DEBUG
) == True:
526 self
.logger
.debug('dependency graph:')
527 self
.pp
.pprint(dependency_graph
)
530 ret
= ifaceSched
.run_iface_dependency_graph_parallel(self
,
531 dependency_graph
, op
)
533 ret
= ifaceSched
.run_iface_dependency_graph(self
,
534 dependency_graph
, op
)
537 def print_dependency(self
, op
, ifacenames
, format
):
538 dependency_graph
= {}
539 if ifacenames
is None:
540 ifacenames
= self
.ifaceobjdict
.keys()
542 # generate dependency graph of interfaces
543 self
._DELETE
_DEPENDENT
_IFACES
_WITH
_NOCONFIG
= False
544 self
.generate_dependency_info(ifacenames
, dependency_graph
, op
)
547 self
.pp
.pprint(dependency_graph
)
548 elif format
== 'dot':
550 map(lambda i
: indegrees
.update({i
:
551 self
.get_iface_refcnt(i
)}),
552 dependency_graph
.keys())
553 graph
.generate_dots(dependency_graph
, indegrees
)
555 def validate_ifaces(self
, ifacenames
):
556 """ validates interface list for config existance.
558 returns -1 if one or more interface not found. else, returns 0
564 ifaceobjs
= self
.get_iface_objs(i
)
565 if ifaceobjs
is None:
568 if len(err_iface
) != 0:
569 self
.logger
.error('did not find interfaces: %s' %err_iface
)
575 def iface_whitelisted(self
, auto
, allow_classes
, excludepats
, ifacename
):
576 """ Checks if interface is whitelisted depending on set of parameters.
579 interfaces are checked against the allow_classes and auto lists.
583 # If the interface matches
584 if excludepats
is not None and len(excludepats
) > 0:
585 for e
in excludepats
:
586 if re
.search(e
, ifacename
) is not None:
589 ifaceobjs
= self
.get_iface_objs(ifacename
)
590 if ifaceobjs
is None:
591 self
.logger
.debug('iface %s' %ifacename
+ ' not found')
594 # We check classes first
595 if allow_classes
is not None and len(allow_classes
) > 0:
597 if (len(i
.get_classes()) > 0):
598 common
= Set([allow_classes
]).intersection(
599 Set(i
.get_classes()))
606 if i
.get_auto() == True:
612 def generate_running_env(self
, ifaceobj
, op
):
613 """ Generates a dictionary with env variables required for
614 an interface. Used to support script execution for interfaces.
618 iface_env
= ifaceobj
.get_env()
619 if iface_env
is not None:
622 cenv
.update(iface_env
)
626 cenv
['MODE'] = self
.compat_conv_op_to_mode(op
)
631 def run(self
, op
, auto
=False, allow_classes
=None,
632 ifacenames
=None, excludepats
=None,
633 format
=None, printdependency
=None):
634 """ main ifupdown run method """
637 self
.logger
.debug('setting flag ALL')
640 # Only read new iface config for 'up'
641 # operations. For 'downs' we only rely on
643 if op
== 'query-running':
644 # create fake devices to all dependents that dont have config
645 map(lambda i
: self
.create_n_save_ifaceobj(i
), ifacenames
)
646 elif op
== 'up' or op
[:5] == 'query':
648 self
.read_iface_config()
652 # for down we need to look at old state
653 self
.logger
.debug('down op, looking at old state ..')
655 if len(self
.statemanager
.get_ifaceobjdict()) > 0:
656 self
.read_old_iface_config()
657 elif self
.FORCE
== True:
658 # If no old state available
659 self
.logger
.info('old state not available. ' +
660 'Force option set. Loading new iface config file')
662 self
.read_iface_config()
664 raise Exception('error reading iface config (%s)'
667 raise Exception('old state not available...aborting.' +
668 ' try running with --force option')
671 if ifacenames
is not None and op
!= 'query-running':
672 # If iface list is given, always check if iface is present
673 if self
.validate_ifaces(ifacenames
) != 0:
674 raise Exception('all or some interfaces not found')
676 # if iface list not given by user, assume all from config file
677 if ifacenames
is None: ifacenames
= self
.ifaceobjdict
.keys()
679 # filter interfaces based on auto and allow classes
680 if op
== 'query-running':
681 filtered_ifacenames
= ifacenames
683 filtered_ifacenames
= [i
for i
in ifacenames
684 if self
.iface_whitelisted(auto
, allow_classes
,
685 excludepats
, i
) == True]
687 if len(filtered_ifacenames
) == 0:
688 raise Exception('no ifaces found matching ' +
692 return self
.print_ifaceobjs_raw(filtered_ifacenames
)
693 elif op
== 'query-presumed':
694 return self
.print_ifaceobjs_saved_state_pretty(
696 elif op
== 'query-presumeddetailed':
697 return self
.print_ifaceobjs_saved_state_detailed_pretty(
700 if printdependency
is not None:
701 self
.print_dependency(op
, filtered_ifacenames
, printdependency
)
704 #if op.split('-')[0] == 'query' or self.NODEPENDS == True:
705 if op
.split('-')[0] == 'query' or self
.NODEPENDS
== True:
706 self
.run_without_dependents(op
, filtered_ifacenames
)
708 self
.run_with_dependents(op
, filtered_ifacenames
)
710 if op
== 'query-checkcurr':
711 # print curr state of all interfaces
712 ret
= self
.print_ifaceobjscurr_pretty(filtered_ifacenames
)
714 # if any of the object has an error, signal that silently
716 elif op
== 'query-running':
717 # print curr state of all interfaces
718 self
.print_ifaceobjsrunning_pretty(filtered_ifacenames
)
721 # Update persistant iface states
724 self
.statemanager
.flush_state(self
.ifaceobjdict
)
726 self
.statemanager
.flush_state()
728 if self
.logger
.isEnabledFor(logging
.DEBUG
):
729 t
= sys
.exc_info()[2]
730 traceback
.print_tb(t
)
731 self
.logger
.warning('error saving state (%s)' %str
(e
))
734 def up(self
, auto
=False, allow
=None, ifacenames
=None,
735 excludepats
=None, printdependency
=None):
736 return self
.run('up', auto
, allow
, ifacenames
,
737 excludepats
=excludepats
,
738 printdependency
=printdependency
)
740 def down(self
, auto
=False, allow
=None, ifacenames
=None, excludepats
=None):
741 return self
.run('down', auto
, allow
, ifacenames
,
742 excludepats
=excludepats
);
744 def query(self
, op
, auto
=False, allow_classes
=None, ifacenames
=None,
745 excludepats
=None, printdependency
=None,
747 """ main ifupdown run method """
749 self
.logger
.debug('setting flag ALL')
752 if op
== 'query-running':
753 self
._DELETE
_DEPENDENT
_IFACES
_WITH
_NOCONFIG
= False
754 # create fake devices to all dependents that dont have config
755 map(lambda i
: self
.create_n_save_ifaceobj(i
), ifacenames
)
758 self
.read_iface_config()
762 if ifacenames
is not None and op
!= 'query-running':
763 # If iface list is given, always check if iface is present
764 if self
.validate_ifaces(ifacenames
) != 0:
765 raise Exception('all or some interfaces not found')
767 # if iface list not given by user, assume all from config file
768 if ifacenames
is None: ifacenames
= self
.ifaceobjdict
.keys()
770 # filter interfaces based on auto and allow classes
771 if op
== 'query-running':
772 filtered_ifacenames
= ifacenames
774 filtered_ifacenames
= [i
for i
in ifacenames
775 if self
.iface_whitelisted(auto
, allow_classes
,
776 excludepats
, i
) == True]
778 if len(filtered_ifacenames
) == 0:
779 raise Exception('no ifaces found matching ' +
783 return self
.print_ifaceobjs_raw(filtered_ifacenames
)
784 elif op
== 'query-presumed':
785 return self
.print_ifaceobjs_saved_state_pretty(
787 elif op
== 'query-presumeddetailed':
788 return self
.print_ifaceobjs_saved_state_detailed_pretty(
791 if printdependency
is not None:
792 self
.print_dependency(op
, filtered_ifacenames
, printdependency
)
795 if self
.NODEPENDS
== True:
796 self
.run_without_dependents(op
, filtered_ifacenames
)
798 self
.run_with_dependents(op
, filtered_ifacenames
)
800 if op
== 'query-checkcurr':
801 ret
= self
.print_ifaceobjscurr_pretty(filtered_ifacenames
)
803 # if any of the object has an error, signal that silently
805 elif op
== 'query-running':
806 self
.print_ifaceobjsrunning_pretty(filtered_ifacenames
)
809 def reload(self
, auto
=False, allow
=None,
810 ifacenames
=None, excludepats
=None):
811 """ main ifupdown run method """
814 self
.logger
.debug('reloading interface config ..')
817 self
.logger
.debug('setting flag ALL')
821 self
.read_iface_config()
825 # Save a copy of new iface objects
826 new_ifaceobjdict
= self
.get_ifaceobjdict()
828 if len(self
.statemanager
.get_ifaceobjdict()) > 0:
829 # if old state is present, read old state and mark op for 'down'
830 # followed by 'up' aka: reload
831 self
.read_old_iface_config()
834 # oldconfig not available, continue with 'up' with new config
837 if ifacenames
is None: ifacenames
= self
.ifaceobjdict
.keys()
838 if (op
== 'reload' and ifacenames
is not None and
839 len(ifacenames
) != 0):
840 filtered_ifacenames
= [i
for i
in ifacenames
841 if self
.iface_whitelisted(auto
, allow_classes
,
842 excludepats
, i
) == True]
843 ifacedownlist
= Set(filtered_ifacenames
).difference(
844 Set(new_ifaceobjdict
.keys()))
845 if ifacedownlist
is not None and len(ifacedownlist
) > 0:
846 self
.logger
.debug('bringing down interfaces: %s'
848 if self
.NODEPENDS
== True:
849 self
.run_without_dependents('down', ifacedownlist
)
851 self
.run_with_dependents('down', ifacedownlist
)
853 # Update persistant iface states
856 self
.statemanager
.flush_state(self
.ifaceobjdict
)
858 self
.statemanager
.flush_state()
860 if self
.logger
.isEnabledFor(logging
.DEBUG
):
861 t
= sys
.exc_info()[2]
862 traceback
.print_tb(t
)
863 self
.logger
.warning('error saving state (%s)' %str
(e
))
865 # Now, run up with new dict
866 self
.set_ifaceobjdict(new_ifaceobjdict
)
867 ifacenames
= self
.ifaceobjdict
.keys()
868 filtered_ifacenames
= [i
for i
in ifacenames
869 if self
.iface_whitelisted(auto
, allow_classes
,
870 excludepats
, i
) == True]
872 self
.logger
.debug('bringing up interfaces: %s'
873 %str
(filtered_ifacenames
))
874 if self
.NODEPENDS
== True:
875 self
.run_without_dependents('up', filtered_ifacenames
)
877 self
.run_with_dependents('up', filtered_ifacenames
)
879 # Update persistant iface states
882 self
.statemanager
.flush_state(self
.get_ifaceobjdict())
884 self
.statemanager
.flush_state()
886 if self
.logger
.isEnabledFor(logging
.DEBUG
):
887 t
= sys
.exc_info()[2]
888 traceback
.print_tb(t
)
889 self
.logger
.warning('error saving state (%s)' %str
(e
))
892 """ all state dump """
894 print 'ifupdown object dump'
895 print self
.pp
.pprint(self
.modules
)
896 print self
.pp
.pprint(self
.ifaces
)
897 self
.state_manager
.dump()
899 def print_state(self
, ifacenames
=None):
900 self
.statemanager
.dump(ifacenames
)
902 def print_ifaceobjs_raw(self
, ifacenames
):
904 ifaceobjs
= self
.get_iface_objs(i
)
906 i
.dump_raw(self
.logger
)
909 def print_ifaceobjs_pretty(self
, ifacenames
):
911 ifaceobjs
= self
.get_iface_objs(i
)
913 i
.dump_pretty(self
.logger
)
916 def dump_ifaceobjs(self
, ifacenames
):
918 ifaceobjs
= self
.get_iface_objs(i
)
923 def print_ifaceobjscurr_pretty(self
, ifacenames
, format
=None):
924 """ Dumps current running state of interfaces.
926 returns 1 if any of the interface has an error,
931 ifaceobj
= self
.get_ifaceobjcurr(i
)
932 if ifaceobj
is None: continue
933 if ifaceobj
.get_status() == ifaceStatus
.NOTFOUND
:
934 print 'iface %s' %ifaceobj
.get_name() + ' (not found)'
937 elif ifaceobj
.get_status() == ifaceStatus
.ERROR
:
939 if format
is None or format
== 'nwifaces':
940 ifaceobj
.dump_pretty(self
.logger
)
942 ifaceobj
.dump_json(self
.logger
)
944 dlist
= ifaceobj
.get_dependents()
945 if dlist
is None or len(dlist
) == 0: continue
946 self
.print_ifaceobjscurr_pretty(dlist
, format
)
950 def print_ifaceobjsrunning_pretty(self
, ifacenames
, format
=None):
952 ifaceobj
= self
.get_iface_obj_first(i
)
953 if ifaceobj
.get_status() == ifaceStatus
.NOTFOUND
:
954 print 'iface %s' %ifaceobj
.get_name() + ' (not found)'
957 if format
is None or format
== 'nwifaces':
958 ifaceobj
.dump_pretty(self
.logger
)
959 elif format
== 'json':
960 ifaceobj
.dump_json(self
.logger
)
962 dlist
= ifaceobj
.get_dependents()
963 if dlist
is None or len(dlist
) == 0: continue
964 self
.print_ifaceobjsrunning_pretty(dlist
, format
)
967 def print_ifaceobjs_saved_state_pretty(self
, ifacenames
):
968 self
.statemanager
.print_state_pretty(ifacenames
, self
.logger
)
970 def print_ifaceobjs_saved_state_detailed_pretty(self
, ifacenames
):
971 self
.statemanager
.print_state_detailed_pretty(ifacenames
, self
.logger
)