pyinfra 3.0.dev0__py2.py3-none-any.whl → 3.0.2__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 (148) hide show
  1. pyinfra/api/__init__.py +3 -0
  2. pyinfra/api/arguments.py +115 -97
  3. pyinfra/api/arguments_typed.py +80 -0
  4. pyinfra/api/command.py +5 -3
  5. pyinfra/api/config.py +139 -39
  6. pyinfra/api/connectors.py +5 -2
  7. pyinfra/api/deploy.py +19 -19
  8. pyinfra/api/exceptions.py +35 -4
  9. pyinfra/api/facts.py +62 -86
  10. pyinfra/api/host.py +102 -15
  11. pyinfra/api/inventory.py +4 -0
  12. pyinfra/api/operation.py +188 -120
  13. pyinfra/api/operations.py +66 -113
  14. pyinfra/api/state.py +53 -34
  15. pyinfra/api/util.py +64 -33
  16. pyinfra/connectors/base.py +65 -20
  17. pyinfra/connectors/chroot.py +15 -13
  18. pyinfra/connectors/docker.py +62 -72
  19. pyinfra/connectors/dockerssh.py +20 -19
  20. pyinfra/connectors/local.py +32 -22
  21. pyinfra/connectors/ssh.py +162 -86
  22. pyinfra/connectors/sshuserclient/client.py +1 -1
  23. pyinfra/connectors/terraform.py +57 -39
  24. pyinfra/connectors/util.py +26 -27
  25. pyinfra/connectors/vagrant.py +27 -26
  26. pyinfra/context.py +1 -0
  27. pyinfra/facts/apk.py +7 -2
  28. pyinfra/facts/apt.py +15 -7
  29. pyinfra/facts/brew.py +28 -13
  30. pyinfra/facts/bsdinit.py +9 -6
  31. pyinfra/facts/cargo.py +6 -3
  32. pyinfra/facts/choco.py +8 -4
  33. pyinfra/facts/deb.py +21 -9
  34. pyinfra/facts/dnf.py +11 -6
  35. pyinfra/facts/docker.py +30 -5
  36. pyinfra/facts/files.py +49 -33
  37. pyinfra/facts/gem.py +7 -2
  38. pyinfra/facts/git.py +14 -21
  39. pyinfra/facts/gpg.py +4 -1
  40. pyinfra/facts/hardware.py +186 -138
  41. pyinfra/facts/launchd.py +7 -2
  42. pyinfra/facts/lxd.py +8 -2
  43. pyinfra/facts/mysql.py +19 -12
  44. pyinfra/facts/npm.py +3 -1
  45. pyinfra/facts/openrc.py +8 -2
  46. pyinfra/facts/pacman.py +13 -5
  47. pyinfra/facts/pip.py +2 -0
  48. pyinfra/facts/pkg.py +5 -1
  49. pyinfra/facts/pkgin.py +7 -2
  50. pyinfra/facts/postgres.py +170 -0
  51. pyinfra/facts/postgresql.py +5 -162
  52. pyinfra/facts/rpm.py +21 -15
  53. pyinfra/facts/runit.py +70 -0
  54. pyinfra/facts/selinux.py +12 -4
  55. pyinfra/facts/server.py +240 -82
  56. pyinfra/facts/snap.py +8 -2
  57. pyinfra/facts/systemd.py +37 -13
  58. pyinfra/facts/sysvinit.py +7 -4
  59. pyinfra/facts/upstart.py +7 -2
  60. pyinfra/facts/util/packaging.py +3 -2
  61. pyinfra/facts/vzctl.py +8 -4
  62. pyinfra/facts/xbps.py +7 -2
  63. pyinfra/facts/yum.py +10 -5
  64. pyinfra/facts/zypper.py +9 -4
  65. pyinfra/operations/apk.py +5 -3
  66. pyinfra/operations/apt.py +28 -25
  67. pyinfra/operations/brew.py +60 -29
  68. pyinfra/operations/bsdinit.py +6 -4
  69. pyinfra/operations/cargo.py +3 -1
  70. pyinfra/operations/choco.py +3 -1
  71. pyinfra/operations/dnf.py +16 -20
  72. pyinfra/operations/docker.py +339 -0
  73. pyinfra/operations/files.py +187 -168
  74. pyinfra/operations/gem.py +3 -1
  75. pyinfra/operations/git.py +23 -25
  76. pyinfra/operations/iptables.py +33 -25
  77. pyinfra/operations/launchd.py +5 -6
  78. pyinfra/operations/lxd.py +7 -4
  79. pyinfra/operations/mysql.py +59 -55
  80. pyinfra/operations/npm.py +8 -1
  81. pyinfra/operations/openrc.py +5 -3
  82. pyinfra/operations/pacman.py +6 -7
  83. pyinfra/operations/pip.py +19 -12
  84. pyinfra/operations/pkg.py +3 -1
  85. pyinfra/operations/pkgin.py +5 -3
  86. pyinfra/operations/postgres.py +349 -0
  87. pyinfra/operations/postgresql.py +18 -335
  88. pyinfra/operations/puppet.py +3 -1
  89. pyinfra/operations/python.py +8 -19
  90. pyinfra/operations/runit.py +182 -0
  91. pyinfra/operations/selinux.py +47 -29
  92. pyinfra/operations/server.py +138 -67
  93. pyinfra/operations/snap.py +3 -1
  94. pyinfra/operations/ssh.py +18 -16
  95. pyinfra/operations/systemd.py +18 -12
  96. pyinfra/operations/sysvinit.py +7 -5
  97. pyinfra/operations/upstart.py +7 -5
  98. pyinfra/operations/util/__init__.py +12 -0
  99. pyinfra/operations/util/docker.py +177 -0
  100. pyinfra/operations/util/files.py +24 -16
  101. pyinfra/operations/util/packaging.py +54 -38
  102. pyinfra/operations/util/service.py +39 -47
  103. pyinfra/operations/vzctl.py +12 -10
  104. pyinfra/operations/xbps.py +5 -3
  105. pyinfra/operations/yum.py +15 -19
  106. pyinfra/operations/zypper.py +9 -10
  107. pyinfra/version.py +5 -2
  108. {pyinfra-3.0.dev0.dist-info → pyinfra-3.0.2.dist-info}/METADATA +51 -58
  109. pyinfra-3.0.2.dist-info/RECORD +168 -0
  110. {pyinfra-3.0.dev0.dist-info → pyinfra-3.0.2.dist-info}/WHEEL +1 -1
  111. {pyinfra-3.0.dev0.dist-info → pyinfra-3.0.2.dist-info}/entry_points.txt +0 -3
  112. pyinfra_cli/__main__.py +4 -3
  113. pyinfra_cli/commands.py +3 -2
  114. pyinfra_cli/exceptions.py +75 -43
  115. pyinfra_cli/inventory.py +52 -31
  116. pyinfra_cli/log.py +10 -2
  117. pyinfra_cli/main.py +88 -65
  118. pyinfra_cli/prints.py +37 -109
  119. pyinfra_cli/util.py +15 -10
  120. tests/test_api/test_api.py +2 -0
  121. tests/test_api/test_api_arguments.py +9 -9
  122. tests/test_api/test_api_deploys.py +15 -19
  123. tests/test_api/test_api_facts.py +4 -5
  124. tests/test_api/test_api_operations.py +18 -20
  125. tests/test_api/test_api_util.py +41 -2
  126. tests/test_cli/test_cli.py +14 -50
  127. tests/test_cli/test_cli_deploy.py +17 -14
  128. tests/test_cli/test_cli_exceptions.py +50 -19
  129. tests/test_cli/test_cli_inventory.py +66 -0
  130. tests/test_cli/util.py +1 -1
  131. tests/test_connectors/test_dockerssh.py +11 -8
  132. tests/test_connectors/test_ssh.py +88 -23
  133. tests/test_connectors/test_sshuserclient.py +1 -1
  134. tests/test_connectors/test_terraform.py +11 -8
  135. tests/test_connectors/test_vagrant.py +6 -6
  136. pyinfra/connectors/ansible.py +0 -175
  137. pyinfra/connectors/mech.py +0 -189
  138. pyinfra/connectors/pyinfrawinrmsession/__init__.py +0 -28
  139. pyinfra/connectors/winrm.py +0 -312
  140. pyinfra/facts/windows.py +0 -366
  141. pyinfra/facts/windows_files.py +0 -90
  142. pyinfra/operations/windows.py +0 -59
  143. pyinfra/operations/windows_files.py +0 -538
  144. pyinfra-3.0.dev0.dist-info/RECORD +0 -170
  145. tests/test_connectors/test_ansible.py +0 -64
  146. tests/test_connectors/test_mech.py +0 -126
  147. {pyinfra-3.0.dev0.dist-info → pyinfra-3.0.2.dist-info}/LICENSE.md +0 -0
  148. {pyinfra-3.0.dev0.dist-info → pyinfra-3.0.2.dist-info}/top_level.txt +0 -0
