]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/usercmds.py
Merge 'vlan filtering bridge + vxlan + mlag + vrr' support from internal
[mirror_ifupdown2.git] / ifupdown2 / addons / usercmds.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6
7 import subprocess
8 import ifupdownaddons
9
10 class usercmds(ifupdownaddons.modulebase.moduleBase):
11 """ ifupdown2 addon module to configure user specified commands """
12
13 _modinfo = {'mhelp' : 'user commands for interfaces',
14 'attrs' : {
15 'pre-up' :
16 {'help' : 'run command before bringing the interface up'},
17 'up' :
18 {'help' : 'run command at interface bring up'},
19 'post-up' :
20 {'help' : 'run command after interface bring up'},
21 'pre-down' :
22 {'help' : 'run command before bringing the interface down'},
23 'down' :
24 {'help' : 'run command at interface down'},
25 'post-down' :
26 {'help' : 'run command after bringing interface down'}}}
27
28 def _exec_user_cmd(self, cmd):
29 """ exec's commands using subprocess Popen
30
31 special wrapper using use closefds=True and shell=True
32 for user commands
33 """
34
35 cmd_returncode = 0
36 try:
37 self.logger.info('executing %s' %cmd)
38 if self.DRYRUN:
39 return
40 ch = subprocess.Popen(cmd,
41 stdout=subprocess.PIPE,
42 shell=True,
43 stderr=subprocess.STDOUT,
44 close_fds=True)
45 cmd_returncode = ch.wait()
46 cmdout = ch.communicate()[0]
47 except Exception, e:
48 raise Exception('failed to execute cmd \'%s\' (%s)'
49 %(cmd, str(e)))
50 if cmd_returncode != 0:
51 raise Exception(cmdout)
52 return cmdout
53
54 def _run_command(self, ifaceobj, op):
55 cmd_list = ifaceobj.get_attr_value(op)
56 if cmd_list:
57 for cmd in cmd_list:
58 self.logger.info('executing cmd \'%s\'' %cmd)
59 try:
60 self._exec_user_cmd(cmd)
61 except Exception, e:
62 if not self.ignore_error(str(e)):
63 self.logger.warn('%s: %s cmd \'%s\' failed (%s)'
64 %(ifaceobj.name, op, cmd, str(e).strip('\n')))
65 pass
66
67 _run_ops = {'pre-up' : _run_command,
68 'pre-down' : _run_command,
69 'up' : _run_command,
70 'post-up' : _run_command,
71 'down' : _run_command,
72 'post-down' : _run_command}
73
74 def get_ops(self):
75 """ returns list of ops supported by this module """
76 return self._run_ops.keys()
77
78 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
79 """ run user commands
80
81 Args:
82 **ifaceobj** (object): iface object
83
84 **operation** (str): list of ops
85
86 Kwargs:
87 **query_ifaceobj** (object): query check ifaceobject. This is only
88 valid when op is 'query-checkcurr'. It is an object same as
89 ifaceobj, but contains running attribute values and its config
90 status. The modules can use it to return queried running state
91 of interfaces. status is success if the running state is same
92 as user required state in ifaceobj. error otherwise.
93 """
94 op_handler = self._run_ops.get(operation)
95 if not op_handler:
96 return
97 op_handler(self, ifaceobj, operation)