From d41b9e56058c50117e70c7d2639ebbead466da27 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 15:12:06 -0500 Subject: migration: T2029: set default vintage; remove target from virtual The VirtualMigrator updates the syntax of the component version string. Remove the default target, allowing downgrade to old syntax. --- python/vyos/migrator.py | 5 +---- src/helpers/run-config-migration.py | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/python/vyos/migrator.py b/python/vyos/migrator.py index f05228041..e60d862ff 100644 --- a/python/vyos/migrator.py +++ b/python/vyos/migrator.py @@ -25,7 +25,7 @@ class MigratorError(Exception): pass class Migrator(object): - def __init__(self, config_file, force=False, set_vintage=None): + def __init__(self, config_file, force=False, set_vintage='vyos'): self._config_file = config_file self._force = force self._set_vintage = set_vintage @@ -204,9 +204,6 @@ class Migrator(object): return self._changed class VirtualMigrator(Migrator): - def __init__(self, config_file, vintage='vyos'): - super().__init__(config_file, set_vintage = vintage) - def run(self): cfg_file = self._config_file diff --git a/src/helpers/run-config-migration.py b/src/helpers/run-config-migration.py index a57a19cdf..6ffd038f3 100755 --- a/src/helpers/run-config-migration.py +++ b/src/helpers/run-config-migration.py @@ -72,7 +72,7 @@ def main(): migration = Migrator(config_file_name, force=force_on, set_vintage=vintage) else: - migration = VirtualMigrator(config_file_name) + migration = VirtualMigrator(config_file_name, set_vintage=vintage) migration.run() -- cgit v1.2.3 From 10b2b9d9bd9e8da76247651c996627a7921a04c3 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 15:13:48 -0500 Subject: migration: T2029: run virtual migration before migration Update syntax of component version string, before passing config file to configtree in migration scripts. --- python/vyos/migrator.py | 3 +-- src/helpers/run-config-migration.py | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/vyos/migrator.py b/python/vyos/migrator.py index e60d862ff..7d77e6bac 100644 --- a/python/vyos/migrator.py +++ b/python/vyos/migrator.py @@ -209,8 +209,7 @@ class VirtualMigrator(Migrator): cfg_versions = self.read_config_file_versions() if not cfg_versions: - raise MigratorError("Config file has no version information;" - " virtual migration not possible.") + return if self.update_vintage(): self._changed = True diff --git a/src/helpers/run-config-migration.py b/src/helpers/run-config-migration.py index 6ffd038f3..3c06e38f8 100755 --- a/src/helpers/run-config-migration.py +++ b/src/helpers/run-config-migration.py @@ -69,15 +69,22 @@ def main(): sys.exit(1) if not virtual: - migration = Migrator(config_file_name, force=force_on, - set_vintage=vintage) + virtual_migration = VirtualMigrator(config_file_name) + virtual_migration.run() + + migration = Migrator(config_file_name, force=force_on) + migration.run() + + if not migration.config_changed(): + os.remove(backup_file_name) else: - migration = VirtualMigrator(config_file_name, set_vintage=vintage) + virtual_migration = VirtualMigrator(config_file_name, + set_vintage=vintage) - migration.run() + virtual_migration.run() - if not migration._changed: - os.remove(backup_file_name) + if not virtual_migration.config_changed(): + os.remove(backup_file_name) if __name__ == '__main__': main() -- cgit v1.2.3 From 3bd140506ee9b250bf88a15ad9ad20ab905d3c54 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 15:14:28 -0500 Subject: migration: T2029: remove reference to default vintage in Migrator --- python/vyos/migrator.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/vyos/migrator.py b/python/vyos/migrator.py index 7d77e6bac..9a5fdef2f 100644 --- a/python/vyos/migrator.py +++ b/python/vyos/migrator.py @@ -61,9 +61,6 @@ class Migrator(object): if self._set_vintage: self._config_file_vintage = self._set_vintage - if not self._config_file_vintage: - self._config_file_vintage = vyos.defaults.cfg_vintage - if self._config_file_vintage not in ['vyatta', 'vyos']: raise MigratorError("Unknown vintage.") -- cgit v1.2.3 From 626e1a6f53b07a303bf6a17aa41534e41f106543 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 15:15:05 -0500 Subject: migration: T2029: process new version syntax in configtree --- python/vyos/configtree.py | 62 ++++++----------------------------------------- tests/data/config.valid | 4 +-- 2 files changed, 9 insertions(+), 57 deletions(-) diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 0274f3573..84c25ebec 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -24,58 +24,10 @@ def escape_backslash(string: str) -> str: result = p.sub(r'\\\\', string) return result -def strip_comments(s): - """ Split a config string into the config section and the trailing comments """ - INITIAL = 0 - IN_COMMENT = 1 - - i = len(s) - 1 - - state = INITIAL - - config_end = 0 - - # Find the first character of the comments section at the end, - # if it exists - while (i >= 0): - c = s[i] - - if (state == INITIAL) and re.match(r'\s', c): - # Ignore whitespace - if (i != 0): - i -= 1 - else: - config_end = 0 - break - elif (state == INITIAL) and not re.match(r'(\s|\/)', c): - # Assume there are no (more) trailing comments, - # this is an end of a node: either a brace of the last character - # of a leaf node value - config_end = i + 1 - break - elif (state == INITIAL) and (c == '/'): - # A comment begins, or it's a stray slash - if (s[i-1] == '*'): - state = IN_COMMENT - i -= 2 - else: - raise ValueError("Invalid syntax: stray slash at character {0}".format(i + 1)) - elif (state == IN_COMMENT) and (c == '*'): - # A comment ends here - try: - if (s[i-1] == '/'): - state = INITIAL - i -= 2 - except: - raise ValueError("Invalid syntax: malformed commend end at character {0}".format(i + 1)) - elif (state == IN_COMMENT) and (c != '*'): - # Ignore everything inside comments, including braces - i -= 1 - else: - # Shouldn't happen - raise ValueError("Invalid syntax at character {0}: invalid character {1}".format(i + 1, c)) - - return (s[0:config_end], s[config_end+1:]) +def strip_version(s): + """ Split a config string into the config section and the version string """ + t = re.split('(^//)', s, maxsplit=1, flags=re.MULTILINE) + return (t[0], ''.join(t[1:])) def check_path(path): # Necessary type checking @@ -174,7 +126,7 @@ class ConfigTree(object): self.__destroy = self.__lib.destroy self.__destroy.argtypes = [c_void_p] - config_section, comments_section = strip_comments(config_string) + config_section, version_section = strip_version(config_string) config_section = escape_backslash(config_section) config = self.__from_string(config_section.encode()) if config is None: @@ -182,7 +134,7 @@ class ConfigTree(object): raise ValueError("Failed to parse config: {0}".format(msg)) else: self.__config = config - self.__comments = comments_section + self.__version = version_section def __del__(self): if self.__config is not None: @@ -193,7 +145,7 @@ class ConfigTree(object): def to_string(self): config_string = self.__to_string(self.__config).decode() - config_string = "{0}\n{1}".format(config_string, self.__comments) + config_string = "{0}\n{1}".format(config_string, self.__version) return config_string def to_commands(self): diff --git a/tests/data/config.valid b/tests/data/config.valid index a21c6a4d1..1fbdd1505 100644 --- a/tests/data/config.valid +++ b/tests/data/config.valid @@ -35,5 +35,5 @@ empty-node { trailing-leaf-node-without-value -/* Trailing commend */ -/* Another trailing comment */ +// Trailing comment +// Another trailing comment -- cgit v1.2.3 From 66e0b6fa6e26786660912edd20df49c91ce13e40 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 15:15:56 -0500 Subject: migration: T2029: extract the version string instead of stripping --- python/vyos/configtree.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 84c25ebec..a0b0eb3c1 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -24,10 +24,10 @@ def escape_backslash(string: str) -> str: result = p.sub(r'\\\\', string) return result -def strip_version(s): - """ Split a config string into the config section and the version string """ +def extract_version(s): + """ Extract the version string from the config string """ t = re.split('(^//)', s, maxsplit=1, flags=re.MULTILINE) - return (t[0], ''.join(t[1:])) + return (s, ''.join(t[1:])) def check_path(path): # Necessary type checking @@ -126,7 +126,7 @@ class ConfigTree(object): self.__destroy = self.__lib.destroy self.__destroy.argtypes = [c_void_p] - config_section, version_section = strip_version(config_string) + config_section, version_section = extract_version(config_string) config_section = escape_backslash(config_section) config = self.__from_string(config_section.encode()) if config is None: -- cgit v1.2.3 From b8ff6641837c164b056324e9eca5f266023472d7 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 15:17:59 -0500 Subject: config merge: T2052: update for version string syntax change --- src/helpers/vyos-merge-config.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/helpers/vyos-merge-config.py b/src/helpers/vyos-merge-config.py index c5216daa6..10a5ea4bc 100755 --- a/src/helpers/vyos-merge-config.py +++ b/src/helpers/vyos-merge-config.py @@ -21,9 +21,9 @@ import subprocess import tempfile import vyos.defaults import vyos.remote -import vyos.migrator from vyos.config import Config from vyos.configtree import ConfigTree +from vyos.migrator import Migrator, VirtualMigrator if (len(sys.argv) < 2): @@ -61,9 +61,13 @@ 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) + virtual_migration = VirtualMigrator(file_to_migrate.name) + virtual_migration.run() + + migration = Migrator(file_to_migrate.name) migration.run() - if migration.config_changed(): + + if virtual_migration.config_changed() or migration.config_changed(): with open(file_to_migrate.name, 'r') as fd: config_file = fd.read() -- cgit v1.2.3 From 1c74d7ec8a239427c8f7e9f6d864c5085372b0e6 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 30 Mar 2020 16:32:14 -0500 Subject: config load: T2053: update for version string syntax change --- src/helpers/vyos-load-config.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/helpers/vyos-load-config.py b/src/helpers/vyos-load-config.py index 4e6d67efa..693529c23 100755 --- a/src/helpers/vyos-load-config.py +++ b/src/helpers/vyos-load-config.py @@ -28,7 +28,7 @@ import tempfile import vyos.defaults import vyos.remote from vyos.config import Config, VyOSError -from vyos.migrator import Migrator, MigratorError +from vyos.migrator import Migrator, VirtualMigrator, MigratorError system_config_file = 'config.boot' @@ -73,6 +73,12 @@ with tempfile.NamedTemporaryFile() as fp: with open(fp.name, 'w') as fd: fd.write(config_file) + virtual_migration = VirtualMigrator(fp.name) + try: + virtual_migration.run() + except MigratorError as err: + sys.exit('{}'.format(err)) + migration = Migrator(fp.name) try: migration.run() -- cgit v1.2.3