diff --git a/src/remote_launcher.py b/src/remote_launcher.py index 3e47e4d0..45996c07 100755 --- a/src/remote_launcher.py +++ b/src/remote_launcher.py @@ -9,7 +9,7 @@ import psutil import sys import subprocess - from time import sleep + import time sys.path.insert(0, '/etc/ConsolePi/src/pypkg') from consolepi import config, utils # type: ignore # NoQA from consolepi.consolepi import ConsolePi # type: ignore # NoQA @@ -40,33 +40,57 @@ def terminate_process(pid): x += 1 +def check_hung_process(cmd: str, device: str) -> int: + ppid = find_procs_by_name(cmd, device) + retry = 0 + msg = f"\n{_device.replace('/dev/', '')} appears to be in use (may be a previous hung session)." + msg += '\nDo you want to Terminate the existing session' + if ppid is not None and utils.user_input_bool(msg): + while ppid is not None and retry < 3: + print('Terminating Existing Session...') + try: + terminate_process(ppid) + time.sleep(3) + ppid = find_procs_by_name(cmd, device) + except PermissionError: + print('This appears to be a locally established session, if you want to kill that do it yourself') + break + except psutil.AccessDenied: + print('This appears to be a locally established session, session will not be terminated') + break + except psutil.NoSuchProcess: + ppid = find_procs_by_name(cmd, device) + retry += 1 + + return ppid + + if __name__ == '__main__': - if len(sys.argv) >= 3: + # Allow user to ssh to configured port which using ForceCommand and specifying only the device they want to connect to + if len(sys.argv) == 2 and "picocom" not in sys.argv[1]: + _device = f'/dev/{sys.argv[1].replace("/dev/", "")}' + adapter_data = cpi.local.adapters.get(_device) + if not adapter_data: + print(f'{_device.replace("/dev/", "")} Not found on system... Refreshing local adapters.') + cpi.local.build_adapter_dict(refresh=True) + adapter_data = cpi.local.adapters.get(_device) + + if adapter_data: + print(f'Establishing Connection to {_device.replace("/dev/", "")}...') + _cmd = adapter_data["config"]["cmd"].replace("{{timestamp}}", time.strftime("%F_%H.%M")).split() + else: + print(f'{_device.replace("/dev/", "")} Not found on system... Exiting.') + sys.exit(1) + # Sent from menu on remote full command is sent + elif len(sys.argv) >= 3: _cmd = sys.argv[1:] - ppid = find_procs_by_name(sys.argv[1], sys.argv[2]) - retry = 0 - msg = f"\n{sys.argv[2].replace('/dev/', '')} appears to be in use (may be a previous hung session)." - msg += '\nDo you want to Terminate the existing session' - if ppid is not None and utils.user_input_bool(msg): - while ppid is not None and retry < 3: - print('Terminating Existing Session...') - try: - terminate_process(ppid) - sleep(3) - ppid = find_procs_by_name(sys.argv[1], sys.argv[2]) - except PermissionError: - print('This appears to be a locally established session, if you want to kill that do it yourself') - break - except psutil.AccessDenied: - print('This appears to be a locally established session, session will not be terminated') - break - except psutil.NoSuchProcess: - ppid = find_procs_by_name(sys.argv[1], sys.argv[2]) - retry += 1 + _device = f'/dev/{sys.argv[2].replace("/dev/", "")}' + + ppid = check_hung_process(_cmd[0], _device) - if ppid is None: - # if power feature enabled and adapter linked - ensure outlet is on - # TODO try/except block here - if config.power: - cpi.cpiexec.exec_auto_pwron(sys.argv[2]) - subprocess.run(_cmd) + if ppid is None: + # if power feature enabled and adapter linked - ensure outlet is on + # TODO try/except block here + if config.power: + cpi.cpiexec.exec_auto_pwron(_device) + subprocess.run(_cmd)