pyinfra 2.9.1__py2.py3-none-any.whl → 3.0__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 (156) hide show
  1. pyinfra/api/__init__.py +3 -0
  2. pyinfra/api/arguments.py +265 -253
  3. pyinfra/api/arguments_typed.py +80 -0
  4. pyinfra/api/command.py +68 -53
  5. pyinfra/api/config.py +139 -32
  6. pyinfra/api/connect.py +1 -1
  7. pyinfra/api/connectors.py +7 -26
  8. pyinfra/api/deploy.py +21 -52
  9. pyinfra/api/exceptions.py +33 -8
  10. pyinfra/api/facts.py +102 -137
  11. pyinfra/api/host.py +150 -82
  12. pyinfra/api/inventory.py +21 -25
  13. pyinfra/api/operation.py +240 -198
  14. pyinfra/api/operations.py +102 -148
  15. pyinfra/api/state.py +137 -79
  16. pyinfra/api/util.py +79 -86
  17. pyinfra/connectors/base.py +147 -0
  18. pyinfra/connectors/chroot.py +160 -169
  19. pyinfra/connectors/docker.py +220 -237
  20. pyinfra/connectors/dockerssh.py +231 -253
  21. pyinfra/connectors/local.py +196 -208
  22. pyinfra/connectors/ssh.py +530 -613
  23. pyinfra/connectors/ssh_util.py +114 -0
  24. pyinfra/connectors/sshuserclient/client.py +5 -3
  25. pyinfra/connectors/terraform.py +86 -65
  26. pyinfra/connectors/util.py +211 -137
  27. pyinfra/connectors/vagrant.py +60 -53
  28. pyinfra/context.py +4 -2
  29. pyinfra/facts/apk.py +2 -0
  30. pyinfra/facts/apt.py +2 -0
  31. pyinfra/facts/brew.py +2 -0
  32. pyinfra/facts/bsdinit.py +2 -0
  33. pyinfra/facts/cargo.py +2 -0
  34. pyinfra/facts/choco.py +2 -0
  35. pyinfra/facts/deb.py +7 -2
  36. pyinfra/facts/dnf.py +2 -0
  37. pyinfra/facts/docker.py +19 -0
  38. pyinfra/facts/files.py +47 -32
  39. pyinfra/facts/gem.py +2 -0
  40. pyinfra/facts/git.py +3 -1
  41. pyinfra/facts/gpg.py +3 -1
  42. pyinfra/facts/hardware.py +34 -24
  43. pyinfra/facts/iptables.py +5 -3
  44. pyinfra/facts/launchd.py +2 -0
  45. pyinfra/facts/lxd.py +2 -0
  46. pyinfra/facts/mysql.py +13 -6
  47. pyinfra/facts/npm.py +1 -0
  48. pyinfra/facts/openrc.py +2 -0
  49. pyinfra/facts/pacman.py +6 -2
  50. pyinfra/facts/pip.py +2 -0
  51. pyinfra/facts/pkg.py +2 -0
  52. pyinfra/facts/pkgin.py +2 -0
  53. pyinfra/facts/postgres.py +168 -0
  54. pyinfra/facts/postgresql.py +6 -160
  55. pyinfra/facts/rpm.py +12 -9
  56. pyinfra/facts/runit.py +68 -0
  57. pyinfra/facts/selinux.py +3 -1
  58. pyinfra/facts/server.py +80 -36
  59. pyinfra/facts/snap.py +2 -0
  60. pyinfra/facts/systemd.py +31 -12
  61. pyinfra/facts/sysvinit.py +10 -10
  62. pyinfra/facts/upstart.py +2 -0
  63. pyinfra/facts/util/packaging.py +7 -4
  64. pyinfra/facts/vzctl.py +2 -0
  65. pyinfra/facts/xbps.py +2 -0
  66. pyinfra/facts/yum.py +2 -0
  67. pyinfra/facts/zypper.py +2 -0
  68. pyinfra/local.py +4 -5
  69. pyinfra/operations/apk.py +6 -4
  70. pyinfra/operations/apt.py +46 -65
  71. pyinfra/operations/brew.py +17 -22
  72. pyinfra/operations/bsdinit.py +9 -7
  73. pyinfra/operations/cargo.py +4 -2
  74. pyinfra/operations/choco.py +4 -2
  75. pyinfra/operations/dnf.py +19 -23
  76. pyinfra/operations/docker.py +339 -0
  77. pyinfra/operations/files.py +188 -386
  78. pyinfra/operations/gem.py +4 -2
  79. pyinfra/operations/git.py +24 -53
  80. pyinfra/operations/iptables.py +29 -35
  81. pyinfra/operations/launchd.py +6 -7
  82. pyinfra/operations/lxd.py +8 -13
  83. pyinfra/operations/mysql.py +62 -81
  84. pyinfra/operations/npm.py +9 -2
  85. pyinfra/operations/openrc.py +6 -4
  86. pyinfra/operations/pacman.py +7 -8
  87. pyinfra/operations/pip.py +25 -24
  88. pyinfra/operations/pkg.py +4 -2
  89. pyinfra/operations/pkgin.py +6 -4
  90. pyinfra/operations/postgres.py +349 -0
  91. pyinfra/operations/postgresql.py +18 -379
  92. pyinfra/operations/puppet.py +3 -1
  93. pyinfra/operations/python.py +8 -19
  94. pyinfra/operations/runit.py +182 -0
  95. pyinfra/operations/selinux.py +47 -44
  96. pyinfra/operations/server.py +111 -127
  97. pyinfra/operations/snap.py +4 -4
  98. pyinfra/operations/ssh.py +20 -33
  99. pyinfra/operations/systemd.py +19 -15
  100. pyinfra/operations/sysvinit.py +9 -16
  101. pyinfra/operations/upstart.py +9 -7
  102. pyinfra/operations/util/__init__.py +12 -0
  103. pyinfra/operations/util/docker.py +177 -0
  104. pyinfra/operations/util/files.py +24 -16
  105. pyinfra/operations/util/packaging.py +55 -57
  106. pyinfra/operations/util/service.py +39 -51
  107. pyinfra/operations/vzctl.py +12 -10
  108. pyinfra/operations/xbps.py +6 -4
  109. pyinfra/operations/yum.py +18 -22
  110. pyinfra/operations/zypper.py +12 -13
  111. pyinfra/version.py +5 -2
  112. {pyinfra-2.9.1.dist-info → pyinfra-3.0.dist-info}/METADATA +40 -41
  113. pyinfra-3.0.dist-info/RECORD +167 -0
  114. {pyinfra-2.9.1.dist-info → pyinfra-3.0.dist-info}/WHEEL +1 -1
  115. pyinfra-3.0.dist-info/entry_points.txt +11 -0
  116. pyinfra_cli/__main__.py +4 -3
  117. pyinfra_cli/commands.py +7 -2
  118. pyinfra_cli/exceptions.py +78 -42
  119. pyinfra_cli/inventory.py +40 -6
  120. pyinfra_cli/log.py +17 -3
  121. pyinfra_cli/main.py +133 -90
  122. pyinfra_cli/prints.py +95 -127
  123. pyinfra_cli/util.py +62 -29
  124. tests/test_api/test_api.py +2 -0
  125. tests/test_api/test_api_arguments.py +13 -13
  126. tests/test_api/test_api_deploys.py +28 -29
  127. tests/test_api/test_api_facts.py +60 -98
  128. tests/test_api/test_api_operations.py +101 -201
  129. tests/test_cli/test_cli.py +18 -49
  130. tests/test_cli/test_cli_deploy.py +11 -37
  131. tests/test_cli/test_cli_exceptions.py +50 -19
  132. tests/test_cli/util.py +1 -1
  133. tests/test_connectors/test_chroot.py +6 -6
  134. tests/test_connectors/test_docker.py +4 -4
  135. tests/test_connectors/test_dockerssh.py +38 -50
  136. tests/test_connectors/test_local.py +11 -12
  137. tests/test_connectors/test_ssh.py +105 -93
  138. tests/test_connectors/test_terraform.py +9 -15
  139. tests/test_connectors/test_util.py +24 -46
  140. tests/test_connectors/test_vagrant.py +7 -7
  141. pyinfra/api/operation.pyi +0 -117
  142. pyinfra/connectors/ansible.py +0 -171
  143. pyinfra/connectors/mech.py +0 -186
  144. pyinfra/connectors/pyinfrawinrmsession/__init__.py +0 -28
  145. pyinfra/connectors/winrm.py +0 -320
  146. pyinfra/facts/windows.py +0 -366
  147. pyinfra/facts/windows_files.py +0 -90
  148. pyinfra/operations/windows.py +0 -59
  149. pyinfra/operations/windows_files.py +0 -551
  150. pyinfra-2.9.1.dist-info/RECORD +0 -170
  151. pyinfra-2.9.1.dist-info/entry_points.txt +0 -14
  152. tests/test_connectors/test_ansible.py +0 -64
  153. tests/test_connectors/test_mech.py +0 -126
  154. tests/test_connectors/test_winrm.py +0 -76
  155. {pyinfra-2.9.1.dist-info → pyinfra-3.0.dist-info}/LICENSE.md +0 -0
  156. {pyinfra-2.9.1.dist-info → pyinfra-3.0.dist-info}/top_level.txt +0 -0
