From 244b987b093c3e9773a96e5b29366a1a5a32d102 Mon Sep 17 00:00:00 2001 From: aiyion Date: Tue, 30 Dec 2025 03:31:11 +0100 Subject: [PATCH] util/agentwrapper: Implement _labgrid-agent a helper to execute agents on the exporter host. Signed-off-by: aiyion --- labgrid/util/agentwrapper.py | 58 ++++++++++++++++++++++++++---------- pyproject.toml | 1 + 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/labgrid/util/agentwrapper.py b/labgrid/util/agentwrapper.py index f505b861d..f018fa351 100644 --- a/labgrid/util/agentwrapper.py +++ b/labgrid/util/agentwrapper.py @@ -5,6 +5,7 @@ import subprocess import traceback import logging +from subprocess import CalledProcessError from .ssh import get_ssh_connect_timeout @@ -47,6 +48,7 @@ def __init__(self, host=None): os.path.abspath(os.path.dirname(__file__)), 'agent.py') agent_prefix = os.environ.get("LG_AGENT_PREFIX", "") + labgrid_agent_exists: bool = False if host: # copy agent.py and run via ssh with open(agent, 'rb') as agent_fd: @@ -55,24 +57,48 @@ def __init__(self, host=None): agent_remote = os.path.join(agent_prefix, f'.labgrid_agent_{agent_hash}.py') connect_timeout = get_ssh_connect_timeout() ssh_opts = f'ssh -x -o ConnectTimeout={connect_timeout} -o PasswordAuthentication=no'.split() - subprocess.check_call( - ['rsync', '-e', ' '.join(ssh_opts), '-tq', agent, - f'{host}:{agent_remote}'], - ) - self.agent = subprocess.Popen( - ssh_opts + [host, '--', 'python3', agent_remote], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - start_new_session=True, - ) + try: + labgrid_agent_exists = (0 == subprocess.check_call(ssh_opts + [host, '--', 'which', '_labgrid-agent'],)) + except CalledProcessError: + pass + if not labgrid_agent_exists: + subprocess.check_call( + ['rsync', '-e', ' '.join(ssh_opts), '-tq', agent, + f'{host}:{agent_remote}'], + ) + self.agent = subprocess.Popen( + ssh_opts + [host, '--', 'python3', agent_remote], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + start_new_session=True, + ) + else: + self.agent = subprocess.Popen( + ssh_opts + [host, '--', '_labgrid-agent',], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + start_new_session=True, + ) else: # run locally - self.agent = subprocess.Popen( - ['python3', agent], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - start_new_session=True, - ) + try: + labgrid_agent_exists = (0 == subprocess.check_call(['which', '_labgrid-agent'],)) + except CalledProcessError: + pass + if not labgrid_agent_exists: + self.agent = subprocess.Popen( + ['python3', agent], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + start_new_session=True, + ) + else: + self.agent = subprocess.Popen( + ['_labgrid-agent'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + start_new_session=True, + ) def __del__(self): self.close() diff --git a/pyproject.toml b/pyproject.toml index 587e82fa3..664e82f40 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,6 +102,7 @@ labgrid-client = "labgrid.remote.client:main" labgrid-exporter = "labgrid.remote.exporter:main" labgrid-suggest = "labgrid.resource.suggest:main" labgrid-coordinator = "labgrid.remote.coordinator:main" +_labgrid-agent = "labgrid.util.agent:main" # the following makes a plugin available to pytest [project.entry-points.pytest11]