@@ -1,347 +1,30 @@
1
- """
2
- The PostgreSQL modules manage PostgreSQL databases, users and privileges.
1
+ from __future__ import annotations
3
2
 
4
- Requires the ``psql`` CLI executable on the target host(s).
3
+ from pyinfra.api import operation
5
4
 
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
5
+ from . import postgres
11
6
 
12
- See example/postgresql.py for detailed example
13
7
 
14
- """
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)
15
11
 
16
- from pyinfra import host
17
- from pyinfra.api import MaskString, StringCommand, operation
18
- from pyinfra.facts.postgresql import (
19
- PostgresqlDatabases,
20
- PostgresqlRoles,
21
- make_execute_psql_command,
22
- make_psql_command,
23
- )
24
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)
25
16
 
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
17
 
39
- + sql: SQL command(s) to execute
40
- + database: optional database to execute against
41
- + psql_*: global module arguments, see above
42
- """
18
+ @operation(is_idempotent=False, is_deprecated=True)
19
+ def database(*args, **kwargs):
20
+ yield from postgres.database._inner(*args, **kwargs)
43
21
 
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
22
 
23
+ @operation(is_idempotent=False, is_deprecated=True)
24
+ def dump(*args, **kwargs):
25
+ yield from postgres.dump._inner(*args, **kwargs)
53
26
 
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
27
 
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
- PostgresqlRoles,
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
- PostgresqlDatabases,
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
- )
28
+ @operation(is_idempotent=False, is_deprecated=True)
29
+ def load(*args, **kwargs):
30
+ yield from postgres.load._inner(*args, **kwargs)
@@ -1,8 +1,10 @@
1
+ from __future__ import annotations
2
+
1
3
  from pyinfra.api import operation
2
4
 
3
5
 
4
6
  @operation(is_idempotent=False)
5
- def agent(server=None, port=None):
7
+ def agent(server: str | None = None, port: int | None = None):
6
8
  """
