summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2025-10-21 19:14:12 -0500
committerJohn Estabrook <jestabro@vyos.io>2025-11-05 09:47:55 -0600
commit4c391ed39c51172cbb8f2de6732d6fe4e3a8b125 (patch)
tree089366567965f7116c83e8e735b1bc34c7a99f22 /python
parent2a574f7e146a82beb04661d85d7fd83553b2c160 (diff)
downloadvyos-1x-4c391ed39c51172cbb8f2de6732d6fe4e3a8b125.tar.gz
vyos-1x-4c391ed39c51172cbb8f2de6732d6fe4e3a8b125.zip
T7910: add keyword extant, for use by teardown script
The standalone script teardown-config-session.py is called on CLI config-mode exit, to close the persistent vyconf config session. Instead of injecting the config-mode env var into the external script to indicate a non-ephemeral session, add keyword 'extant' to find existing session.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/vyconf_session.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/python/vyos/vyconf_session.py b/python/vyos/vyconf_session.py
index 1e113c2ad..225765f0b 100644
--- a/python/vyos/vyconf_session.py
+++ b/python/vyos/vyconf_session.py
@@ -49,7 +49,11 @@ def new_session(pid: int, sudo_user: str, user: str):
class VyconfSession:
def __init__(
- self, token: str = None, pid: int = None, on_error: Type[Exception] = None
+ self,
+ token: str = None,
+ pid: int = None,
+ extant=False,
+ on_error: Type[Exception] = None,
):
self.pid = pid if pid else os.getpid()
self.sudo_user = os.environ.get('SUDO_USER', None)
@@ -60,7 +64,9 @@ class VyconfSession:
match token:
case None:
# config-mode sessions are persistent, and managed by caller (CLI or ConfigSession)
- # op-mode sessions are ephemeral: a new session on init; teardown in finalizer
+ #
+ # op-mode sessions are ephemeral, unless forced with extant=True:
+ # --- open a new session on init; teardown in finalizer
if self.in_config_session:
out = vyconf_client.send_request(
'session_of_pid', client_pid=self.pid
@@ -75,14 +81,22 @@ class VyconfSession:
else:
self.__token = out.output
else:
- self.__token = new_session(self.pid, self.sudo_user, self.user)
+ if not extant:
+ self.__token = new_session(self.pid, self.sudo_user, self.user)
+ else:
+ out = vyconf_client.send_request(
+ 'session_of_pid', client_pid=self.pid
+ )
+ if out.output is None:
+ raise ValueError(f'No existing session for pid {self.pid}')
+ self.__token = out.output
case _:
out = vyconf_client.send_request('session_exists', token=token)
if out.status:
raise ValueError(f'No existing session for token: {token}')
self.__token = token
- if not self.in_config_session:
+ if not self.in_config_session and not extant:
self._finalizer = weakref.finalize(self, self._teardown, self.__token)
self.on_error = on_error