Skip to content
Merged
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: 1 addition & 1 deletion libvyosconfig
6 changes: 5 additions & 1 deletion python/vyos/config_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ def revert_soft(self) -> Tuple[str, int]:
session = ConfigSession(os.getpid(), app='config-mgmt')

try:
session.load_explicit(revert_ct)
if session.vyconf_backend():
session.load_config_obj(revert_ct)
else:
session.load_explicit(revert_ct)

session.commit()
except ConfigSessionError as e:
raise ConfigMgmtError(e) from e
Expand Down
34 changes: 24 additions & 10 deletions python/vyos/configsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import re
import sys
import subprocess
from tempfile import NamedTemporaryFile
from typing import TypeAlias
from typing import Union

from vyos.defaults import directories
from vyos.utils.process import is_systemd_service_running
Expand All @@ -26,6 +29,10 @@
from vyos.vyconf_session import VyconfSession
from vyos.base import Warning as Warn
from vyos.defaults import DEFAULT_COMMIT_CONFIRM_MINUTES
from vyos.configtree import ConfigTree

# type of config file path or configtree
ConfigObj: TypeAlias = Union[str, ConfigTree]


CLI_SHELL_API = '/bin/cli-shell-api'
Expand Down Expand Up @@ -238,6 +245,9 @@ def __run_command(self, cmd_list):
def get_session_env(self):
return self.__session_env

def vyconf_backend(self) -> bool:
return bool(self._vyconf_session)

def set(self, path, value=None):
if not value:
value = []
Expand Down Expand Up @@ -309,18 +319,12 @@ def commit(self):
return out

def commit_confirm(self, minutes: int = DEFAULT_COMMIT_CONFIRM_MINUTES):
if self._vyconf_session is None:
out = self.__run_command(COMMIT_CONFIRM + [f'-t {minutes}'])
else:
out = 'unimplemented'
out = self.__run_command(COMMIT_CONFIRM + [f'-t {minutes}'])

return out

def confirm(self):
if self._vyconf_session is None:
out = self.__run_command(CONFIRM)
else:
out = 'unimplemented'
out = self.__run_command(CONFIRM)

return out

Expand All @@ -339,11 +343,13 @@ def show_config(self, path, format='raw'):
if format == 'raw':
return config_data

def load_config(self, file_path):
def load_config(self, file_path, cached: bool = False):
if self._vyconf_session is None:
out = self.__run_command(LOAD_CONFIG + [file_path])
else:
out, _ = self._vyconf_session.load_config(file_name=file_path)
out, _ = self._vyconf_session.load_config(
file_name=file_path, cached=cached
)

return out

Expand All @@ -356,6 +362,14 @@ def load_explicit(self, file_path):
except LoadConfigError as e:
raise ConfigSessionError(e) from e

def load_config_obj(self, config_obj: ConfigObj):
if isinstance(config_obj, ConfigTree):
with NamedTemporaryFile() as f:
config_obj.write_cache(f.name)
self.load_config(f.name, cached=True)
else:
self.load_config(config_obj)

def migrate_and_load_config(self, file_path):
if self._vyconf_session is None:
out = self.__run_command(MIGRATE_LOAD_CONFIG + [file_path])
Expand Down
Loading