diff options
Diffstat (limited to 'python/vyos')
-rw-r--r-- | python/vyos/config.py | 6 | ||||
-rw-r--r-- | python/vyos/migrator.py | 36 | ||||
-rw-r--r-- | python/vyos/util.py | 6 |
3 files changed, 45 insertions, 3 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py index 13b2c107e..e6a41a92d 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -69,6 +69,7 @@ import json import subprocess import vyos.configtree +import vyos.util class VyOSError(Exception): @@ -111,6 +112,11 @@ class Config(object): else: session_config_text = running_config_text + # The output of showConfig does not escape backslashes, as is expected + # by ConfigTree(). + session_config_text = vyos.util.escape_backslash(session_config_text) + running_config_text = vyos.util.escape_backslash(running_config_text) + self._session_config = vyos.configtree.ConfigTree(session_config_text) self._running_config = vyos.configtree.ConfigTree(running_config_text) diff --git a/python/vyos/migrator.py b/python/vyos/migrator.py index 59d68f0f7..86e1af183 100644 --- a/python/vyos/migrator.py +++ b/python/vyos/migrator.py @@ -30,6 +30,7 @@ class Migrator(object): self._force = force self._set_vintage = set_vintage self._config_file_vintage = None + self._log_file = None self._changed = False def read_config_file_versions(self): @@ -71,11 +72,30 @@ class Migrator(object): else: return True + def open_log_file(self): + """ + Open log file for migration, catching any error. + Note that, on boot, migration takes place before the canonical log + directory is created, hence write to the config file directory. + """ + self._log_file = os.path.join(vyos.defaults.directories['config'], + 'vyos-migrate.log') + try: + log = open('{0}'.format(self._log_file), 'w') + log.write("List of executed migration scripts:\n") + except Exception as e: + print("Logging error: {0}".format(e)) + return None + + return log + def run_migration_scripts(self, config_file_versions, system_versions): """ Run migration scripts iteratively, until config file version equals system component version. """ + log = self.open_log_file() + cfg_versions = config_file_versions sys_versions = system_versions @@ -101,18 +121,28 @@ class Migrator(object): '{}-to-{}'.format(cfg_ver, next_ver)) try: - subprocess.check_output([migrate_script, + subprocess.check_call([migrate_script, self._config_file]) except FileNotFoundError: pass - except subprocess.CalledProcessError as err: - print("Called process error: {}.".format(err)) + except Exception as err: + print("\nMigration script error: {0}: {1}." + "".format(migrate_script, err)) sys.exit(1) + if log: + try: + log.write('{0}\n'.format(migrate_script)) + except Exception as e: + print("Error writing log: {0}".format(e)) + cfg_ver = next_ver rev_versions[key] = cfg_ver + if log: + log.close() + return rev_versions def write_config_file_versions(self, cfg_versions): diff --git a/python/vyos/util.py b/python/vyos/util.py index 67a602f7a..659a702fd 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -199,3 +199,9 @@ def is_admin() -> bool: current_user = getpass.getuser() (_, _, _, admin_group_members) = grp.getgrnam('sudo') return current_user in admin_group_members + +def escape_backslash(string: str) -> str: + """Escape single backslashes in string that are not in escape sequence""" + p = re.compile(r'(?<!\\)[\\](?!b|f|n|r|t|\\[^bfnrt])') + result = p.sub(r'\\\\', string) + return result |