pyinfra/operations/npm.py CHANGED
@@ -2,6 +2,8 @@
2
2
  Manage npm (aka node aka Node.js) packages.
3
3
  """
4
4
 
5
+ from __future__ import annotations
6
+
5
7
  from pyinfra import host
6
8
  from pyinfra.api import operation
7
9
  from pyinfra.facts.npm import NpmPackages
@@ -9,8 +11,13 @@ from pyinfra.facts.npm import NpmPackages
9
11
  from .util.packaging import ensure_packages
10
12
 
11
13
 
12
- @operation
13
- def packages(packages=None, present=True, latest=False, directory=None):
14
+ @operation()
15
+ def packages(
16
+ packages: str | list[str] | None = None,
17
+ present=True,
18
+ latest=False,
19
+ directory: str | None = None,
20
+ ):
14
21
  """
15
22
  Install/remove/update npm packages.
16
23
 
@@ -2,6 +2,8 @@
2
2
  Manage OpenRC init services.
3
3
  """
4
4
 
5
+ from __future__ import annotations
6
+
5
7
  from pyinfra import host
6
8
  from pyinfra.api import operation
7
9
  from pyinfra.facts.openrc import OpenrcEnabled, OpenrcStatus
@@ -9,14 +11,14 @@ from pyinfra.facts.openrc import OpenrcEnabled, OpenrcStatus
9
11
  from .util.service import handle_service_control
10
12
 
11
13
 
12
- @operation
14
+ @operation()
13
15
  def service(
14
- service,
16
+ service: str,
15
17
  running=True,
16
18
  restarted=False,
17
19
  reloaded=False,
18
- command=None,
19
- enabled=None,
20
+ command: str | None = None,
21
+ enabled: bool | None = None,
20
22
  runlevel="default",
21
23
  ):
22
24
  """
