]>
Commit | Line | Data |
---|---|---|
15ef32ea RP |
1 | #!/usr/bin/python |
2 | # | |
d486dd0d | 3 | # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved. |
15ef32ea RP |
4 | # Author: Roopa Prabhu, roopa@cumulusnetworks.com |
5 | # | |
6 | ||
cd45e629 | 7 | import os |
a4a53f4b | 8 | |
d486dd0d JF |
9 | try: |
10 | from ifupdown2.ifupdown.utils import utils | |
15ef32ea | 11 | |
d486dd0d JF |
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): | |
15ef32ea RP |
20 | """ ifupdown2 addon module to configure user specified commands """ |
21 | ||
22 | _modinfo = {'mhelp' : 'user commands for interfaces', | |
23 | 'attrs' : { | |
24 | 'pre-up' : | |
c6370b56 DW |
25 | {'help' : 'run command before bringing the interface up', |
26 | 'multiline' : True}, | |
15ef32ea | 27 | 'up' : |
c6370b56 DW |
28 | {'help' : 'run command at interface bring up', |
29 | 'multiline' : True}, | |
15ef32ea | 30 | 'post-up' : |
c6370b56 DW |
31 | {'help' : 'run command after interface bring up', |
32 | 'multiline' : True}, | |
15ef32ea | 33 | 'pre-down' : |
c6370b56 DW |
34 | {'help' : 'run command before bringing the interface down', |
35 | 'multiline' : True}, | |
15ef32ea | 36 | 'down' : |
c6370b56 DW |
37 | {'help' : 'run command at interface down', |
38 | 'multiline' : True}, | |
15ef32ea | 39 | 'post-down' : |
c6370b56 DW |
40 | {'help' : 'run command after bringing interface down', |
41 | 'multiline' : True}}} | |
15ef32ea | 42 | |
15ef32ea RP |
43 | def _run_command(self, ifaceobj, op): |
44 | cmd_list = ifaceobj.get_attr_value(op) | |
45 | if cmd_list: | |
cd45e629 JF |
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 '' | |
004d1e65 | 49 | os.environ['ADDRFAM'] = ','.join(ifaceobj.addr_family) if ifaceobj.addr_family else '' |
15ef32ea | 50 | for cmd in cmd_list: |
15ef32ea | 51 | try: |
a193d8d1 | 52 | utils.exec_user_command(cmd) |
15ef32ea RP |
53 | except Exception, e: |
54 | if not self.ignore_error(str(e)): | |
a193d8d1 JF |
55 | self.logger.warn('%s: %s %s' % (ifaceobj.name, op, |
56 | str(e).strip('\n'))) | |
15ef32ea RP |
57 | pass |
58 | ||
dbac1c09 JF |
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 | ||
15ef32ea RP |
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, | |
dbac1c09 JF |
75 | 'post-down' : _run_command, |
76 | 'query-checkcurr': _query_check} | |
15ef32ea RP |
77 | |
78 | def get_ops(self): | |
79 | """ returns list of ops supported by this module """ | |
80 | return self._run_ops.keys() | |
81 | ||
84ca006f | 82 | def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args): |
15ef32ea RP |
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 | |
dbac1c09 JF |
101 | if operation == 'query-checkcurr': |
102 | op_handler(self, ifaceobj, query_ifaceobj) | |
103 | else: | |
104 | op_handler(self, ifaceobj, operation) |