diff options
-rw-r--r-- | python/vyos/migrator.py | 2 | ||||
-rwxr-xr-x | src/helpers/vyos-merge-config.py | 35 |
2 files changed, 29 insertions, 8 deletions
diff --git a/python/vyos/migrator.py b/python/vyos/migrator.py index 2d4bc7ffc..59d68f0f7 100644 --- a/python/vyos/migrator.py +++ b/python/vyos/migrator.py @@ -165,6 +165,8 @@ class Migrator(object): self.write_config_file_versions(rev_versions) + def config_changed(self): + return self._changed class VirtualMigrator(Migrator): def __init__(self, config_file, vintage='vyos'): diff --git a/src/helpers/vyos-merge-config.py b/src/helpers/vyos-merge-config.py index f0d5d1595..e9a14ae98 100755 --- a/src/helpers/vyos-merge-config.py +++ b/src/helpers/vyos-merge-config.py @@ -18,8 +18,10 @@ import sys import os import subprocess +import tempfile import vyos.defaults import vyos.remote +import vyos.migrator from vyos.config import Config from vyos.configtree import ConfigTree @@ -55,15 +57,25 @@ else: print(err) sys.exit(1) -path = None -if (len(sys.argv) > 2): - path = " ".join(sys.argv[2:]) +with tempfile.NamedTemporaryFile() as file_to_migrate: + with open(file_to_migrate.name, 'w') as fd: + fd.write(config_file) + + migration = vyos.migrator.Migrator(file_to_migrate.name) + migration.run() + if migration.config_changed(): + with open(file_to_migrate.name, 'r') as fd: + config_file = fd.read() merge_config_tree = ConfigTree(config_file) effective_config = Config() output_effective_config = effective_config.show_config() +# showConfig (called by config.show_config() does not escape +# backslashes, which configtree expects; cf. T1001. +output_effective_config = output_effective_config.replace("\\", "\\\\") + effective_config_tree = ConfigTree(output_effective_config) effective_cmds = effective_config_tree.to_commands() @@ -75,12 +87,19 @@ merge_cmd_list = merge_cmds.splitlines() effective_cmd_set = set(effective_cmd_list) add_cmds = [ cmd for cmd in merge_cmd_list if cmd not in effective_cmd_set ] -if path: - if not effective_config.exists(path): - print("path {} does not exist in running config; will use " - "root.".format(path)) +path = None +if (len(sys.argv) > 2): + path = sys.argv[2:] + if (not effective_config_tree.exists(path) and not + merge_config_tree.exists(path)): + print("path {} does not exist in either effective or merge" + " config; will use root.".format(path)) + path = None else: - add_cmds = [ cmd for cmd in add_cmds if path in cmd ] + path = " ".join(path) + +if path: + add_cmds = [ cmd for cmd in add_cmds if path in cmd ] for cmd in add_cmds: cmd = "/opt/vyatta/sbin/my_" + cmd |