]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/usercmds.py
addons: address: add l3_intf_arp_accept policy to control ARP_ACCEPT
[mirror_ifupdown2.git] / ifupdown2 / addons / usercmds.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6
7 import os
8
9 try:
10 from ifupdown2.ifupdown.utils import utils
11
12 from ifupdown2.ifupdownaddons.modulebase import moduleBase
13 except ImportError:
14 from ifupdown.utils import utils
15
16 from ifupdownaddons.modulebase import moduleBase
17
18
19 class usercmds(moduleBase):
20 """ ifupdown2 addon module to configure user specified commands """
21
22 _modinfo = {'mhelp' : 'user commands for interfaces',
23 'attrs' : {
24 'pre-up' :
25 {'help' : 'run command before bringing the interface up',
26 'multiline' : True},
27 'up' :
28 {'help' : 'run command at interface bring up',
29 'multiline' : True},
30 'post-up' :
31 {'help' : 'run command after interface bring up',
32 'multiline' : True},
33 'pre-down' :
34 {'help' : 'run command before bringing the interface down',
35 'multiline' : True},
36 'down' :
37 {'help' : 'run command at interface down',
38 'multiline' : True},
39 'post-down' :
40 {'help' : 'run command after bringing interface down',
41 'multiline' : True}}}
42
43 def _run_command(self, ifaceobj, op):
44 cmd_list = ifaceobj.get_attr_value(op)
45 if cmd_list:
46 os.environ['IFACE'] = ifaceobj.name if ifaceobj.name else ''
47 os.environ['LOGICAL'] = ifaceobj.name if ifaceobj.name else ''
48 os.environ['METHOD'] = ifaceobj.addr_method if ifaceobj.addr_method else ''
49 os.environ['ADDRFAM'] = ','.join(ifaceobj.addr_family) if ifaceobj.addr_family else ''
50 for cmd in cmd_list:
51 try:
52 utils.exec_user_command(cmd)
53 except Exception, e:
54 if not self.ignore_error(str(e)):
55 self.logger.warn('%s: %s %s' % (ifaceobj.name, op,
56 str(e).strip('\n')))
57 pass
58
59 def _query_check(self, ifaceobj, ifaceobjcurr):
60 if ifaceobj.config:
61 for ops in ['pre-up',
62 'up',
63 'post-up',
64 'pre-down',
65 'down',
66 'post-down']:
67 for cmd in ifaceobj.config.get(ops, []):
68 ifaceobjcurr.update_config_with_status(ops, cmd, -1)
69
70 _run_ops = {'pre-up' : _run_command,
71 'pre-down' : _run_command,
72 'up' : _run_command,
73 'post-up' : _run_command,
74 'down' : _run_command,
75 'post-down' : _run_command,
76 'query-checkcurr': _query_check}
77
78 def get_ops(self):
79 """ returns list of ops supported by this module """
80 return self._run_ops.keys()
81
82 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
83 """ run user commands
84
85 Args:
86 **ifaceobj** (object): iface object
87
88 **operation** (str): list of ops
89
90 Kwargs:
91 **query_ifaceobj** (object): query check ifaceobject. This is only
92 valid when op is 'query-checkcurr'. It is an object same as
93 ifaceobj, but contains running attribute values and its config
94 status. The modules can use it to return queried running state
95 of interfaces. status is success if the running state is same
96 as user required state in ifaceobj. error otherwise.
97 """
98 op_handler = self._run_ops.get(operation)
99 if not op_handler:
100 return
101 if operation == 'query-checkcurr':
102 op_handler(self, ifaceobj, query_ifaceobj)
103 else:
104 op_handler(self, ifaceobj, operation)