You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/test/lib/ansible_test/_internal/become.py

53 lines
1.6 KiB
Python

"""Become abstraction for interacting with test hosts."""
from __future__ import annotations
import abc
import shlex
import typing as t
class Become(metaclass=abc.ABCMeta):
"""Base class for become implementations."""
@property
@abc.abstractmethod
def method(self): # type: () -> str
"""The name of the Ansible become plugin that is equivalent to this."""
@abc.abstractmethod
def prepare_command(self, command): # type: (t.List[str]) -> t.List[str]
"""Return the given command, if any, with privilege escalation."""
class Su(Become):
"""Become using 'su'."""
@property
def method(self): # type: () -> str
"""The name of the Ansible become plugin that is equivalent to this."""
return 'su'
def prepare_command(self, command): # type: (t.List[str]) -> t.List[str]
"""Return the given command, if any, with privilege escalation."""
become = ['su', '-l', 'root']
if command:
become.extend(['-c', ' '.join(shlex.quote(c) for c in command)])
return become
class Sudo(Become):
"""Become using 'sudo'."""
@property
def method(self): # type: () -> str
"""The name of the Ansible become plugin that is equivalent to this."""
return 'sudo'
def prepare_command(self, command): # type: (t.List[str]) -> t.List[str]
"""Return the given command, if any, with privilege escalation."""
become = ['sudo', '-in']
if command:
become.extend(['sh', '-c', ' '.join(shlex.quote(c) for c in command)])
return become