pyinfra 3.0b0__py2.py3-none-any.whl → 3.0b1__py2.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 (45) hide show
  1. pyinfra/api/__init__.py +3 -0
  2. pyinfra/api/arguments.py +5 -4
  3. pyinfra/api/arguments_typed.py +12 -2
  4. pyinfra/api/exceptions.py +19 -0
  5. pyinfra/api/facts.py +1 -1
  6. pyinfra/api/host.py +46 -7
  7. pyinfra/api/operation.py +77 -39
  8. pyinfra/api/operations.py +10 -11
  9. pyinfra/api/state.py +11 -2
  10. pyinfra/connectors/base.py +1 -1
  11. pyinfra/connectors/chroot.py +5 -6
  12. pyinfra/connectors/docker.py +11 -10
  13. pyinfra/connectors/dockerssh.py +5 -4
  14. pyinfra/connectors/local.py +5 -5
  15. pyinfra/connectors/ssh.py +44 -23
  16. pyinfra/connectors/terraform.py +9 -6
  17. pyinfra/connectors/util.py +1 -1
  18. pyinfra/connectors/vagrant.py +6 -5
  19. pyinfra/facts/choco.py +1 -1
  20. pyinfra/facts/deb.py +2 -2
  21. pyinfra/facts/postgres.py +168 -0
  22. pyinfra/facts/postgresql.py +5 -164
  23. pyinfra/facts/systemd.py +26 -10
  24. pyinfra/operations/files.py +5 -3
  25. pyinfra/operations/iptables.py +6 -0
  26. pyinfra/operations/pip.py +5 -0
  27. pyinfra/operations/postgres.py +347 -0
  28. pyinfra/operations/postgresql.py +17 -336
  29. pyinfra/operations/systemd.py +5 -3
  30. {pyinfra-3.0b0.dist-info → pyinfra-3.0b1.dist-info}/METADATA +6 -6
  31. {pyinfra-3.0b0.dist-info → pyinfra-3.0b1.dist-info}/RECORD +44 -43
  32. pyinfra_cli/commands.py +3 -2
  33. pyinfra_cli/exceptions.py +5 -0
  34. pyinfra_cli/main.py +2 -0
  35. pyinfra_cli/prints.py +22 -104
  36. tests/test_api/test_api_deploys.py +5 -5
  37. tests/test_api/test_api_operations.py +4 -4
  38. tests/test_connectors/test_ssh.py +52 -0
  39. tests/test_connectors/test_terraform.py +11 -8
  40. tests/test_connectors/test_vagrant.py +3 -3
  41. pyinfra_cli/inventory_dsl.py +0 -23
  42. {pyinfra-3.0b0.dist-info → pyinfra-3.0b1.dist-info}/LICENSE.md +0 -0
  43. {pyinfra-3.0b0.dist-info → pyinfra-3.0b1.dist-info}/WHEEL +0 -0
  44. {pyinfra-3.0b0.dist-info → pyinfra-3.0b1.dist-info}/entry_points.txt +0 -0
  45. {pyinfra-3.0b0.dist-info → pyinfra-3.0b1.dist-info}/top_level.txt +0 -0
