pyinfra 0.11.dev3__py3-none-any.whl → 3.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. pyinfra/__init__.py +9 -12
  2. pyinfra/__main__.py +4 -0
  3. pyinfra/api/__init__.py +18 -3
  4. pyinfra/api/arguments.py +406 -0
  5. pyinfra/api/arguments_typed.py +79 -0
  6. pyinfra/api/command.py +274 -0
  7. pyinfra/api/config.py +222 -28
  8. pyinfra/api/connect.py +33 -13
  9. pyinfra/api/connectors.py +27 -0
  10. pyinfra/api/deploy.py +65 -66
  11. pyinfra/api/exceptions.py +67 -18
  12. pyinfra/api/facts.py +253 -202
  13. pyinfra/api/host.py +413 -50
  14. pyinfra/api/inventory.py +121 -160
  15. pyinfra/api/operation.py +432 -262
  16. pyinfra/api/operations.py +273 -260
  17. pyinfra/api/state.py +302 -248
  18. pyinfra/api/util.py +291 -368
  19. pyinfra/connectors/base.py +173 -0
  20. pyinfra/connectors/chroot.py +212 -0
  21. pyinfra/connectors/docker.py +381 -0
  22. pyinfra/connectors/dockerssh.py +297 -0
  23. pyinfra/connectors/local.py +238 -0
  24. pyinfra/connectors/scp/__init__.py +1 -0
  25. pyinfra/connectors/scp/client.py +204 -0
  26. pyinfra/connectors/ssh.py +670 -0
  27. pyinfra/connectors/ssh_util.py +114 -0
  28. pyinfra/connectors/sshuserclient/client.py +309 -0
  29. pyinfra/connectors/sshuserclient/config.py +102 -0
  30. pyinfra/connectors/terraform.py +135 -0
  31. pyinfra/connectors/util.py +410 -0
  32. pyinfra/connectors/vagrant.py +183 -0
  33. pyinfra/context.py +145 -0
  34. pyinfra/facts/__init__.py +7 -6
  35. pyinfra/facts/apk.py +22 -7
  36. pyinfra/facts/apt.py +117 -60
  37. pyinfra/facts/brew.py +100 -15
  38. pyinfra/facts/bsdinit.py +23 -0
  39. pyinfra/facts/cargo.py +37 -0
  40. pyinfra/facts/choco.py +47 -0
  41. pyinfra/facts/crontab.py +195 -0
  42. pyinfra/facts/deb.py +94 -0
  43. pyinfra/facts/dnf.py +48 -0
  44. pyinfra/facts/docker.py +96 -23
  45. pyinfra/facts/efibootmgr.py +113 -0
  46. pyinfra/facts/files.py +630 -58
  47. pyinfra/facts/flatpak.py +77 -0
  48. pyinfra/facts/freebsd.py +70 -0
  49. pyinfra/facts/gem.py +19 -6
  50. pyinfra/facts/git.py +59 -14
  51. pyinfra/facts/gpg.py +150 -0
  52. pyinfra/facts/hardware.py +313 -167
  53. pyinfra/facts/iptables.py +72 -62
  54. pyinfra/facts/launchd.py +44 -0
  55. pyinfra/facts/lxd.py +17 -4
  56. pyinfra/facts/mysql.py +122 -86
  57. pyinfra/facts/npm.py +17 -9
  58. pyinfra/facts/openrc.py +71 -0
  59. pyinfra/facts/opkg.py +246 -0
  60. pyinfra/facts/pacman.py +50 -7
  61. pyinfra/facts/pip.py +24 -7
  62. pyinfra/facts/pipx.py +82 -0
  63. pyinfra/facts/pkg.py +15 -6
  64. pyinfra/facts/pkgin.py +35 -0
  65. pyinfra/facts/podman.py +54 -0
  66. pyinfra/facts/postgres.py +178 -0
  67. pyinfra/facts/postgresql.py +6 -147
  68. pyinfra/facts/rpm.py +105 -0
  69. pyinfra/facts/runit.py +77 -0
  70. pyinfra/facts/selinux.py +161 -0
  71. pyinfra/facts/server.py +746 -285
  72. pyinfra/facts/snap.py +88 -0
  73. pyinfra/facts/systemd.py +139 -0
  74. pyinfra/facts/sysvinit.py +59 -0
  75. pyinfra/facts/upstart.py +35 -0
  76. pyinfra/facts/util/__init__.py +17 -0
  77. pyinfra/facts/util/databases.py +4 -6
  78. pyinfra/facts/util/packaging.py +37 -6
  79. pyinfra/facts/util/units.py +30 -0
  80. pyinfra/facts/util/win_files.py +99 -0
  81. pyinfra/facts/vzctl.py +20 -13
  82. pyinfra/facts/xbps.py +35 -0
  83. pyinfra/facts/yum.py +34 -40
  84. pyinfra/facts/zfs.py +77 -0
  85. pyinfra/facts/zypper.py +42 -0
  86. pyinfra/local.py +45 -83
  87. pyinfra/operations/__init__.py +12 -0
  88. pyinfra/operations/apk.py +98 -0
  89. pyinfra/operations/apt.py +488 -0
  90. pyinfra/operations/brew.py +231 -0
  91. pyinfra/operations/bsdinit.py +59 -0
  92. pyinfra/operations/cargo.py +45 -0
  93. pyinfra/operations/choco.py +61 -0
  94. pyinfra/operations/crontab.py +191 -0
  95. pyinfra/operations/dnf.py +210 -0
  96. pyinfra/operations/docker.py +446 -0
  97. pyinfra/operations/files.py +1939 -0
  98. pyinfra/operations/flatpak.py +94 -0
  99. pyinfra/operations/freebsd/__init__.py +12 -0
  100. pyinfra/operations/freebsd/freebsd_update.py +70 -0
  101. pyinfra/operations/freebsd/pkg.py +219 -0
  102. pyinfra/operations/freebsd/service.py +116 -0
  103. pyinfra/operations/freebsd/sysrc.py +92 -0
  104. pyinfra/operations/gem.py +47 -0
  105. pyinfra/operations/git.py +419 -0
  106. pyinfra/operations/iptables.py +311 -0
  107. pyinfra/operations/launchd.py +45 -0
  108. pyinfra/operations/lxd.py +68 -0
  109. pyinfra/operations/mysql.py +609 -0
  110. pyinfra/operations/npm.py +57 -0
  111. pyinfra/operations/openrc.py +63 -0
  112. pyinfra/operations/opkg.py +88 -0
  113. pyinfra/operations/pacman.py +81 -0
  114. pyinfra/operations/pip.py +205 -0
  115. pyinfra/operations/pipx.py +102 -0
  116. pyinfra/operations/pkg.py +70 -0
  117. pyinfra/operations/pkgin.py +91 -0
  118. pyinfra/operations/postgres.py +436 -0
  119. pyinfra/operations/postgresql.py +30 -0
  120. pyinfra/operations/puppet.py +40 -0
  121. pyinfra/operations/python.py +72 -0
  122. pyinfra/operations/runit.py +184 -0
  123. pyinfra/operations/selinux.py +189 -0
  124. pyinfra/operations/server.py +1099 -0
  125. pyinfra/operations/snap.py +117 -0
  126. pyinfra/operations/ssh.py +216 -0
  127. pyinfra/operations/systemd.py +149 -0
  128. pyinfra/operations/sysvinit.py +141 -0
  129. pyinfra/operations/upstart.py +68 -0
  130. pyinfra/operations/util/__init__.py +12 -0
  131. pyinfra/operations/util/docker.py +251 -0
  132. pyinfra/operations/util/files.py +247 -0
  133. pyinfra/operations/util/packaging.py +336 -0
  134. pyinfra/operations/util/service.py +46 -0
  135. pyinfra/operations/vzctl.py +137 -0
  136. pyinfra/operations/xbps.py +77 -0
  137. pyinfra/operations/yum.py +210 -0
  138. pyinfra/operations/zfs.py +175 -0
  139. pyinfra/operations/zypper.py +192 -0
  140. pyinfra/progress.py +44 -32
  141. pyinfra/py.typed +0 -0
  142. pyinfra/version.py +9 -1
  143. pyinfra-3.5.1.dist-info/METADATA +141 -0
  144. pyinfra-3.5.1.dist-info/RECORD +159 -0
  145. {pyinfra-0.11.dev3.dist-info → pyinfra-3.5.1.dist-info}/WHEEL +1 -2
  146. pyinfra-3.5.1.dist-info/entry_points.txt +12 -0
  147. {pyinfra-0.11.dev3.dist-info → pyinfra-3.5.1.dist-info/licenses}/LICENSE.md +1 -1
  148. pyinfra_cli/__init__.py +1 -0
  149. pyinfra_cli/cli.py +780 -0
  150. pyinfra_cli/commands.py +66 -0
  151. pyinfra_cli/exceptions.py +155 -65
  152. pyinfra_cli/inventory.py +233 -89
  153. pyinfra_cli/log.py +39 -43
  154. pyinfra_cli/main.py +26 -495
  155. pyinfra_cli/prints.py +215 -156
  156. pyinfra_cli/util.py +172 -105
  157. pyinfra_cli/virtualenv.py +25 -20
  158. pyinfra/api/connectors/__init__.py +0 -21
  159. pyinfra/api/connectors/ansible.py +0 -99
  160. pyinfra/api/connectors/docker.py +0 -178
  161. pyinfra/api/connectors/local.py +0 -169
  162. pyinfra/api/connectors/ssh.py +0 -402
  163. pyinfra/api/connectors/sshuserclient/client.py +0 -105
  164. pyinfra/api/connectors/sshuserclient/config.py +0 -90
  165. pyinfra/api/connectors/util.py +0 -63
  166. pyinfra/api/connectors/vagrant.py +0 -155
  167. pyinfra/facts/init.py +0 -176
  168. pyinfra/facts/util/files.py +0 -102
  169. pyinfra/hook.py +0 -41
  170. pyinfra/modules/__init__.py +0 -11
  171. pyinfra/modules/apk.py +0 -64
  172. pyinfra/modules/apt.py +0 -272
  173. pyinfra/modules/brew.py +0 -122
  174. pyinfra/modules/files.py +0 -711
  175. pyinfra/modules/gem.py +0 -30
  176. pyinfra/modules/git.py +0 -115
  177. pyinfra/modules/init.py +0 -344
  178. pyinfra/modules/iptables.py +0 -271
  179. pyinfra/modules/lxd.py +0 -45
  180. pyinfra/modules/mysql.py +0 -347
  181. pyinfra/modules/npm.py +0 -47
  182. pyinfra/modules/pacman.py +0 -60
  183. pyinfra/modules/pip.py +0 -99
  184. pyinfra/modules/pkg.py +0 -43
  185. pyinfra/modules/postgresql.py +0 -245
  186. pyinfra/modules/puppet.py +0 -20
  187. pyinfra/modules/python.py +0 -37
  188. pyinfra/modules/server.py +0 -524
  189. pyinfra/modules/ssh.py +0 -150
  190. pyinfra/modules/util/files.py +0 -52
  191. pyinfra/modules/util/packaging.py +0 -118
  192. pyinfra/modules/vzctl.py +0 -133
  193. pyinfra/modules/yum.py +0 -171
  194. pyinfra/pseudo_modules.py +0 -64
  195. pyinfra-0.11.dev3.dist-info/.DS_Store +0 -0
  196. pyinfra-0.11.dev3.dist-info/METADATA +0 -135
  197. pyinfra-0.11.dev3.dist-info/RECORD +0 -95
  198. pyinfra-0.11.dev3.dist-info/entry_points.txt +0 -3
  199. pyinfra-0.11.dev3.dist-info/top_level.txt +0 -2
  200. pyinfra_cli/__main__.py +0 -40
  201. pyinfra_cli/config.py +0 -92
  202. /pyinfra/{modules/util → connectors}/__init__.py +0 -0
  203. /pyinfra/{api/connectors → connectors}/sshuserclient/__init__.py +0 -0
