Skip to content

Commit b669f71

Browse files
author
github-actions
committed
Docs build 2025-01-09
1 parent 6caa87f commit b669f71

File tree

320 files changed

+2120
-3215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

320 files changed

+2120
-3215
lines changed

en/latest/_sources/api/facts.md.txt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Writing Facts
2+
3+
[Facts](../facts) are written as Python classes. They provide a ``command`` (as either a string or method)
4+
and a ``process`` function. The command is executed on the target host and the output
5+
passed (as a ``list`` of lines) to the ``process`` handler to generate fact data. Facts can output anything, normally a ``list`` or ``dict``.
6+
7+
Fact classes may provide a ``default`` function that takes no arguments (except ``self``). The return value of this function is used if an error
8+
occurs during fact collection. Additionally, a ``requires_command`` variable can be set on the fact that specifies a command that must be available
9+
on the host to collect the fact. If this command is not present on the host, the fact will be set to the default, or empty if no ``default`` function
10+
is available.
11+
12+
### Importing & Using Facts
13+
14+
Like operations, facts are imported from Python modules and executed by calling `Host.get_fact`. For example:
15+
16+
```py
17+
from pyinfra import host
18+
from pyinfra.facts.server import Which
19+
20+
host.get_fact(Which, command='htop')
21+
```
22+
23+
### Example: getting swap status
24+
25+
This fact returns a boolean indicating whether swap is enabled. For this fact the ``command`` is declared as a class attribute.
26+
27+
```py
28+
from pyinfra.api import FactBase
29+
30+
class SwapEnabled(FactBase):
31+
'''
32+
Returns a boolean indicating whether swap is enabled.
33+
'''
34+
35+
command = 'swapon --show'
36+
37+
def process(self, output):
38+
return len(output) > 0 # we have one+ lines
39+
```
40+
41+
This fact could then be used like so:
42+
43+
```py
44+
is_swap_enabled = host.get_fact(SwapEnabled)
45+
```
46+
47+
### Example: getting the list of files in a directory
48+
49+
This fact returns a list of files found in a given directory. For this fact the ``command`` is declared as a class method, indicating the fact takes arguments.
50+
51+
```py
52+
from pyinfra.api import FactBase
53+
54+
class FindFiles(FactBase):
55+
'''
56+
Returns a list of files from a start point, recursively using find.
57+
'''
58+
59+
def command(self, path):
60+
# Find files in the given location
61+
return 'find {0} -type f'.format(path)
62+
63+
def process(self, output):
64+
return output # return the list of lines (files) as-is
65+
```
66+
67+
This fact could then be used like so:
68+
69+
```py
70+
list_of_files = host.get_fact(FindFiles, path='/somewhere')
71+
```
72+
73+
### Example: getting any output from a command
74+
75+
This fact returns the raw output of any command. For this fact the ``command`` is declared as a class method, indicating the fact takes arguments.
76+
77+
```py
78+
from pyinfra.api import FactBase
79+
80+
class RawCommandOutput(FactBase):
81+
'''
82+
Returns the raw output of a command.
83+
'''
84+
85+
def command(self, command):
86+
return command
87+
88+
def process(self, output):
89+
return '\n'.join(output) # re-join and return the output lines
90+
```
91+
92+
This fact could then be used like so:
93+
94+
```py
95+
command_output = host.get_fact(RawCommandOutput, command='execute this command')
96+
```

en/latest/_sources/api/index.rst.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ In addition to :doc:`the pyinfra CLI <../cli>`, pyinfra provides a full Python A
66
You can also reference `pyinfra's own main.py <https://github.com/pyinfra-dev/pyinfra/blob/3.x/pyinfra_cli/main.py>`_, and the `pyinfra API source code <https://github.com/pyinfra-dev/pyinfra/tree/3.x/pyinfra/api>`_.
77

88
Full Example
9-
============
9+
------------
1010

1111
`A full example of how to use the API can be found here <https://github.com/pyinfra-dev/pyinfra-examples/blob/main/.old/api_deploy.py>`_. (Note: this is not yet tested and is pending future updates)
1212

1313
Basic Localhost Example
14-
=======================
14+
-----------------------
1515

1616
.. code:: python
1717
Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
# Writing Facts & Operations
2-
3-
Operations & facts are the building blocks pyinfra uses to make changes to hosts. This document describes how you can write your own to extend pyinfra's functionality.
4-
5-
## Operations
1+
# Writing Operations
62

