Skip to content

Commit

Permalink
feat: add pty support to execute() (#438)
Browse files Browse the repository at this point in the history
By default, paramiko doesn't allocate a pty, but sometimes interactive
ssh emulation is required. Plumb keyward arguments which allow get_pty
to be passed as a kwarg to execute().
  • Loading branch information
holmanb authored Oct 7, 2024
1 parent 113e5a0 commit 24aea02
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1!10.0.2
1!10.0.3
9 changes: 5 additions & 4 deletions pycloudlib/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def clean(self):
self.execute("sudo echo 'uninitialized' > /etc/machine-id")
self.execute("sudo rm -rf /var/log/syslog")

def _run_command(self, command, stdin):
def _run_command(self, command, stdin, get_pty=False):
"""Run command in the instance."""
return self._ssh(list(command), stdin=stdin)
return self._ssh(list(command), stdin=stdin, get_pty=get_pty)

def execute(
self,
Expand Down Expand Up @@ -376,12 +376,13 @@ def update(self):
]
)

def _ssh(self, command, stdin=None):
def _ssh(self, command, stdin=None, get_pty=False):
"""Run a command via SSH.
Args:
command: string or list of the command to run
stdin: optional, values to be passed in
get_pty: optional, allocates a pty
Returns:
tuple of stdout, stderr and the return code
Expand All @@ -390,7 +391,7 @@ def _ssh(self, command, stdin=None):
cmd = shell_pack(command)
client = self._ssh_connect()
try:
fp_in, fp_out, fp_err = client.exec_command(cmd)
fp_in, fp_out, fp_err = client.exec_command(cmd, get_pty=get_pty)
except (ConnectionResetError, NoValidConnectionsError, EOFError) as e:
raise SSHException from e
channel = fp_in.channel
Expand Down
4 changes: 2 additions & 2 deletions pycloudlib/lxd/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def __repr__(self):
"""Create string representation for class."""
return "LXDInstance(name={})".format(self.name)

def _run_command(self, command, stdin):
def _run_command(self, command, stdin, get_pty=False):
"""Run command in the instance."""
if self.execute_via_ssh:
return super()._run_command(command, stdin)
return super()._run_command(command, stdin, get_pty=get_pty)

base_cmd = [
"lxc",
Expand Down

0 comments on commit 24aea02

Please sign in to comment.