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

executer: support run with sudo. #140

Merged
merged 9 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ that means SSH server must be running there already.
host.executor_factory = rrmngmnt.ssh.RemoteExecutorFactory(use_pkey=True)

exec = h.executor()
# Run with sudo
exec = h.executor(sudo=True)

print exec.run_cmd(['echo', 'Hello World'])


Expand Down
4 changes: 2 additions & 2 deletions rrmngmnt/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def package_manager(self):
def power_manager(self):
return self.get_power_manager()

def executor(self, user=None, pkey=False):
def executor(self, user=None, pkey=False, sudo=False):
"""
Gives you executor to allowing command execution

Expand All @@ -229,7 +229,7 @@ def executor(self, user=None, pkey=False):
ef = copy.copy(ssh.RemoteExecutorFactory)
ef.use_pkey = pkey
return ef(self.ip, user)
return self.executor_factory.build(self, user)
return self.executor_factory.build(self, user, sudo=sudo)

def run_command(
self, command, input_=None, tcp_timeout=None, io_timeout=None,
Expand Down
11 changes: 8 additions & 3 deletions rrmngmnt/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,20 @@ def run(self, input_, timeout=None, get_pty=False):
self.err = normalize_string(err.read())
return self.rc, self.out, self.err

def __init__(self, user, address, use_pkey=False, port=22):
def __init__(self, user, address, use_pkey=False, port=22, sudo=False):
"""
Args:
use_pkey (bool): Use ssh private key in the connection
user (instance of User): User
address (str): Ip / hostname
port (int): Port to connect
sudo (bool): Use sudo to execute command.
"""
super(RemoteExecutor, self).__init__(user)
self.address = address
self.use_pkey = use_pkey
self.port = port
self.sudo = sudo

def session(self, timeout=None):
"""
Expand All @@ -240,6 +242,9 @@ def run_cmd(self, cmd, input_=None, tcp_timeout=None, io_timeout=None):
Returns:
tuple (int, str, str): Rc, out, err
"""
if self.sudo:
cmd.insert(0, "sudo")

with self.session(tcp_timeout) as session:
return session.run_cmd(cmd, input_, io_timeout)

Expand Down Expand Up @@ -306,6 +311,6 @@ def __init__(self, use_pkey=False, port=22):
self.use_pkey = use_pkey
self.port = port

def build(self, host, user):
def build(self, host, user, sudo=False):
return RemoteExecutor(
user, host.ip, use_pkey=self.use_pkey, port=self.port)
user, host.ip, use_pkey=self.use_pkey, port=self.port, sudo=sudo)
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(self, cmd_to_data, files_content):
self.cmd_to_data = cmd_to_data.copy()
self.files_content = files_content

def build(self, host, user):
def build(self, host, user, sudo):
fe = FakeExecutor(user, host.ip)
fe.cmd_to_data = self.cmd_to_data.copy()
fe.files_content = self.files_content
Expand Down
12 changes: 9 additions & 3 deletions tests/test_package_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,26 @@ def set_base_data(cls):
list2cmdline(['which', manager_]): rc,
})

def get_host(self, ip='1.1.1.1'):
def get_host(self, ip='1.1.1.1', sudo=False):
h = Host(ip)
h.add_user(User('root', '11111'))
if sudo:
h.executor(sudo=sudo)

return h

def get_pm(self):
return self.get_host().package_manager
def get_pm(self, sudo=False):
return self.get_host(sudo=sudo).package_manager

def test_info(self):
assert not self.get_pm().info(self.packages['installed_1'])

def test_info_negative(self):
assert not self.get_pm().info(self.packages['not_installed'])

def test_exist_with_sudo(self):
assert self.get_pm(sudo=True).exist(self.packages['installed_1'])

def test_exist(self):
assert self.get_pm().exist(self.packages['installed_1'])

Expand Down