2019-12-17 16:40:10 +01:00
|
|
|
#!/usr/bin/env python3
|
2014-10-09 16:02:46 -07:00
|
|
|
#
|
2018-12-13 11:43:32 -08:00
|
|
|
# Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
|
2014-10-09 16:02:46 -07:00
|
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
|
|
#
|
|
|
|
|
2016-09-20 09:16:57 -07:00
|
|
|
import os
|
2016-04-10 18:55:56 +02:00
|
|
|
|
2018-12-13 11:43:32 -08:00
|
|
|
try:
|
|
|
|
from ifupdown2.ifupdown.utils import utils
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2018-12-13 11:43:32 -08:00
|
|
|
from ifupdown2.ifupdownaddons.modulebase import moduleBase
|
2019-12-17 17:25:32 +01:00
|
|
|
except (ImportError, ModuleNotFoundError):
|
2018-12-13 11:43:32 -08:00
|
|
|
from ifupdown.utils import utils
|
|
|
|
|
|
|
|
from ifupdownaddons.modulebase import moduleBase
|
|
|
|
|
|
|
|
|
|
|
|
class usercmds(moduleBase):
|
2014-10-09 16:02:46 -07:00
|
|
|
""" ifupdown2 addon module to configure user specified commands """
|
|
|
|
|
|
|
|
_modinfo = {'mhelp' : 'user commands for interfaces',
|
|
|
|
'attrs' : {
|
|
|
|
'pre-up' :
|
2016-05-31 13:12:21 -07:00
|
|
|
{'help' : 'run command before bringing the interface up',
|
|
|
|
'multiline' : True},
|
2014-10-09 16:02:46 -07:00
|
|
|
'up' :
|
2016-05-31 13:12:21 -07:00
|
|
|
{'help' : 'run command at interface bring up',
|
|
|
|
'multiline' : True},
|
2014-10-09 16:02:46 -07:00
|
|
|
'post-up' :
|
2016-05-31 13:12:21 -07:00
|
|
|
{'help' : 'run command after interface bring up',
|
|
|
|
'multiline' : True},
|
2014-10-09 16:02:46 -07:00
|
|
|
'pre-down' :
|
2016-05-31 13:12:21 -07:00
|
|
|
{'help' : 'run command before bringing the interface down',
|
|
|
|
'multiline' : True},
|
2014-10-09 16:02:46 -07:00
|
|
|
'down' :
|
2016-05-31 13:12:21 -07:00
|
|
|
{'help' : 'run command at interface down',
|
|
|
|
'multiline' : True},
|
2014-10-09 16:02:46 -07:00
|
|
|
'post-down' :
|
2016-05-31 13:12:21 -07:00
|
|
|
{'help' : 'run command after bringing interface down',
|
|
|
|
'multiline' : True}}}
|
2014-10-09 16:02:46 -07:00
|
|
|
|
|
|
|
def _run_command(self, ifaceobj, op):
|
|
|
|
cmd_list = ifaceobj.get_attr_value(op)
|
|
|
|
if cmd_list:
|
2021-07-06 12:38:22 +02:00
|
|
|
env = dict(os.environ)
|
|
|
|
env.update({
|
2021-04-20 18:42:40 +02:00
|
|
|
'LOGICAL': ifaceobj.name if ifaceobj.name else '',
|
|
|
|
'METHOD': ifaceobj.addr_method if ifaceobj.addr_method else '',
|
|
|
|
'ADDRFAM': ','.join(ifaceobj.addr_family) if ifaceobj.addr_family else ''
|
2021-07-06 12:38:22 +02:00
|
|
|
})
|
|
|
|
env.update(ifaceobj.get_env())
|
2014-10-09 16:02:46 -07:00
|
|
|
for cmd in cmd_list:
|
|
|
|
try:
|
2021-04-20 18:42:40 +02:00
|
|
|
utils.exec_user_command(cmd, env=env)
|
2019-12-17 16:55:49 +01:00
|
|
|
except Exception as e:
|
2014-10-09 16:02:46 -07:00
|
|
|
if not self.ignore_error(str(e)):
|
2019-12-09 22:31:46 +01:00
|
|
|
self.logger.warning('%s: %s %s' % (ifaceobj.name, op,
|
2016-05-13 19:52:57 +02:00
|
|
|
str(e).strip('\n')))
|
2014-10-09 16:02:46 -07:00
|
|
|
pass
|
|
|
|
|
2016-11-16 12:02:54 +01:00
|
|
|
def _query_check(self, ifaceobj, ifaceobjcurr):
|
|
|
|
if ifaceobj.config:
|
|
|
|
for ops in ['pre-up',
|
|
|
|
'up',
|
|
|
|
'post-up',
|
|
|
|
'pre-down',
|
|
|
|
'down',
|
|
|
|
'post-down']:
|
|
|
|
for cmd in ifaceobj.config.get(ops, []):
|
|
|
|
ifaceobjcurr.update_config_with_status(ops, cmd, -1)
|
|
|
|
|
2014-10-09 16:02:46 -07:00
|
|
|
_run_ops = {'pre-up' : _run_command,
|
|
|
|
'pre-down' : _run_command,
|
|
|
|
'up' : _run_command,
|
|
|
|
'post-up' : _run_command,
|
|
|
|
'down' : _run_command,
|
2016-11-16 12:02:54 +01:00
|
|
|
'post-down' : _run_command,
|
|
|
|
'query-checkcurr': _query_check}
|
2014-10-09 16:02:46 -07:00
|
|
|
|
|
|
|
def get_ops(self):
|
|
|
|
""" returns list of ops supported by this module """
|
2019-12-17 16:55:49 +01:00
|
|
|
return list(self._run_ops.keys())
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2014-10-24 10:11:07 -07:00
|
|
|
def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
|
2014-10-09 16:02:46 -07:00
|
|
|
""" run user commands
|
|
|
|
|
|
|
|
Args:
|
|
|
|
**ifaceobj** (object): iface object
|
|
|
|
|
|
|
|
**operation** (str): list of ops
|
|
|
|
|
|
|
|
Kwargs:
|
|
|
|
**query_ifaceobj** (object): query check ifaceobject. This is only
|
|
|
|
valid when op is 'query-checkcurr'. It is an object same as
|
|
|
|
ifaceobj, but contains running attribute values and its config
|
|
|
|
status. The modules can use it to return queried running state
|
|
|
|
of interfaces. status is success if the running state is same
|
|
|
|
as user required state in ifaceobj. error otherwise.
|
|
|
|
"""
|
|
|
|
op_handler = self._run_ops.get(operation)
|
|
|
|
if not op_handler:
|
|
|
|
return
|
2016-11-16 12:02:54 +01:00
|
|
|
if operation == 'query-checkcurr':
|
|
|
|
op_handler(self, ifaceobj, query_ifaceobj)
|
|
|
|
else:
|
|
|
|
op_handler(self, ifaceobj, operation)
|