diff options
| author | Christian Breunig <christian@breunig.cc> | 2026-04-26 19:38:17 +0200 |
|---|---|---|
| committer | Christian Breunig <christian@breunig.cc> | 2026-04-26 19:38:17 +0200 |
| commit | e5c61bdfef7a060120625ff50e302923753b2ef3 (patch) | |
| tree | 3ee0ffdfb49573b6ba3ee30e20347c3563c1c9b6 /python | |
| parent | 0cc2bbfc54370cce2d2c7540fb155e464207f1be (diff) | |
| download | vyos-1x-e5c61bdfef7a060120625ff50e302923753b2ef3.tar.gz vyos-1x-e5c61bdfef7a060120625ff50e302923753b2ef3.zip | |
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.
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/utils/config.py | 29 |
1 files changed, 29 insertions, 0 deletions
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 |
