Skip to content

Commit ca78bcd

Browse files
committed
docs: update/improve using operations, use consistent imports
1 parent e064e8b commit ca78bcd

File tree

2 files changed

+40
-63
lines changed

2 files changed

+40
-63
lines changed

docs/deploy-process.rst

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,24 @@ And here's the deploy code:
2525

2626
.. code:: python
2727
28-
from pyinfra import host, operations
28+
from pyinfra import host
29+
from pyinfra.operations import apt
2930
30-
operations.apt.packages(
31+
apt.packages(
3132
name="Install base debugging packages",
3233
packages=["htop", "iftop"],
3334
update=True,
3435
cache_time=3600,
3536
)
3637
3738
if "db_server" in host.groups:
38-
operations.apt.packages(
39+
apt.packages(
3940
name="Install postgres server",
4041
packages=["postgresql-server"],
4142
)
4243
4344
if "web_servers" in host.groups:
44-
operations.apt.packages(
45+
apt.packages(
4546
name="Install nginx",
4647
packages=["nginx"],
4748
)
@@ -73,15 +74,17 @@ Let's look at an example - the deploy code here is bad but highlights the orderi
7374

7475
.. code:: python
7576
76-
from pyinfra import facts, host, operations
77+
from pyinfra import facts, host
78+
from pyinfra.operations import apt
79+
from pyinfra.facts.files import File
7780
78-
operations.apt.packages(
81+
apt.packages(
7982
name="Install nginx",
8083
packages=["nginx"],
8184
)
8285
83-
if host.get_fact(facts.files.File, path="/etc/nginx/sites-enabled/default"):
84-
operations.files.file(
86+
if host.get_fact(File, path="/etc/nginx/sites-enabled/default"):
87+
files.file(
8588
name="Remove nginx default site",
8689
path="/etc/nginx/sites-enabled/default",
8790
present=False,
@@ -99,14 +102,15 @@ This gets executed *before* the ``apt.packages`` install, and evaluates to ``Fal
99102

100103
.. code:: python
101104
102-
from pyinfra import facts, host, operations
105+
from pyinfra import facts, host
106+
from pyinfra.operations import apt, files
103107
104-
operations.apt.packages(
108+
apt.packages(
105109
name="Install nginx",
106110
packages=["nginx"],
107111
)
108112
109-
operations.files.file(
113+
files.file(
110114
name="Remove nginx default site",
111115
path="/etc/nginx/sites-enabled/default",
112116
present=False,
@@ -124,21 +128,22 @@ Let's use a simple example as above with add a conditional reload based on the o
124128

125129
.. code:: python
126130
127-
from pyinfra import facts, host, operations
131+
from pyinfra import facts, host
132+
from pyinfra.operations import apt, files, server
128133
129-
operations.apt.packages(
134+
apt.packages(
130135
name="Install nginx",
131136
packages=["nginx"],
132137
)
133138
134-
remove_default_site = operations.files.file(
139+
remove_default_site = files.file(
135140
name="Remove nginx default site",
136141
path="/etc/nginx/sites-enabled/default",
137142
present=False,
138143
)
139144
140145
if remove_default_site.changed:
141-
operation.server.service(
146+
server.service(
142147
name="Reload nginx",
143148
service="nginx",
144149
reloaded=True,
@@ -154,20 +159,21 @@ Since this gets executed before nginx is installed by ``apt.packages`` operation
154159

155160
.. code:: python
156161
157-
from pyinfra import facts, host, operations
162+
from pyinfra import facts, host
163+
from pyinfra.operations import apt, files, server
158164
159-
operations.apt.packages(
165+
apt.packages(
160166
name="Install nginx",
161167
packages=["nginx"],
162168
)
163169
164-
remove_default_site = operations.files.file(
170+
remove_default_site = files.file(
165171
name="Remove nginx default site",
166172
path="/etc/nginx/sites-enabled/default",
167173
present=False,
168174
)
169175
170-
operations.server.service(
176+
server.service(
171177
name="Reload nginx",
172178
service="nginx",
173179
reloaded=True,

docs/using-operations.rst

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ See :doc:`facts` for a full list of available facts and arguments.
109109
Only use immutable facts in deploy code (installed OS, Arch, etc) unless you are absolutely sure they will not change. See: `using host facts <deploy-process.html#using-host-facts>`_.
110110

111111
Fact Errors
112-
~~~~~~~~~~~
112+
^^^^^^^^^^^
113113

114114
When facts fail due to an error the host will be marked as failed just as it would when an operation fails. This can be avoided by passing the ``_ignore_errors`` argument:
115115

@@ -141,29 +141,23 @@ Like ``host``, there is an ``inventory`` object that can be used to access the e
141141
)
142142
143143
144-
Operation Changes & Output
145-
--------------------------
144+
Change Detection
145+
----------------
146146

147147
All operations return an operation meta object which provides information about the changes the operation *will* execute. This can be used to control other operations via the ``_if`` argument:
148148

149149
.. code:: python
150150
151151
from pyinfra.operations import server
152152
153-
create_user = server.user(
154-
name="Create user myuser",
155-
user="myuser",
156-
)
157-
158-
create_otheruser = server.user(
159-
name="Create user otheruser",
160-
user="otheruser",
161-
)
153+
create_user = server.user(...)
154+
create_otheruser = server.user(...)
162155
163156
server.shell(
164157
name="Bootstrap myuser",
165158
commands=["..."],
166-
_if=create_user.did_change,
159+
# Only execute this operation if the first user create executed any changes
160+
_if=create_user.did_change, # also: did_not_change, did_succeed, did_error
167161
)
168162
169163
# A list can be provided to run an operation if **all** functions return true
@@ -183,10 +177,10 @@ All operations return an operation meta object which provides information about
183177
server.shell(commands=["..."], _if=any_changed(create_user, create_otheruser))
184178
server.shell(commands=["..."], _if=all_changed(create_user, create_otheruser))
185179
186-
Operation Output
187-
~~~~~~~~~~~~~~~~
180+
Output & Callbacks
181+
------------------
188182

189-
pyinfra doesn't immediately execute operations, meaning output is not available right away. It is possible to access this output at runtime by providing a callback function using the :ref:`operations:python.call` operation.
183+
pyinfra doesn't immediately execute operations, meaning output is not available right away. It is possible to access this output at runtime by providing a callback function using the :ref:`operations:python.call` operation. Callback functions may also call other operations which will be immediately executed. Why/how this works `is described here <deploy-process.html#how-pyinfra-detects-changes-orders-operations>`_.
190184

191185
.. code:: python
192186
@@ -218,40 +212,17 @@ There is also the possibility to use pyinfra's logging functionality which may b
218212
219213
220214
Produces output similar to:
215+
216+
.. code::
217+
221218
--> Preparing Operations...
222219
Loading: deploy_create_users.py
223220
Checking output of ufw_usable: None
224221
[multitest.example.com] Ready: deploy_create_users.py
225222
226223
227-
228-
Nested Operations
229-
-----------------
230-
231-
Nested operations are called during the execution phase within a callback function passed into a :ref:`operations:python.call`. Calling a nested operation immediately executes it on the target machine. This is useful in complex scenarios where one operation output is required in another.
232-
233-
Because nested operations are executed immediately, the output is always available right away:
234-
235-
.. code:: python
236-
237-
from pyinfra import logger
238-
from pyinfra.operations import python, server
239-
240-
def callback():
241-
result = server.shell(
242-
commands=["echo output"],
243-
)
244-
245-
logger.info(f"Got result: {result.stdout}")
246-
247-
python.call(
248-
name="Execute callback function",
249-
function=callback,
250-
)
251-
252-
253-
Include Multiple Files
254-
----------------------
224+
Include Files
225+
-------------
255226

256227
Including files can be used to break out operations across multiple files. Files can be included using ``local.include``.
257228

0 commit comments

Comments
 (0)