diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/helpers/vyos-load-config.py | 99 | ||||
-rwxr-xr-x | src/helpers/vyos-merge-config.py | 47 |
2 files changed, 60 insertions, 86 deletions
diff --git a/src/helpers/vyos-load-config.py b/src/helpers/vyos-load-config.py index cd6bff0d4..01a6a88dc 100755 --- a/src/helpers/vyos-load-config.py +++ b/src/helpers/vyos-load-config.py @@ -16,84 +16,57 @@ # # -"""Load config file from within config session. -Config file specified by URI or path (without scheme prefix). -Example: load https://somewhere.net/some.config - or - load /tmp/some.config -""" - import os import sys -import gzip +import argparse import tempfile -import vyos.defaults -import vyos.remote -from vyos.configsource import ConfigSourceSession, VyOSError + +from vyos.remote import get_config_file +from vyos.config import Config from vyos.migrate import ConfigMigrate from vyos.migrate import ConfigMigrateError +from vyos.load_config import load as load_config -class LoadConfig(ConfigSourceSession): - """A subclass for calling 'loadFile'. - This does not belong in configsource.py, and only has a single caller. - """ - def load_config(self, path): - return self._run(['/bin/cli-shell-api','loadFile',path]) - -file_name = sys.argv[1] if len(sys.argv) > 1 else 'config.boot' -configdir = vyos.defaults.directories['config'] -protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp'] -def get_local_config(filename): - if os.path.isfile(filename): - fname = filename - elif os.path.isfile(os.path.join(configdir, filename)): - fname = os.path.join(configdir, filename) - else: - sys.exit(f"No such file '{filename}'") +parser = argparse.ArgumentParser() +parser.add_argument('config_file', help='config file to load') +parser.add_argument( + '--migrate', action='store_true', help='migrate config file before merge' +) - if fname.endswith('.gz'): - with gzip.open(fname, 'rb') as f: - try: - config_str = f.read().decode() - except OSError as e: - sys.exit(e) - else: - with open(fname, 'r') as f: - try: - config_str = f.read() - except OSError as e: - sys.exit(e) +args = parser.parse_args() - return config_str - -if any(file_name.startswith(f'{x}://') for x in protocols): - config_string = vyos.remote.get_remote_config(file_name) - if not config_string: - sys.exit(f"No such config file at '{file_name}'") -else: - config_string = get_local_config(file_name) +file_name = args.config_file -config = LoadConfig() +# pylint: disable=consider-using-with +file_path = tempfile.NamedTemporaryFile(delete=False).name +err = get_config_file(file_name, file_path) +if err: + os.remove(file_path) + sys.exit(err) -print(f"Loading configuration from '{file_name}'") +if args.migrate: + migrate = ConfigMigrate(file_path) + try: + migrate.run() + except ConfigMigrateError as e: + os.remove(file_path) + sys.exit(e) -with tempfile.NamedTemporaryFile() as fp: - with open(fp.name, 'w') as fd: - fd.write(config_string) +config = Config() - config_migrate = ConfigMigrate(fp.name) - try: - config_migrate.run() - except ConfigMigrateError as err: - sys.exit(err) +if config.vyconf_session is not None: + out, err = config.vyconf_session.load_config(file_path) + if err: + os.remove(file_path) + sys.exit(out) + print(out) +else: + load_config(file_path) - try: - config.load_config(fp.name) - except VyOSError as err: - sys.exit(err) +os.remove(file_path) if config.session_changed(): print("Load complete. Use 'commit' to make changes effective.") else: - print("No configuration changes to commit.") + print('No configuration changes to commit.') diff --git a/src/helpers/vyos-merge-config.py b/src/helpers/vyos-merge-config.py index 4879b0e9c..e8a696eb5 100755 --- a/src/helpers/vyos-merge-config.py +++ b/src/helpers/vyos-merge-config.py @@ -2,22 +2,27 @@ # Copyright VyOS maintainers and contributors <maintainers@vyos.io> # +# 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 library is distributed in the hope that it will be useful, +# 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 -# Lesser General Public License for more details. +# 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/>. +# # -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. import os import sys import shlex import argparse +import tempfile -from vyos.defaults import directories -from vyos.remote import get_remote_config +from vyos.remote import get_config_file from vyos.config import Config from vyos.configtree import ConfigTree from vyos.configtree import mask_inclusive @@ -42,28 +47,19 @@ args = parser.parse_args() file_name = args.config_file paths = [shlex.split(s) for s in args.paths] if args.paths else [] -configdir = directories['config'] - -protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp'] - -if any(file_name.startswith(f'{x}://') for x in protocols): - file_path = get_remote_config(file_name) - if not file_path: - sys.exit(f'No such file {file_name}') -else: - full_path = os.path.realpath(file_name) - if os.path.isfile(full_path): - file_path = full_path - else: - file_path = os.path.join(configdir, file_name) - if not os.path.isfile(file_path): - sys.exit(f'No such file {file_name}') +# pylint: disable=consider-using-with +file_path = tempfile.NamedTemporaryFile(delete=False).name +err = get_config_file(file_name, file_path) +if err: + os.remove(file_path) + sys.exit(err) if args.migrate: migrate = ConfigMigrate(file_path) try: migrate.run() except ConfigMigrateError as e: + os.remove(file_path) sys.exit(e) with open(file_path) as f: @@ -78,6 +74,9 @@ if paths: merge_ct = mask_inclusive(merge_ct, mask) +with open(file_path, 'w') as f: + f.write(merge_ct.to_string()) + config = Config() if config.vyconf_session is not None: @@ -85,6 +84,7 @@ if config.vyconf_session is not None: file_path, destructive=args.destructive ) if err: + os.remove(file_path) sys.exit(out) print(out) else: @@ -93,6 +93,7 @@ else: load_explicit(merge_res) +os.remove(file_path) if config.session_changed(): print("Merge complete. Use 'commit' to make changes effective.") |