pyinfra/__init__.py CHANGED
@@ -1,24 +1,21 @@
1
- '''
1
+ """
2
2
  Welcome to pyinfra.
3
- '''
3
+ """
4
4
 
5
5
  import logging
6
6
 
7
-
8
7
  # Global flag set True by `pyinfra_cli.__main__`
9
8
  is_cli = False
10
9
 
11
10
  # Global pyinfra logger
12
- logger = logging.getLogger('pyinfra')
11
+ logger = logging.getLogger("pyinfra")
12
+
13
+ # Trigger context module creation
14
+ from .context import config, host, init_base_classes, inventory, state # noqa
13
15
 
14
16
  # Setup package level version
15
17
  from .version import __version__ # noqa
16
18
 
17
- # Trigger pseudo_* creation
18
- from . import pseudo_modules # noqa
19
-
20
- # Trigger fact index creation
21
- from . import facts # noqa
22
-
23
- # Trigger module imports
24
- from . import modules # noqa # pragma: no cover
19
+ # Initialise base classes - this sets the context modules to point at the underlying
20
+ # class objects (Host, etc), which makes ipython/etc work as expected.
21
+ init_base_classes()
pyinfra/__main__.py ADDED
@@ -0,0 +1,4 @@
1
+ from pyinfra_cli.main import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
pyinfra/api/__init__.py CHANGED
@@ -1,11 +1,26 @@
1
+ from .command import FileDownloadCommand # noqa: F401 # pragma: no cover
2
+ from .command import ( # noqa: F401
3
+ FileUploadCommand,
4
+ FunctionCommand,
5
+ MaskString,
6
+ QuoteString,
7
+ RsyncCommand,
8
+ StringCommand,
9
+ )
1
10
  from .config import Config # noqa: F401 # pragma: no cover
2
11
  from .deploy import deploy # noqa: F401 # pragma: no cover
3
- from .exceptions import ( # noqa: F401 # pragma: no cover
4
- DeployError,
12
+ from .exceptions import DeployError # noqa: F401 # pragma: no cover
13
+ from .exceptions import ( # noqa: F401
14
+ FactError,
15
+ FactTypeError,
16
+ FactValueError,
5
17
  InventoryError,
6
18
  OperationError,
19
+ OperationTypeError,
20
+ OperationValueError,
7
21
  )
8
22
  from .facts import FactBase, ShortFactBase # noqa: F401 # pragma: no cover
23
+ from .host import Host # noqa: F401 # pragma: no cover
9
24
  from .inventory import Inventory # noqa: F401 # pragma: no cover
10
25
  from .operation import operation # noqa: F401 # pragma: no cover
11
- from .state import State # noqa: F401 # pragma: no cover
26
+ from .state import BaseStateCallback, State # noqa: F401 # pragma: no cover
@@ -0,0 +1,406 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import (
4
+ TYPE_CHECKING,
5
+ Any,
6
+ Callable,
7
+ Generic,
8
+ Iterable,
9
+ List,
10
+ Mapping,
11
+ Optional,
12
+ Type,
13
+ TypeVar,
14
+ Union,
15
+ cast,
16
+ get_type_hints,
17
+ )
18
+
19
+ from typing_extensions import TypedDict
20
+
21
+ from pyinfra.api.exceptions import ArgumentTypeError
22
+ from pyinfra.api.util import raise_if_bad_type
23
+ from pyinfra.context import ctx_config
24
+
25
+ if TYPE_CHECKING:
26
+ from pyinfra.api import Config, Host, State
27
+
28
+ T = TypeVar("T")
29
+ default_sentinel = object()
30
+
31
+
32
+ class ArgumentMeta(Generic[T]):
33
+ description: str
34
+ default: Callable[["Config"], T]
35
+ handler: Optional[Callable[["Config", T], T]]
36
+
37
+ def __init__(self, description, default, handler=None) -> None:
38
+ self.description = description
39
+ self.default = default
40
+ self.handler = handler
41
+
42
+
43
+ # Connector arguments
44
+ # These are arguments passed to the various connectors that provide the underlying
45
+ # API to read/write external systems.
46
+
47
+
48
+ # Note: ConnectorArguments is specifically not total as it's used to type many
49
+ # functions via Unpack and we don't want to specify every kwarg.
50
+ class ConnectorArguments(TypedDict, total=False):
51
+ # Auth arguments
52
+ _sudo: bool
53
+ _sudo_user: str
54
+ _use_sudo_login: bool
55
+ _sudo_password: str
56
+ _preserve_sudo_env: bool
57
+ _su_user: str
58
+ _use_su_login: bool
59
+ _preserve_su_env: bool
60
+ _su_shell: str
61
+ _doas: bool
62
+ _doas_user: str
63
+
64
+ # Shell arguments
65
+ _shell_executable: str
66
+ _chdir: str
67
+ _env: Mapping[str, str]
68
+
69
+ # Connector control (outside of command generation)
70
+ _success_exit_codes: Iterable[int]
71
+ _timeout: int
72
+ _get_pty: bool
73
+ _stdin: Union[str, Iterable[str]]
74
+
75
+ # Retry arguments
76
+ _retries: int
77
+ _retry_delay: Union[int, float]
78
+ _retry_until: Optional[Callable[[dict], bool]]
79
+
80
+
81
+ def generate_env(config: "Config", value: dict) -> dict:
82
+ env = config.ENV.copy()
83
+ env.update(value)
84
+ return env
85
+
86
+
87
+ auth_argument_meta: dict[str, ArgumentMeta] = {
88
+ "_sudo": ArgumentMeta(
89
+ "Execute/apply any changes with sudo.",
90
+ default=lambda config: config.SUDO,
91
+ ),
92
+ "_sudo_user": ArgumentMeta(
93
+ "Execute/apply any changes with sudo as a non-root user.",
94
+ default=lambda config: config.SUDO_USER,
95
+ ),
96
+ "_use_sudo_login": ArgumentMeta(
97
+ "Execute sudo with a login shell.",
98
+ default=lambda config: config.USE_SUDO_LOGIN,
99
+ ),
100
+ "_sudo_password": ArgumentMeta(
101
+ "Password to sudo with. If needed and not specified pyinfra will prompt for it.",
102
+ default=lambda config: config.SUDO_PASSWORD,
103
+ ),
104
+ "_preserve_sudo_env": ArgumentMeta(
105
+ "Preserve the shell environment of the connecting user when using sudo.",
106
+ default=lambda config: config.PRESERVE_SUDO_ENV,
107
+ ),
108
+ "_su_user": ArgumentMeta(
109
+ "Execute/apply any changes with this user using su.",
110
+ default=lambda config: config.SU_USER,
111
+ ),
112
+ "_use_su_login": ArgumentMeta(
113
+ "Execute su with a login shell.",
114
+ default=lambda config: config.USE_SU_LOGIN,
115
+ ),
116
+ "_preserve_su_env": ArgumentMeta(
117
+ "Preserve the shell environment of the connecting user when using su.",
118
+ default=lambda config: config.PRESERVE_SU_ENV,
119
+ ),
120
+ "_su_shell": ArgumentMeta(
121
+ "Use this shell (instead of user login shell) when using ``_su``). "
122
+ + "Only available under Linux, for use when using `su` with a user that "
123
+ + "has nologin/similar as their login shell.",
124
+ default=lambda config: config.SU_SHELL,
125
+ ),
126
+ "_doas": ArgumentMeta(
127
+ "Execute/apply any changes with doas.",
128
+ default=lambda config: config.DOAS,
129
+ ),
130
+ "_doas_user": ArgumentMeta(
131
+ "Execute/apply any changes with doas as a non-root user.",
132
+ default=lambda config: config.DOAS_USER,
133
+ ),
134
+ }
135
+
136
+ shell_argument_meta: dict[str, ArgumentMeta] = {
137
+ "_shell_executable": ArgumentMeta(
138
+ "The shell executable to use for executing commands.",
139
+ default=lambda config: config.SHELL,
140
+ ),
141
+ "_chdir": ArgumentMeta(
142
+ "Directory to switch to before executing the command.",
143
+ default=lambda _: None,
144
+ ),
145
+ "_env": ArgumentMeta(
146
+ "Dictionary of environment variables to set.",
147
+ default=lambda _: {},
148
+ handler=generate_env,
149
+ ),
150
+ "_success_exit_codes": ArgumentMeta(
151
+ "List of exit codes to consider a success.",
152
+ default=lambda _: [0],
153
+ ),
154
+ "_timeout": ArgumentMeta(
155
+ "Timeout for *each* command executed during the operation.",
156
+ default=lambda _: None,
157
+ ),
158
+ "_get_pty": ArgumentMeta(
159
+ "Whether to get a pseudoTTY when executing any commands.",
160
+ default=lambda _: False,
161
+ ),
162
+ "_stdin": ArgumentMeta(
163
+ "String or buffer to send to the stdin of any commands.",
164
+ default=lambda _: None,
165
+ ),
166
+ }
167
+
168
+
169
+ # Meta arguments
170
+ # These provide/extend additional operation metadata
171
+
172
+
173
+ class MetaArguments(TypedDict):
174
+ name: str
175
+ _ignore_errors: bool
176
+ _continue_on_error: bool
177
+ _if: Union[List[Callable[[], bool]], Callable[[], bool], None]
178
+
179
+
180
+ meta_argument_meta: dict[str, ArgumentMeta] = {
181
+ # NOTE: name is the only non-_-prefixed argument
182
+ "name": ArgumentMeta(
183
+ "Name of the operation.",
184
+ default=lambda _: None,
185
+ ),
186
+ "_ignore_errors": ArgumentMeta(
187
+ "Ignore errors when executing the operation.",
188
+ default=lambda config: config.IGNORE_ERRORS,
189
+ ),
190
+ "_continue_on_error": ArgumentMeta(
191
+ (
192
+ "Continue executing operation commands after error. "
193
+ "Only applies when ``_ignore_errors`` is true."
194
+ ),
195
+ default=lambda _: False,
196
+ ),
197
+ "_if": ArgumentMeta(
198
+ "Only run this operation if these functions return True",
199
+ default=lambda _: [],
200
+ ),
201
+ }
202
+
203
+
204
+ # Execution arguments
205
+ # These alter how pyinfra is to execute an operation. Notably these must all have the same value
206
+ # over every target host for the same operation.
207
+
208
+
209
+ class ExecutionArguments(TypedDict):
210
+ _parallel: int
211
+ _run_once: bool
212
+ _serial: bool
213
+
214
+
215
+ execution_argument_meta: dict[str, ArgumentMeta] = {
216
+ "_parallel": ArgumentMeta(
217
+ "Run this operation in batches of hosts.",
218
+ default=lambda config: config.PARALLEL,
219
+ ),
220
+ "_run_once": ArgumentMeta(
221
+ "Only execute this operation once, on the first host to see it.",
222
+ default=lambda _: False,
223
+ ),
224
+ "_serial": ArgumentMeta(
225
+ "Run this operation host by host, rather than in parallel.",
226
+ default=lambda _: False,
227
+ ),
228
+ }
229
+
230
+
231
+ class AllArguments(ConnectorArguments, MetaArguments, ExecutionArguments):
232
+ pass
233
+
234
+
235
+ def all_global_arguments() -> List[tuple[str, Type]]:
236
+ """Return all global arguments and their types."""
237
+ return list(get_type_hints(AllArguments).items())
238
+
239
+
240
+ # Create a dictionary for retry arguments
241
+ retry_argument_meta: dict[str, ArgumentMeta] = {
242
+ "_retries": ArgumentMeta(
243
+ "Number of times to retry failed operations.",
244
+ default=lambda config: config.RETRY,
245
+ ),
246
+ "_retry_delay": ArgumentMeta(
247
+ "Delay in seconds between retry attempts.",
248
+ default=lambda config: config.RETRY_DELAY,
249
+ ),
250
+ "_retry_until": ArgumentMeta(
251
+ "Callable taking output data that returns True to continue retrying.",
252
+ default=lambda config: None,
253
+ ),
254
+ }
255
+
256
+ all_argument_meta: dict[str, ArgumentMeta] = {
257
+ **auth_argument_meta,
258
+ **shell_argument_meta,
259
+ **meta_argument_meta,
260
+ **execution_argument_meta,
261
+ **retry_argument_meta, # Add retry arguments
262
+ }
263
+
264
+ EXECUTION_KWARG_KEYS = list(ExecutionArguments.__annotations__.keys())
265
+ CONNECTOR_ARGUMENT_KEYS = list(ConnectorArguments.__annotations__.keys())
266
+
267
+ __argument_docs__ = {
268
+ "Privilege & user escalation": (
269
+ auth_argument_meta,
270
+ """
271
+ .. caution::
272
+ When combining privilege escalation arguments it is important to know the order they
273
+ are applied: ``doas`` -> ``sudo`` -> ``su``. For example
274
+ ``_sudo=True,_su_user="pyinfra"`` yields a command like ``sudo su pyinfra..``.
275
+ """,
276
+ """
277
+ .. code:: python
278
+
279
+ # Execute a command with sudo
280
+ server.user(
281
+ name="Create pyinfra user using sudo",
282
+ user="pyinfra",
283
+ _sudo=True,
284
+ )
285
+
286
+ # Execute a command with a specific sudo password
287
+ server.user(
288
+ name="Create pyinfra user using sudo",
289
+ user="pyinfra",
290
+ _sudo=True,
291
+ _sudo_password="my-secret-password",
292
+ )
293
+ """,
294
+ ),
295
+ "Shell control & features": (
296
+ shell_argument_meta,
297
+ "",
298
+ """
299
+ .. code:: python
300
+
301
+ # Execute from a specific directory
302
+ server.shell(
303
+ name="Bootstrap nginx params",
304
+ commands=["openssl dhparam -out dhparam.pem 4096"],
305
+ _chdir="/etc/ssl/certs",
306
+ )
307
+ """,
308
+ ),
309
+ "Operation meta & callbacks": (meta_argument_meta, "", ""),
310
+ "Execution strategy": (execution_argument_meta, "", ""),
311
+ "Retry behavior": (
312
+ retry_argument_meta,
313
+ """
314
+ Retry arguments allow you to automatically retry operations that fail. You can specify
315
+ how many times to retry, the delay between retries, and optionally a condition
316
+ function to determine when to stop retrying.
317
+ """,
318
+ """
319
+ .. code:: python
320
+
321
+ # Retry a command up to 3 times with the default 5 second delay
322
+ server.shell(
323
+ name="Run flaky command with retries",
324
+ commands=["flaky_command"],
325
+ _retries=3,
326
+ )
327
+ # Retry with a custom delay
328
+ server.shell(
329
+ name="Run flaky command with custom delay",
330
+ commands=["flaky_command"],
331
+ _retries=2,
332
+ _retry_delay=10, # 10 second delay between retries
333
+ )
334
+ # Retry with a custom condition
335
+ def retry_on_specific_error(output_data):
336
+ # Retry if stderr contains "temporary failure"
337
+ for line in output_data["stderr_lines"]:
338
+ if "temporary failure" in line.lower():
339
+ return True
340
+ return False
341
+
342
+ server.shell(
343
+ name="Run command with conditional retry",
344
+ commands=["flaky_command"],
345
+ _retries=5,
346
+ _retry_until=retry_on_specific_error,
347
+ )
348
+ """,
349
+ ),
350
+ }
351
+
352
+
353
+ def pop_global_arguments(
354
+ state: "State",
355
+ host: "Host",
356
+ kwargs: dict[str, Any],
357
+ ) -> tuple[AllArguments, list[str]]:
358
+ """
359
+ Pop and return operation global keyword arguments, in preferred order:
360
+
361
+ + From the current context (a direct @operator or @deploy function being called)
362
+ + From any current @deploy context (deploy kwargs)
363
+ + From the host data variables
364
+ + From the config variables
365
+ """
366
+
367
+ config = state.config
368
+ if ctx_config.isset():
369
+ config = ctx_config.get()
370
+ assert config is not None
371
+
372
+ cdkwargs = host.current_deploy_kwargs
373
+ meta_kwargs: dict[str, Any] = cdkwargs or {} # type: ignore[assignment]
374
+
375
+ arguments: dict[str, Any] = {}
376
+ found_keys: list[str] = []
377
+
378
+ for key, type_ in all_global_arguments():
379
+ argument_meta = all_argument_meta[key]
380
+ handler = argument_meta.handler
381
+ default: Any = argument_meta.default(config)
382
+
383
+ host_default = getattr(host.data, key, default_sentinel)
384
+ if host_default is not default_sentinel:
385
+ default = host_default
386
+
387
+ if key in kwargs:
388
+ found_keys.append(key)
389
+ value = kwargs.pop(key)
390
+ else:
391
+ value = meta_kwargs.get(key, default)
392
+
393
+ if handler:
394
+ value = handler(config, value)
395
+
396
+ if value != default:
397
+ raise_if_bad_type(
398
+ value,
399
+ type_,
400
+ ArgumentTypeError,
401
+ f"Invalid argument `{key}`:",
402
+ )
403
+
404
+ # TODO: why is type failing here?
405
+ arguments[key] = value # type: ignore
406
+ return cast(AllArguments, arguments), found_keys
@@ -0,0 +1,79 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import (
4
+ TYPE_CHECKING,
5
+ Callable,
6
+ Generator,
7
+ Generic,
8
+ Iterable,
9
+ List,
10
+ Mapping,
11
+ Optional,
12
+ Union,
13
+ )
14
+
15
+ from typing_extensions import ParamSpec, Protocol
16
+
17
+ if TYPE_CHECKING:
18
+ from pyinfra.api.operation import OperationMeta
19
+
20
+ P = ParamSpec("P")
21
+
22
+
23
+ # Unfortunately we have to re-type out all of the global arguments here because
24
+ # Python typing doesn't (yet) support merging kwargs. This acts as the operation
25
+ # decorator output function which merges actual operation args/kwargs in paramspec
26
+ # with the global arguments available in all operations.
27
+ # The nature of "global arguments" is somewhat opposed to static typing as it's
28
+ # indirect and somewhat magic, but we are where we are.
29
+ class PyinfraOperation(Generic[P], Protocol):
30
+ _inner: Callable[P, Generator]
31
+
32
+ def __call__(
33
+ self,
34
+ #
35
+ # ConnectorArguments
36
+ #
37
+ # Auth
38
+ _sudo: bool = False,
39
+ _sudo_user: Optional[str] = None,
40
+ _use_sudo_login: bool = False,
41
+ _sudo_password: Optional[str] = None,
42
+ _preserve_sudo_env: bool = False,
43
+ _su_user: Optional[str] = None,
44
+ _use_su_login: bool = False,
45
+ _preserve_su_env: bool = False,
46
+ _su_shell: Optional[str] = None,
47
+ _doas: bool = False,
48
+ _doas_user: Optional[str] = None,
49
+ # Shell arguments
50
+ _shell_executable: Optional[str] = None,
51
+ _chdir: Optional[str] = None,
52
+ _env: Optional[Mapping[str, str]] = None,
53
+ # Connector control
54
+ _success_exit_codes: Iterable[int] = (0,),
55
+ _timeout: Optional[int] = None,
56
+ _get_pty: bool = False,
57
+ _stdin: Union[None, str, list[str], tuple[str, ...]] = None,
58
+ #
59
+ # MetaArguments
60
+ #
61
+ name: Optional[str] = None,
62
+ _ignore_errors: bool = False,
63
+ _continue_on_error: bool = False,
64
+ _if: Union[List[Callable[[], bool]], Callable[[], bool], None] = None,
65
+ #
66
+ # ExecutionArguments
67
+ #
68
+ _parallel: Optional[int] = None,
69
+ _run_once: bool = False,
70
+ _serial: bool = False,
71
+ #
72
+ # op args
73
+ #
74
+ *args: P.args,
75
+ #
76
+ # op kwargs
77
+ #
78
+ **kwargs: P.kwargs,
79
+ ) -> "OperationMeta": ...