@@ -2,6 +2,8 @@
2
2
  Manage pacman packages. (Arch Linux package manager)
3
3
  """
4
4
 
5
+ from __future__ import annotations
6
+
5
7
  from pyinfra import host
6
8
  from pyinfra.api import operation
7
9
  from pyinfra.facts.pacman import PacmanPackages, PacmanUnpackGroup
@@ -18,7 +20,7 @@ def upgrade():
18
20
  yield "pacman --noconfirm -Su"
19
21
 
20
22
 
21
- _upgrade = upgrade # noqa: E305
23
+ _upgrade = upgrade._inner # noqa: E305
22
24
 
23
25
 
24
26
  @operation(is_idempotent=False)
@@ -30,12 +32,12 @@ def update():
30
32
  yield "pacman -Sy"
31
33
 
32
34
 
33
- _update = update # noqa: E305
35
+ _update = update._inner # noqa: E305
34
36
 
35
37
 
36
- @operation
38
+ @operation()
37
39
  def packages(
38
- packages=None,
40
+ packages: str | list[str] | None = None,
39
41
  present=True,
40
42
  update=False,
41
43
  upgrade=False,
@@ -75,8 +77,5 @@ def packages(
75
77
  present,
76
78
  install_command="pacman --noconfirm -S",
77
79
  uninstall_command="pacman --noconfirm -R",
78
- expand_package_fact=lambda package: host.get_fact(
79
- PacmanUnpackGroup,
80
- name=package,
81
- ),
80
+ expand_package_fact=lambda package: host.get_fact(PacmanUnpackGroup, package=package),
82
81
  )
pyinfra/operations/pip.py CHANGED
@@ -3,6 +3,8 @@ Manage pip (python) packages. Compatible globally or inside
3
3
  a virtualenv (virtual environment).
4
4
  """
