diff options
author | John Estabrook <jestabro@vyos.io> | 2025-04-16 15:02:45 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2025-05-22 13:26:48 -0500 |
commit | 553450438afedd12b6c44fe21eb4372c62d1a5a2 (patch) | |
tree | ace8e43ce9998aed22f2cafcac1a9f28f554e32b /python | |
parent | 0cf87061b4216af395651aa5b935a5c320737de3 (diff) | |
download | vyos-1x-553450438afedd12b6c44fe21eb4372c62d1a5a2.tar.gz vyos-1x-553450438afedd12b6c44fe21eb4372c62d1a5a2.zip |
T7363: add pid aware initialization
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/configsession.py | 35 | ||||
-rw-r--r-- | python/vyos/vyconf_session.py | 32 |
2 files changed, 44 insertions, 23 deletions
diff --git a/python/vyos/configsession.py b/python/vyos/configsession.py index 6490e6feb..a070736cd 100644 --- a/python/vyos/configsession.py +++ b/python/vyos/configsession.py @@ -179,26 +179,29 @@ class ConfigSession(object): self._vyconf_session = None def __del__(self): - try: - output = ( - subprocess.check_output( - [CLI_SHELL_API, 'teardownSession'], env=self.__session_env + if self._vyconf_session is None: + try: + output = ( + subprocess.check_output( + [CLI_SHELL_API, 'teardownSession'], env=self.__session_env + ) + .decode() + .strip() ) - .decode() - .strip() - ) - if output: + if output: + print( + 'cli-shell-api teardownSession output for sesion {0}: {1}'.format( + self.__session_id, output + ), + file=sys.stderr, + ) + except Exception as e: print( - 'cli-shell-api teardownSession output for sesion {0}: {1}'.format( - self.__session_id, output - ), + 'Could not tear down session {0}: {1}'.format(self.__session_id, e), file=sys.stderr, ) - except Exception as e: - print( - 'Could not tear down session {0}: {1}'.format(self.__session_id, e), - file=sys.stderr, - ) + else: + self._vyconf_session.teardown() def __run_command(self, cmd_list): p = subprocess.Popen( diff --git a/python/vyos/vyconf_session.py b/python/vyos/vyconf_session.py index 506095625..cbf2fc873 100644 --- a/python/vyos/vyconf_session.py +++ b/python/vyos/vyconf_session.py @@ -15,6 +15,7 @@ # # +import os import tempfile import shutil from functools import wraps @@ -24,6 +25,7 @@ from vyos.proto import vyconf_client from vyos.migrate import ConfigMigrate from vyos.migrate import ConfigMigrateError from vyos.component_version import append_system_version +from vyos.utils.session import in_config_session def output(o): @@ -35,14 +37,35 @@ def output(o): class VyconfSession: - def __init__(self, token: str = None, on_error: Type[Exception] = None): + def __init__( + self, token: str = None, pid: int = None, on_error: Type[Exception] = None + ): + self.pid = os.getpid() if pid is None else pid if token is None: - out = vyconf_client.send_request('setup_session') + # CLI applications with arg pid=getppid() allow coordination + # with the ambient session; other uses (such as ConfigSession) + # may default to self pid + out = vyconf_client.send_request('session_of_pid', client_pid=self.pid) + if out.output is None: + out = vyconf_client.send_request('setup_session', client_pid=self.pid) self.__token = out.output else: + out = vyconf_client.send_request( + 'session_update_pid', token=token, client_pid=self.pid + ) + if out.status: + raise ValueError(f'No existing session for token: {token}') self.__token = token self.on_error = on_error + self.in_config_session = in_config_session() + + def __del__(self): + if not self.in_config_session: + self.teardown() + + def teardown(self): + vyconf_client.send_request('teardown', token=self.__token) @staticmethod def raise_exception(f): @@ -116,8 +139,3 @@ class VyconfSession: path = [] out = vyconf_client.send_request('show_config', token=self.__token, path=path) return output(out), out.status - - def __del__(self): - out = vyconf_client.send_request('teardown', token=self.__token) - if out.status: - print(f'Could not tear down session {self.__token}: {output(out)}') |