diff options
| author | Christian Poessinger <christian@poessinger.com> | 2022-07-26 12:55:21 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-26 12:55:21 +0200 | 
| commit | 183dc5fc17b5b9fe5c02162bbca8d78883a01b2f (patch) | |
| tree | f0ff4dd8858ab1fdd132f22f59039d0740d39d00 | |
| parent | 8274e9706adf33544e4c990134e65a0ddee976d8 (diff) | |
| parent | e72795a11fe1ab79aade677a09f6f0f716421e86 (diff) | |
| download | vyos-1x-183dc5fc17b5b9fe5c02162bbca8d78883a01b2f.tar.gz vyos-1x-183dc5fc17b5b9fe5c02162bbca8d78883a01b2f.zip | |
Merge pull request #1435 from sever-sever/T4569
bridge: T4569: Rewrite show bridge script of vyos.opmode format
| -rw-r--r-- | op-mode-definitions/show-bridge.xml.in | 2 | ||||
| -rwxr-xr-x | src/op_mode/bridge.py | 104 | 
2 files changed, 105 insertions, 1 deletions
| diff --git a/op-mode-definitions/show-bridge.xml.in b/op-mode-definitions/show-bridge.xml.in index 0f8d3064d..8b857888b 100644 --- a/op-mode-definitions/show-bridge.xml.in +++ b/op-mode-definitions/show-bridge.xml.in @@ -19,7 +19,7 @@          <properties>            <help>Show bridging information</help>          </properties> -        <command>bridge -c link show</command> +        <command>${vyos_op_scripts_dir}/bridge.py show</command>        </leafNode>        <tagNode name="bridge">          <properties> diff --git a/src/op_mode/bridge.py b/src/op_mode/bridge.py new file mode 100755 index 000000000..4ab127156 --- /dev/null +++ b/src/op_mode/bridge.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2022 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program.  If not, see <http://www.gnu.org/licenses/>. + +import jmespath +import json +import sys + +from sys import exit +from tabulate import tabulate + +from vyos.util import cmd +from vyos.util import dict_search + +import vyos.opmode + + +def _get_json_data(): +    """ +    Get bridge data format JSON +    """ +    return cmd(f'sudo bridge --json link show') + + +def _get_raw_data_summary(): +    """Get interested rules +    :returns dict +    """ +    data = _get_json_data() +    data_dict = json.loads(data) +    return data_dict + + +def _get_bridge_members(bridge: str) -> list: +    """ +    Get list of interface bridge members +    :param bridge: str +    :default: ['n/a'] +    :return: list +    """ +    data = _get_raw_data_summary() +    members = jmespath.search(f'[?master == `{bridge}`].ifname', data) +    return [member for member in members] if members else ['n/a'] + + +def _get_member_options(bridge: str): +    data = _get_raw_data_summary() +    options = jmespath.search(f'[?master == `{bridge}`]', data) +    return options + + +def _get_formatted_output_summary(data): +    data_entries = '' +    bridges = set(jmespath.search('[*].master', data)) +    for bridge in bridges: +        member_options = _get_member_options(bridge) +        member_entries = [] +        for option in member_options: +            interface = option.get('ifname') +            ifindex = option.get('ifindex') +            state = option.get('state') +            mtu = option.get('mtu') +            flags = ','.join(option.get('flags')).lower() +            prio = option.get('priority') +            member_entries.append([interface, state, mtu, flags, prio]) +        member_headers = ["Member", "State", "MTU", "Flags", "Prio"] +        output_members = tabulate(member_entries, member_headers, numalign="left") +        output_bridge = f"""Bridge interface {bridge}: +{output_members} + +""" +        data_entries += output_bridge +    output = data_entries +    return output + + +def show(raw: bool): +    bridge_data = _get_raw_data_summary() +    if raw: +        return bridge_data +    else: +        return _get_formatted_output_summary(bridge_data) + + +if __name__ == '__main__': +    try: +        res = vyos.opmode.run(sys.modules[__name__]) +        if res: +            print(res) +    except ValueError as e: +        print(e) +        sys.exit(1) | 