73
[Operations](../operations) are defined as Python functions. They are passed the current deploy state, the target host, and any operation arguments. Operation functions read state from the host, compare it to the arguments, and yield commands.
84

@@ -82,100 +78,3 @@ def file(name, present=True):
8278
elif info and not present:
8379
yield "rm -f {0}".format(name)
8480
```
85-
86-
## Facts
87-
88-
[Facts](../facts) are written as Python classes. They provide a ``command`` (as either a string or method)
89-
and a ``process`` function. The command is executed on the target host and the output
90-
passed (as a ``list`` of lines) to the ``process`` handler to generate fact data. Facts can output anything, normally a ``list`` or ``dict``.
91-
92-
Fact classes may provide a ``default`` function that takes no arguments (except ``self``). The return value of this function is used if an error
93-
occurs during fact collection. Additionally, a ``requires_command`` variable can be set on the fact that specifies a command that must be available
94-
on the host to collect the fact. If this command is not present on the host, the fact will be set to the default, or empty if no ``default`` function
95-
is available.
96-
97-
### Importing & Using Facts
98-
99-
Like operations, facts are imported from Python modules and executed by calling `Host.get_fact`. For example:
100-
101-
```py
102-
from pyinfra import host
103-
from pyinfra.facts.server import Which
104-
105-
host.get_fact(Which, command='htop')
106-
```
107-
108-
### Example: getting swap status
109-
110-
This fact returns a boolean indicating whether swap is enabled. For this fact the ``command`` is declared as a class attribute.
111-
112-
```py
113-
from pyinfra.api import FactBase
114-
115-
class SwapEnabled(FactBase):
116-
'''
117-
Returns a boolean indicating whether swap is enabled.
118-
'''
119-
120-
command = 'swapon --show'
121-
122-
def process(self, output):
123-
return len(output) > 0 # we have one+ lines
124-
```
125-
126-
This fact could then be used like so:
127-
128-
```py
129-
is_swap_enabled = host.get_fact(SwapEnabled)
130-
```
131-
132-
### Example: getting the list of files in a directory
133-
134-
This fact returns a list of files found in a given directory. For this fact the ``command`` is declared as a class method, indicating the fact takes arguments.
135-
136-
```py
137-
from pyinfra.api import FactBase
138-
139-
class FindFiles(FactBase):
140-
'''
141-
Returns a list of files from a start point, recursively using find.
142-
'''
143-
144-
def command(self, path):
145-
# Find files in the given location
146-
return 'find {0} -type f'.format(path)
147-
148-
def process(self, output):
149-
return output # return the list of lines (files) as-is
150-
```
151-
152-
This fact could then be used like so:
153-
154-
```py
155-
list_of_files = host.get_fact(FindFiles, path='/somewhere')
156-
```
157-
158-
### Example: getting any output from a command
159-
160-
This fact returns the raw output of any command. For this fact the ``command`` is declared as a class method, indicating the fact takes arguments.
161-
162-
```py
163-
from pyinfra.api import FactBase
164-
165-
class RawCommandOutput(FactBase):
166-
'''
167-
Returns the raw output of a command.
168-
'''
169-
170-
def command(self, command):
171-
return command
172-
173-
def process(self, output):
174-
return '\n'.join(output) # re-join and return the output lines
175-
```
176-
177-
This fact could then be used like so:
178-
179-
```py
180-
command_output = host.get_fact(RawCommandOutput, command='execute this command')
181-
```

en/latest/_sources/connectors/docker.rst.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
``@docker`` Connector
22
---------------------
33

4-
The docker connector allows you to build Docker images or modify running
5-
Docker containers. You can pass either an image name or existing container ID:
4+
The Docker connector allows you to use pyinfra to create new Docker images or modify running
5+
Docker containers.
66

7-
+ Image - will create a new container from the image, execute operations against it, save into a new Docker image and remove the container
8-
+ Existing container ID - will execute operations against the running container, leaving it running
7+
.. note::
8+
9+
The Docker connector allows pyinfra to target Docker containers as inventory and is
10+
unrelated to the :doc:`../operations/docker` & :doc:`../facts/docker`.
11+
12+
You can pass either an image name or existing container ID:
13+
14+
+ Image - will create a new container from the image, execute operations against it, save into a new Docker image and remove the container
15+
+ Existing container ID - will execute operations against the running container, leaving it running
916

1017
.. code:: shell
1118
@@ -18,6 +25,10 @@ Docker containers. You can pass either an image name or existing container ID:
1825
# Execute against a running container
1926
pyinfra @docker/2beb8c15a1b1 ...
2027
28+
The Docker connector is great for testing pyinfra operations locally, rather than connecting to
29+
a remote host over SSH each time. This gives you a fast, local-first devloop to iterate on when
30+
writing deploys, operations or facts.
31+
2132
Usage
2233
~~~~~
2334

en/latest/_sources/contributing.md.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Third party pull requests help expand pyinfra's functionality and are essential
77
## Guides
88

99
+ [How to write operations](api/operations)
10-
+ [How to write facts](api/operations.md#facts)
10+
+ [How to write facts](api/facts)
1111
+ [How to write connectors](api/connectors)
1212
+ [API reference](api/reference)
1313

@@ -46,10 +46,6 @@ GitHub will run all the test suites as part of any pull requests. There's a hand
4646
scripts/dev-test.sh
4747
```
4848

