diff options
author | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-04-14 09:43:19 +0100 |
---|---|---|
committer | Thomas Mangin <thomas.mangin@exa.net.uk> | 2020-04-14 12:04:39 +0100 |
commit | fdfd050431f786f9c9b7bfdcb51b5e8aca3f79f5 (patch) | |
tree | a4f559e957a122f49b2574a4e4098cc3eb9ed503 /src | |
parent | 9389f3e1d7ee754fd654b4d6b88338754dab452f (diff) | |
download | vyos-1x-fdfd050431f786f9c9b7bfdcb51b5e8aca3f79f5.tar.gz vyos-1x-fdfd050431f786f9c9b7bfdcb51b5e8aca3f79f5.zip |
vrrp: T2223: move VRRP within ifconfig
Tidied up the code and moved it under VRRP in view to use
with show-interface (which has VRRP filtering)
No change in functionality
Diffstat (limited to 'src')
-rwxr-xr-x | src/conf_mode/vrrp.py | 21 | ||||
-rwxr-xr-x | src/op_mode/vrrp.py | 87 |
2 files changed, 16 insertions, 92 deletions
diff --git a/src/conf_mode/vrrp.py b/src/conf_mode/vrrp.py index b9b0405e2..f358891a5 100755 --- a/src/conf_mode/vrrp.py +++ b/src/conf_mode/vrrp.py @@ -22,16 +22,13 @@ from json import dumps from pathlib import Path import vyos.config -import vyos.keepalived from vyos import ConfigError from vyos.util import call from vyos.template import render +from vyos.ifconfig.vrrp import VRRP -daemon_file = "/etc/default/keepalived" -config_file = "/etc/keepalived/keepalived.conf" -config_dict_path = "/run/keepalived_config.dict" def get_config(): vrrp_groups = [] @@ -127,7 +124,7 @@ def get_config(): sync_groups.append(sync_group) # create a file with dict with proposed configuration - with open("{}.temp".format(config_dict_path), 'w') as dict_file: + with open("{}.temp".format(VRRP.location['vyos']), 'w') as dict_file: dict_file.write(dumps({'vrrp_groups': vrrp_groups, 'sync_groups': sync_groups})) return (vrrp_groups, sync_groups) @@ -212,9 +209,9 @@ def generate(data): # Filter out disabled groups vrrp_groups = list(filter(lambda x: x["disable"] is not True, vrrp_groups)) - render(config_file, 'vrrp/keepalived.conf.tmpl', - {"groups": vrrp_groups, "sync_groups": sync_groups}) - render(daemon_file, 'vrrp/daemon.tmpl', {}) + render(VRRP.location['config'], 'vrrp/keepalived.conf.tmpl', + {"groups": vrrp_groups, "sync_groups": sync_groups}) + render(VRRP.location['daemon'], 'vrrp/daemon.tmpl', {}) return None @@ -223,12 +220,12 @@ def apply(data): if vrrp_groups: # safely rename a temporary file with configuration dict try: - dict_file = Path("{}.temp".format(config_dict_path)) - dict_file.rename(Path(config_dict_path)) + dict_file = Path("{}.temp".format(VRRP.location['vyos'])) + dict_file.rename(Path(VRRP.location['vyos'])) except Exception as err: print("Unable to rename the file with keepalived config for FIFO pipe: {}".format(err)) - if not vyos.keepalived.vrrp_running(): + if not VRRP.is_running(): print("Starting the VRRP process") ret = call("sudo systemctl restart keepalived.service") else: @@ -241,7 +238,7 @@ def apply(data): # VRRP is removed in the commit print("Stopping the VRRP process") call("sudo systemctl stop keepalived.service") - os.unlink(config_file) + os.unlink(VRRP.location['daemon']) return None diff --git a/src/op_mode/vrrp.py b/src/op_mode/vrrp.py index 8a993f92c..923cfa4d4 100755 --- a/src/op_mode/vrrp.py +++ b/src/op_mode/vrrp.py @@ -24,81 +24,8 @@ import tabulate import vyos.keepalived import vyos.util -config_dict_path = '/run/keepalived_config.dict' - - -# get disabled instances from a config -def vrrp_get_disabled(): - # read the dictionary file with configuration - with open(config_dict_path, 'r') as dict_file: - vrrp_config_dict = json.load(dict_file) - vrrp_disabled = [] - # add disabled groups to the list - for vrrp_group in vrrp_config_dict['vrrp_groups']: - if vrrp_group['disable']: - vrrp_disabled.append([vrrp_group['name'], vrrp_group['interface'], vrrp_group['vrid'], 'DISABLED', '']) - # return list with disabled instances - return vrrp_disabled - - -def print_summary(): - try: - vyos.keepalived.force_json_dump() - # Wait for keepalived to produce the data - # Replace with inotify or similar if it proves problematic - time.sleep(0.2) - json_data = vyos.keepalived.get_json_data() - vyos.keepalived.remove_vrrp_data("json") - except: - print("VRRP information is not available") - sys.exit(1) - - groups = [] - for group in json_data: - data = group["data"] - - name = data["iname"] - - ltrans_timestamp = float(data["last_transition"]) - ltrans_time = vyos.util.seconds_to_human(int(time.time() - ltrans_timestamp)) - - interface = data["ifp_ifname"] - vrid = data["vrid"] - - state = vyos.keepalived.decode_state(data["state"]) - - row = [name, interface, vrid, state, ltrans_time] - groups.append(row) - - # add to the active list disabled instances - groups.extend(vrrp_get_disabled()) - headers = ["Name", "Interface", "VRID", "State", "Last Transition"] - output = tabulate.tabulate(groups, headers) - print(output) - - -def print_statistics(): - try: - vyos.keepalived.force_stats_dump() - time.sleep(0.2) - output = vyos.keepalived.get_statistics() - print(output) - vyos.keepalived.remove_vrrp_data("stats") - except: - print("VRRP statistics are not available") - sys.exit(1) - - -def print_state_data(): - try: - vyos.keepalived.force_state_data_dump() - time.sleep(0.2) - output = vyos.keepalived.get_state_data() - print(output) - vyos.keepalived.remove_vrrp_data("state") - except: - print("VRRP information is not available") - sys.exit(1) +from vyos.ifconfig.vrrp import VRRP +from vyos.ifconfig.vrrp import VRRPError parser = argparse.ArgumentParser() @@ -110,16 +37,16 @@ group.add_argument("-d", "--data", action="store_true", help="Print detailed VRR args = parser.parse_args() # Exit early if VRRP is dead or not configured -if not vyos.keepalived.vrrp_running(): - print("VRRP is not running") +if not VRRP.is_running(): + print('VRRP is not running') sys.exit(0) if args.summary: - print_summary() + print(VRRP.format(VRRP.collect('json'))) elif args.statistics: - print_statistics() + print(VRRP.collect('stats')) elif args.data: - print_state_data() + print(VRRP.collect('state')) else: parser.print_help() sys.exit(1) |