Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support local connections without ssh #11

Merged
merged 4 commits into from
Jul 17, 2024
Merged
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
74 changes: 47 additions & 27 deletions robmuxinator/robmuxinator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import concurrent.futures
import io
import logging
import operator
import os
Expand Down Expand Up @@ -43,11 +44,10 @@ def is_master_online(master_uri=None):

try:
code, msg, val = handle.getPid(caller_id)
if code == 1:
return True
else:
return False
except:
logger.debug(f"[is_master_online] code: '{code}', msg: '{msg}', val: {val}")
return bool(code == 1)
except Exception as e:
logger.debug(f"[is_master_online] exception: {e}")
return False


Expand Down Expand Up @@ -98,8 +98,8 @@ def format(self, record):
logFile = "robmuxinator"
fileHandler = RotatingFileHandler(
"{0}/{1}.log".format(logPath, logFile), maxBytes=1024*10000, backupCount=4)
format = logging.Formatter("[%(levelname)s] [%(asctime)s]: %(message)s")
fileHandler.setFormatter(format)
log_formatter = logging.Formatter("[%(levelname)s] [%(asctime)s]: %(message)s")
fileHandler.setFormatter(log_formatter)
logger.addHandler(fileHandler)

paramiko_version_major = int(paramiko.__version__.split(".")[0])
Expand Down Expand Up @@ -132,6 +132,9 @@ def __init__(self, user, hostname, port=DEFAULT_PORT):
# check if user has sudo privileges
self._sudo_user = True if os.getuid() == 0 else False

self._use_local_connection = self._hostname in ["localhost", "127.0.0.1"] \
and self._user == os.getenv("USER", "INVALID_USER")

# TODO: handle exceptions
self.ssh_cli = None

Expand Down Expand Up @@ -173,12 +176,29 @@ def init_connection(self):
def send_cmd(self, cmd, wait_for_exit_status=True, get_pty=False):
start = datetime.now()
try:
self.init_connection()
stdin, stdout, stderr = self.ssh_cli.exec_command(cmd, get_pty=get_pty)
logger.debug(f"{cmd}")
returncode = 0
if wait_for_exit_status:
returncode = stdout.channel.recv_exit_status()
if not self._use_local_connection:
self.init_connection()
stdin, stdout, stderr = self.ssh_cli.exec_command(cmd, get_pty=get_pty)
logger.debug(f"{cmd}")
if wait_for_exit_status:
returncode = stdout.channel.recv_exit_status()
else:
logger.debug(" using local connection")
process = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
text=True)
stdout = process.stdout
stderr = process.stderr

if wait_for_exit_status:
stdout_str, stderr_str = process.communicate()
# wrap output in IO buffers so it is compatible with .readlines()
stdout = io.StringIO(stdout_str)
stderr = io.StringIO(stderr_str)
returncode = process.returncode
logger.debug(
"send_cmd: {} took {} secs".format(
cmd, (datetime.now() - start).total_seconds()
Expand Down Expand Up @@ -425,18 +445,18 @@ def shutdown(self, timeout=60):


class Session(object):
def __init__(self, ssh_client, session_name, yaml, envs=None) -> None:
def __init__(self, ssh_client, session_name, yaml_session, envs=None) -> None:
self._session_name = session_name
self._ssh_client = ssh_client # type: SSHClient
self._envs = envs

if "user" in yaml:
self._user = yaml["user"]
if "user" in yaml_session:
self._user = yaml_session["user"]
else:
self._user = DEFAULT_USER

if "host" in yaml:
self._host = yaml["host"]
if "host" in yaml_session:
self._host = yaml_session["host"]
else:
self._host = DEFAULT_HOST

Expand All @@ -445,28 +465,28 @@ def __init__(self, ssh_client, session_name, yaml, envs=None) -> None:
for env in self._envs:
command_env_prefix += "export {}={} && ".format(env[0], env[1])

if "command" in yaml:
self._command = command_env_prefix + yaml["command"]
if "command" in yaml_session:
self._command = command_env_prefix + yaml_session["command"]
else:
raise Exception("No command in session section")

if "wait_for_core" in yaml:
self._wait_for_core = yaml["wait_for_core"]
if "wait_for_core" in yaml_session:
self._wait_for_core = yaml_session["wait_for_core"]
else:
self._wait_for_core = True

if "pre_condition" in yaml:
self._pre_condition = yaml["pre_condition"]
if "pre_condition" in yaml_session:
self._pre_condition = yaml_session["pre_condition"]
else:
self._pre_condition = None

if "prio" in yaml:
self.prio = int(yaml["prio"])
if "prio" in yaml_session:
self.prio = int(yaml_session["prio"])
else:
self.prio = 10

if "locked" in yaml:
self._locked = yaml["locked"]
if "locked" in yaml_session:
self._locked = yaml_session["locked"]
else:
self._locked = False

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="robmuxinator",
version="0.1.2",
version="0.1.3",
author="Benjamin Maidel",
author_email="benjamin.maidel@4am-robotics.com",
maintainer="Philipp Gehring",
Expand Down