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
Changes from 1 commit
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
31 changes: 26 additions & 5 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 @@ -132,6 +133,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 == "localhost" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localhost could also be 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 +177,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(f" 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