5
5
 
6
+ from __future__ import annotations
7
+
6
8
  from pyinfra import host
7
9
  from pyinfra.api import operation
8
10
  from pyinfra.facts.files import File
@@ -12,10 +14,10 @@ from . import files
12
14
  from .util.packaging import ensure_packages
13
15
 
14
16
 
15
- @operation
17
+ @operation()
16
18
  def virtualenv(
17
- path,
18
- python=None,
19
+ path: str,
20
+ python: str | None = None,
19
21
  venv=False,
20
22
  site_packages=False,
21
23
  always_copy=False,
@@ -46,7 +48,7 @@ def virtualenv(
46
48
 
47
49
  if present is False:
48
50
  if host.get_fact(File, path=activate_script_path):
49
- yield from files.directory(path, present=False)
51
+ yield from files.directory._inner(path, present=False)
50
52
  else:
51
53
  host.noop("virtualenv {0} does not exist".format(path))
52
54
 
@@ -72,23 +74,17 @@ def virtualenv(
72
74
  command.append(path)
73
75
 
74
76
  yield " ".join(command)
75
-
76
- host.create_fact(
77
- File,
78
- kwargs={"path": activate_script_path},
79
- data={"user": None, "group": None},
80
- )
81
77
  else:
82
78
  host.noop("virtualenv {0} exists".format(path))
83
79
 
84
80
 
85
- _virtualenv = virtualenv # noqa
81
+ _virtualenv = virtualenv._inner # noqa
86
82
 
87
83
 
88
- @operation
84
+ @operation()
89
85
  def venv(
90
- path,
91
- python=None,
86
+ path: str,
87
+ python: str | None = None,
92
88
  site_packages=False,
93
89
  always_copy=False,
94
90
  present=True,
@@ -111,7 +107,7 @@ def venv(
111
107
  )
112
108
  """
113
109
 
114
- yield from virtualenv(
110
+ yield from _virtualenv(
115
111
  venv=True,
116
112
  path=path,
117
113
  python=python,
@@ -121,16 +117,16 @@ def venv(
121
117
  )
122
118
 
123
119
 
124
- @operation
120
+ @operation()
125
121
  def packages(
126
- packages=None,
122
+ packages: str | list[str] | None = None,
127
123
  present=True,
128
124
  latest=False,
129
- requirements=None,
125
+ requirements: str | None = None,
130
126
  pip="pip",
131
- virtualenv=None,
132
- virtualenv_kwargs=None,
133
- extra_install_args=None,
127
+ virtualenv: str | None = None,
128
+ virtualenv_kwargs: dict | None = None,
129
+ extra_install_args: str | None = None,
134
130
  ):
135
131
  """
136
132
  Install/remove/update pip packages.
@@ -173,10 +169,10 @@ def packages(
173
169
  virtualenv = virtualenv.rstrip("/")
174
170
  pip = "{0}/bin/{1}".format(virtualenv, pip)
175
171
 
176
- install_command = [pip, "install"]
172
+ install_command_args = [pip, "install"]
177
173
  if extra_install_args:
178
- install_command.append(extra_install_args)
179
- install_command = " ".join(install_command)
174
+ install_command_args.append(extra_install_args)
175
+ install_command = " ".join(install_command_args)
180
176
 
181
177
  uninstall_command = " ".join([pip, "uninstall", "--yes"])
182
178
 
@@ -191,6 +187,11 @@ def packages(
191
187
  if packages:
192
188
  current_packages = host.get_fact(PipPackages, pip=pip)
193
189
 
190
+ # PEP-0426 states that Python packages should be compared using lowercase, so lowercase both
191
+ # the input packages and the fact packages before comparison.
192
+ packages = [pkg.lower() for pkg in packages]
193
+ current_packages = {pkg.lower(): versions for pkg, versions in current_packages.items()}
194
+
194
195
  yield from ensure_packages(
195
196
  host,
196
197
  packages,
pyinfra/operations/pkg.py CHANGED
@@ -2,6 +2,8 @@
2
2
  Manage BSD packages and repositories. Note that BSD package names are case-sensitive.
3
3
  """
4
4
 
5
+ from __future__ import annotations
6
+
5
7
  from pyinfra import host
6
8
  from pyinfra.api import operation
7
9
  from pyinfra.facts.files import File
@@ -11,8 +13,8 @@ from pyinfra.facts.server import Arch, Os, OsVersion, Which
11
13
  from .util.packaging import ensure_packages
12
14
 
13
15
 
14
- @operation
15
- def packages(packages=None, present=True, pkg_path=None):
16
+ @operation()
17
+ def packages(packages: str | list[str] | None = None, present=True, pkg_path: str | None = None):
16
18
  """
17
19
  Install/remove/update pkg packages. This will use ``pkg ...`` where available
18
20
  (FreeBSD) and the ``pkg_*`` variants elsewhere.
@@ -2,6 +2,8 @@
2
2
  Manage pkgin packages.
3
3
  """
4
4
 
5
+ from __future__ import annotations
6
+
5
7
  from pyinfra import host
6
8
  from pyinfra.api import operation
7
9
  from pyinfra.facts.pkgin import PkginPackages
@@ -18,7 +20,7 @@ def upgrade():
18
20
  yield "pkgin -y upgrade"
19
21
 
20
22
 
21
- _upgrade = upgrade # noqa: E305
23
+ _upgrade = upgrade._inner # noqa: E305
22
24
 
23
25
 
24
26
  @operation(is_idempotent=False)
@@ -30,12 +32,12 @@ def update():
30
32
  yield "pkgin -y update"
31
33
 
32
34
 
33
- _update = update # noqa: E305
35
+ _update = update._inner # noqa: E305
34
36
 
35
37
 
36
- @operation
38
+ @operation()
37
39
  def packages(
38
- packages=None,
40
+ packages: str | list[str] | None = None,
39
41
  present=True,
40
42
  latest=False,
41
43
  update=False,
@@ -0,0 +1,349 @@
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 __future__ import annotations
17
+
18
+ from pyinfra import host
19
+ from pyinfra.api import MaskString, StringCommand, operation
20
+ from pyinfra.facts.postgres import (
21
+ PostgresDatabases,
22
+ PostgresRoles,
23
+ make_execute_psql_command,
24
+ make_psql_command,
25
+ )
26
+
27
+
28
+ @operation(is_idempotent=False)
29
+ def sql(
30
+ sql: str,
31
+ database: str | None = None,
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
+ ):
38
+ """
39
+ Execute arbitrary SQL against PostgreSQL.
40
+
41
+ + sql: SQL command(s) to execute
42
+ + database: optional database to execute against
43
+ + psql_*: global module arguments, see above
44
+ """
45
+
46
+ yield make_execute_psql_command(
47
+ sql,
48
+ database=database,
49
+ user=psql_user,
50
+ password=psql_password,
51
+ host=psql_host,
52
+ port=psql_port,
53
+ )
54
+
55
+
56
+ @operation()
57
+ def role(
58
+ role: str,
59
+ present=True,
60
+ password: str | None = None,
61
+ login=True,
62
+ superuser=False,
63
+ inherit=False,
64
+ createdb=False,
65
+ createrole=False,
66
+ replication=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
+ ):
74
+ """
75
+ Add/remove PostgreSQL roles.
76
+
77
+ + role: name of the role
78
+ + present: whether the role should be present or absent
79
+ + password: the password for the role
80
+ + login: whether the role can login
81
+ + superuser: whether role will be a superuser
82
+ + inherit: whether the role inherits from other roles
83
+ + createdb: whether the role is allowed to create databases
84
+ + createrole: whether the role is allowed to create new roles
85
+ + replication: whether this role is allowed to replicate
86
+ + connection_limit: the connection limit for the role
87
+ + psql_*: global module arguments, see above
88
+
89
+ Updates:
90
+ pyinfra will not attempt to change existing roles - it will either
91
+ create or drop roles, but not alter them (if the role exists this
92
+ operation will make no changes).
93
+
94
+ **Example:**
95
+
96
+ .. code:: python
97
+
98
+ postgresql.role(
99
+ name="Create the pyinfra PostgreSQL role",
100
+ role="pyinfra",
101
+ password="somepassword",
102
+ superuser=True,
103
+ login=True,
104
+ sudo_user="postgres",
105
+ )
106
+
107
+ """
108
+
109
+ roles = host.get_fact(
110
+ PostgresRoles,
111
+ psql_user=psql_user,
112
+ psql_password=psql_password,
113
+ psql_host=psql_host,
114
+ psql_port=psql_port,
115
+ )
116
+
117
+ is_present = role in roles
118
+
119
+ # User not wanted?
120
+ if not present:
121
+ if is_present:
122
+ yield make_execute_psql_command(
123
+ 'DROP ROLE "{0}"'.format(role),
124
+ user=psql_user,
125
+ password=psql_password,
126
+ host=psql_host,
127
+ port=psql_port,
128
+ )
129
+ else:
130
+ host.noop("postgresql role {0} does not exist".format(role))
131
+ return
132
+
133
+ # If we want the user and they don't exist
134
+ if not is_present:
135
+ sql_bits = ['CREATE ROLE "{0}"'.format(role)]
136
+
137
+ for key, value in (
138
+ ("LOGIN", login),
139
+ ("SUPERUSER", superuser),
140
+ ("INHERIT", inherit),
141
+ ("CREATEDB", createdb),
142
+ ("CREATEROLE", createrole),
143
+ ("REPLICATION", replication),
144
+ ):
145
+ if value:
146
+ sql_bits.append(key)
147
+
148
+ if connection_limit:
149
+ sql_bits.append("CONNECTION LIMIT {0}".format(connection_limit))
150
+
151
+ if password:
152
+ sql_bits.append(MaskString("PASSWORD '{0}'".format(password)))
153
+
154
+ yield make_execute_psql_command(
155
+ StringCommand(*sql_bits),
156
+ user=psql_user,
157
+ password=psql_password,
158
+ host=psql_host,
159
+ port=psql_port,
160
+ )
161
+ else:
162
+ host.noop("postgresql role {0} exists".format(role))
163
+
164
+
165
+ @operation()
166
+ def database(
167
+ database: str,
168
+ present=True,
169
+ owner: str | None = None,
170
+ template: str | None = None,
171
+ encoding: str | None = None,
172
+ lc_collate: str | None = None,
173
+ lc_ctype: str | None = None,
174
+ tablespace: str | None = None,
175
+ connection_limit: int | None = None,
176
+ # Details for speaking to PostgreSQL via `psql` CLI
177
+ psql_user: str | None = None,
178
+ psql_password: str | None = None,
179
+ psql_host: str | None = None,
180
+ psql_port: int | None = None,
181
+ ):
182
+ """
183
+ Add/remove PostgreSQL databases.
184
+
185
+ + name: name of the database
186
+ + present: whether the database should exist or not
187
+ + owner: the PostgreSQL role that owns the database
188
+ + template: name of the PostgreSQL template to use
189
+ + encoding: encoding of the database
190
+ + lc_collate: lc_collate of the database
191
+ + lc_ctype: lc_ctype of the database
192
+ + tablespace: the tablespace to use for the template
193
+ + connection_limit: the connection limit to apply to the database
194
+ + psql_*: global module arguments, see above
195
+
196
+ Updates:
197
+ pyinfra will not attempt to change existing databases - it will either
198
+ create or drop databases, but not alter them (if the db exists this
199
+ operation will make no changes).
200
+
201
+ **Example:**
202
+
203
+ .. code:: python
204
+
205
+ postgresql.database(
206
+ name="Create the pyinfra_stuff database",
207
+ database="pyinfra_stuff",
208
+ owner="pyinfra",
209
+ encoding="UTF8",
210
+ sudo_user="postgres",
211
+ )
212
+
213
+ """
214
+
215
+ current_databases = host.get_fact(
216
+ PostgresDatabases,
217
+ psql_user=psql_user,
218
+ psql_password=psql_password,
219
+ psql_host=psql_host,
220
+ psql_port=psql_port,
221
+ )
222
+
223
+ is_present = database in current_databases
224
+
225
+ if not present:
226
+ if is_present:
227
+ yield make_execute_psql_command(
228
+ 'DROP DATABASE "{0}"'.format(database),
229
+ user=psql_user,
230
+ password=psql_password,
231
+ host=psql_host,
232
+ port=psql_port,
233
+ )
234
+ else:
235
+ host.noop("postgresql database {0} does not exist".format(database))
236
+ return
237
+
238
+ # We want the database but it doesn't exist
239
+ if present and not is_present:
240
+ sql_bits = ['CREATE DATABASE "{0}"'.format(database)]
241
+
242
+ for key, value in (
243
+ ("OWNER", '"{0}"'.format(owner) if owner else owner),
244
+ ("TEMPLATE", template),
245
+ ("ENCODING", encoding),
246
+ ("LC_COLLATE", lc_collate),
247
+ ("LC_CTYPE", lc_ctype),
248
+ ("TABLESPACE", tablespace),
249
+ ("CONNECTION LIMIT", connection_limit),
250
+ ):
251
+ if value:
252
+ sql_bits.append("{0} {1}".format(key, value))
253
+
254
+ yield make_execute_psql_command(
255
+ StringCommand(*sql_bits),
256
+ user=psql_user,
257
+ password=psql_password,
258
+ host=psql_host,
259
+ port=psql_port,
260
+ )
261
+ else:
262
+ host.noop("postgresql database {0} exists".format(database))
263
+
264
+
265
+ @operation(is_idempotent=False)
266
+ def dump(
267
+ dest: str,
268
+ database: str | None = None,
269
+ # Details for speaking to PostgreSQL via `psql` CLI
270
+ psql_user: str | None = None,
271
+ psql_password: str | None = None,
272
+ psql_host: str | None = None,
273
+ psql_port: int | None = None,
274
+ ):
275
+ """
276
+ Dump a PostgreSQL database into a ``.sql`` file. Requires ``pg_dump``.
277
+
278
+ + dest: name of the file to dump the SQL to
279
+ + database: name of the database to dump
280
+ + psql_*: global module arguments, see above
281
+
282
+ **Example:**
283
+
284
+ .. code:: python
285
+
286
+ postgresql.dump(
287
+ name="Dump the pyinfra_stuff database",
288
+ dest="/tmp/pyinfra_stuff.dump",
289
+ database="pyinfra_stuff",
290
+ sudo_user="postgres",
291
+ )
292
+
293
+ """
294
+
295
+ yield StringCommand(
296
+ make_psql_command(
297
+ executable="pg_dump",
298
+ database=database,
299
+ user=psql_user,
300
+ password=psql_password,
301
+ host=psql_host,
302
+ port=psql_port,
303
+ ),
304
+ ">",
305
+ dest,
306
+ )
307
+
308
+
309
+ @operation(is_idempotent=False)
310
+ def load(
311
+ src: str,
312
+ database: str | None = None,
313
+ # Details for speaking to PostgreSQL via `psql` CLI
314
+ psql_user: str | None = None,
315
+ psql_password: str | None = None,
316
+ psql_host: str | None = None,
317
+ psql_port: int | None = None,
318
+ ):
319
+ """
320
+ Load ``.sql`` file into a database.
321
+
322
+ + src: the filename to read from
323
+ + database: name of the database to import into
324
+ + psql_*: global module arguments, see above
325
+
326
+ **Example:**
327
+
328
+ .. code:: python
329
+
330
+ postgresql.load(
331
+ name="Import the pyinfra_stuff dump into pyinfra_stuff_copy",
332
+ src="/tmp/pyinfra_stuff.dump",
333
+ database="pyinfra_stuff_copy",
334
+ sudo_user="postgres",
335
+ )
336
+
337
+ """
338
+
339
+ yield StringCommand(
340
+ make_psql_command(
341
+ database=database,
342
+ user=psql_user,
343
+ password=psql_password,
344
+ host=psql_host,
345
+ port=psql_port,
346
+ ),
347
+ "<",
348
+ src,
349
+ )