3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
17 from statemanager
import *
18 from networkinterfaces
import *
20 from scheduler
import *
21 from collections
import deque
22 from collections
import OrderedDict
26 class ifupdownMain(ifupdownBase
):
27 """ ifupdown2 main class """
32 COMPAT_EXEC_SCRIPTS
= False
33 STATEMANAGER_ENABLE
= True
34 STATEMANAGER_UPDATE
= True
37 # priv flags to mark iface objects
41 scripts_dir
='/etc/network'
42 addon_modules_dir
='/usr/share/ifupdownaddons'
43 addon_modules_configfile
='/etc/network/.addons.conf'
45 # iface dictionary in the below format:
46 # { '<ifacename>' : [<ifaceobject1>, <ifaceobject2> ..] }
48 # { 'swp1' : [<iface swp1>, <iface swp2> ..] }
50 # Each ifaceobject corresponds to a configuration block for
52 # The value in the dictionary is a list because the network
53 # interface configuration file supports more than one iface section
54 # in the interfaces file
55 ifaceobjdict
= OrderedDict()
57 # iface dictionary representing the curr running state of an iface
58 # in the below format:
59 # {'<ifacename>' : <ifaceobject>}
60 ifaceobjcurrdict
= OrderedDict()
62 # Dictionary representing operation and modules
64 module_ops
= OrderedDict([('pre-up', []),
67 ('query-checkcurr', []),
68 ('query-running', []),
69 ('query-dependency', []),
76 # For old style /etc/network/ bash scripts
77 script_ops
= OrderedDict([('pre-up', []),
84 # Handlers for ops that ifupdown2 owns
85 def run_up(self
, ifaceobj
):
86 ifacename
= ifaceobj
.name
87 if self
.link_exists(ifacename
):
88 self
.link_up(ifacename
)
90 def run_down(self
, ifaceobj
):
91 ifacename
= ifaceobj
.name
92 if self
.link_exists(ifacename
):
93 self
.link_down(ifacename
)
95 # ifupdown object interface operation handlers
96 ops_handlers
= OrderedDict([('up', run_up
),
99 def run_sched_ifaceobj_posthook(self
, ifaceobj
, op
):
100 if ((ifaceobj
.priv_flags
& self
.BUILTIN
) or
101 (ifaceobj
.priv_flags
& self
.NOCONFIG
)):
103 if self
.STATEMANAGER_UPDATE
:
104 self
.statemanager
.ifaceobj_sync(ifaceobj
, op
)
106 # ifupdown object interface scheduler pre and posthooks
107 sched_hooks
= {'posthook' : run_sched_ifaceobj_posthook
}
109 def __init__(self
, force
=False, dryrun
=False, nowait
=False,
110 perfmode
=False, withdepends
=False, njobs
=1,
111 cache
=False, addons_enable
=True, statemanager_enable
=True):
112 self
.logger
= logging
.getLogger('ifupdown')
116 self
.PERFMODE
= perfmode
117 self
.WITH_DEPENDS
= withdepends
118 self
.STATEMANAGER_ENABLE
= statemanager_enable
121 self
.logger
.debug("Roopa: DRYRUN = %s" %self
.DRYRUN
)
123 # Can be used to provide hints for caching
124 self
.CACHE_FLAGS
= 0x0
125 self
._DELETE
_DEPENDENT
_IFACES
_WITH
_NOCONFIG
= False
126 self
.ADDONS_ENABLE
= addons_enable
128 self
.ifaces
= OrderedDict()
130 self
.pp
= pprint
.PrettyPrinter(indent
=4)
131 self
.modules
= OrderedDict({})
132 self
.module_attrs
= {}
134 self
.load_addon_modules(self
.addon_modules_dir
)
135 if self
.COMPAT_EXEC_SCRIPTS
:
136 self
.load_scripts(self
.scripts_dir
)
137 self
.dependency_graph
= OrderedDict({})
139 if self
.STATEMANAGER_ENABLE
:
141 self
.statemanager
= stateManager()
142 self
.statemanager
.read_saved_state()
144 # XXX Maybe we should continue by ignoring old state
145 self
.logger
.warning('error reading state (%s)' %str
(e
))
148 self
.STATEMANAGER_UPDATE
= False
150 def get_ifaceobjs(self
, ifacename
):
151 return self
.ifaceobjdict
.get(ifacename
)
153 def get_ifaceobj_first(self
, ifacename
):
154 ifaceobjs
= self
.get_ifaceobjs(ifacename
)
159 def get_ifacenames(self
):
160 return self
.ifaceobjdict
.keys()
162 def get_iface_obj_last(self
, ifacename
):
163 return self
.ifaceobjdict
.get(ifacename
)[-1]
165 def create_n_save_ifaceobj(self
, ifacename
, priv_flags
=None,
167 """ creates a iface object and adds it to the iface dictionary """
169 ifaceobj
.name
= ifacename
170 ifaceobj
.priv_flags
= priv_flags
173 ifaceobj
.inc_refcnt()
174 self
.ifaceobjdict
[ifacename
] = [ifaceobj
]
177 def create_n_save_ifaceobjcurr(self
, ifaceobj
):
178 """ creates a copy of iface object and adds it to the iface dict containing current iface objects
180 ifaceobjcurr
= self
.get_ifaceobjcurr(ifaceobj
.name
)
183 ifaceobjcurr
= iface()
184 ifaceobjcurr
.name
= ifaceobj
.name
185 ifaceobjcurr
.lowerifaces
= ifaceobj
.lowerifaces
186 ifaceobjcurr
.priv_flags
= ifaceobj
.priv_flags
187 self
.ifaceobjcurrdict
[ifaceobj
.name
] = ifaceobjcurr
190 def get_ifaceobjcurr(self
, ifacename
):
191 return self
.ifaceobjcurrdict
.get(ifacename
)
193 def get_ifaceobjrunning(self
, ifacename
):
194 return self
.ifaceobjrunningdict
.get(ifacename
)
196 def get_iface_refcnt(self
, ifacename
):
197 """ Return iface ref count """
199 ifaceobjs
= self
.get_ifaceobjs(ifacename
)
207 def is_iface_builtin_byname(self
, ifacename
):
208 """ Returns true if iface name is a builtin interface.
210 A builtin interface is an interface which ifupdown understands.
211 The following are currently considered builtin ifaces:
212 - vlan interfaces in the format <ifacename>.<vlanid>
214 return '.' in ifacename
216 def is_ifaceobj_builtin(self
, ifaceobj
):
217 """ Returns true if iface name is a builtin interface.
219 A builtin interface is an interface which ifupdown understands.
220 The following are currently considered builtin ifaces:
221 - vlan interfaces in the format <ifacename>.<vlanid>
223 return (ifaceobj
.priv_flags
& self
.BUILTIN
)
225 def is_ifaceobj_noconfig(self
, ifaceobj
):
226 """ Returns true if iface object did not have a user defined config.
228 These interfaces appear only when they are dependents of interfaces
229 which have user defined config
231 return (ifaceobj
.priv_flags
& self
.NOCONFIG
)
233 def is_iface_noconfig(self
, ifacename
):
234 """ Returns true if iface has no config """
236 ifaceobj
= self
.get_ifaceobj_first(ifacename
)
237 if not ifaceobj
: return True
238 return self
.is_ifaceobj_noconfig(ifaceobj
)
240 def preprocess_dependency_list(self
, upperifacename
, dlist
, ops
):
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 dilist
= self
.get_ifaceobjs(d
)
260 if self
.is_iface_builtin_byname(d
):
261 self
.create_n_save_ifaceobj(d
, self
.BUILTIN | self
.NOCONFIG
,
262 True).add_to_upperifaces(upperifacename
)
263 elif not self
._DELETE
_DEPENDENT
_IFACES
_WITH
_NOCONFIG
:
264 self
.create_n_save_ifaceobj(d
, self
.NOCONFIG
,
265 True).add_to_upperifaces(upperifacename
)
271 di
.add_to_upperifaces(upperifacename
)
276 def query_dependents(self
, ifaceobj
, ops
):
277 """ Gets iface dependents by calling into respective modules """
280 # Get dependents for interface by querying respective modules
281 for mname
, module
in self
.modules
.items():
282 module
= self
.modules
.get(mname
)
283 if ops
[0] == 'query-running':
284 if (not hasattr(module
,
285 'get_dependent_ifacenames_running')):
287 dlist
= module
.get_dependent_ifacenames_running(ifaceobj
)
289 if (not hasattr(module
, 'get_dependent_ifacenames')):
291 dlist
= module
.get_dependent_ifacenames(ifaceobj
,
292 self
.ifaceobjdict
.keys())
294 self
.logger
.debug('%s: ' %ifaceobj
.name
+
295 'lowerifaces/dependents: %s' %str
(dlist
))
299 def populate_dependency_info(self
, ops
, ifacenames
=None):
300 """ recursive function to generate iface dependency info """
303 ifacenames
= self
.ifaceobjdict
.keys()
305 self
.logger
.debug('populating dependency info for %s' %str
(ifacenames
))
306 iqueue
= deque(ifacenames
)
309 # Go through all modules and find dependent ifaces
311 ifaceobj
= self
.get_ifaceobj_first(i
)
314 dlist
= ifaceobj
.lowerifaces
316 dlist
= self
.query_dependents(ifaceobj
, ops
)
320 self
.preprocess_dependency_list(ifaceobj
.name
,
322 self
.logger
.debug('%s: lowerifaces/dependents after processing: %s'
324 ifaceobj
.lowerifaces
= dlist
325 [iqueue
.append(d
) for d
in dlist
]
326 if not self
.dependency_graph
.get(i
):
327 self
.dependency_graph
[i
] = dlist
329 def _save_iface(self
, ifaceobj
):
330 currentifaceobjlist
= self
.ifaceobjdict
.get(ifaceobj
.name
)
331 if not currentifaceobjlist
:
332 self
.ifaceobjdict
[ifaceobj
.name
]= [ifaceobj
]
334 if ifaceobj
.compare(currentifaceobjlist
[0]):
335 self
.logger
.warn('duplicate interface %s found' %ifaceobj
.name
)
337 self
.ifaceobjdict
[ifaceobj
.name
].append(ifaceobj
)
339 def _module_syntax_checker(self
, attrname
, attrval
):
340 for m
, mdict
in self
.module_attrs
.items():
341 attrsdict
= mdict
.get('attrs')
342 if attrsdict
and attrname
in attrsdict
.keys():
346 def read_default_iface_config(self
):
347 """ Reads default network interface config /etc/network/interfaces. """
348 nifaces
= networkInterfaces()
349 nifaces
.subscribe('iface_found', self
._save
_iface
)
350 nifaces
.subscribe('validate', self
._module
_syntax
_checker
)
353 def read_iface_config(self
):
354 return self
.read_default_iface_config()
356 def read_old_iface_config(self
):
357 """ Reads the saved iface config instead of default iface config. """
358 self
.ifaceobjdict
= copy
.deepcopy(self
.statemanager
.ifaceobjdict
)
360 def _load_addon_modules_config(self
):
361 """ Load addon modules config file """
363 with
open(self
.addon_modules_configfile
, 'r') as f
:
364 lines
= f
.readlines()
366 litems
= l
.rstrip(' \n').split(',')
367 operation
= litems
[0]
369 self
.module_ops
[operation
].append(mname
)
371 def load_addon_modules(self
, modules_dir
):
372 """ load python modules from modules_dir
374 Default modules_dir is /usr/share/ifupdownmodules
377 self
.logger
.info('loading builtin modules from %s' %modules_dir
)
378 self
._load
_addon
_modules
_config
()
379 if not modules_dir
in sys
.path
:
380 sys
.path
.append(modules_dir
)
382 for op
, mlist
in self
.module_ops
.items():
384 if self
.modules
.get(mname
):
386 mpath
= modules_dir
+ '/' + mname
+ '.py'
387 if os
.path
.exists(mpath
):
389 m
= __import__(mname
)
390 mclass
= getattr(m
, mname
)
393 minstance
= mclass(force
=self
.FORCE
,
396 perfmode
=self
.PERFMODE
,
398 cacheflags
=self
.CACHE_FLAGS
)
399 self
.modules
[mname
] = minstance
401 self
.module_attrs
[mname
] = minstance
.get_modinfo()
407 # Assign all modules to query operations
408 self
.module_ops
['query-checkcurr'] = self
.modules
.keys()
409 self
.module_ops
['query-running'] = self
.modules
.keys()
410 self
.module_ops
['query-dependency'] = self
.modules
.keys()
411 self
.module_ops
['query'] = self
.modules
.keys()
412 self
.module_ops
['query-raw'] = self
.modules
.keys()
414 def _modules_help(self
):
415 """ Prints addon modules supported syntax """
418 for m
, mdict
in self
.module_attrs
.items():
421 print('%s: %s' %(m
, mdict
.get('mhelp')))
422 attrdict
= mdict
.get('attrs')
426 for attrname
, attrvaldict
in attrdict
.items():
427 if attrvaldict
.get('compat', False):
429 print('%s%s' %(indent
, attrname
))
430 print('%shelp: %s' %(indent
+ ' ',
431 attrvaldict
.get('help', '')))
432 print ('%srequired: %s' %(indent
+ ' ',
433 attrvaldict
.get('required', False)))
434 default
= attrvaldict
.get('default')
436 print('%sdefault: %s' %(indent
+ ' ', default
))
438 validrange
= attrvaldict
.get('validrange')
440 print('%svalidrange: %s'
441 %(indent
+ ' ', '-'.join(validrange
)))
443 validvals
= attrvaldict
.get('validvals')
445 print('%svalidvals: %s'
446 %(indent
+ ' ', ','.join(validvals
)))
448 examples
= attrvaldict
.get('example')
452 print '%sexample:' %(indent
+ ' ')
454 print '%s%s' %(indent
+ ' ', e
)
459 def load_scripts(self
, modules_dir
):
460 """ loading user modules from /etc/network/.
462 Note that previously loaded python modules override modules found
463 under /etc/network if any
467 self
.logger
.info('looking for user scripts under %s' %modules_dir
)
468 for op
, mlist
in self
.script_ops
.items():
469 msubdir
= modules_dir
+ '/if-%s.d' %op
470 self
.logger
.info('loading scripts under %s ...' %msubdir
)
472 module_list
= os
.listdir(msubdir
)
473 for module
in module_list
:
474 if self
.modules
.get(module
) is not None:
476 self
.script_ops
[op
].append(
477 msubdir
+ '/' + module
)
482 def _sched_ifaces(self
, ifacenames
, ops
):
483 self
.logger
.debug('scheduling \'%s\' for %s'
484 %(str(ops
), str(ifacenames
)))
486 self
._pretty
_print
_ordered
_dict
('dependency graph',
487 self
.dependency_graph
)
488 return ifaceScheduler
.sched_ifaces(self
, ifacenames
, ops
,
489 dependency_graph
=self
.dependency_graph
,
490 order
=ifaceSchedulerFlags
.INORDER
492 else ifaceSchedulerFlags
.POSTORDER
,
493 followdependents
=True if self
.WITH_DEPENDS
else False)
495 def _validate_ifaces(self
, ifacenames
):
496 """ validates interface list for config existance.
498 returns -1 if one or more interface not found. else, returns 0
503 ifaceobjs
= self
.get_ifaceobjs(i
)
507 self
.logger
.error('cannot find interfaces: %s' %err_iface
)
511 def _iface_whitelisted(self
, auto
, allow_classes
, excludepats
, ifacename
):
512 """ Checks if interface is whitelisted depending on set of parameters.
514 interfaces are checked against the allow_classes and auto lists.
519 for e
in excludepats
:
520 if re
.search(e
, ifacename
):
522 ifaceobjs
= self
.get_ifaceobjs(ifacename
)
524 self
.logger
.debug('iface %s' %ifacename
+ ' not found')
526 # We check classes first
530 common
= Set([allow_classes
]).intersection(
542 def _compat_conv_op_to_mode(self
, op
):
543 """ Returns old op name to work with existing scripts """
546 elif op
== 'pre-down':
551 def generate_running_env(self
, ifaceobj
, op
):
552 """ Generates a dictionary with env variables required for
553 an interface. Used to support script execution for interfaces.
557 iface_env
= ifaceobj
.env
561 cenv
.update(iface_env
)
564 cenv
['MODE'] = self
._compat
_conv
_op
_to
_mode
(op
)
567 def _save_state(self
):
568 if not self
.STATEMANAGER_ENABLE
or not self
.STATEMANAGER_UPDATE
:
571 # Update persistant iface states
572 self
.statemanager
.save_state()
574 if self
.logger
.isEnabledFor(logging
.DEBUG
):
575 t
= sys
.exc_info()[2]
576 traceback
.print_tb(t
)
577 self
.logger
.warning('error saving state (%s)' %str
(e
))
579 def up(self
, ops
, auto
=False, allow_classes
=None, ifacenames
=None,
580 excludepats
=None, printdependency
=None, syntaxcheck
=False):
581 """ up an interface """
583 if not self
.ADDONS_ENABLE
: self
.STATEMANAGER_UPDATE
= False
586 self
.WITH_DEPENDS
= True
588 self
.read_iface_config()
592 # If only syntax check was requested, return here
597 # If iface list is given by the caller, always check if iface
599 if not self
._validate
_ifaces
(ifacenames
):
600 raise Exception('all or some interfaces not found')
602 # if iface list not given by user, assume all from config file
603 if not ifacenames
: ifacenames
= self
.ifaceobjdict
.keys()
605 # filter interfaces based on auto and allow classes
606 filtered_ifacenames
= [i
for i
in ifacenames
607 if self
._iface
_whitelisted
(auto
, allow_classes
,
609 if not filtered_ifacenames
:
610 raise Exception('no ifaces found matching given allow lists')
613 self
.populate_dependency_info(ops
, filtered_ifacenames
)
614 self
.print_dependency(filtered_ifacenames
, printdependency
)
617 self
.populate_dependency_info(ops
)
620 self
._sched
_ifaces
(filtered_ifacenames
, ops
)
622 if not self
.DRYRUN
and self
.ADDONS_ENABLE
:
625 def down(self
, ops
, auto
=False, allow_classes
=None, ifacenames
=None,
626 excludepats
=None, printdependency
=None, usecurrentconfig
=False):
627 """ down an interface """
629 if not self
.ADDONS_ENABLE
: self
.STATEMANAGER_UPDATE
= False
632 self
.WITH_DEPENDS
= True
633 # For down we need to look at old state, unless usecurrentconfig
635 if (not usecurrentconfig
and self
.STATEMANAGER_ENABLE
and
636 self
.statemanager
.ifaceobjdict
):
637 # Since we are using state manager objects,
638 # skip the updating of state manager objects
639 self
.logger
.debug('Looking at old state ..')
640 self
.read_old_iface_config()
642 # If no old state available
643 self
.logger
.info('Loading current iface config file')
645 self
.read_iface_config()
647 raise Exception('error reading iface config (%s)' %str
(e
))
649 # If iface list is given by the caller, always check if iface
651 if not self
._validate
_ifaces
(ifacenames
):
652 raise Exception('interface(s) was probably never up')
653 # if iface list not given by user, assume all from config file
654 if not ifacenames
: ifacenames
= self
.ifaceobjdict
.keys()
656 # filter interfaces based on auto and allow classes
657 filtered_ifacenames
= [i
for i
in ifacenames
658 if self
._iface
_whitelisted
(auto
, allow_classes
,
660 if not filtered_ifacenames
:
661 raise Exception('no ifaces found matching given allow lists')
663 self
.populate_dependency_info(ops
, filtered_ifacenames
)
664 self
.print_dependency(filtered_ifacenames
, printdependency
)
667 self
.populate_dependency_info(ops
)
670 self
._sched
_ifaces
(filtered_ifacenames
, ops
)
672 if not self
.DRYRUN
and self
.ADDONS_ENABLE
:
675 def query(self
, ops
, auto
=False, allow_classes
=None, ifacenames
=None,
676 excludepats
=None, printdependency
=None,
678 """ query an interface """
680 if self
.STATEMANAGER_ENABLE
and ops
[0] == 'query-savedstate':
681 return self
.statemanager
.dump_pretty(ifacenames
)
683 self
.STATEMANAGER_UPDATE
= False
685 self
.logger
.debug('setting flag ALL')
687 self
.WITH_DEPENDS
= True
689 if ops
[0] == 'query-syntax':
692 elif ops
[0] == 'query-running':
693 # create fake devices to all dependents that dont have config
694 map(lambda i
: self
.create_n_save_ifaceobj(i
, self
.NOCONFIG
),
698 self
.read_iface_config()
702 if ifacenames
and ops
[0] != 'query-running':
703 # If iface list is given, always check if iface is present
704 if not self
._validate
_ifaces
(ifacenames
):
705 raise Exception('all or some interfaces not found')
707 # if iface list not given by user, assume all from config file
708 if not ifacenames
: ifacenames
= self
.ifaceobjdict
.keys()
710 # filter interfaces based on auto and allow classes
711 if ops
[0] == 'query-running':
712 filtered_ifacenames
= ifacenames
714 filtered_ifacenames
= [i
for i
in ifacenames
715 if self
._iface
_whitelisted
(auto
, allow_classes
,
717 if not filtered_ifacenames
:
718 raise Exception('no ifaces found matching ' +
721 if ops
[0] == 'query-dependency' and printdependency
:
722 self
.populate_dependency_info(ops
, filtered_ifacenames
)
723 self
.print_dependency(filtered_ifacenames
, printdependency
)
726 self
.populate_dependency_info(ops
)
728 if ops
[0] == 'query':
729 return self
.print_ifaceobjs_pretty(filtered_ifacenames
, format
)
730 elif ops
[0] == 'query-raw':
731 return self
.print_ifaceobjs_raw(filtered_ifacenames
)
733 self
._sched
_ifaces
(filtered_ifacenames
, ops
)
735 if ops
[0] == 'query-checkcurr':
736 ret
= self
.print_ifaceobjscurr_pretty(filtered_ifacenames
, format
)
738 # if any of the object has an error, signal that silently
740 elif ops
[0] == 'query-running':
741 self
.print_ifaceobjsrunning_pretty(filtered_ifacenames
, format
)
744 def reload(self
, upops
, downops
, auto
=False, allow
=None,
745 ifacenames
=None, excludepats
=None):
746 """ reload interface config """
750 self
.logger
.debug('reloading interface config ..')
753 self
.WITH_DEPENDS
= True
756 # Read the current interface config
757 self
.read_iface_config()
761 # generate dependency graph of interfaces
762 self
.populate_dependency_info(upops
)
764 # Save a copy of new iface objects and dependency_graph
765 new_ifaceobjdict
= dict(self
.ifaceobjdict
)
766 new_dependency_graph
= dict(self
.dependency_graph
)
768 if self
.STATEMANAGER_ENABLE
and self
.statemanager
.ifaceobjdict
:
769 # if old state is present, read old state and mark op for 'down'
770 # followed by 'up' aka: reload
771 # old interface config is read into self.ifaceobjdict
773 self
.read_old_iface_config()
776 # oldconfig not available, continue with 'up' with new config
779 if not ifacenames
: ifacenames
= self
.ifaceobjdict
.keys()
780 if op
== 'reload' and ifacenames
:
781 filtered_ifacenames
= [i
for i
in ifacenames
782 if self
._iface
_whitelisted
(auto
, allow_classes
,
784 # Generate the interface down list
785 # Interfaces that go into the down list:
786 # - interfaces that were present in last config and are not
787 # present in the new config
788 # - interfaces that were changed between the last and current
792 for ifname
, lastifaceobjlist
in self
.ifaceobjdict
.items():
794 # If interface is not present in the new file
795 # append it to the down list
796 newifaceobjlist
= new_ifaceobjdict
.get(ifname
)
797 if not newifaceobjlist
:
798 ifacedownlist
.append(ifname
)
800 # If interface has changed between the current file
801 # and the last installed append it to the down list
802 if len(newifaceobjlist
) != len(lastifaceobjlist
):
803 ifacedownlist
.append(ifname
)
805 # compare object list
806 for objidx
in range(0, len(lastifaceobjlist
)):
807 oldobj
= lastifaceobjlist
[objidx
]
808 newobj
= newifaceobjlist
[objidx
]
809 if not newobj
.compare(oldobj
):
810 ifacedownlist
.append(ifname
)
814 self
.logger
.info('Executing down on interfaces: %s'
816 # reinitialize dependency graph
817 self
.dependency_graph
= OrderedDict({})
818 # Generate dependency info for old config
819 self
.populate_dependency_info(downops
)
820 self
._sched
_ifaces
(ifacedownlist
, downops
)
822 self
.logger
.debug('no interfaces to down ..')
824 # Now, run 'up' with new config dict
825 # reset statemanager update flag to default
826 self
.ifaceobjdict
= new_ifaceobjdict
827 self
.dependency_graph
= new_dependency_graph
828 ifacenames
= self
.ifaceobjdict
.keys()
829 filtered_ifacenames
= [i
for i
in ifacenames
830 if self
._iface
_whitelisted
(auto
, allow_classes
,
833 self
.logger
.info('Scheduling up on interfaces: %s'
834 %str
(filtered_ifacenames
))
835 self
._sched
_ifaces
(filtered_ifacenames
, upops
)
840 def _pretty_print_ordered_dict(self
, prefix
, argdict
):
841 outbuf
= prefix
+ ' {\n'
842 for k
, vlist
in argdict
.items():
843 outbuf
+= '\t%s : %s\n' %(k
, str(vlist
))
844 self
.logger
.debug(outbuf
+ '}')
846 def print_dependency(self
, ifacenames
, format
):
847 """ prints iface dependency information """
850 ifacenames
= self
.ifaceobjdict
.keys()
852 for k
,v
in self
.dependency_graph
.items():
853 print '%s : %s' %(k
, str(v
))
854 elif format
== 'dot':
856 map(lambda i
: indegrees
.update({i
:
857 self
.get_iface_refcnt(i
)}),
858 self
.dependency_graph
.keys())
859 graph
.generate_dots(self
.dependency_graph
, indegrees
)
861 def print_ifaceobjs_raw(self
, ifacenames
):
862 """ prints raw lines for ifaces from config file """
865 for ifaceobj
in self
.get_ifaceobjs(i
):
866 if (self
.is_ifaceobj_builtin(ifaceobj
) or
867 not ifaceobj
.is_config_present()):
869 ifaceobj
.dump_raw(self
.logger
)
871 if self
.WITH_DEPENDS
and not self
.ALL
:
872 dlist
= ifaceobj
.lowerifaces
873 if not dlist
: continue
874 self
.print_ifaceobjs_raw(dlist
)
876 def print_ifaceobjs_pretty(self
, ifacenames
, format
='native'):
877 """ pretty prints iface in format given by keyword arg format """
880 for ifaceobj
in self
.get_ifaceobjs(i
):
881 if (self
.is_ifaceobj_noconfig(ifaceobj
)):
886 ifaceobj
.dump_pretty()
887 if self
.WITH_DEPENDS
and not self
.ALL
:
888 dlist
= ifaceobj
.lowerifaces
889 if not dlist
: continue
890 self
.print_ifaceobjs_pretty(dlist
, format
)
892 def print_ifaceobjscurr_pretty(self
, ifacenames
, format
='native'):
893 """ pretty prints current running state of interfaces with status.
895 returns 1 if any of the interface has an error,
901 ifaceobj
= self
.get_ifaceobjcurr(i
)
902 if not ifaceobj
: continue
903 if ifaceobj
.status
== ifaceStatus
.NOTFOUND
:
904 print 'iface %s (%s)\n' %(ifaceobj
.name
,
905 ifaceStatus
.to_str(ifaceStatus
.NOTFOUND
))
908 elif ifaceobj
.status
== ifaceStatus
.ERROR
:
910 if (self
.is_ifaceobj_noconfig(ifaceobj
)):
913 ifaceobj
.dump_json(with_status
=True)
915 ifaceobj
.dump_pretty(with_status
=True)
916 if self
.WITH_DEPENDS
and not self
.ALL
:
917 dlist
= ifaceobj
.lowerifaces
918 if not dlist
: continue
919 self
.print_ifaceobjscurr_pretty(dlist
, format
)
922 def print_ifaceobjsrunning_pretty(self
, ifacenames
, format
='native'):
923 """ pretty prints iface running state """
926 ifaceobj
= self
.get_ifaceobj_first(i
)
927 if ifaceobj
.status
== ifaceStatus
.NOTFOUND
:
928 print 'iface %s' %ifaceobj
.name
+ ' (not found)\n'
930 if not ifaceobj
.is_config_present():
935 ifaceobj
.dump_pretty()
936 if self
.WITH_DEPENDS
and not self
.ALL
:
937 dlist
= ifaceobj
.lowerifaces
938 if not dlist
: continue
939 self
.print_ifaceobjsrunning_pretty(dlist
, format
)
943 print 'ifupdown main object dump'
944 print self
.pp
.pprint(self
.modules
)
945 print self
.pp
.pprint(self
.ifaceobjdict
)
947 def _dump_ifaceobjs(self
, ifacenames
):
949 ifaceobjs
= self
.get_ifaceobjs(i
)