]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - addons/usercmds.py
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
10 class usercmds(ifupdownaddons
.modulebase
.moduleBase
):
11 """ ifupdown2 addon module to configure user specified commands """
13 _modinfo
= {'mhelp' : 'user commands for interfaces',
16 {'help' : 'run command before bringing the interface up'},
18 {'help' : 'run command at interface bring up'},
20 {'help' : 'run command after interface bring up'},
22 {'help' : 'run command before bringing the interface down'},
24 {'help' : 'run command at interface down'},
26 {'help' : 'run command after bringing interface down'}}}
28 def _exec_user_cmd(self
, cmd
):
29 """ exec's commands using subprocess Popen
31 special wrapper using use closefds=True and shell=True
37 self
.logger
.info('executing %s' %cmd
)
40 ch
= subprocess
.Popen(cmd
,
41 stdout
=subprocess
.PIPE
,
43 stderr
=subprocess
.STDOUT
,
45 cmd_returncode
= ch
.wait()
46 cmdout
= ch
.communicate()[0]
48 raise Exception('failed to execute cmd \'%s\' (%s)'
50 if cmd_returncode
!= 0:
51 raise Exception(cmdout
)
54 def _run_command(self
, ifaceobj
, op
):
55 cmd_list
= ifaceobj
.get_attr_value(op
)
58 self
.logger
.info('executing cmd \'%s\'' %cmd
)
60 self
._exec
_user
_cmd
(cmd
)
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')))
67 _run_ops
= {'pre-up' : _run_command
,
68 'pre-down' : _run_command
,
70 'post-up' : _run_command
,
71 'down' : _run_command
,
72 'post-down' : _run_command
}
75 """ returns list of ops supported by this module """
76 return self
._run
_ops
.keys()
78 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None, **extra_args
):
82 **ifaceobj** (object): iface object
84 **operation** (str): list of ops
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.
94 op_handler
= self
._run
_ops
.get(operation
)
97 op_handler(self
, ifaceobj
, operation
)