49-
#### Unit Tests
50-
51-
Use `pytest` to run the unit tests, or `pytest --cov` to run with coverage. Pull requests are expected to be tested and not drop overall project coverage by >1%.
52-
5349
To limit the pytests to a specific fact or operation:
5450

5551
```sh
@@ -65,6 +61,9 @@ pytest tests/test_operations.py -k "selinux."
6561
The end to end tests are also executed via `pytest` but not selected by default, options/usage:
6662

6763
```sh
64+
# Run all the e2e tests (local, SSH, Docker)
65+
scripts/dev-test-e2e.sh
66+
6867
# Run local e2e tests (works on Linux / MacOS, no Windows yet)
6968
pytest -m end_to_end_local
7069

@@ -78,11 +77,11 @@ pytest -m end_to_end_docker
7877
To generate:
7978

8079
```sh
81-
sphinx-build -a docs/ docs/build/
80+
scripts/build-public-docs.sh
8281
```
8382

8483
To view ([localhost:8000](http://localhost:8000)):
8584

8685
```sh
87-
python -m http.server -d docs/build/
86+
python -m http.server -d docs/public/en/latest/
8887
```

en/latest/_sources/facts/crontab.rst.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ See also: :doc:`../operations/crontab`.
1515
Returns a dictionary of CrontabFile.
1616

1717
.. code:: python
18+
1819
# CrontabFile.items()
1920
{
2021
"/path/to/command": {

en/latest/_sources/facts/docker.rst.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Docker Facts
22
------------
33

4+
5+
Facts about Docker containers, volumes and networks. These facts give you information from the view
6+
of the current inventory host. See the :doc:`../connectors/docker` to use Docker containers as
7+
inventory directly.
8+
49
See also: :doc:`../operations/docker`.
510

611
.. _facts:docker.DockerContainer:

en/latest/_sources/index.rst.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ Using pyinfra
4040
Deploy Reference
4141
----------------
4242

43-
.. compound::
44-
:doc:`examples`
45-
A set of documented example deploys that focus on common patterns and use-cases.
46-
47-
.. compound::
48-
:doc:`connectors`
49-
A list of connectors to target different hosts such as ``@docker``, ``@local`` and ``@terraform``.
50-
5143
.. compound::
5244
:doc:`operations`
5345
A list of all available operations and their arguments, e.g. ``apt.packages``.
@@ -56,6 +48,10 @@ Deploy Reference
5648
:doc:`facts`
5749
A list of all facts pyinfra can gather from hosts, e.g. ``server.Hostname``.
5850

51+
.. compound::
52+
:doc:`connectors`
53+
A list of connectors to target different hosts such as ``@docker``, ``@local`` and ``@terraform``.
54+
5955

6056
How pyinfra Works
6157
-----------------
@@ -74,7 +70,11 @@ How pyinfra Works
7470

7571
.. compound::
7672
:doc:`api/operations`
77-
How to write your own facts & operations for pyinfra.
73+
How to write your own operations for pyinfra.
74+
75+
.. compound::
76+
:doc:`api/facts`
77+
How to write your own facts for pyinfra.
7878

7979
.. compound::
8080
:doc:`api/index`
@@ -97,19 +97,19 @@ How pyinfra Works
9797
:maxdepth: 1
9898
:caption: Deploy Reference
9999

100-
examples
101-
connectors
102100
operations
103101
facts
102+
connectors
104103

105104
.. toctree::
106105
:hidden:
107106
:caption: How pyinfra Works
108107

109108
deploy-process
110109
api/deploys
111-
api/connectors
112110
api/operations
111+
api/facts
112+
api/connectors
113113
api/index
114114

115115
.. toctree::

0 commit comments

Comments
 (0)