Skip to content

Commit 244b987

Browse files
committed
util/agentwrapper: Implement _labgrid-agent
a helper to execute agents on the exporter host. Signed-off-by: aiyion <[email protected]>
1 parent cb7b04f commit 244b987

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

labgrid/util/agentwrapper.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import traceback
77
import logging
8+
from subprocess import CalledProcessError
89

910
from .ssh import get_ssh_connect_timeout
1011

@@ -47,6 +48,7 @@ def __init__(self, host=None):
4748
os.path.abspath(os.path.dirname(__file__)),
4849
'agent.py')
4950
agent_prefix = os.environ.get("LG_AGENT_PREFIX", "")
51+
labgrid_agent_exists: bool = False
5052
if host:
5153
# copy agent.py and run via ssh
5254
with open(agent, 'rb') as agent_fd:
@@ -55,24 +57,48 @@ def __init__(self, host=None):
5557
agent_remote = os.path.join(agent_prefix, f'.labgrid_agent_{agent_hash}.py')
5658
connect_timeout = get_ssh_connect_timeout()
5759
ssh_opts = f'ssh -x -o ConnectTimeout={connect_timeout} -o PasswordAuthentication=no'.split()
58-
subprocess.check_call(
59-
['rsync', '-e', ' '.join(ssh_opts), '-tq', agent,
60-
f'{host}:{agent_remote}'],
61-
)
62-
self.agent = subprocess.Popen(
63-
ssh_opts + [host, '--', 'python3', agent_remote],
64-
stdin=subprocess.PIPE,
65-
stdout=subprocess.PIPE,
66-
start_new_session=True,
67-
)
60+
try:
61+
labgrid_agent_exists = (0 == subprocess.check_call(ssh_opts + [host, '--', 'which', '_labgrid-agent'],))
62+
except CalledProcessError:
63+
pass
64+
if not labgrid_agent_exists:
65+
subprocess.check_call(
66+
['rsync', '-e', ' '.join(ssh_opts), '-tq', agent,
67+
f'{host}:{agent_remote}'],
68+
)
69+
self.agent = subprocess.Popen(
70+
ssh_opts + [host, '--', 'python3', agent_remote],
71+
stdin=subprocess.PIPE,
72+
stdout=subprocess.PIPE,
73+
start_new_session=True,
74+
)
75+
else:
76+
self.agent = subprocess.Popen(
77+
ssh_opts + [host, '--', '_labgrid-agent',],
78+
stdin=subprocess.PIPE,
79+
stdout=subprocess.PIPE,
80+
start_new_session=True,
81+
)
6882
else:
6983
# run locally
70-
self.agent = subprocess.Popen(
71-
['python3', agent],
72-
stdin=subprocess.PIPE,
73-
stdout=subprocess.PIPE,
74-
start_new_session=True,
75-
)
84+
try:
85+
labgrid_agent_exists = (0 == subprocess.check_call(['which', '_labgrid-agent'],))
86+
except CalledProcessError:
87+
pass
88+
if not labgrid_agent_exists:
89+
self.agent = subprocess.Popen(
90+
['python3', agent],
91+
stdin=subprocess.PIPE,
92+
stdout=subprocess.PIPE,
93+
start_new_session=True,
94+
)
95+
else:
96+
self.agent = subprocess.Popen(
97+
['_labgrid-agent'],
98+
stdin=subprocess.PIPE,
99+
stdout=subprocess.PIPE,
100+
start_new_session=True,
101+
)
76102

77103
def __del__(self):
78104
self.close()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ labgrid-client = "labgrid.remote.client:main"
102102
labgrid-exporter = "labgrid.remote.exporter:main"
103103
labgrid-suggest = "labgrid.resource.suggest:main"
104104
labgrid-coordinator = "labgrid.remote.coordinator:main"
105+
_labgrid-agent = "labgrid.util.agent:main"
105106

106107
# the following makes a plugin available to pytest
107108
[project.entry-points.pytest11]

0 commit comments

Comments
 (0)