From e5c61bdfef7a060120625ff50e302923753b2ef3 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 26 Apr 2026 19:38:17 +0200 Subject: utils: T8541: add write-config-file-value helper Introduce src/helpers/write-config-file-value.py to patch a saved config.boot from scripts by writing a quoted CLI path with an optional value (omit --value to create a valueless node). Extend vyos.utils.config.write_saved_value() to safely update saved configs: create missing parent nodes before tagging (ConfigTree.set_tag requires nodes to exist) and then write the requested value/valueless leaf back to disk. Remove the legacy serial console activation script which is now replaced by the helper-based approach. The helper is run once during ISO Image assembly and will inject the approprate serial console defintiions into config.boot.default. --- python/vyos/utils/config.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'python') diff --git a/python/vyos/utils/config.py b/python/vyos/utils/config.py index 1f067e91e..5bba69a61 100644 --- a/python/vyos/utils/config.py +++ b/python/vyos/utils/config.py @@ -44,6 +44,35 @@ def read_saved_value(path: list): return ' '.join(res) return res +def write_saved_value(path: list, value=None, config_path: str=config_file): + """Write or replace a node in a saved configuration. + + - If value is None, the node is treated as valueless. + - Tag nodes in the path are detected via XML reference tree and marked on + the ConfigTree prior to setting the node. + """ + if not isinstance(path, list) or not path: + raise ValueError('path must be a non-empty list') + + from vyos.configtree import ConfigTree + from vyos.utils.file import read_file + from vyos.utils.file import write_file + + config_string = read_file(config_path) + ct = ConfigTree(config_string) + + # ConfigTree.set_tag() requires the node to exist. + # Create missing nodes along the path so tag marking works even when + # writing a completely new subtree into config.boot. + for target in flag(path): + if not ct.exists(target): + ct.create_node(target) + + set_tags(ct, path) + + ct.set(path, value=value, replace=True) + write_file(config_path, ct.to_string()) + def flag(l: list) -> list: res = [l[0:i] for i,_ in enumerate(l, start=1)] return res -- cgit v1.2.3