From 00cb7fd19587771129c9923a781488929c03f3f8 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Fri, 23 May 2025 10:40:40 -0500 Subject: T7488: add utility for automatic rollback of section on apply stage err --- src/helpers/reset_section.py | 102 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 src/helpers/reset_section.py (limited to 'src/helpers/reset_section.py') diff --git a/src/helpers/reset_section.py b/src/helpers/reset_section.py new file mode 100755 index 000000000..22b608f00 --- /dev/null +++ b/src/helpers/reset_section.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2025 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 . +# +# + + +import argparse +import sys +import os + +from vyos.configsession import ConfigSession +from vyos.config import Config +from vyos.configdiff import get_config_diff +from vyos.xml_ref import is_leaf + + +def type_str_to_list(value): + if isinstance(value, str): + return value.split() + raise argparse.ArgumentTypeError('path must be a whitespace separated string') + + +parser = argparse.ArgumentParser() +parser.add_argument('path', type=type_str_to_list, help='section to reload/rollback') +parser.add_argument('--pid', help='pid of config session') + +group = parser.add_mutually_exclusive_group() +group.add_argument('--reload', action='store_true', help='retry proposed commit') +group.add_argument( + '--rollback', action='store_true', default=True, help='rollback to stable commit' +) + +args = parser.parse_args() + +path = args.path +reload = args.reload +rollback = args.rollback +pid = args.pid + +try: + if is_leaf(path): + sys.exit('path is leaf node: neither allowed nor useful') +except ValueError: + sys.exit('nonexistent path: neither allowed nor useful') + +test = Config() +if not test.in_session(): + sys.exit('reset_section not available outside of a config session') + +diff = get_config_diff(test) +if not diff.is_node_changed(path): + # No discrepancies at path after commit, hence no error to revert. + sys.exit() + +del diff +del test + + +session_id = int(pid) if pid else os.getppid() + +# check hint left by vyshim when ConfigError is from apply stage +hint_name = f'/tmp/apply_{session_id}' +if not os.path.exists(hint_name): + # no apply error; exit + sys.exit() +else: + # cleanup hint and continue with reset + os.unlink(hint_name) + +session = ConfigSession(session_id, shared=True) + +session_env = session.get_session_env() +config = Config(session_env) + +effective = not bool(reload) + +d = config.get_config_dict(path, effective=effective, get_first_key=True) + +session.discard() + +session.delete(path) +session.commit() + +if not d: + # nothing more to do in either case of reload/rollback + sys.exit() + +session.set_section(path, d) +session.commit() -- cgit v1.2.3 From 4c1bffe71454c7f15ecc1fa27e4a3ecb1176361f Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 26 May 2025 09:11:52 -0500 Subject: T7488: allow reloads outside of config session --- src/helpers/reset_section.py | 60 ++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 21 deletions(-) (limited to 'src/helpers/reset_section.py') diff --git a/src/helpers/reset_section.py b/src/helpers/reset_section.py index 22b608f00..f1e4cfc6c 100755 --- a/src/helpers/reset_section.py +++ b/src/helpers/reset_section.py @@ -20,6 +20,7 @@ import argparse import sys import os +import grp from vyos.configsession import ConfigSession from vyos.config import Config @@ -27,6 +28,9 @@ from vyos.configdiff import get_config_diff from vyos.xml_ref import is_leaf +CFG_GROUP = 'vyattacfg' + + def type_str_to_list(value): if isinstance(value, str): return value.split() @@ -57,39 +61,52 @@ except ValueError: sys.exit('nonexistent path: neither allowed nor useful') test = Config() -if not test.in_session(): - sys.exit('reset_section not available outside of a config session') +in_session = test.in_session() -diff = get_config_diff(test) -if not diff.is_node_changed(path): - # No discrepancies at path after commit, hence no error to revert. - sys.exit() +if in_session: + if reload: + sys.exit('reset_section reload not available inside of a config session') + + diff = get_config_diff(test) + if not diff.is_node_changed(path): + # No discrepancies at path after commit, hence no error to revert. + sys.exit() + + del diff +else: + if not reload: + sys.exit('reset_section rollback not available outside of a config session') -del diff del test session_id = int(pid) if pid else os.getppid() -# check hint left by vyshim when ConfigError is from apply stage -hint_name = f'/tmp/apply_{session_id}' -if not os.path.exists(hint_name): - # no apply error; exit - sys.exit() -else: - # cleanup hint and continue with reset - os.unlink(hint_name) +if in_session: + # check hint left by vyshim when ConfigError is from apply stage + hint_name = f'/tmp/apply_{session_id}' + if not os.path.exists(hint_name): + # no apply error; exit + sys.exit() + else: + # cleanup hint and continue with reset + os.unlink(hint_name) + +cfg_group = grp.getgrnam(CFG_GROUP) +os.setgid(cfg_group.gr_gid) +os.umask(0o002) -session = ConfigSession(session_id, shared=True) +shared = not bool(reload) + +session = ConfigSession(session_id, shared=shared) session_env = session.get_session_env() config = Config(session_env) -effective = not bool(reload) - -d = config.get_config_dict(path, effective=effective, get_first_key=True) +d = config.get_config_dict(path, effective=True, get_first_key=True) -session.discard() +if in_session: + session.discard() session.delete(path) session.commit() @@ -99,4 +116,5 @@ if not d: sys.exit() session.set_section(path, d) -session.commit() +out = session.commit() +print(out) -- cgit v1.2.3 From 2ef19495e1a2750330a5a520f41601202ca2b703 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 10 Jun 2025 11:06:05 -0500 Subject: T7488: exit silently if path doesn't exist, unless debug --- src/helpers/reset_section.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/helpers/reset_section.py') diff --git a/src/helpers/reset_section.py b/src/helpers/reset_section.py index f1e4cfc6c..32857f650 100755 --- a/src/helpers/reset_section.py +++ b/src/helpers/reset_section.py @@ -29,6 +29,7 @@ from vyos.xml_ref import is_leaf CFG_GROUP = 'vyattacfg' +DEBUG = False def type_str_to_list(value): @@ -58,7 +59,10 @@ try: if is_leaf(path): sys.exit('path is leaf node: neither allowed nor useful') except ValueError: - sys.exit('nonexistent path: neither allowed nor useful') + if DEBUG: + sys.exit('nonexistent path: neither allowed nor useful') + else: + sys.exit() test = Config() in_session = test.in_session() -- cgit v1.2.3