]> git.proxmox.com Git - mirror_ifupdown2.git/blame - pkg/scheduler.py
Fix splits everywhere to include space and tabs. Use regex split
[mirror_ifupdown2.git] / pkg / scheduler.py
CommitLineData
a6f80f0e 1#!/usr/bin/python
3e8ee54f 2#
3# Copyright 2013. Cumulus Networks, Inc.
4# Author: Roopa Prabhu, roopa@cumulusnetworks.com
5#
6# ifaceScheduler --
7# interface scheduler
8#
a6f80f0e 9
a6f80f0e 10from statemanager import *
11from iface import *
12from graph import *
13from collections import deque
14from collections import OrderedDict
a6f80f0e 15import logging
d08d5f54 16import traceback
69f58278 17import sys
a6f80f0e 18from graph import *
19from collections import deque
20from threading import *
21from ifupdownbase import *
22
d08d5f54 23class ifaceSchedulerFlags():
c798b0f4 24 INORDER = 0x1
25 POSTORDER = 0x2
d08d5f54 26
be0b20f2 27class ifaceScheduler():
28 """ scheduler functions to schedule configuration of interfaces.
a6f80f0e 29
a6f80f0e 30 supports scheduling of interfaces serially in plain interface list
31 or dependency graph format.
99b212b0 32
33 Algo:
34 - run topological sort on the iface objects
35 - In the sorted iface object list, pick up interfaces with no parents
36 and run ops on them and their children.
37 - If operation is up and user gave interface list (ie not all)
38 option, also see if there were upper-devices and run ops on them.
39 - if operation is down, dont down the interface if it still
40 has upperifaces present. The down operation is executed when the
41 last upperiface goes away. If force option is set, this rule does not
42 apply.
43 - run ops calls addon modules run operation passing the iface
44 object and op to each module.
45 - ops are [pre-up, up, post-up, pre-down, down,
46 post-down, query-running, query-check]
a6f80f0e 47 """
48
c798b0f4 49 _STATE_CHECK = True
50
be0b20f2 51 @classmethod
44a6ca06 52 def run_iface_op(cls, ifupdownobj, ifaceobj, op, cenv=None):
a6f80f0e 53 """ Runs sub operation on an interface """
62ddec8b 54 ifacename = ifaceobj.name
a6f80f0e 55
c798b0f4 56 if (cls._STATE_CHECK and
62ddec8b 57 (ifaceobj.state >= ifaceState.from_str(op)) and
58 (ifaceobj.status == ifaceStatus.SUCCESS)):
be0b20f2 59 ifupdownobj.logger.debug('%s: already in state %s' %(ifacename, op))
d08d5f54 60 return
20dd6242 61 if not ifupdownobj.ADDONS_ENABLE: return
44a6ca06
RP
62 if op == 'query-checkcurr':
63 query_ifaceobj=ifupdownobj.create_n_save_ifaceobjcurr(ifaceobj)
be0b20f2 64 for mname in ifupdownobj.module_ops.get(op):
37c0543d 65 m = ifupdownobj.modules.get(mname)
a6f80f0e 66 err = 0
67 try:
d08d5f54 68 if hasattr(m, 'run'):
a690dfae 69 msg = ('%s: %s : running module %s' %(ifacename, op, mname))
739f665b 70 if op == 'query-checkcurr':
d08d5f54 71 # Dont check curr if the interface object was
37c0543d 72 # auto generated
d08d5f54 73 if (ifaceobj.priv_flags & ifupdownobj.NOCONFIG):
37c0543d 74 continue
a690dfae 75 ifupdownobj.logger.debug(msg)
44a6ca06 76 m.run(ifaceobj, op, query_ifaceobj)
a6f80f0e 77 else:
a690dfae 78 ifupdownobj.logger.debug(msg)
d08d5f54 79 m.run(ifaceobj, op)
a6f80f0e 80 except Exception, e:
81 err = 1
be0b20f2 82 ifupdownobj.log_error(str(e))
a6f80f0e 83 finally:
31a5f4c3 84 if err:
85 ifaceobj.set_state_n_status(ifaceState.from_str(op),
86 ifaceStatus.ERROR)
d08d5f54 87 else:
31a5f4c3 88 ifaceobj.set_state_n_status(ifaceState.from_str(op),
89 ifaceStatus.SUCCESS)
6bd7fc74 90
91 if ifupdownobj.COMPAT_EXEC_SCRIPTS:
92 # execute /etc/network/ scripts
93 for mname in ifupdownobj.script_ops.get(op, []):
94 ifupdownobj.logger.debug('%s: %s : running script %s'
d08d5f54 95 %(ifacename, op, mname))
6bd7fc74 96 try:
97 ifupdownobj.exec_command(mname, cmdenv=cenv)
98 except Exception, e:
99 ifupdownobj.log_error(str(e))
37c0543d 100
be0b20f2 101 @classmethod
923290bd 102 def run_iface_list_ops(cls, ifupdownobj, ifaceobjs, ops):
103 """ Runs all operations on a list of interface
104 configurations for the same interface
105 """
a690dfae 106 # minor optimization. If operation is 'down', proceed only
107 # if interface exists in the system
923290bd 108 ifacename = ifaceobjs[0].name
109 if ('down' in ops[0] and
110 not ifupdownobj.link_exists(ifacename)):
525f0a30 111 ifupdownobj.logger.debug('%s: does not exist' %ifacename)
923290bd 112 # run posthook before you get out of here, so that
113 # appropriate cleanup is done
114 posthookfunc = ifupdownobj.sched_hooks.get('posthook')
115 if posthookfunc:
116 for ifaceobj in ifaceobjs:
117 ifaceobj.status = ifaceStatus.SUCCESS
118 posthookfunc(ifupdownobj, ifaceobj, 'down')
a690dfae 119 return
923290bd 120 for op in ops:
121 # first run ifupdownobj handlers. This is good enough
122 # for the first object in the list
123 handler = ifupdownobj.ops_handlers.get(op)
124 if handler:
125 if (not ifaceobjs[0].addr_method or
126 (ifaceobjs[0].addr_method and
127 ifaceobjs[0].addr_method != 'manual')):
128 handler(ifupdownobj, ifaceobjs[0])
129 for ifaceobj in ifaceobjs:
130 cls.run_iface_op(ifupdownobj, ifaceobj, op,
923290bd 131 cenv=ifupdownobj.generate_running_env(ifaceobj, op)
132 if ifupdownobj.COMPAT_EXEC_SCRIPTS else None)
133 posthookfunc = ifupdownobj.sched_hooks.get('posthook')
134 if posthookfunc:
135 posthookfunc(ifupdownobj, ifaceobj, op)
21c7daa7 136
137 @classmethod
fa3da4be 138 def _check_upperifaces(cls, ifupdownobj, ifaceobj, ops, parent,
139 followdependents=False):
99b212b0 140 """ Check if upperifaces are hanging off us and help caller decide
141 if he can proceed with the ops on this device
21c7daa7 142
99b212b0 143 Returns True or False indicating the caller to proceed with the
144 operation.
145 """
86fc62e2 146 # proceed only for down operation
147 if 'down' not in ops[0]:
148 return True
149
65c48517 150 if (ifupdownobj.FORCE or
151 not ifupdownobj.ADDONS_ENABLE or
86fc62e2 152 (not ifupdownobj.is_ifaceobj_noconfig(ifaceobj) and
33e106da
RP
153 ifupdownobj.config.get('warn_on_ifdown', '0') == '0' and
154 not ifupdownobj.ALL)):
99b212b0 155 return True
65c48517 156
62ddec8b 157 ulist = ifaceobj.upperifaces
99b212b0 158 if not ulist:
159 return True
33e106da 160
99b212b0 161 # Get the list of upper ifaces other than the parent
162 tmpulist = ([u for u in ulist if u != parent] if parent
163 else ulist)
164 if not tmpulist:
165 return True
166 # XXX: This is expensive. Find a cheaper way to do this.
167 # if any of the upperdevs are present,
168 # return false to the caller to skip this interface
169 for u in tmpulist:
170 if ifupdownobj.link_exists(u):
171 if not ifupdownobj.ALL:
86fc62e2 172 if ifupdownobj.is_ifaceobj_noconfig(ifaceobj):
173 ifupdownobj.logger.info('%s: skipping interface down,'
174 %ifaceobj.name + ' upperiface %s still around ' %u)
175 else:
176 ifupdownobj.logger.warn('%s: skipping interface down,'
177 %ifaceobj.name + ' upperiface %s still around ' %u)
99b212b0 178 return False
21c7daa7 179 return True
180
be0b20f2 181 @classmethod
182 def run_iface_graph(cls, ifupdownobj, ifacename, ops, parent=None,
d08d5f54 183 order=ifaceSchedulerFlags.POSTORDER,
184 followdependents=True):
6ef5bfa2 185 """ runs interface by traversing all nodes rooted at itself """
186
d08d5f54 187 # Each ifacename can have a list of iface objects
31a5f4c3 188 ifaceobjs = ifupdownobj.get_ifaceobjs(ifacename)
189 if not ifaceobjs:
d08d5f54 190 raise Exception('%s: not found' %ifacename)
a6f80f0e 191
d08d5f54 192 for ifaceobj in ifaceobjs:
ca3f4fc7 193 if not cls._check_upperifaces(ifupdownobj, ifaceobj,
194 ops, parent, followdependents):
21c7daa7 195 return
f3215127 196
923290bd 197 # If inorder, run the iface first and then its dependents
198 if order == ifaceSchedulerFlags.INORDER:
199 cls.run_iface_list_ops(ifupdownobj, ifaceobjs, ops)
200
201 for ifaceobj in ifaceobjs:
f3215127 202 # Run lowerifaces or dependents
62ddec8b 203 dlist = ifaceobj.lowerifaces
f3215127 204 if dlist:
fa3da4be 205 ifupdownobj.logger.debug('%s: found dependents %s'
206 %(ifacename, str(dlist)))
d08d5f54 207 try:
208 if not followdependents:
209 # XXX: this is yet another extra step,
210 # but is needed for interfaces that are
f3215127 211 # implicit dependents. even though we are asked to
212 # not follow dependents, we must follow the ones
213 # that dont have user given config. Because we own them
d08d5f54 214 new_dlist = [d for d in dlist
923290bd 215 if ifupdownobj.is_iface_noconfig(d)]
6ef5bfa2 216 if new_dlist:
be0b20f2 217 cls.run_iface_list(ifupdownobj, new_dlist, ops,
923290bd 218 ifacename, order, followdependents,
219 continueonfailure=False)
d08d5f54 220 else:
be0b20f2 221 cls.run_iface_list(ifupdownobj, dlist, ops,
f3215127 222 ifacename, order,
223 followdependents,
6ef5bfa2 224 continueonfailure=False)
d08d5f54 225 except Exception, e:
be0b20f2 226 if (ifupdownobj.ignore_error(str(e))):
d08d5f54 227 pass
228 else:
229 # Dont bring the iface up if children did not come up
a690dfae 230 ifaceobj.set_state_n_status(ifaceState.NEW,
923290bd 231 ifaceStatus.ERROR)
d08d5f54 232 raise
923290bd 233 if order == ifaceSchedulerFlags.POSTORDER:
234 cls.run_iface_list_ops(ifupdownobj, ifaceobjs, ops)
a6f80f0e 235
be0b20f2 236 @classmethod
237 def run_iface_list(cls, ifupdownobj, ifacenames,
f3215127 238 ops, parent=None, order=ifaceSchedulerFlags.POSTORDER,
6ef5bfa2 239 followdependents=True, continueonfailure=True):
d08d5f54 240 """ Runs interface list """
a6f80f0e 241
d08d5f54 242 for ifacename in ifacenames:
a6f80f0e 243 try:
be0b20f2 244 cls.run_iface_graph(ifupdownobj, ifacename, ops, parent,
d08d5f54 245 order, followdependents)
a6f80f0e 246 except Exception, e:
6ef5bfa2 247 if continueonfailure:
69f58278 248 if ifupdownobj.logger.isEnabledFor(logging.DEBUG):
249 traceback.print_tb(sys.exc_info()[2])
be0b20f2 250 ifupdownobj.logger.error('%s : %s' %(ifacename, str(e)))
d08d5f54 251 pass
252 else:
be0b20f2 253 if (ifupdownobj.ignore_error(str(e))):
6ef5bfa2 254 pass
255 else:
9dce3561 256 raise Exception('%s : (%s)' %(ifacename, str(e)))
d08d5f54 257
be0b20f2 258 @classmethod
c798b0f4 259 def run_iface_graph_upper(cls, ifupdownobj, ifacename, ops, parent=None,
260 followdependents=True, skip_root=False):
261 """ runs interface by traversing all nodes rooted at itself """
262
263 # Each ifacename can have a list of iface objects
264 ifaceobjs = ifupdownobj.get_ifaceobjs(ifacename)
265 if not ifaceobjs:
266 raise Exception('%s: not found' %ifacename)
267
923290bd 268 if not skip_root:
269 # run the iface first and then its upperifaces
270 cls.run_iface_list_ops(ifupdownobj, ifaceobjs, ops)
c798b0f4 271 for ifaceobj in ifaceobjs:
c798b0f4 272 # Run upperifaces
62ddec8b 273 ulist = ifaceobj.upperifaces
c798b0f4 274 if ulist:
fa3da4be 275 ifupdownobj.logger.debug('%s: found upperifaces %s'
276 %(ifacename, str(ulist)))
c798b0f4 277 try:
278 cls.run_iface_list_upper(ifupdownobj, ulist, ops,
279 ifacename,
280 followdependents,
281 continueonfailure=True)
282 except Exception, e:
283 if (ifupdownobj.ignore_error(str(e))):
284 pass
285 else:
286 raise
287
288 @classmethod
289 def run_iface_list_upper(cls, ifupdownobj, ifacenames,
290 ops, parent=None, followdependents=True,
291 continueonfailure=True, skip_root=False):
292 """ Runs interface list """
293
294 for ifacename in ifacenames:
295 try:
296 cls.run_iface_graph_upper(ifupdownobj, ifacename, ops, parent,
297 followdependents, skip_root)
298 except Exception, e:
299 if continueonfailure:
300 if ifupdownobj.logger.isEnabledFor(logging.DEBUG):
301 traceback.print_tb(sys.exc_info()[2])
302 ifupdownobj.logger.error('%s : %s' %(ifacename, str(e)))
303 pass
304 else:
305 if (ifupdownobj.ignore_error(str(e))):
306 pass
307 else:
9dce3561 308 raise Exception('%s : (%s)' %(ifacename, str(e)))
c798b0f4 309
5ee3e1a8
RP
310 @classmethod
311 def get_sorted_iface_list(cls, ifupdownobj, ifacenames, ops,
312 dependency_graph, indegrees=None):
313 if len(ifacenames) == 1:
314 return ifacenames
315 # Get a sorted list of all interfaces
316 if not indegrees:
317 indegrees = OrderedDict()
318 for ifacename in dependency_graph.keys():
319 indegrees[ifacename] = ifupdownobj.get_iface_refcnt(ifacename)
320 ifacenames_all_sorted = graph.topological_sort_graphs_all(
321 dependency_graph, indegrees)
322 # if ALL was set, return all interfaces
323 if ifupdownobj.ALL:
324 return ifacenames_all_sorted
325
326 # else return ifacenames passed as argument in sorted order
327 ifacenames_sorted = []
328 [ifacenames_sorted.append(ifacename)
329 for ifacename in ifacenames_all_sorted
330 if ifacename in ifacenames]
331 return ifacenames_sorted
332
c798b0f4 333 @classmethod
334 def sched_ifaces(cls, ifupdownobj, ifacenames, ops,
335 dependency_graph=None, indegrees=None,
d08d5f54 336 order=ifaceSchedulerFlags.POSTORDER,
337 followdependents=True):
338 """ Runs iface dependeny graph by visiting all the nodes
339
340 Parameters:
341 -----------
342 ifupdownobj : ifupdown object (used for getting and updating iface
343 object state)
344 dependency_graph : dependency graph in adjacency list
345 format (contains more than one dependency graph)
346 ops : list of operations to perform eg ['pre-up', 'up', 'post-up']
347
5ee3e1a8
RP
348 indegrees : indegree array if present is used to topologically sort
349 the graphs in the dependency_graph
d08d5f54 350 """
5ee3e1a8
RP
351 #
352 # Algo:
353 # if ALL/auto interfaces are specified,
354 # - walk the dependency tree in postorder or inorder depending
355 # on the operation.
356 # (This is to run interfaces correctly in order)
357 # else:
358 # - sort iface list if the ifaces belong to a "class"
359 # - else just run iface list in the order they were specified
360 #
361 # Run any upperifaces if available
362 #
363 followupperifaces = []
364 run_queue = []
365 skip_ifacesort = int(ifupdownobj.config.get('skip_ifacesort', '0'))
366 if not skip_ifacesort and not indegrees:
367 indegrees = OrderedDict()
368 for ifacename in dependency_graph.keys():
369 indegrees[ifacename] = ifupdownobj.get_iface_refcnt(ifacename)
c798b0f4 370
5ee3e1a8 371 if not ifupdownobj.ALL:
ca3f4fc7 372 # If there is any interface that does exist, maybe it is a
5ee3e1a8
RP
373 # logical interface and we have to followupperifaces when it
374 # comes up, so get that list.
ca3f4fc7 375 followupperifaces = (True if
376 [i for i in ifacenames
923290bd 377 if not ifupdownobj.link_exists(i)]
378 else False)
5ee3e1a8
RP
379 if not skip_ifacesort and ifupdownobj.IFACE_CLASS:
380 # sort interfaces only if allow class was specified and
381 # not skip_ifacesort
382 run_queue = cls.get_sorted_iface_list(ifupdownobj, ifacenames,
383 ops, dependency_graph, indegrees)
384 if run_queue and 'up' in ops[0]:
385 run_queue.reverse()
ba7b1d60 386 else:
5ee3e1a8
RP
387 # if -a is set, we dont really have to sort. We pick the interfaces
388 # that have no parents and
389 if not skip_ifacesort:
390 sorted_ifacenames = cls.get_sorted_iface_list(ifupdownobj,
391 ifacenames, ops, dependency_graph,
392 indegrees)
393 if sorted_ifacenames:
394 # pick interfaces that user asked
395 # and those that dont have any dependents first
396 [run_queue.append(ifacename)
397 for ifacename in sorted_ifacenames
398 if ifacename in ifacenames and
399 not indegrees.get(ifacename)]
400 ifupdownobj.logger.debug('graph roots (interfaces that ' +
401 'dont have dependents):' + ' %s' %str(run_queue))
402 else:
403 ifupdownobj.logger.warn('interface sort returned None')
404
405 # If queue not present, just run interfaces that were asked by the user
406 if not run_queue:
407 run_queue = list(ifacenames)
408 if 'down' in ops[0]:
409 run_queue.reverse()
410
411 # run interface list
412 ifupdownobj.logger.info('running interfaces: %s' %str(run_queue))
413 cls.run_iface_list(ifupdownobj, run_queue, ops,
414 parent=None, order=order,
415 followdependents=followdependents)
416 if (((not ifupdownobj.ALL and followdependents) or
417 followupperifaces) and
418 'up' in ops[0]):
419 # If user had given a set of interfaces to bring up
420 # try and execute 'up' on the upperifaces
421 ifupdownobj.logger.info('running upperifaces if available ..')
422 cls._STATE_CHECK = False
423 cls.run_iface_list_upper(ifupdownobj, ifacenames, ops,
424 skip_root=True)
425 cls._STATE_CHECK = True