diff options
author | John Estabrook <jestabro@vyos.io> | 2022-03-08 11:48:29 -0600 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2022-03-08 11:48:29 -0600 |
commit | c4d389488970c8510200cac96a67182e9333b891 (patch) | |
tree | 9e29d3d5497ff116f8be559517e3078774227629 | |
parent | 534f677d36285863decb2cdff179687b4fd690cb (diff) | |
download | vyos-1x-c4d389488970c8510200cac96a67182e9333b891.tar.gz vyos-1x-c4d389488970c8510200cac96a67182e9333b891.zip |
save-config: T4292: rewrite vyatta-save-config.pl to Python
-rw-r--r-- | python/vyos/config.py | 5 | ||||
-rw-r--r-- | python/vyos/configsession.py | 2 | ||||
-rwxr-xr-x | src/helpers/vyos-save-config.py | 54 |
3 files changed, 60 insertions, 1 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py index a5c1ad122..829167820 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -93,6 +93,11 @@ class Config(object): (self._running_config, self._session_config) = self._config_source.get_configtree_tuple() + def get_config_tree(self, effective=False): + if effective: + return self._running_config + return self._session_config + def _make_path(self, path): # Backwards-compatibility stuff: original implementation used string paths # libvyosconfig paths are lists, but since node names cannot contain whitespace, diff --git a/python/vyos/configsession.py b/python/vyos/configsession.py index f28ad09c5..a7eda6e61 100644 --- a/python/vyos/configsession.py +++ b/python/vyos/configsession.py @@ -28,7 +28,7 @@ DISCARD = '/opt/vyatta/sbin/my_discard' SHOW_CONFIG = ['/bin/cli-shell-api', 'showConfig'] LOAD_CONFIG = ['/bin/cli-shell-api', 'loadFile'] MIGRATE_LOAD_CONFIG = ['/usr/libexec/vyos/vyos-load-config.py'] -SAVE_CONFIG = ['/opt/vyatta/sbin/vyatta-save-config.pl'] +SAVE_CONFIG = ['/usr/libexec/vyos/vyos-save-config.py'] INSTALL_IMAGE = ['/opt/vyatta/sbin/install-image', '--url'] REMOVE_IMAGE = ['/opt/vyatta/bin/vyatta-boot-image.pl', '--del'] GENERATE = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'generate'] diff --git a/src/helpers/vyos-save-config.py b/src/helpers/vyos-save-config.py new file mode 100755 index 000000000..628d510dd --- /dev/null +++ b/src/helpers/vyos-save-config.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2022 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# +import os +import re +import sys +from tempfile import NamedTemporaryFile + +from vyos.config import Config +from vyos.remote import urlc +from vyos.component_version import system_footer + +DEFAULT_CONFIG_FILE = '/opt/vyatta/etc/config/config.boot' +remote_save = None + +if len(sys.argv) > 1: + save_file = sys.argv[1] +else: + save_file = DEFAULT_CONFIG_FILE + +if re.match(r'\w+:/', save_file): + try: + remote_save = urlc(save_file) + except ValueError as e: + sys.exit(e) + +config = Config() +ct = config.get_config_tree(effective=True) + +write_file = save_file if remote_save is None else NamedTemporaryFile(delete=False).name +with open(write_file, 'w') as f: + f.write(ct.to_string()) + f.write("\n") + f.write(system_footer()) + +if remote_save is not None: + try: + remote_save.upload(write_file) + finally: + os.remove(write_file) |