7
9
  Run puppet agent
8
10
 
@@ -2,15 +2,15 @@
2
2
  The Python module allows you to execute Python code within the context of a deploy.
3
3
  """
4
4
 
5
- from inspect import getfullargspec
5
+ from __future__ import annotations
6
+
7
+ from typing import Callable
6
8
 
7
- from pyinfra import logger
8
9
  from pyinfra.api import FunctionCommand, operation
9
- from pyinfra.api.util import get_call_location
10
10
 
11
11
 
12
- @operation(is_idempotent=False)
13
- def call(function, *args, **kwargs):
12
+ @operation(is_idempotent=False, _set_in_op=False)
13
+ def call(function: Callable, *args, **kwargs):
14
14
  """
15
15
  Execute a Python function within a deploy.
16
16
 
@@ -43,20 +43,11 @@ def call(function, *args, **kwargs):
43
43
 
44
44
  """
45
45
 
46
- argspec = getfullargspec(function)
47
- if "state" in argspec.args and "host" in argspec.args:
48
- logger.warning(
49
- "Callback functions used in `python.call` operations no "
50
- f"longer take `state` and `host` arguments: {get_call_location(frame_offset=3)}",
51
- )
52
-
53
- kwargs.pop("state", None)
54
- kwargs.pop("host", None)
55
46
  yield FunctionCommand(function, args, kwargs)
56
47
 
57
48
 
58
- @operation(is_idempotent=False)
59
- def raise_exception(exception, *args, **kwargs):
49
+ @operation(is_idempotent=False, _set_in_op=False)
50
+ def raise_exception(exception: Exception, *args, **kwargs):
60
51
  """
