Skip to content

Commit

Permalink
Get SSH username from given SSH alias when available
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Apr 3, 2016
1 parent 87fbdde commit 1adbece
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions executor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: March 22, 2016
# Last Change: April 3, 2016
# URL: https://executor.readthedocs.org

"""
Expand Down Expand Up @@ -64,7 +64,7 @@
unicode = str

# Semi-standard module versioning.
__version__ = '9.3'
__version__ = '9.4'

# Initialize a logger.
logger = logging.getLogger(__name__)
Expand Down
19 changes: 14 additions & 5 deletions executor/ssh/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: March 21, 2016
# Last Change: April 3, 2016
# URL: https://executor.readthedocs.org

"""
Expand Down Expand Up @@ -180,7 +180,9 @@ def __init__(self, ssh_alias, *command, **options):
"""
Initialize a :class:`RemoteCommand` object.
:param ssh_alias: Used to set :attr:`ssh_alias`.
:param ssh_alias: Used to set :attr:`ssh_alias` and optionally
:attr:`ssh_user` (if the value contains two tokens
delimited by a ``@`` character).
:param command: Any additional positional arguments are converted to a
list and used to set :attr:`~.ExternalCommand.command`.
:param options: Keyword arguments can be used to conveniently override
Expand All @@ -195,11 +197,18 @@ def __init__(self, ssh_alias, *command, **options):
:func:`~executor.ExternalCommand.start()` or
:func:`~executor.ExternalCommand.wait()`.
"""
# Store the SSH alias.
self.ssh_alias = ssh_alias
# Inject our logger as a default.
options.setdefault('logger', logger)
# Set the default remote working directory.
self.remote_directory = DEFAULT_WORKING_DIRECTORY
# Store the SSH alias (and an optional username prefixed to it).
user, _, host = ssh_alias.rpartition('@')
if user and host:
self.ssh_user = user
self.ssh_alias = host
else:
self.ssh_alias = ssh_alias
# Initialize the super class.
options.setdefault('logger', logger)
super(RemoteCommand, self).__init__(*command, **options)

@mutable_property
Expand Down
9 changes: 8 additions & 1 deletion executor/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automated tests for the `executor' module.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: March 22, 2016
# Last Change: April 3, 2016
# URL: https://executor.readthedocs.org

"""
Expand Down Expand Up @@ -598,6 +598,13 @@ def test_command_pool_logs_directory(self):
contents = handle.read()
assert filename == ('%s.log' % contents.strip())

def test_ssh_user_at_host(self):
"""Make sure a username can be injected via an SSH alias."""
cmd = RemoteCommand('root@host', 'true')
assert cmd.ssh_user == 'root'
assert cmd.ssh_alias == 'host'
assert cmd.have_superuser_privileges

def test_ssh_command_lines(self):
"""Make sure SSH client command lines are correctly generated."""
# Construct a remote command using as much defaults as possible and
Expand Down

0 comments on commit 1adbece

Please sign in to comment.