Debian Codegraph @

test/lib/ansible_test/_internal/become.py

raw
1 """Become abstraction for interacting with test hosts."""
2
3 from __future__ import annotations
4
5 import abc
6 import shlex
7
8 from .util import (
9 get_subclasses,
10 )
11
12
13 class Become(metaclass=abc.ABCMeta):
14 """Base class for become implementations."""
15
16 @classmethod
17 def name(cls) -> str:
18 """The name of this plugin."""
19 return cls.__name__.lower()
20
21 @property
22 @abc.abstractmethod
23 def method(self) -> str:
24 """The name of the Ansible become plugin that is equivalent to this."""
25
26 @abc.abstractmethod
27 def prepare_command(self, command: list[str]) -> list[str]:
28 """Return the given command, if any, with privilege escalation."""
29
30
31 class Doas(Become):
32 """Become using 'doas'."""
33
34 @property
35 def method(self) -> str:
36 """The name of the Ansible become plugin that is equivalent to this."""
37 raise NotImplementedError('Ansible has no built-in doas become plugin.')
38
39 def prepare_command(self, command: list[str]) -> list[str]:
40 """Return the given command, if any, with privilege escalation."""
41 become = ['doas', '-n']
42
43 if command:
44 become.extend(['sh', '-c', shlex.join(command)])
45 else:
46 become.extend(['-s'])
47
48 return become
49
50
51 class DoasSudo(Doas):
52 """Become using 'doas' in ansible-test and then after bootstrapping use 'sudo' for other ansible commands."""
53
54 @classmethod
55 def name(cls) -> str:
56 """The name of this plugin."""
57 return 'doas_sudo'
58
59 @property
60 def method(self) -> str:
61 """The name of the Ansible become plugin that is equivalent to this."""
62 return 'sudo'
63
64
65 class Su(Become):
66 """Become using 'su'."""
67
68 @property
69 def method(self) -> str:
70 """The name of the Ansible become plugin that is equivalent to this."""
71 return 'su'
72
73 def prepare_command(self, command: list[str]) -> list[str]:
74 """Return the given command, if any, with privilege escalation."""
75 become = ['su', '-l', 'root']
76
77 if command:
78 become.extend(['-c', shlex.join(command)])
79
80 return become
81
82
83 class SuSudo(Su):
84 """Become using 'su' in ansible-test and then after bootstrapping use 'sudo' for other ansible commands."""
85
86 @classmethod
87 def name(cls) -> str:
88 """The name of this plugin."""
89 return 'su_sudo'
90
91 @property
92 def method(self) -> str:
93 """The name of the Ansible become plugin that is equivalent to this."""
94 return 'sudo'
95
96
97 class Sudo(Become):
98 """Become using 'sudo'."""
99
100 @property
101 def method(self) -> str:
102 """The name of the Ansible become plugin that is equivalent to this."""
103 return 'sudo'
104
105 def prepare_command(self, command: list[str]) -> list[str]:
106 """Return the given command, if any, with privilege escalation."""
107 become = ['sudo', '-in']
108
109 if command:
110 become.extend(['sh', '-c', shlex.join(command)])
111
112 return become
113
114
115 SUPPORTED_BECOME_METHODS = {cls.name(): cls for cls in get_subclasses(Become)}