diff options
author | John Estabrook <jestabro@vyos.io> | 2025-06-29 17:11:44 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2025-07-08 08:26:11 -0500 |
commit | 04a714fbf22c4accd9253f55826b0eefcd9f88fa (patch) | |
tree | fd856743aff1b0ebadf51308d591bf7dcea3c1e9 /python | |
parent | b43e3d7dd90c72decc9a01d58608878503a60212 (diff) | |
download | vyos-1x-04a714fbf22c4accd9253f55826b0eefcd9f88fa.tar.gz vyos-1x-04a714fbf22c4accd9253f55826b0eefcd9f88fa.zip |
T7499: add utility to download/uncompress config file, for load/merge
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/remote.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py index f6ab5c3f9..b73f486c0 100644 --- a/python/vyos/remote.py +++ b/python/vyos/remote.py @@ -22,6 +22,7 @@ import stat import sys import tempfile import urllib.parse +import gzip from contextlib import contextmanager from pathlib import Path @@ -44,6 +45,7 @@ from vyos.utils.misc import begin from vyos.utils.process import cmd, rc_cmd from vyos.version import get_version from vyos.base import Warning +from vyos.defaults import directories CHUNK_SIZE = 8192 @@ -478,3 +480,45 @@ def get_remote_config(urlstring, source_host='', source_port=0): return f.read() finally: os.remove(temp) + + +def get_config_file(file_in: str, file_out: str, source_host='', source_port=0): + protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp'] + config_dir = directories['config'] + + with tempfile.NamedTemporaryFile() as tmp_file: + if any(file_in.startswith(f'{x}://') for x in protocols): + try: + download( + tmp_file.name, + file_in, + check_space=True, + source_host='', + source_port=0, + raise_error=True, + ) + except Exception as e: + return e + file_name = tmp_file.name + else: + full_path = os.path.realpath(file_in) + if os.path.isfile(full_path): + file_in = full_path + else: + file_in = os.path.join(config_dir, file_in) + if not os.path.isfile(file_in): + return ValueError(f'No such file {file_in}') + + file_name = file_in + + if file_in.endswith('.gz'): + try: + with gzip.open(file_name, 'rb') as f_in: + with open(file_out, 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) + except Exception as e: + return e + else: + shutil.copyfile(file_name, file_out) + + return None |