Skip to content

T7499: update config merge tools #4574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: current
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/vyos/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ class Config(object):
subtrees.
"""
def __init__(self, session_env=None, config_source=None):
self.vyconf_session = None
if config_source is None:
if vyconf_backend() and boot_configuration_complete():
self._config_source = ConfigSourceVyconfSession(session_env)
self.vyconf_session = self._config_source._vyconf_session
else:
self._config_source = ConfigSourceSession(session_env)
else:
Expand Down
8 changes: 3 additions & 5 deletions python/vyos/configsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,9 @@ def migrate_and_load_config(self, file_path):

return out

def merge_config(self, file_path):
if self._vyconf_session is None:
out = self.__run_command(MERGE_CONFIG + [file_path])
else:
out = 'unimplemented'
def merge_config(self, file_path, destructive=False):
destr = ['--destructive'] if destructive else []
out = self.__run_command(MERGE_CONFIG + [file_path] + destr)

return out

Expand Down
24 changes: 23 additions & 1 deletion python/vyos/configtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def get_version_string(self):
return self.__version

def write_cache(self, file_name):
self.__write_internal(self._get_config(), file_name)
self.__write_internal(self._get_config(), file_name.encode())

def to_string(self, ordered_values=False, no_version=False):
config_string = self.__to_string(self.__config, ordered_values).decode()
Expand Down Expand Up @@ -499,6 +499,28 @@ def union(left, right, libpath=LIBPATH):
return tree


def merge(left, right, destructive=False, libpath=LIBPATH):
if left is None:
left = ConfigTree(config_string='\n')
if right is None:
right = ConfigTree(config_string='\n')
if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)):
raise TypeError('Arguments must be instances of ConfigTree')

__lib = cdll.LoadLibrary(libpath)
__tree_merge = __lib.tree_merge
__tree_merge.argtypes = [c_bool, c_void_p, c_void_p]
__tree_merge.restype = c_void_p
__get_error = __lib.get_error
__get_error.argtypes = []
__get_error.restype = c_char_p

res = __tree_merge(destructive, left._get_config(), right._get_config())
tree = ConfigTree(address=res)

return tree


def mask_inclusive(left, right, libpath=LIBPATH):
if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)):
raise TypeError('Arguments must be instances of ConfigTree')
Expand Down
70 changes: 35 additions & 35 deletions python/vyos/proto/vyconf_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions python/vyos/proto/vyconf_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Rollback:
@dataclass
class Load:
Location: str = ""
cached: bool = False
format: ConfigFormat = None

@dataclass
Expand Down Expand Up @@ -298,8 +299,8 @@ def set_request_rollback(token: str = None, revision: int = 0):
req_env = RequestEnvelope(token, req)
return req_env

def set_request_load(token: str = None, location: str = "", format: ConfigFormat = None):
reqi = Load (location, format)
def set_request_load(token: str = None, location: str = "", cached: bool = False, format: ConfigFormat = None):
reqi = Load (location, cached, format)
req = Request(load=reqi)
req_env = RequestEnvelope(token, req)
return req_env
Expand Down
8 changes: 6 additions & 2 deletions python/vyos/vyconf_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def discard(self) -> tuple[str, int]:

@raise_exception
@config_mode
def load_config(self, file: str, migrate: bool = False) -> tuple[str, int]:
def load_config(
self, file: str, migrate: bool = False, cached: bool = False
) -> tuple[str, int]:
# pylint: disable=consider-using-with
if migrate:
tmp = tempfile.NamedTemporaryFile()
Expand All @@ -178,7 +180,9 @@ def load_config(self, file: str, migrate: bool = False) -> tuple[str, int]:
else:
tmp = ''

out = vyconf_client.send_request('load', token=self.__token, location=file)
out = vyconf_client.send_request(
'load', token=self.__token, location=file, cached=cached
)
if tmp:
tmp.close()

Expand Down
Loading
Loading