61
52
  Raise a Python exception within a deploy.
62
53
 
@@ -76,8 +67,6 @@ def raise_exception(exception, *args, **kwargs):
76
67
  """
77
68
 
78
69
  def raise_exc(*args, **kwargs): # pragma: no cover
79
- raise exception(*args, **kwargs)
70
+ raise exception(*args, **kwargs) # type: ignore[operator]
80
71
 
81
- kwargs.pop("state", None)
82
- kwargs.pop("host", None)
83
72
  yield FunctionCommand(raise_exc, args, kwargs)
@@ -0,0 +1,182 @@
1
+ """
2
+ Manage runit services.
3
+ """
4
+
5
+ from typing import Optional
6
+
7
+ from pyinfra import host
8
+ from pyinfra.api import operation
9
+ from pyinfra.facts.files import File
10
+ from pyinfra.facts.runit import RunitManaged, RunitStatus
11
+
12
+ from .files import file, link
13
+ from .util.service import handle_service_control
14
+
15
+
16
+ @operation()
17
+ def service(
18
+ service: str,
19
+ running: bool = True,
20
+ restarted: bool = False,
21
+ reloaded: bool = False,
22
+ command: Optional[str] = None,
23
+ enabled: Optional[bool] = None,
24
+ managed: bool = True,
25
+ svdir: str = "/var/service",
26
+ sourcedir: str = "/etc/sv",
27
+ ):
28
+ """
29
+ Manage the state of runit services.
30
+
31
+ + service: name of the service to manage
32
+ + running: whether the service should be running
33
+ + restarted: whether the service should be restarted
34
+ + reloaded: whether the service should be reloaded
35
+ + command: custom command to pass like: ``sv <command> <service>``
36
+ + enabled: whether this service should be enabled/disabled on boot
37
+ + managed: whether runit should manage this service
38
+
39
+ For services to be controlled, they first need to be managed by runit by
40
+ adding a symlink to the service in ``SVDIR``.
41
+ By setting ``managed=False`` the symlink will be removed.
42
+ Other options won't have any effect after that.
43
+ Although the ``<service>/down`` file can still be controlled with the
44
+ ``enabled`` option.
45
+
46
+ + svdir: alternative ``SVDIR``
47
+
48
+ An alternative ``SVDIR`` can be specified. This can be used for user services.
49
+
50
+ + sourcedir: where to search for available services
51
+
52
+ An alternative directory for available services can be specified.
53
+ Example: ``sourcedir=/etc/sv.local`` for services managed by the administrator.
54
+ """
55
+
56
+ was_managed = service in host.get_fact(RunitManaged, service=service, svdir=svdir)
57
+ was_auto = not host.get_fact(File, path="{0}/{1}/down".format(sourcedir, service))
58
+
59
+ # Disable autostart for previously unmanaged services.
60
+ #
61
+ # Where ``running=False`` is requested, this prevents one case of briefly
62
+ # starting and stopping the service.
63
+ if not was_managed and managed and was_auto:
64
+ yield from auto._inner(
65
+ service=service,
66
+ auto=False,
67
+ sourcedir=sourcedir,
68
+ )
69
+
70
+ yield from manage._inner(
71
+ service=service,
72
+ managed=managed,
73
+ svdir=svdir,
74
+ sourcedir=sourcedir,
75
+ )
76
+
77
+ # Service wasn't managed before, so wait for ``runsv`` to start.
78
+ # ``runsvdir`` will check at least every 5 seconds for new services.
79
+ # Wait for at most 10 seconds for the service to be managed, otherwise fail.
80
+ if not was_managed and managed:
81
+ yield from wait_runsv._inner(
82
+ service=service,
83
+ svdir=svdir,
84
+ )
85
+
86
+ if isinstance(enabled, bool):
87
+ yield from auto._inner(
88
+ service=service,
89
+ auto=enabled,
90
+ sourcedir=sourcedir,
91
+ )
92
+ else:
93
+ # restore previous state of ``<service>/down``
94
+ yield from auto._inner(
95
+ service=service,
96
+ auto=was_auto,
97
+ sourcedir=sourcedir,
98
+ )
99
+
100
+ # Services need to be managed by ``runit`` for the other options to make sense.
101
+ if not managed:
102
+ return
103
+
104
+ yield from handle_service_control(
105
+ host,
106
+ service,
107
+ host.get_fact(RunitStatus, service=service, svdir=svdir),
108
+ "SVDIR={0} sv {{1}} {{0}}".format(svdir),
109
+ running,
110
+ restarted,
111
+ reloaded,
112
+ command,
113
+ )
114
+
115
+
116
+ @operation()
117
+ def manage(
118
+ service: str,
119
+ managed: bool = True,
120
+ svdir: str = "/var/service",
121
+ sourcedir: str = "/etc/sv",
122
+ ):
123
+ """
124
+ Manage runit svdir links.
125
+
126
+ + service: name of the service to manage
127
+ + managed: whether the link should exist
128
+ + svdir: alternative ``SVDIR``
129
+ + sourcedir: where to search for available services
130
+ """
131
+
132
+ yield from link._inner(
133
+ path="{0}/{1}".format(svdir, service),
134
+ target="{0}/{1}".format(sourcedir, service),
135
+ present=managed,
136
+ create_remote_dir=False,
137
+ )
138
+
139
+
140
+ @operation(is_idempotent=False)
141
+ def wait_runsv(
142
+ service: str,
143
+ svdir: str = "/var/service",
144
+ timeout: int = 10,
145
+ ):
146
+ """
147
+ Wait for runsv for ``service`` to be available.
148
+
149
+ + service: name of the service to manage
150
+ + svdir: alternative ``SVDIR``
151
+ + timeout: time in seconds to wait
152
+ """
153
+
154
+ yield (
155
+ "export SVDIR={0}\n"
156
+ "for i in $(seq {1}); do\n"
157
+ " sv status {2} > /dev/null && exit 0\n"
158
+ " sleep 1;\n"
159
+ "done\n"
160
+ "exit 1"
161
+ ).format(svdir, timeout, service)
162
+
163
+
164
+ @operation()
165
+ def auto(
166
+ service: str,
167
+ auto: bool = True,
168
+ sourcedir: str = "/etc/sv",
169
+ ):
170
+ """
171
+ Start service automatically by managing the ``service/down`` file.
172
+
173
+ + service: name of the service to manage
174
+ + auto: whether the service should start automatically
175
+ + sourcedir: where to search for available services
176
+ """
177
+
178
+ yield from file._inner(
179
+ path="{0}/{1}/down".format(sourcedir, service),
180
+ present=not auto,
181
+ create_remote_dir=False,
182
+ )