1414import signal
1515import sys
1616import shlex
17- import shutil
1817import json
1918import itertools
2019import ipaddress
4645from ..exceptions import NoDriverFoundError , NoResourceFoundError , InvalidConfigError
4746from .generated import labgrid_coordinator_pb2 , labgrid_coordinator_pb2_grpc
4847from ..resource .remote import RemotePlaceManager , RemotePlace
49- from ..util import diff_dict , flat_dict , dump , atomic_replace , labgrid_version , Timeout
48+ from ..util import diff_dict , flat_dict , dump , atomic_replace , labgrid_version , Timeout , term
5049from ..util .proxy import proxymanager
5150from ..util .helper import processwrapper
5251from ..driver import Mode , ExecutionError
@@ -1072,56 +1071,16 @@ async def _console(self, place, target, timeout, *, logfile=None, loop=False, li
10721071
10731072 # check for valid resources
10741073 assert port is not None , "Port is not set"
1075-
1076- microcom_bin = shutil .which ("microcom" )
1077-
1078- if microcom_bin is not None :
1079- call = [microcom_bin , "-s" , str (resource .speed ), "-t" , f"{ host } :{ port } " ]
1080-
1081- if listen_only :
1082- call .append ("--listenonly" )
1083-
1084- if logfile :
1085- call .append (f"--logfile={ logfile } " )
1086- else :
1087- call = ["telnet" , host , str (port )]
1088-
1089- logging .info ("microcom not available, using telnet instead" )
1090-
1091- if listen_only :
1092- logging .warning ("--listenonly option not supported by telnet, ignoring" )
1093-
1094- if logfile :
1095- logging .warning ("--logfile option not supported by telnet, ignoring" )
1096-
1097- if logfile :
1098- call .append (f"--logfile={ logfile } " )
1099- logging .info ("connecting to %s calling %s" , resource , " " .join (call ))
11001074 try :
1101- p = await asyncio .create_subprocess_exec (* call )
1075+ returncode = await term .external (
1076+ lambda : self .is_allowed (place ), host , port , resource , logfile , listen_only
1077+ )
11021078 except FileNotFoundError as e :
11031079 raise ServerError (f"failed to execute remote console command: { e } " )
1104- while p .returncode is None :
1105- try :
1106- await asyncio .wait_for (p .wait (), 1.0 )
1107- except asyncio .TimeoutError :
1108- # subprocess is still running
1109- pass
11101080
1111- try :
1112- self ._check_allowed (place )
1113- except UserError :
1114- p .terminate ()
1115- try :
1116- await asyncio .wait_for (p .wait (), 1.0 )
1117- except asyncio .TimeoutError :
1118- # try harder
1119- p .kill ()
1120- await asyncio .wait_for (p .wait (), 1.0 )
1121- raise
1122- if p .returncode :
1123- print ("connection lost" , file = sys .stderr )
1124- return p .returncode
1081+ # Raise an exception if the place was released
1082+ self ._check_allowed (place )
1083+ return returncode
11251084
11261085 async def console (self , place , target ):
11271086 while True :
@@ -1133,7 +1092,7 @@ async def console(self, place, target):
11331092 break
11341093 if not self .args .loop :
11351094 if res :
1136- raise InteractiveCommandError ("microcom error" , res )
1095+ raise InteractiveCommandError ("console error" , res )
11371096 break
11381097 await asyncio .sleep (1.0 )
11391098
0 commit comments