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
@@ -0,0 +1,436 @@
1
+ """
2
+ The PostgreSQL modules manage PostgreSQL databases, users and privileges.
3
+
4
+ Requires the ``psql`` CLI executable on the target host(s).
5
+
6
+ All operations in this module take four optional arguments:
7
+ + ``psql_user``: the username to connect to postgresql to
8
+ + ``psql_password``: the password for the connecting user
9
+ + ``psql_host``: the hostname of the server to connect to
10
+ + ``psql_port``: the port of the server to connect to
11
+ + ``psql_database``: the database on the server to connect to
12
+
13
+ See example/postgresql.py for detailed example
14
+
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ from pyinfra import host
20
+ from pyinfra.api import MaskString, QuoteString, StringCommand, operation
21
+ from pyinfra.facts.postgres import (
22
+ PostgresDatabases,
23
+ PostgresRoles,
24
+ make_execute_psql_command,
25
+ make_psql_command,
26
+ )
27
+
28
+
29
+ @operation(is_idempotent=False)
30
+ def sql(
31
+ sql: str,
32
+ # Details for speaking to PostgreSQL via `psql` CLI
33
+ psql_user: str | None = None,
34
+ psql_password: str | None = None,
35
+ psql_host: str | None = None,
36
+ psql_port: int | None = None,
37
+ psql_database: str | None = None,
38
+ ):
39
+ """
40
+ Execute arbitrary SQL against PostgreSQL.
41
+
42
+ + sql: SQL command(s) to execute
43
+ + psql_*: global module arguments, see above
44
+ """
45
+
46
+ yield make_execute_psql_command(
47
+ sql,
48
+ user=psql_user,
49
+ password=psql_password,
50
+ host=psql_host,
51
+ port=psql_port,
52
+ database=psql_database,
53
+ )
54
+
55
+
56
+ @operation()
57
+ def role(
58
+ role: str,
59
+ present: bool = True,
60
+ password: str | None = None,
61
+ login: bool = True,
62
+ superuser: bool = False,
63
+ inherit: bool = False,
64
+ createdb: bool = False,
65
+ createrole: bool = False,
66
+ replication: bool = False,
67
+ connection_limit: int | None = None,
68
+ # Details for speaking to PostgreSQL via `psql` CLI
69
+ psql_user: str | None = None,
70
+ psql_password: str | None = None,
71
+ psql_host: str | None = None,
72
+ psql_port: int | None = None,
73
+ psql_database: str | None = None,
74
+ ):
75
+ """
76
+ Add/remove PostgreSQL roles.
77
+
78
+ + role: name of the role
79
+ + present: whether the role should be present or absent
80
+ + password: the password for the role
81
+ + login: whether the role can login
82
+ + superuser: whether role will be a superuser
83
+ + inherit: whether the role inherits from other roles
84
+ + createdb: whether the role is allowed to create databases
85
+ + createrole: whether the role is allowed to create new roles
86
+ + replication: whether this role is allowed to replicate
87
+ + connection_limit: the connection limit for the role
88
+ + psql_*: global module arguments, see above
89
+
90
+ Updates:
91
+ pyinfra will not attempt to change existing roles - it will either
92
+ create or drop roles, but not alter them (if the role exists this
93
+ operation will make no changes).
94
+
95
+ **Example:**
96
+
97
+ .. code:: python
98
+
99
+ postgresql.role(
100
+ name="Create the pyinfra PostgreSQL role",
101
+ role="pyinfra",
102
+ password="somepassword",
103
+ superuser=True,
104
+ login=True,
105
+ _sudo_user="postgres",
106
+ )
107
+
108
+ """
109
+
110
+ roles = host.get_fact(
111
+ PostgresRoles,
112
+ psql_user=psql_user,
113
+ psql_password=psql_password,
114
+ psql_host=psql_host,
115
+ psql_port=psql_port,
116
+ psql_database=psql_database,
117
+ )
118
+
119
+ is_present = role in roles
120
+
121
+ # User not wanted?
122
+ if not present:
123
+ if is_present:
124
+ yield make_execute_psql_command(
125
+ 'DROP ROLE "{0}"'.format(role),
126
+ user=psql_user,
127
+ password=psql_password,
128
+ host=psql_host,
129
+ port=psql_port,
130
+ database=psql_database,
131
+ )
132
+ else:
133
+ host.noop("postgresql role {0} does not exist".format(role))
134
+ return
135
+
136
+ # If we want the user and they don't exist
137
+ if not is_present:
138
+ sql_bits = ['CREATE ROLE "{0}"'.format(role)]
139
+
140
+ for key, value in (
141
+ ("LOGIN", login),
142
+ ("SUPERUSER", superuser),
143
+ ("INHERIT", inherit),
144
+ ("CREATEDB", createdb),
145
+ ("CREATEROLE", createrole),
146
+ ("REPLICATION", replication),
147
+ ):
148
+ if value:
149
+ sql_bits.append(key)
150
+
151
+ if connection_limit:
152
+ sql_bits.append("CONNECTION LIMIT {0}".format(connection_limit))
153
+
154
+ if password:
155
+ sql_bits.append(MaskString("PASSWORD '{0}'".format(password)))
156
+
157
+ yield make_execute_psql_command(
158
+ StringCommand(*sql_bits),
159
+ user=psql_user,
160
+ password=psql_password,
161
+ host=psql_host,
162
+ port=psql_port,
163
+ database=psql_database,
164
+ )
165
+ else:
166
+ # Check if any attributes need updating
167
+ current_role = roles[role]
168
+ should_execute = False
169
+ sql_bits = ['ALTER ROLE "{0}"'.format(role)]
170
+ if login and "login" in current_role and current_role["login"] != login:
171
+ sql_bits.append("LOGIN")
172
+ should_execute = True
173
+ if superuser and "superuser" in current_role and current_role["superuser"] != superuser:
174
+ sql_bits.append("SUPERUSER")
175
+ should_execute = True
176
+ if inherit and "inherit" in current_role and current_role["inherit"] != inherit:
177
+ sql_bits.append("INHERIT")
178
+ should_execute = True
179
+ if createdb and "createdb" in current_role and current_role["createdb"] != createdb:
180
+ sql_bits.append("CREATEDB")
181
+ should_execute = True
182
+ if createrole and "createrole" in current_role and current_role["createrole"] != createrole:
183
+ sql_bits.append("CREATEROLE")
184
+ should_execute = True
185
+ if (
186
+ connection_limit
187
+ and "connection_limit" in current_role
188
+ and roles[role]["connection_limit"] != connection_limit
189
+ ):
190
+ sql_bits.append("CONNECTION LIMIT {0}".format(connection_limit))
191
+ should_execute = True
192
+ if password:
193
+ sql_bits.append(MaskString("PASSWORD '{0}'".format(password)))
194
+ should_execute = True
195
+
196
+ if should_execute:
197
+ yield make_execute_psql_command(
198
+ StringCommand(*sql_bits),
199
+ user=psql_user,
200
+ password=psql_password,
201
+ host=psql_host,
202
+ port=psql_port,
203
+ database=psql_database,
204
+ )
205
+ else:
206
+ host.noop("postgresql role {0} exists and does not need updates".format(role))
207
+
208
+
209
+ @operation()
210
+ def database(
211
+ database: str,
212
+ present=True,
213
+ owner: str | None = None,
214
+ template: str | None = None,
215
+ encoding: str | None = None,
216
+ lc_collate: str | None = None,
217
+ lc_ctype: str | None = None,
218
+ tablespace: str | None = None,
219
+ connection_limit: int | None = None,
220
+ # Details for speaking to PostgreSQL via `psql` CLI
221
+ psql_user: str | None = None,
222
+ psql_password: str | None = None,
223
+ psql_host: str | None = None,
224
+ psql_port: int | None = None,
225
+ psql_database: str | None = None,
226
+ ):
227
+ """
228
+ Add/remove PostgreSQL databases.
229
+
230
+ + name: name of the database
231
+ + present: whether the database should exist or not
232
+ + owner: the PostgreSQL role that owns the database
233
+ + template: name of the PostgreSQL template to use
234
+ + encoding: encoding of the database
235
+ + lc_collate: lc_collate of the database
236
+ + lc_ctype: lc_ctype of the database
237
+ + tablespace: the tablespace to use for the template
238
+ + connection_limit: the connection limit to apply to the database
239
+ + psql_*: global module arguments, see above
240
+
241
+ Updates:
242
+ pyinfra will change existing databases - but some parameters are not
243
+ changeable (template, encoding, lc_collate and lc_ctype).
244
+
245
+ **Example:**
246
+
247
+ .. code:: python
248
+
249
+ postgresql.database(
250
+ name="Create the pyinfra_stuff database",
251
+ database="pyinfra_stuff",
252
+ owner="pyinfra",
253
+ encoding="UTF8",
254
+ _sudo_user="postgres",
255
+ )
256
+
257
+ """
258
+
259
+ current_databases = host.get_fact(
260
+ PostgresDatabases,
261
+ psql_user=psql_user,
262
+ psql_password=psql_password,
263
+ psql_host=psql_host,
264
+ psql_port=psql_port,
265
+ psql_database=psql_database,
266
+ )
267
+
268
+ is_present = database in current_databases
269
+
270
+ if not present:
271
+ if is_present:
272
+ yield make_execute_psql_command(
273
+ 'DROP DATABASE "{0}"'.format(database),
274
+ user=psql_user,
275
+ password=psql_password,
276
+ host=psql_host,
277
+ port=psql_port,
278
+ database=psql_database,
279
+ )
280
+ else:
281
+ host.noop("postgresql database {0} does not exist".format(database))
282
+ return
283
+
284
+ # We want the database but it doesn't exist
285
+ if present and not is_present:
286
+ sql_bits = ['CREATE DATABASE "{0}"'.format(database)]
287
+
288
+ for key, value in (
289
+ ("OWNER", '"{0}"'.format(owner) if owner else owner),
290
+ ("TEMPLATE", template),
291
+ ("ENCODING", encoding),
292
+ ("LC_COLLATE", "'{0}'".format(lc_collate) if lc_collate else lc_collate),
293
+ ("LC_CTYPE", "'{0}'".format(lc_ctype) if lc_ctype else lc_ctype),
294
+ ("TABLESPACE", tablespace),
295
+ ("CONNECTION LIMIT", connection_limit),
296
+ ):
297
+ if value:
298
+ sql_bits.append("{0} {1}".format(key, value))
299
+
300
+ yield make_execute_psql_command(
301
+ StringCommand(*sql_bits),
302
+ user=psql_user,
303
+ password=psql_password,
304
+ host=psql_host,
305
+ port=psql_port,
306
+ database=psql_database,
307
+ )
308
+ else:
309
+ current_db = current_databases[database]
310
+
311
+ for key, value, current_value in (
312
+ ("TEMPLATE", template, current_db.get("istemplate")),
313
+ ("ENCODING", encoding, current_db.get("encoding")),
314
+ ("LC_COLLATE", lc_collate, None),
315
+ ("LC_CTYPE", lc_ctype, None),
316
+ ):
317
+ if value and (current_value is None or current_value != value):
318
+ host.noop(
319
+ "postgresql database {0} already exists, skipping {1}".format(database, key)
320
+ )
321
+
322
+ sql_bits = []
323
+
324
+ if owner and "owner" in current_db and current_db["owner"] != owner:
325
+ sql_bits.append('ALTER DATABASE "{0}" OWNER TO "{1}";'.format(database, owner))
326
+
327
+ if tablespace and "tablespace" in current_db and current_db["tablespace"] != tablespace:
328
+ sql_bits.append(
329
+ 'ALTER DATABASE "{0}" SET TABLESPACE "{1}";'.format(database, tablespace)
330
+ )
331
+
332
+ if (
333
+ connection_limit
334
+ and "connlimit" in current_db
335
+ and current_db["connlimit"] != connection_limit
336
+ ):
337
+ sql_bits.append(
338
+ 'ALTER DATABASE "{0}" CONNECTION LIMIT {1};'.format(database, connection_limit)
339
+ )
340
+
341
+ if len(sql_bits) > 0:
342
+ yield make_execute_psql_command(
343
+ StringCommand(*sql_bits),
344
+ user=psql_user,
345
+ password=psql_password,
346
+ host=psql_host,
347
+ port=psql_port,
348
+ database=psql_database,
349
+ )
350
+ else:
351
+ host.noop(
352
+ "postgresql database {0} already exists with the same parameters".format(database)
353
+ )
354
+
355
+
356
+ @operation(is_idempotent=False)
357
+ def dump(
358
+ dest: str,
359
+ # Details for speaking to PostgreSQL via `psql` CLI
360
+ psql_user: str | None = None,
361
+ psql_password: str | None = None,
362
+ psql_host: str | None = None,
363
+ psql_port: int | None = None,
364
+ psql_database: str | None = None,
365
+ ):
366
+ """
367
+ Dump a PostgreSQL database into a ``.sql`` file. Requires ``pg_dump``.
368
+
369
+ + dest: name of the file to dump the SQL to
370
+ + psql_*: global module arguments, see above
371
+
372
+ **Example:**
373
+
374
+ .. code:: python
375
+
376
+ postgresql.dump(
377
+ name="Dump the pyinfra_stuff database",
378
+ dest="/tmp/pyinfra_stuff.dump",
379
+ sudo_user="postgres",
380
+ )
381
+
382
+ """
383
+
384
+ yield StringCommand(
385
+ make_psql_command(
386
+ executable="pg_dump",
387
+ user=psql_user,
388
+ password=psql_password,
389
+ host=psql_host,
390
+ port=psql_port,
391
+ database=psql_database,
392
+ ),
393
+ ">",
394
+ QuoteString(dest),
395
+ )
396
+
397
+
398
+ @operation(is_idempotent=False)
399
+ def load(
400
+ src: str,
401
+ # Details for speaking to PostgreSQL via `psql` CLI
402
+ psql_user: str | None = None,
403
+ psql_password: str | None = None,
404
+ psql_host: str | None = None,
405
+ psql_port: int | None = None,
406
+ psql_database: str | None = None,
407
+ ):
408
+ """
409
+ Load ``.sql`` file into a database.
410
+
411
+ + src: the filename to read from
412
+ + psql_*: global module arguments, see above
413
+
414
+ **Example:**
415
+
416
+ .. code:: python
417
+
418
+ postgresql.load(
419
+ name="Import the pyinfra_stuff dump into pyinfra_stuff_copy",
420
+ src="/tmp/pyinfra_stuff.dump",
421
+ sudo_user="postgres",
422
+ )
423
+
424
+ """
425
+
426
+ yield StringCommand(
427
+ make_psql_command(
428
+ user=psql_user,
429
+ password=psql_password,
430
+ host=psql_host,
431
+ port=psql_port,
432
+ database=psql_database,
433
+ ),
434
+ "<",
435
+ QuoteString(src),
436
+ )
@@ -0,0 +1,30 @@
1
+ from __future__ import annotations
2
+
3
+ from pyinfra.api import operation
4
+
5
+ from . import postgres
6
+
7
+
8
+ @operation(is_idempotent=False, is_deprecated=True, deprecated_for="postgres.sql")
9
+ def sql(*args, **kwargs):
10
+ yield from postgres.sql._inner(*args, **kwargs)
11
+
12
+
13
+ @operation(is_idempotent=False, is_deprecated=True, deprecated_for="postgres.role")
14
+ def role(*args, **kwargs):
15
+ yield from postgres.role._inner(*args, **kwargs)
16
+
17
+
18
+ @operation(is_idempotent=False, is_deprecated=True)
19
+ def database(*args, **kwargs):
20
+ yield from postgres.database._inner(*args, **kwargs)
21
+
22
+
23
+ @operation(is_idempotent=False, is_deprecated=True)
24
+ def dump(*args, **kwargs):
25
+ yield from postgres.dump._inner(*args, **kwargs)
26
+
27
+
28
+ @operation(is_idempotent=False, is_deprecated=True)
29
+ def load(*args, **kwargs):
30
+ yield from postgres.load._inner(*args, **kwargs)
@@ -0,0 +1,40 @@
1
+ from __future__ import annotations
2
+
3
+ from pyinfra.api import operation
4
+
5
+
6
+ @operation(is_idempotent=False)
7
+ def agent(server: str | None = None, port: int | None = None):
8
+ """
9
+ Run puppet agent
10
+
11
+ + server: master server URL
12
+ + port: puppet master port
13
+
14
+ Note: Either 'USE_SUDO_LOGIN=True' or 'USE_SU_LOGIN=True'
15
+ for puppet.agent() as `puppet` is added to the path in
16
+ the .bash_profile.
17
+
18
+ **Example:**
19
+
20
+ .. code:: python
21
+
22
+ puppet.agent()
23
+
24
+ # We also expect a return code of:
25
+ # 0=no changes or 2=changes applied
26
+ puppet.agent(
27
+ name="Run the puppet agent",
28
+ success_exit_codes=[0, 2],
29
+ )
30
+
31
+ """
32
+
33
+ args = []
34
+
35
+ if server:
36
+ args.append("--server=%s" % server)
37
+ if port:
38
+ args.append("--masterport=%s" % port)
39
+
40
+ yield "puppet agent -t %s" % " ".join(args)
@@ -0,0 +1,72 @@
1
+ """
2
+ The Python module allows you to execute Python code within the context of a deploy.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from typing import Callable
8
+
9
+ from pyinfra.api import FunctionCommand, operation
10
+
11
+
12
+ @operation(is_idempotent=False, _set_in_op=False)
13
+ def call(function: Callable, *args, **kwargs):
14
+ """
15
+ Execute a Python function within a deploy.
16
+
17
+ + function: the function to execute
18
+ + args: arguments to pass to the function
19
+ + kwargs: keyword arguments to pass to the function
20
+
21
+ **Example:**
22
+
23
+ .. code:: python
24
+
25
+ def my_callback(hello=None):
26
+ command = 'echo hello'
27
+ if hello:
28
+ command = command + ' ' + hello
29
+
30
+ status, stdout, stderr = host.run_shell_command(command=command, sudo=SUDO)
31
+ assert status is True # ensure the command executed OK
32
+
33
+ if 'hello ' not in '\\n'.join(stdout): # stdout/stderr is a *list* of lines
34
+ raise Exception(
35
+ f'`{command}` problem with callback stdout:{stdout} stderr:{stderr}',
36
+ )
37
+
38
+ python.call(
39
+ name="Run my_callback function",
40
+ function=my_callback,
41
+ hello="world",
42
+ )
43
+
44
+ """
45
+
46
+ yield FunctionCommand(function, args, kwargs)
47
+
48
+
49
+ @operation(is_idempotent=False, _set_in_op=False)
50
+ def raise_exception(exception: Exception, *args, **kwargs):
51
+ """
52
+ Raise a Python exception within a deploy.
53
+
54
+ + exception: the exception class to raise
55
+ + args: arguments passed to the exception creation
56
+ + kwargs: keyword arguments passed to the exception creation
57
+
58
+ **Example**:
59
+
60
+ .. code:: python
61
+
62
+ python.raise_exception(
63
+ name="Raise NotImplementedError exception",
64
+ exception=NotImplementedError,
65
+ message="This is not implemented",
66
+ )
67
+ """
68
+
69
+ def raise_exc(*args, **kwargs): # pragma: no cover
70
+ raise exception(*args, **kwargs) # type: ignore[operator]
71
+
72
+ yield FunctionCommand(raise_exc, args, kwargs)