@@ -263,6 +263,9 @@ def rule(
263
263
  if source:
264
264
  args.extend(("-s", source))
265
265
 
266
+ if destination:
267
+ args.extend(("-d", destination))
268
+
266
269
  if in_interface:
267
270
  args.extend(("-i", in_interface))
268
271
 
@@ -275,6 +278,9 @@ def rule(
275
278
  if not_source:
276
279
  args.extend(("!", "-s", not_source))
277
280
 
281
+ if not_destination:
282
+ args.extend(("!", "-d", not_destination))
283
+
278
284
  if not_in_interface:
279
285
  args.extend(("!", "-i", not_in_interface))
280
286
 
pyinfra/operations/pip.py CHANGED
@@ -185,6 +185,11 @@ def packages(
185
185
  if packages:
186
186
  current_packages = host.get_fact(PipPackages, pip=pip)
187
187
 
188
+ # PEP-0426 states that Python packages should be compared using lowercase, so lowercase both
189
+ # the input packages and the fact packages before comparison.
190
+ packages = [pkg.lower() for pkg in packages]
191
+ current_packages = {pkg.lower(): versions for pkg, versions in current_packages.items()}
192
+
188
193
  yield from ensure_packages(
189
194
  host,
190
195
  packages,
@@ -0,0 +1,347 @@
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
+
12
+ See example/postgresql.py for detailed example
13
+
14
+ """
15
+
16
+ from pyinfra import host
17
+ from pyinfra.api import MaskString, StringCommand, operation
18
+ from pyinfra.facts.postgres import (
19
+ PostgresDatabases,
20
+ PostgresRoles,
21
+ make_execute_psql_command,
22
+ make_psql_command,
23
+ )
24
+
25
+
26
+ @operation(is_idempotent=False)
27
+ def sql(
28
+ sql,
29
+ database=None,
30
+ # Details for speaking to PostgreSQL via `psql` CLI
31
+ psql_user=None,
32
+ psql_password=None,
33
+ psql_host=None,
34
+ psql_port=None,
35
+ ):
36
+ """
37
+ Execute arbitrary SQL against PostgreSQL.
38
+
39
+ + sql: SQL command(s) to execute
40
+ + database: optional database to execute against
41
+ + psql_*: global module arguments, see above
42
+ """
43
+
44
+ yield make_execute_psql_command(
45
+ sql,
46
+ database=database,
47
+ user=psql_user,
48
+ password=psql_password,
49
+ host=psql_host,
50
+ port=psql_port,
51
+ )
52
+
53
+
54
+ @operation()
55
+ def role(
56
+ role,
57
+ present=True,
58
+ password=None,
59
+ login=True,
60
+ superuser=False,
61
+ inherit=False,
62
+ createdb=False,
63
+ createrole=False,
64
+ replication=False,
65
+ connection_limit=None,
66
+ # Details for speaking to PostgreSQL via `psql` CLI
67
+ psql_user=None,
68
+ psql_password=None,
69
+ psql_host=None,
70
+ psql_port=None,
71
+ ):
72
+ """
73
+ Add/remove PostgreSQL roles.
74
+
75
+ + role: name of the role
76
+ + present: whether the role should be present or absent
77
+ + password: the password for the role
78
+ + login: whether the role can login
79
+ + superuser: whether role will be a superuser
80
+ + inherit: whether the role inherits from other roles
81
+ + createdb: whether the role is allowed to create databases
82
+ + createrole: whether the role is allowed to create new roles
83
+ + replication: whether this role is allowed to replicate
84
+ + connection_limit: the connection limit for the role
85
+ + psql_*: global module arguments, see above
86
+
87
+ Updates:
88
+ pyinfra will not attempt to change existing roles - it will either
89
+ create or drop roles, but not alter them (if the role exists this
90
+ operation will make no changes).
91
+
92
+ **Example:**
93
+
94
+ .. code:: python
95
+
96
+ postgresql.role(
97
+ name="Create the pyinfra PostgreSQL role",
98
+ role="pyinfra",
99
+ password="somepassword",
100
+ superuser=True,
101
+ login=True,
102
+ sudo_user="postgres",
103
+ )
104
+
105
+ """
106
+
107
+ roles = host.get_fact(
108
+ PostgresRoles,
109
+ psql_user=psql_user,
110
+ psql_password=psql_password,
111
+ psql_host=psql_host,
112
+ psql_port=psql_port,
113
+ )
114
+
115
+ is_present = role in roles
116
+
117
+ # User not wanted?
118
+ if not present:
119
+ if is_present:
120
+ yield make_execute_psql_command(
121
+ 'DROP ROLE "{0}"'.format(role),
122
+ user=psql_user,
123
+ password=psql_password,
124
+ host=psql_host,
125
+ port=psql_port,
126
+ )
127
+ else:
128
+ host.noop("postgresql role {0} does not exist".format(role))
129
+ return
130
+
131
+ # If we want the user and they don't exist
132
+ if not is_present:
133
+ sql_bits = ['CREATE ROLE "{0}"'.format(role)]
134
+
135
+ for key, value in (
136
+ ("LOGIN", login),
137
+ ("SUPERUSER", superuser),
138
+ ("INHERIT", inherit),
139
+ ("CREATEDB", createdb),
140
+ ("CREATEROLE", createrole),
141
+ ("REPLICATION", replication),
142
+ ):
143
+ if value:
144
+ sql_bits.append(key)
145
+
146
+ if connection_limit:
147
+ sql_bits.append("CONNECTION LIMIT {0}".format(connection_limit))
148
+
149
+ if password:
150
+ sql_bits.append(MaskString("PASSWORD '{0}'".format(password)))
151
+
152
+ yield make_execute_psql_command(
153
+ StringCommand(*sql_bits),
154
+ user=psql_user,
155
+ password=psql_password,
156
+ host=psql_host,
157
+ port=psql_port,
158
+ )
159
+ else:
160
+ host.noop("postgresql role {0} exists".format(role))
161
+
162
+
163
+ @operation()
164
+ def database(
165
+ database,
166
+ present=True,
167
+ owner=None,
168
+ template=None,
169
+ encoding=None,
170
+ lc_collate=None,
171
+ lc_ctype=None,
172
+ tablespace=None,
173
+ connection_limit=None,
174
+ # Details for speaking to PostgreSQL via `psql` CLI
175
+ psql_user=None,
176
+ psql_password=None,
177
+ psql_host=None,
178
+ psql_port=None,
179
+ ):
180
+ """
181
+ Add/remove PostgreSQL databases.
182
+
183
+ + name: name of the database
184
+ + present: whether the database should exist or not
185
+ + owner: the PostgreSQL role that owns the database
186
+ + template: name of the PostgreSQL template to use
187
+ + encoding: encoding of the database
188
+ + lc_collate: lc_collate of the database
189
+ + lc_ctype: lc_ctype of the database
190
+ + tablespace: the tablespace to use for the template
191
+ + connection_limit: the connection limit to apply to the database
192
+ + psql_*: global module arguments, see above
193
+
194
+ Updates:
195
+ pyinfra will not attempt to change existing databases - it will either
196
+ create or drop databases, but not alter them (if the db exists this
197
+ operation will make no changes).
198
+
199
+ **Example:**
200
+
201
+ .. code:: python
202
+
203
+ postgresql.database(
204
+ name="Create the pyinfra_stuff database",
205
+ database="pyinfra_stuff",
206
+ owner="pyinfra",
207
+ encoding="UTF8",
208
+ sudo_user="postgres",
209
+ )
210
+
211
+ """
212
+
213
+ current_databases = host.get_fact(
214
+ PostgresDatabases,
215
+ psql_user=psql_user,
216
+ psql_password=psql_password,
217
+ psql_host=psql_host,
218
+ psql_port=psql_port,
219
+ )
220
+
221
+ is_present = database in current_databases
222
+
223
+ if not present:
224
+ if is_present:
225
+ yield make_execute_psql_command(
226
+ 'DROP DATABASE "{0}"'.format(database),
227
+ user=psql_user,
228
+ password=psql_password,
229
+ host=psql_host,
230
+ port=psql_port,
231
+ )
232
+ else:
233
+ host.noop("postgresql database {0} does not exist".format(database))
234
+ return
235
+
236
+ # We want the database but it doesn't exist
237
+ if present and not is_present:
238
+ sql_bits = ['CREATE DATABASE "{0}"'.format(database)]
239
+
240
+ for key, value in (
241
+ ("OWNER", '"{0}"'.format(owner) if owner else owner),
242
+ ("TEMPLATE", template),
243
+ ("ENCODING", encoding),
244
+ ("LC_COLLATE", lc_collate),
245
+ ("LC_CTYPE", lc_ctype),
246
+ ("TABLESPACE", tablespace),
247
+ ("CONNECTION LIMIT", connection_limit),
248
+ ):
249
+ if value:
250
+ sql_bits.append("{0} {1}".format(key, value))
251
+
252
+ yield make_execute_psql_command(
253
+ StringCommand(*sql_bits),
254
+ user=psql_user,
255
+ password=psql_password,
256
+ host=psql_host,
257
+ port=psql_port,
258
+ )
259
+ else:
260
+ host.noop("postgresql database {0} exists".format(database))
261
+
262
+
263
+ @operation(is_idempotent=False)
264
+ def dump(
265
+ dest,
266
+ database=None,
267
+ # Details for speaking to PostgreSQL via `psql` CLI
268
+ psql_user=None,
269
+ psql_password=None,
270
+ psql_host=None,
271
+ psql_port=None,
272
+ ):
273
+ """
274
+ Dump a PostgreSQL database into a ``.sql`` file. Requires ``pg_dump``.
275
+
276
+ + dest: name of the file to dump the SQL to
277
+ + database: name of the database to dump
278
+ + psql_*: global module arguments, see above
279
+
280
+ **Example:**
281
+
282
+ .. code:: python
283
+
284
+ postgresql.dump(
285
+ name="Dump the pyinfra_stuff database",
286
+ dest="/tmp/pyinfra_stuff.dump",
287
+ database="pyinfra_stuff",
288
+ sudo_user="postgres",
289
+ )
290
+
291
+ """
292
+
293
+ yield StringCommand(
294
+ make_psql_command(
295
+ executable="pg_dump",
296
+ database=database,
297
+ user=psql_user,
298
+ password=psql_password,
299
+ host=psql_host,
300
+ port=psql_port,
301
+ ),
302
+ ">",
303
+ dest,
304
+ )
305
+
306
+
307
+ @operation(is_idempotent=False)
308
+ def load(
309
+ src,
310
+ database=None,
311
+ # Details for speaking to PostgreSQL via `psql` CLI
312
+ psql_user=None,
313
+ psql_password=None,
314
+ psql_host=None,
315
+ psql_port=None,
316
+ ):
317
+ """
318
+ Load ``.sql`` file into a database.
319
+
320
+ + src: the filename to read from
321
+ + database: name of the database to import into
322
+ + psql_*: global module arguments, see above
323
+
324
+ **Example:**
325
+
326
+ .. code:: python
327
+
328
+ postgresql.load(
329
+ name="Import the pyinfra_stuff dump into pyinfra_stuff_copy",
330
+ src="/tmp/pyinfra_stuff.dump",
331
+ database="pyinfra_stuff_copy",
332
+ sudo_user="postgres",
333
+ )
334
+
335
+ """
336
+
337
+ yield StringCommand(
338
+ make_psql_command(
339
+ database=database,
340
+ user=psql_user,
341
+ password=psql_password,
342
+ host=psql_host,
343
+ port=psql_port,
344
+ ),
345
+ "<",
346
+ src,
347
+ )