Skip to content
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
58 changes: 42 additions & 16 deletions labgrid/util/agentwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import traceback
import logging
from subprocess import CalledProcessError

from .ssh import get_ssh_connect_timeout

Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down