55import subprocess
66import traceback
77import logging
8+ from subprocess import CalledProcessError
89
910from .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 ()
0 commit comments