Page MenuHomeVyOS Platform

No OneTemporary

Size
44 KB
Referenced Files
None
Subscribers
None
diff --git a/python/vyos/config_mgmt.py b/python/vyos/config_mgmt.py
index fd0fa7a75..e57129ce5 100644
--- a/python/vyos/config_mgmt.py
+++ b/python/vyos/config_mgmt.py
@@ -1,736 +1,729 @@
# Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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.
#
# 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 re
import sys
import gzip
import logging
from typing import Optional, Tuple, Union
from filecmp import cmp
from datetime import datetime
from textwrap import dedent, indent
from pathlib import Path
from tabulate import tabulate
from shutil import copy, chown
from urllib.parse import urlsplit, urlunsplit
from vyos.config import Config
from vyos.configtree import ConfigTree, ConfigTreeError, show_diff
from vyos.defaults import directories
from vyos.version import get_full_version_data
from vyos.utils.io import ask_yes_no
from vyos.utils.boot import boot_configuration_complete
from vyos.utils.process import is_systemd_service_active
from vyos.utils.process import rc_cmd
SAVE_CONFIG = '/usr/libexec/vyos/vyos-save-config.py'
config_json = '/run/vyatta/config/config.json'
# created by vyatta-cfg-postinst
commit_post_hook_dir = '/etc/commit/post-hooks.d'
commit_hooks = {'commit_revision': '01vyos-commit-revision',
'commit_archive': '02vyos-commit-archive'}
DEFAULT_TIME_MINUTES = 10
timer_name = 'commit-confirm'
config_file = os.path.join(directories['config'], 'config.boot')
archive_dir = os.path.join(directories['config'], 'archive')
archive_config_file = os.path.join(archive_dir, 'config.boot')
commit_log_file = os.path.join(archive_dir, 'commits')
logrotate_conf = os.path.join(archive_dir, 'lr.conf')
logrotate_state = os.path.join(archive_dir, 'lr.state')
rollback_config = os.path.join(archive_dir, 'config.boot-rollback')
prerollback_config = os.path.join(archive_dir, 'config.boot-prerollback')
tmp_log_entry = '/tmp/commit-rev-entry'
logger = logging.getLogger('config_mgmt')
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(funcName)s: %(levelname)s:%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
def save_config(target, json_out=None):
if json_out is None:
cmd = f'{SAVE_CONFIG} {target}'
else:
cmd = f'{SAVE_CONFIG} {target} --write-json-file {json_out}'
rc, out = rc_cmd(cmd)
if rc != 0:
logger.critical(f'save config failed: {out}')
def unsaved_commits() -> bool:
if get_full_version_data()['boot_via'] == 'livecd':
return False
tmp_save = '/tmp/config.running'
save_config(tmp_save)
ret = not cmp(tmp_save, config_file, shallow=False)
os.unlink(tmp_save)
return ret
def get_file_revision(rev: int):
revision = os.path.join(archive_dir, f'config.boot.{rev}.gz')
try:
with gzip.open(revision) as f:
r = f.read().decode()
except FileNotFoundError:
logger.warning(f'commit revision {rev} not available')
return ''
return r
def get_config_tree_revision(rev: int):
c = get_file_revision(rev)
return ConfigTree(c)
def is_node_revised(path: list = [], rev1: int = 1, rev2: int = 0) -> bool:
from vyos.configtree import DiffTree
left = get_config_tree_revision(rev1)
right = get_config_tree_revision(rev2)
diff_tree = DiffTree(left, right)
if diff_tree.add.exists(path) or diff_tree.sub.exists(path):
return True
return False
class ConfigMgmtError(Exception):
pass
class ConfigMgmt:
def __init__(self, session_env=None, config=None):
if session_env:
self._session_env = session_env
else:
self._session_env = None
if config is None:
config = Config()
d = config.get_config_dict(['system', 'config-management'],
key_mangling=('-', '_'),
get_first_key=True)
self.max_revisions = int(d.get('commit_revisions', 0))
self.num_revisions = 0
self.locations = d.get('commit_archive', {}).get('location', [])
self.source_address = d.get('commit_archive',
{}).get('source_address', '')
if config.exists(['system', 'host-name']):
self.hostname = config.return_value(['system', 'host-name'])
else:
self.hostname = 'vyos'
# upload only on existence of effective values, notably, on boot.
# one still needs session self.locations (above) for setting
# post-commit hook in conf_mode script
path = ['system', 'config-management', 'commit-archive', 'location']
if config.exists_effective(path):
self.effective_locations = config.return_effective_values(path)
else:
self.effective_locations = []
# a call to compare without args is edit_level aware
edit_level = os.getenv('VYATTA_EDIT_LEVEL', '')
self.edit_path = [l for l in edit_level.split('/') if l]
self.active_config = config._running_config
self.working_config = config._session_config
# Console script functions
#
def commit_confirm(self, minutes: int=DEFAULT_TIME_MINUTES,
no_prompt: bool=False) -> Tuple[str,int]:
"""Commit with reboot to saved config in 'minutes' minutes if
'confirm' call is not issued.
"""
if is_systemd_service_active(f'{timer_name}.timer'):
msg = 'Another confirm is pending'
return msg, 1
if unsaved_commits():
W = '\nYou should save previous commits before commit-confirm !\n'
else:
W = ''
prompt_str = f'''
commit-confirm will automatically reboot in {minutes} minutes unless changes
are confirmed.\n
Proceed ?'''
prompt_str = W + prompt_str
if not no_prompt and not ask_yes_no(prompt_str, default=True):
msg = 'commit-confirm canceled'
return msg, 1
action = 'sg vyattacfg "/usr/bin/config-mgmt revert"'
cmd = f'sudo systemd-run --quiet --on-active={minutes}m --unit={timer_name} {action}'
rc, out = rc_cmd(cmd)
if rc != 0:
raise ConfigMgmtError(out)
# start notify
cmd = f'sudo -b /usr/libexec/vyos/commit-confirm-notify.py {minutes}'
os.system(cmd)
msg = f'Initialized commit-confirm; {minutes} minutes to confirm before reboot'
return msg, 0
def confirm(self) -> Tuple[str,int]:
"""Do not reboot to saved config following 'commit-confirm'.
Update commit log and archive.
"""
if not is_systemd_service_active(f'{timer_name}.timer'):
msg = 'No confirm pending'
return msg, 0
cmd = f'sudo systemctl stop --quiet {timer_name}.timer'
rc, out = rc_cmd(cmd)
if rc != 0:
raise ConfigMgmtError(out)
# kill notify
cmd = 'sudo pkill -f commit-confirm-notify.py'
rc, out = rc_cmd(cmd)
if rc != 0:
raise ConfigMgmtError(out)
entry = self._read_tmp_log_entry()
if self._archive_active_config():
self._add_log_entry(**entry)
self._update_archive()
msg = 'Reboot timer stopped'
return msg, 0
def revert(self) -> Tuple[str,int]:
"""Reboot to saved config, dropping commits from 'commit-confirm'.
"""
_ = self._read_tmp_log_entry()
# archived config will be reverted on boot
rc, out = rc_cmd('sudo systemctl reboot')
if rc != 0:
raise ConfigMgmtError(out)
return '', 0
def rollback(self, rev: int, no_prompt: bool=False) -> Tuple[str,int]:
"""Reboot to config revision 'rev'.
"""
msg = ''
if not self._check_revision_number(rev):
msg = f'Invalid revision number {rev}: must be 0 < rev < {self.num_revisions}'
return msg, 1
prompt_str = 'Proceed with reboot ?'
if not no_prompt and not ask_yes_no(prompt_str, default=True):
msg = 'Canceling rollback'
return msg, 0
rc, out = rc_cmd(f'sudo cp {archive_config_file} {prerollback_config}')
if rc != 0:
raise ConfigMgmtError(out)
path = os.path.join(archive_dir, f'config.boot.{rev}.gz')
with gzip.open(path) as f:
config = f.read()
try:
with open(rollback_config, 'wb') as f:
f.write(config)
copy(rollback_config, config_file)
except OSError as e:
raise ConfigMgmtError from e
rc, out = rc_cmd('sudo systemctl reboot')
if rc != 0:
raise ConfigMgmtError(out)
return msg, 0
def compare(self, saved: bool=False, commands: bool=False,
rev1: Optional[int]=None,
rev2: Optional[int]=None) -> Tuple[str,int]:
"""General compare function for config file revisions:
revision n vs. revision m; working version vs. active version;
or working version vs. saved version.
"""
ct1 = self.active_config
ct2 = self.working_config
msg = 'No changes between working and active configurations.\n'
if saved:
ct1 = self._get_saved_config_tree()
ct2 = self.working_config
msg = 'No changes between working and saved configurations.\n'
if rev1 is not None:
if not self._check_revision_number(rev1):
return f'Invalid revision number {rev1}', 1
ct1 = self._get_config_tree_revision(rev1)
ct2 = self.working_config
msg = f'No changes between working and revision {rev1} configurations.\n'
if rev2 is not None:
if not self._check_revision_number(rev2):
return f'Invalid revision number {rev2}', 1
# compare older to newer
ct2 = ct1
ct1 = self._get_config_tree_revision(rev2)
msg = f'No changes between revisions {rev2} and {rev1} configurations.\n'
out = ''
path = [] if commands else self.edit_path
try:
if commands:
out = show_diff(ct1, ct2, path=path, commands=True)
else:
out = show_diff(ct1, ct2, path=path)
except ConfigTreeError as e:
return e, 1
if out:
msg = out
return msg, 0
def wrap_compare(self, options) -> Tuple[str,int]:
"""Interface to vyatta-cfg-run: args collected as 'options' to parse
for compare.
"""
cmnds = False
r1 = None
r2 = None
if 'commands' in options:
cmnds=True
options.remove('commands')
for i in options:
if not i.isnumeric():
options.remove(i)
if len(options) > 0:
r1 = int(options[0])
if len(options) > 1:
r2 = int(options[1])
return self.compare(commands=cmnds, rev1=r1, rev2=r2)
# Initialization and post-commit hooks for conf-mode
#
def initialize_revision(self):
"""Initialize config archive, logrotate conf, and commit log.
"""
mask = os.umask(0o002)
os.makedirs(archive_dir, exist_ok=True)
json_dir = os.path.dirname(config_json)
try:
os.makedirs(json_dir, exist_ok=True)
chown(json_dir, group='vyattacfg')
except OSError as e:
logger.warning(f'cannot create {json_dir}: {e}')
self._add_logrotate_conf()
if (not os.path.exists(commit_log_file) or
self._get_number_of_revisions() == 0):
user = self._get_user()
via = 'init'
comment = ''
# add empty init config before boot-config load for revision
# and diff consistency
if self._archive_active_config():
self._add_log_entry(user, via, comment)
self._update_archive()
os.umask(mask)
def commit_revision(self):
"""Update commit log and rotate archived config.boot.
commit_revision is called in post-commit-hooks, if
['commit-archive', 'commit-revisions'] is configured.
"""
if os.getenv('IN_COMMIT_CONFIRM', ''):
self._new_log_entry(tmp_file=tmp_log_entry)
return
if self._archive_active_config():
self._add_log_entry()
self._update_archive()
def commit_archive(self):
"""Upload config to remote archive.
"""
from vyos.remote import upload
hostname = self.hostname
t = datetime.now()
timestamp = t.strftime('%Y%m%d_%H%M%S')
remote_file = f'config.boot-{hostname}.{timestamp}'
source_address = self.source_address
if self.effective_locations:
print("Archiving config...")
for location in self.effective_locations:
url = urlsplit(location)
_, _, netloc = url.netloc.rpartition("@")
redacted_location = urlunsplit(url._replace(netloc=netloc))
print(f" {redacted_location}", end=" ", flush=True)
- try:
- upload(archive_config_file, f'{location}/{remote_file}',
- source_host=source_address, raise_error=True)
- print("OK")
- except Exception as e:
- print("FAILED!")
- print()
- print(indent(str(e), " > "))
- print()
+ upload(archive_config_file, f'{location}/{remote_file}',
+ source_host=source_address)
# op-mode functions
#
def get_raw_log_data(self) -> list:
"""Return list of dicts of log data:
keys: [timestamp, user, commit_via, commit_comment]
"""
log = self._get_log_entries()
res_l = []
for line in log:
d = self._get_log_entry(line)
res_l.append(d)
return res_l
@staticmethod
def format_log_data(data: list) -> str:
"""Return formatted log data as str.
"""
res_l = []
for l_no, l in enumerate(data):
time_d = datetime.fromtimestamp(int(l['timestamp']))
time_str = time_d.strftime("%Y-%m-%d %H:%M:%S")
res_l.append([l_no, time_str,
f"by {l['user']}", f"via {l['commit_via']}"])
if l['commit_comment'] != 'commit': # default comment
res_l.append([None, l['commit_comment']])
ret = tabulate(res_l, tablefmt="plain")
return ret
@staticmethod
def format_log_data_brief(data: list) -> str:
"""Return 'brief' form of log data as str.
Slightly compacted format used in completion help for
'rollback'.
"""
res_l = []
for l_no, l in enumerate(data):
time_d = datetime.fromtimestamp(int(l['timestamp']))
time_str = time_d.strftime("%Y-%m-%d %H:%M:%S")
res_l.append(['\t', l_no, time_str,
f"{l['user']}", f"by {l['commit_via']}"])
ret = tabulate(res_l, tablefmt="plain")
return ret
def show_commit_diff(self, rev: int, rev2: Optional[int]=None,
commands: bool=False) -> str:
"""Show commit diff at revision number, compared to previous
revision, or to another revision.
"""
if rev2 is None:
out, _ = self.compare(commands=commands, rev1=rev, rev2=(rev+1))
return out
out, _ = self.compare(commands=commands, rev1=rev, rev2=rev2)
return out
def show_commit_file(self, rev: int) -> str:
return self._get_file_revision(rev)
# utility functions
#
@staticmethod
def _strip_version(s):
return re.split(r'(^//)', s, maxsplit=1, flags=re.MULTILINE)[0]
def _get_saved_config_tree(self):
with open(config_file) as f:
c = self._strip_version(f.read())
return ConfigTree(c)
def _get_file_revision(self, rev: int):
if rev not in range(0, self._get_number_of_revisions()):
raise ConfigMgmtError('revision not available')
revision = os.path.join(archive_dir, f'config.boot.{rev}.gz')
with gzip.open(revision) as f:
r = f.read().decode()
return r
def _get_config_tree_revision(self, rev: int):
c = self._strip_version(self._get_file_revision(rev))
return ConfigTree(c)
def _add_logrotate_conf(self):
conf: str = dedent(f"""\
{archive_config_file} {{
su root vyattacfg
rotate {self.max_revisions}
start 0
compress
copy
}}
""")
conf_file = Path(logrotate_conf)
conf_file.write_text(conf)
conf_file.chmod(0o644)
def _archive_active_config(self) -> bool:
save_to_tmp = (boot_configuration_complete() or not
os.path.isfile(archive_config_file))
mask = os.umask(0o113)
ext = os.getpid()
cmp_saved = f'/tmp/config.boot.{ext}'
if save_to_tmp:
save_config(cmp_saved, json_out=config_json)
else:
copy(config_file, cmp_saved)
# on boot, we need to manually create the config.json file; after
# boot, it is written by save_config, above
if not os.path.exists(config_json):
ct = self._get_saved_config_tree()
try:
with open(config_json, 'w') as f:
f.write(ct.to_json())
chown(config_json, group='vyattacfg')
except OSError as e:
logger.warning(f'cannot create {config_json}: {e}')
try:
if cmp(cmp_saved, archive_config_file, shallow=False):
os.unlink(cmp_saved)
os.umask(mask)
return False
except FileNotFoundError:
pass
rc, out = rc_cmd(f'sudo mv {cmp_saved} {archive_config_file}')
os.umask(mask)
if rc != 0:
logger.critical(f'mv file to archive failed: {out}')
return False
return True
@staticmethod
def _update_archive():
cmd = f"sudo logrotate -f -s {logrotate_state} {logrotate_conf}"
rc, out = rc_cmd(cmd)
if rc != 0:
logger.critical(f'logrotate failure: {out}')
@staticmethod
def _get_log_entries() -> list:
"""Return lines of commit log as list of strings
"""
entries = []
if os.path.exists(commit_log_file):
with open(commit_log_file) as f:
entries = f.readlines()
return entries
def _get_number_of_revisions(self) -> int:
l = self._get_log_entries()
return len(l)
def _check_revision_number(self, rev: int) -> bool:
self.num_revisions = self._get_number_of_revisions()
if not 0 <= rev < self.num_revisions:
return False
return True
@staticmethod
def _get_user() -> str:
import pwd
try:
user = os.getlogin()
except OSError:
try:
user = pwd.getpwuid(os.geteuid())[0]
except KeyError:
user = 'unknown'
return user
def _new_log_entry(self, user: str='', commit_via: str='',
commit_comment: str='', timestamp: Optional[int]=None,
tmp_file: str=None) -> Optional[str]:
# Format log entry and return str or write to file.
#
# Usage is within a post-commit hook, using env values. In case of
# commit-confirm, it can be written to a temporary file for
# inclusion on 'confirm'.
from time import time
if timestamp is None:
timestamp = int(time())
if not user:
user = self._get_user()
if not commit_via:
commit_via = os.getenv('COMMIT_VIA', 'other')
if not commit_comment:
commit_comment = os.getenv('COMMIT_COMMENT', 'commit')
# the commit log reserves '|' as field demarcation, so replace in
# comment if present; undo this in _get_log_entry, below
if re.search(r'\|', commit_comment):
commit_comment = commit_comment.replace('|', '%%')
entry = f'|{timestamp}|{user}|{commit_via}|{commit_comment}|\n'
mask = os.umask(0o113)
if tmp_file is not None:
try:
with open(tmp_file, 'w') as f:
f.write(entry)
except OSError as e:
logger.critical(f'write to {tmp_file} failed: {e}')
os.umask(mask)
return None
os.umask(mask)
return entry
@staticmethod
def _get_log_entry(line: str) -> dict:
log_fmt = re.compile(r'\|.*\|\n?$')
keys = ['user', 'commit_via', 'commit_comment', 'timestamp']
if not log_fmt.match(line):
logger.critical(f'Invalid log format {line}')
return {}
timestamp, user, commit_via, commit_comment = (
tuple(line.strip().strip('|').split('|')))
commit_comment = commit_comment.replace('%%', '|')
d = dict(zip(keys, [user, commit_via,
commit_comment, timestamp]))
return d
def _read_tmp_log_entry(self) -> dict:
try:
with open(tmp_log_entry) as f:
entry = f.read()
os.unlink(tmp_log_entry)
except OSError as e:
logger.critical(f'error on file {tmp_log_entry}: {e}')
return self._get_log_entry(entry)
def _add_log_entry(self, user: str='', commit_via: str='',
commit_comment: str='', timestamp: Optional[int]=None):
mask = os.umask(0o113)
entry = self._new_log_entry(user=user, commit_via=commit_via,
commit_comment=commit_comment,
timestamp=timestamp)
log_entries = self._get_log_entries()
log_entries.insert(0, entry)
if len(log_entries) > self.max_revisions:
log_entries = log_entries[:-1]
try:
with open(commit_log_file, 'w') as f:
f.writelines(log_entries)
except OSError as e:
logger.critical(e)
os.umask(mask)
# entry_point for console script
#
def run():
from argparse import ArgumentParser, REMAINDER
config_mgmt = ConfigMgmt()
for s in list(commit_hooks):
if sys.argv[0].replace('-', '_').endswith(s):
func = getattr(config_mgmt, s)
try:
func()
except Exception as e:
print(f'{s}: {e}')
sys.exit(0)
parser = ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand')
commit_confirm = subparsers.add_parser('commit_confirm',
help="Commit with opt-out reboot to saved config")
commit_confirm.add_argument('-t', dest='minutes', type=int,
default=DEFAULT_TIME_MINUTES,
help="Minutes until reboot, unless 'confirm'")
commit_confirm.add_argument('-y', dest='no_prompt', action='store_true',
help="Execute without prompt")
subparsers.add_parser('confirm', help="Confirm commit")
subparsers.add_parser('revert', help="Revert commit-confirm")
rollback = subparsers.add_parser('rollback',
help="Rollback to earlier config")
rollback.add_argument('--rev', type=int,
help="Revision number for rollback")
rollback.add_argument('-y', dest='no_prompt', action='store_true',
help="Excute without prompt")
compare = subparsers.add_parser('compare',
help="Compare config files")
compare.add_argument('--saved', action='store_true',
help="Compare session config with saved config")
compare.add_argument('--commands', action='store_true',
help="Show difference between commands")
compare.add_argument('--rev1', type=int, default=None,
help="Compare revision with session config or other revision")
compare.add_argument('--rev2', type=int, default=None,
help="Compare revisions")
wrap_compare = subparsers.add_parser('wrap_compare',
help="Wrapper interface for vyatta-cfg-run")
wrap_compare.add_argument('--options', nargs=REMAINDER)
args = vars(parser.parse_args())
func = getattr(config_mgmt, args['subcommand'])
del args['subcommand']
res = ''
try:
res, rc = func(**args)
except ConfigMgmtError as e:
print(e)
sys.exit(1)
if res:
print(res)
sys.exit(rc)
diff --git a/python/vyos/remote.py b/python/vyos/remote.py
index fec44b571..b1efcd10b 100644
--- a/python/vyos/remote.py
+++ b/python/vyos/remote.py
@@ -1,471 +1,471 @@
# Copyright 2021 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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.
#
# 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 pwd
import shutil
import socket
import ssl
import stat
import sys
import tempfile
import urllib.parse
from contextlib import contextmanager
from pathlib import Path
from ftplib import FTP
from ftplib import FTP_TLS
from paramiko import SSHClient, SSHException
from paramiko import MissingHostKeyPolicy
from requests import Session
from requests.adapters import HTTPAdapter
from requests.packages.urllib3 import PoolManager
from vyos.progressbar import Progressbar
from vyos.utils.io import ask_yes_no
from vyos.utils.io import is_interactive
from vyos.utils.io import print_error
from vyos.utils.misc import begin
from vyos.utils.process import cmd, rc_cmd
from vyos.version import get_version
CHUNK_SIZE = 8192
class InteractivePolicy(MissingHostKeyPolicy):
"""
Paramiko policy for interactively querying the user on whether to proceed
with SSH connections to unknown hosts.
"""
def missing_host_key(self, client, hostname, key):
print_error(f"Host '{hostname}' not found in known hosts.")
print_error('Fingerprint: ' + key.get_fingerprint().hex())
if is_interactive() and ask_yes_no('Do you wish to continue?'):
if client._host_keys_filename\
and ask_yes_no('Do you wish to permanently add this host/key pair to known hosts?'):
client._host_keys.add(hostname, key.get_name(), key)
client.save_host_keys(client._host_keys_filename)
else:
raise SSHException(f"Cannot connect to unknown host '{hostname}'.")
class SourceAdapter(HTTPAdapter):
"""
urllib3 transport adapter for setting source addresses per session.
"""
def __init__(self, source_pair, *args, **kwargs):
# A source pair is a tuple of a source host string and source port respectively.
# Supply '' and 0 respectively for default values.
self._source_pair = source_pair
super(SourceAdapter, self).__init__(*args, **kwargs)
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(
num_pools=connections, maxsize=maxsize,
block=block, source_address=self._source_pair)
@contextmanager
def umask(mask: int):
"""
Context manager that temporarily sets the process umask.
"""
import os
oldmask = os.umask(mask)
try:
yield
finally:
os.umask(oldmask)
def check_storage(path, size):
"""
Check whether `path` has enough storage space for a transfer of `size` bytes.
"""
path = os.path.abspath(os.path.expanduser(path))
directory = path if os.path.isdir(path) else (os.path.dirname(os.path.expanduser(path)) or os.getcwd())
# `size` can be None or 0 to indicate unknown size.
if not size:
print_error('Warning: Cannot determine size of remote file. Bravely continuing regardless.')
return
if size < 1024 * 1024:
print_error(f'The file is {size / 1024.0:.3f} KiB.')
else:
print_error(f'The file is {size / (1024.0 * 1024.0):.3f} MiB.')
# Will throw `FileNotFoundError' if `directory' is absent.
if size > shutil.disk_usage(directory).free:
raise OSError(f'Not enough disk space available in "{directory}".')
class FtpC:
def __init__(self,
url,
progressbar=False,
check_space=False,
source_host='',
source_port=0,
timeout=10):
self.secure = url.scheme == 'ftps'
self.hostname = url.hostname
self.path = url.path
self.username = url.username or os.getenv('REMOTE_USERNAME', 'anonymous')
self.password = url.password or os.getenv('REMOTE_PASSWORD', '')
self.port = url.port or 21
self.source = (source_host, source_port)
self.progressbar = progressbar
self.check_space = check_space
self.timeout = timeout
def _establish(self):
if self.secure:
return FTP_TLS(source_address=self.source,
context=ssl.create_default_context(),
timeout=self.timeout)
else:
return FTP(source_address=self.source, timeout=self.timeout)
def download(self, location: str):
# Open the file upfront before establishing connection.
with open(location, 'wb') as f, self._establish() as conn:
conn.connect(self.hostname, self.port)
conn.login(self.username, self.password)
# Set secure connection over TLS.
if self.secure:
conn.prot_p()
# Almost all FTP servers support the `SIZE' command.
size = conn.size(self.path)
if self.check_space:
check_storage(path, size)
# No progressbar if we can't determine the size or if the file is too small.
if self.progressbar and size and size > CHUNK_SIZE:
with Progressbar(CHUNK_SIZE / size) as p:
callback = lambda block: begin(f.write(block), p.increment())
conn.retrbinary('RETR ' + self.path, callback, CHUNK_SIZE)
else:
conn.retrbinary('RETR ' + self.path, f.write, CHUNK_SIZE)
def upload(self, location: str):
size = os.path.getsize(location)
with open(location, 'rb') as f, self._establish() as conn:
conn.connect(self.hostname, self.port)
conn.login(self.username, self.password)
if self.secure:
conn.prot_p()
if self.progressbar and size and size > CHUNK_SIZE:
with Progressbar(CHUNK_SIZE / size) as p:
conn.storbinary('STOR ' + self.path, f, CHUNK_SIZE, lambda block: p.increment())
else:
conn.storbinary('STOR ' + self.path, f, CHUNK_SIZE)
class SshC:
known_hosts = os.path.expanduser('~/.ssh/known_hosts')
def __init__(self,
url,
progressbar=False,
check_space=False,
source_host='',
source_port=0,
timeout=10.0):
self.hostname = url.hostname
self.path = url.path
self.username = url.username or os.getenv('REMOTE_USERNAME')
self.password = url.password or os.getenv('REMOTE_PASSWORD')
self.port = url.port or 22
self.source = (source_host, source_port)
self.progressbar = progressbar
self.check_space = check_space
self.timeout = timeout
def _establish(self):
ssh = SSHClient()
ssh.load_system_host_keys()
# Try to load from a user-local known hosts file if one exists.
if os.path.exists(self.known_hosts):
ssh.load_host_keys(self.known_hosts)
ssh.set_missing_host_key_policy(InteractivePolicy())
# `socket.create_connection()` automatically picks a NIC and an IPv4/IPv6 address family
# for us on dual-stack systems.
sock = socket.create_connection((self.hostname, self.port), self.timeout, self.source)
ssh.connect(self.hostname, self.port, self.username, self.password, sock=sock)
return ssh
def download(self, location: str):
with self._establish() as ssh, ssh.open_sftp() as sftp:
if self.check_space:
check_storage(location, sftp.stat(self.path).st_size)
if self.progressbar:
with Progressbar() as p:
sftp.get(self.path, location, callback=p.progress)
else:
sftp.get(self.path, location)
def upload(self, location: str):
with self._establish() as ssh, ssh.open_sftp() as sftp:
try:
# If the remote path is a directory, use the original filename.
if stat.S_ISDIR(sftp.stat(self.path).st_mode):
path = os.path.join(self.path, os.path.basename(location))
# A file exists at this destination. We're simply going to clobber it.
else:
path = self.path
# This path doesn't point at any existing file. We can freely use this filename.
except IOError:
path = self.path
finally:
if self.progressbar:
with Progressbar() as p:
sftp.put(location, path, callback=p.progress)
else:
sftp.put(location, path)
class HttpC:
def __init__(self,
url,
progressbar=False,
check_space=False,
source_host='',
source_port=0,
timeout=10.0):
self.urlstring = urllib.parse.urlunsplit(url)
self.progressbar = progressbar
self.check_space = check_space
self.source_pair = (source_host, source_port)
self.username = url.username or os.getenv('REMOTE_USERNAME')
self.password = url.password or os.getenv('REMOTE_PASSWORD')
self.timeout = timeout
def _establish(self):
session = Session()
session.mount(self.urlstring, SourceAdapter(self.source_pair))
session.headers.update({'User-Agent': 'VyOS/' + get_version()})
if self.username:
session.auth = self.username, self.password
return session
def download(self, location: str):
with self._establish() as s:
# We ask for uncompressed downloads so that we don't have to deal with decoding.
# Not only would it potentially mess up with the progress bar but
# `shutil.copyfileobj(request.raw, file)` does not handle automatic decoding.
s.headers.update({'Accept-Encoding': 'identity'})
with s.head(self.urlstring,
allow_redirects=True,
timeout=self.timeout) as r:
# Abort early if the destination is inaccessible.
r.raise_for_status()
# If the request got redirected, keep the last URL we ended up with.
final_urlstring = r.url
if r.history and self.progressbar:
print_error('Redirecting to ' + final_urlstring)
# Check for the prospective file size.
try:
size = int(r.headers['Content-Length'])
# In case the server does not supply the header.
except KeyError:
size = None
if self.check_space:
check_storage(location, size)
with s.get(final_urlstring, stream=True,
timeout=self.timeout) as r, open(location, 'wb') as f:
if self.progressbar and size:
with Progressbar(CHUNK_SIZE / size) as p:
for chunk in iter(lambda: begin(p.increment(), r.raw.read(CHUNK_SIZE)), b''):
f.write(chunk)
else:
# We'll try to stream the download directly with `copyfileobj()` so that large
# files (like entire VyOS images) don't occupy much memory.
shutil.copyfileobj(r.raw, f)
def upload(self, location: str):
# Does not yet support progressbars.
with self._establish() as s, open(location, 'rb') as f:
s.post(self.urlstring,
data=f,
allow_redirects=True,
timeout=self.timeout)
class TftpC:
# We simply allow `curl` to take over because
# 1. TFTP is rather simple.
# 2. Since there's no concept authentication, we don't need to deal with keys/passwords.
# 3. It would be a waste to import, audit and maintain a third-party library for TFTP.
# 4. I'd rather not implement the entire protocol here, no matter how simple it is.
def __init__(self,
url,
progressbar=False,
check_space=False,
source_host=None,
source_port=0,
timeout=10):
source_option = f'--interface {source_host} --local-port {source_port}' if source_host else ''
progress_flag = '--progress-bar' if progressbar else '-s'
self.command = f'curl {source_option} {progress_flag} --connect-timeout {timeout}'
self.urlstring = urllib.parse.urlunsplit(url)
def download(self, location: str):
with open(location, 'wb') as f:
f.write(cmd(f'{self.command} "{self.urlstring}"').encode())
def upload(self, location: str):
with open(location, 'rb') as f:
cmd(f'{self.command} -T - "{self.urlstring}"', input=f.read())
class GitC:
def __init__(self,
url,
progressbar=False,
check_space=False,
source_host=None,
source_port=0,
timeout=10,
):
self.command = 'git'
self.url = url
self.urlstring = urllib.parse.urlunsplit(url)
if self.urlstring.startswith("git+"):
self.urlstring = self.urlstring.replace("git+", "", 1)
def download(self, location: str):
raise NotImplementedError("not supported")
@umask(0o077)
def upload(self, location: str):
scheme = self.url.scheme
_, _, scheme = scheme.partition("+")
netloc = self.url.netloc
url = Path(self.url.path).parent
with tempfile.TemporaryDirectory(prefix="git-commit-archive-") as directory:
# Determine username, fullname, email for Git commit
pwd_entry = pwd.getpwuid(os.getuid())
user = pwd_entry.pw_name
name = pwd_entry.pw_gecos.split(",")[0] or user
fqdn = socket.getfqdn()
email = f"{user}@{fqdn}"
# environment vars for our git commands
env = {
"GIT_TERMINAL_PROMPT": "0",
"GIT_AUTHOR_NAME": name,
"GIT_AUTHOR_EMAIL": email,
"GIT_COMMITTER_NAME": name,
"GIT_COMMITTER_EMAIL": email,
}
# build ssh command for git
ssh_command = ["ssh"]
# if we are not interactive, we use StrictHostKeyChecking=yes to avoid any prompts
if not sys.stdout.isatty():
ssh_command += ["-o", "StrictHostKeyChecking=yes"]
env["GIT_SSH_COMMAND"] = " ".join(ssh_command)
# git clone
path_repository = Path(directory) / "repository"
scheme = f"{scheme}://" if scheme else ""
rc, out = rc_cmd(
[self.command, "clone", f"{scheme}{netloc}{url}", str(path_repository), "--depth=1"],
env=env,
shell=False,
)
if rc:
raise Exception(out)
# git add
filename = Path(Path(self.url.path).name).stem
dst = path_repository / filename
shutil.copy2(location, dst)
rc, out = rc_cmd(
[self.command, "-C", str(path_repository), "add", filename],
env=env,
shell=False,
)
# git commit -m
commit_message = os.environ.get("COMMIT_COMMENT", "commit")
rc, out = rc_cmd(
[self.command, "-C", str(path_repository), "commit", "-m", commit_message],
env=env,
shell=False,
)
# git push
rc, out = rc_cmd(
[self.command, "-C", str(path_repository), "push"],
env=env,
shell=False,
)
if rc:
raise Exception(out)
def urlc(urlstring, *args, **kwargs):
"""
Dynamically dispatch the appropriate protocol class.
"""
url_classes = {
"http": HttpC,
"https": HttpC,
"ftp": FtpC,
"ftps": FtpC,
"sftp": SshC,
"ssh": SshC,
"scp": SshC,
"tftp": TftpC,
"git": GitC,
}
url = urllib.parse.urlsplit(urlstring)
scheme, _, _ = url.scheme.partition("+")
try:
return url_classes[scheme](url, *args, **kwargs)
except KeyError:
raise ValueError(f'Unsupported URL scheme: "{scheme}"')
def download(local_path, urlstring, progressbar=False, check_space=False,
source_host='', source_port=0, timeout=10.0, raise_error=False):
try:
progressbar = progressbar and is_interactive()
urlc(urlstring, progressbar, check_space, source_host, source_port, timeout).download(local_path)
except Exception as err:
if raise_error:
raise
print_error(f'Unable to download "{urlstring}": {err}')
except KeyboardInterrupt:
print_error('\nDownload aborted by user.')
def upload(local_path, urlstring, progressbar=False,
source_host='', source_port=0, timeout=10.0):
try:
progressbar = progressbar and is_interactive()
- urlc(urlstring, progressbar, source_host, source_port, timeout).upload(local_path)
+ urlc(urlstring, progressbar, False, source_host, source_port, timeout).upload(local_path)
except Exception as err:
print_error(f'Unable to upload "{urlstring}": {err}')
except KeyboardInterrupt:
print_error('\nUpload aborted by user.')
def get_remote_config(urlstring, source_host='', source_port=0):
"""
Quietly download a file and return it as a string.
"""
temp = tempfile.NamedTemporaryFile(delete=False).name
try:
download(temp, urlstring, False, False, source_host, source_port)
with open(temp, 'r') as f:
return f.read()
finally:
os.remove(temp)

File Metadata

Mime Type
text/x-diff
Expires
Thu, Nov 26, 8:54 PM (6 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4931437
Default Alt Text
(44 KB)

Event Timeline