Skip to content

Commit 3a4f746

Browse files
author
github-actions
committed
Docs build 2025-04-07
1 parent a75b283 commit 3a4f746

File tree

8 files changed

+246
-82
lines changed

8 files changed

+246
-82
lines changed

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

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,6 @@ The connector can also be run using `pyinfra @[name of connector in pyinfra.conn
4545
(inventory/data requested for a single host), `make_names_data(_=None)` should be updated to `make_names_data(name)` and the `name == None` case
4646
handled in code separately from `name` being a valid string.
4747

48-
Its worth being aware up front that due to `make_names_data` being a `staticmethod` it has no automatic access to the parent classes attributes.
49-
To work around this - eg to configure an API connector - configuration will have to happen outside the function and be imported in. Two examples
50-
(`getattr` and a function) are provided below.
51-
52-
```py
53-
def load_settings():
54-
settings = {}
55-
# logic here
56-
return settings
57-
58-
class InventoryConnector(BaseConnector):
59-
api_instance = external.ApiClient()
60-
...
61-
62-
@staticmethod
63-
def make_names_data(_=None)
64-
api_client = getattr(InventoryConnector, 'api_instance')
65-
api_settings = load_settings()
66-
...
67-
```
68-
6948
## Executing Connector
7049

7150
A connector that implements execution requires a few more methods:
@@ -136,6 +115,71 @@ class LocalConnector(BaseConnector):
136115
"""
137116
```
138117

118+
## Where to make changes
119+
120+
Connectors enable pyinfra to expand work done `in its 5 stages <deploy-process.html#how-pyinfra-works>`_ by providing methods which can be called at
121+
appropriate times.
122+
123+
To hook in to to the various steps with the methods outlined below.
124+
```
125+
--> Loading config...
126+
--> Loading inventory...
127+
```
128+
`make_names_data` is used to supply inventory data about a host while at 'Loading inventory' stage.
129+
130+
Its worth being aware up front that due to `make_names_data` being a `staticmethod` it has no automatic access to the parent classes attributes.
131+
To work around this - eg to configure an API connector - configuration will have to happen outside the function and be imported in. Two examples
132+
(`getattr` and a function) are provided below.
133+
134+
```py
135+
def load_settings():
136+
settings = {}
137+
# logic here
138+
return settings
139+
140+
class InventoryConnector(BaseConnector):
141+
api_instance = external.ApiClient()
142+
...
143+
144+
@staticmethod
145+
def make_names_data(_=None)
146+
api_client = getattr(InventoryConnector, 'api_instance')
147+
api_settings = load_settings()
148+
...
149+
```
150+
151+
```
152+
--> Connecting to hosts...
153+
[pytest.example.com] Connected
154+
```
155+
`connect` can be used to check access to a host is possible. If the connection fails `ConnectError` should be raised with a message to display on
156+
screen.
157+
158+
```
159+
--> Preparing operations...
160+
--> Preparing Operations...
161+
Loading: deploy_create_users.py
162+
[pytest.example.com] Ready: deploy_create_users.py
163+
164+
--> Detected changes:
165+
[list of changes here]
166+
167+
--> Beginning operation run...
168+
--> Starting operation: sshd_install.py | Install OpenSSH server
169+
[pytest.example.com] No changes
170+
```
171+
172+
`run_shell_command`, `put_file`, `get_file` and `rsync` can be used to change behaviour of pyinfra as it performs operations.
173+
174+
```
175+
--> Results:
176+
Operation Hosts Success Error No Change
177+
--> Disconnecting from hosts...
178+
```
179+
180+
`disconnect` can be used after all operations complete to clean up any connection/s remaining to the hosts being managed.
181+
182+
139183
## pyproject.toml
140184

141185
In order for pyinfra to gain knowledge about your connector, you need to add the following snippet to your connector's `pyproject.toml`:

en/latest/api/connectors.html

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ <h3>Navigation</h3>
108108
<li class="toctree-l1 current"><a class="current reference internal" href="#">Writing Connectors</a><ul>
109109
<li class="toctree-l2"><a class="reference internal" href="#inventory-connector">Inventory Connector</a></li>
110110
<li class="toctree-l2"><a class="reference internal" href="#executing-connector">Executing Connector</a></li>
111+
<li class="toctree-l2"><a class="reference internal" href="#where-to-make-changes">Where to make changes</a></li>
111112
<li class="toctree-l2"><a class="reference internal" href="#pyproject-toml">pyproject.toml</a></li>
112113
</ul>
113114
</li>
@@ -178,25 +179,6 @@ <h2>Inventory Connector<a class="headerlink" href="#inventory-connector" title="
178179
<p>The connector can also be run using <code class="docutils literal notranslate"><span class="pre">pyinfra</span> <span class="pre">&#64;[name</span> <span class="pre">of</span> <span class="pre">connector</span> <span class="pre">in</span> <span class="pre">pyinfra.connectors]/[hostname]</span> <span class="pre">[deployment</span> <span class="pre">script].py</span></code>. If executed this way
179180
(inventory/data requested for a single host), <code class="docutils literal notranslate"><span class="pre">make_names_data(_=None)</span></code> should be updated to <code class="docutils literal notranslate"><span class="pre">make_names_data(name)</span></code> and the <code class="docutils literal notranslate"><span class="pre">name</span> <span class="pre">==</span> <span class="pre">None</span></code> case
180181
handled in code separately from <code class="docutils literal notranslate"><span class="pre">name</span></code> being a valid string.</p>
181-
<p>Its worth being aware up front that due to <code class="docutils literal notranslate"><span class="pre">make_names_data</span></code> being a <code class="docutils literal notranslate"><span class="pre">staticmethod</span></code> it has no automatic access to the parent classes attributes.
182-
To work around this - eg to configure an API connector - configuration will have to happen outside the function and be imported in. Two examples
183-
(<code class="docutils literal notranslate"><span class="pre">getattr</span></code> and a function) are provided below.</p>
184-
<div class="highlight-py notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">load_settings</span><span class="p">():</span>
185-
<span class="n">settings</span> <span class="o">=</span> <span class="p">{}</span>
186-
<span class="c1"># logic here</span>
187-
<span class="k">return</span> <span class="n">settings</span>
188-
189-
<span class="k">class</span> <span class="nc">InventoryConnector</span><span class="p">(</span><span class="n">BaseConnector</span><span class="p">):</span>
190-
<span class="n">api_instance</span> <span class="o">=</span> <span class="n">external</span><span class="o">.</span><span class="n">ApiClient</span><span class="p">()</span>
191-
<span class="o">...</span>
192-
193-
<span class="nd">@staticmethod</span>
194-
<span class="k">def</span> <span class="nf">make_names_data</span><span class="p">(</span><span class="n">_</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
195-
<span class="n">api_client</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">InventoryConnector</span><span class="p">,</span> <span class="s1">&#39;api_instance&#39;</span><span class="p">)</span>
196-
<span class="n">api_settings</span> <span class="o">=</span> <span class="n">load_settings</span><span class="p">()</span>
197-
<span class="o">...</span>
198-
</pre></div>
199-
</div>
200182
</section>
201183
<section id="executing-connector">
202184
<h2>Executing Connector<a class="headerlink" href="#executing-connector" title="Link to this heading"></a></h2>
@@ -267,6 +249,62 @@ <h2>Executing Connector<a class="headerlink" href="#executing-connector" title="
267249
</pre></div>
268250
</div>
269251
</section>
252+
<section id="where-to-make-changes">
253+
<h2>Where to make changes<a class="headerlink" href="#where-to-make-changes" title="Link to this heading"></a></h2>
254+
<p>Connectors enable pyinfra to expand work done <code class="docutils literal notranslate"><span class="pre">in</span> <span class="pre">its</span> <span class="pre">5</span> <span class="pre">stages</span> <span class="pre">&lt;deploy-process.html#how-pyinfra-works&gt;</span></code>_ by providing methods which can be called at
255+
appropriate times.</p>
256+
<p>To hook in to to the various steps with the methods outlined below.</p>
257+
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">--&gt;</span> <span class="n">Loading</span> <span class="n">config</span><span class="o">...</span>
258+
<span class="o">--&gt;</span> <span class="n">Loading</span> <span class="n">inventory</span><span class="o">...</span>
259+
</pre></div>
260+
</div>
261+
<p><code class="docutils literal notranslate"><span class="pre">make_names_data</span></code> is used to supply inventory data about a host while at ‘Loading inventory’ stage.</p>
262+
<p>Its worth being aware up front that due to <code class="docutils literal notranslate"><span class="pre">make_names_data</span></code> being a <code class="docutils literal notranslate"><span class="pre">staticmethod</span></code> it has no automatic access to the parent classes attributes.
263+
To work around this - eg to configure an API connector - configuration will have to happen outside the function and be imported in. Two examples
264+
(<code class="docutils literal notranslate"><span class="pre">getattr</span></code> and a function) are provided below.</p>
265+
<div class="highlight-py notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">load_settings</span><span class="p">():</span>
266+
<span class="n">settings</span> <span class="o">=</span> <span class="p">{}</span>
267+
<span class="c1"># logic here</span>
268+
<span class="k">return</span> <span class="n">settings</span>
269+
270+
<span class="k">class</span> <span class="nc">InventoryConnector</span><span class="p">(</span><span class="n">BaseConnector</span><span class="p">):</span>
271+
<span class="n">api_instance</span> <span class="o">=</span> <span class="n">external</span><span class="o">.</span><span class="n">ApiClient</span><span class="p">()</span>
272+
<span class="o">...</span>
273+
274+
<span class="nd">@staticmethod</span>
275+
<span class="k">def</span> <span class="nf">make_names_data</span><span class="p">(</span><span class="n">_</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
276+
<span class="n">api_client</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">InventoryConnector</span><span class="p">,</span> <span class="s1">&#39;api_instance&#39;</span><span class="p">)</span>
277+
<span class="n">api_settings</span> <span class="o">=</span> <span class="n">load_settings</span><span class="p">()</span>
278+
<span class="o">...</span>
279+
</pre></div>
280+
</div>
281+
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">--&gt;</span> <span class="n">Connecting</span> <span class="n">to</span> <span class="n">hosts</span><span class="o">...</span>
282+
<span class="p">[</span><span class="n">pytest</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="p">]</span> <span class="n">Connected</span>
283+
</pre></div>
284+
</div>
285+
<p><code class="docutils literal notranslate"><span class="pre">connect</span></code> can be used to check access to a host is possible. If the connection fails <code class="docutils literal notranslate"><span class="pre">ConnectError</span></code> should be raised with a message to display on
286+
screen.</p>
287+
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">--&gt;</span> <span class="n">Preparing</span> <span class="n">operations</span><span class="o">...</span>
288+
<span class="o">--&gt;</span> <span class="n">Preparing</span> <span class="n">Operations</span><span class="o">...</span>
289+
<span class="n">Loading</span><span class="p">:</span> <span class="n">deploy_create_users</span><span class="o">.</span><span class="n">py</span>
290+
<span class="p">[</span><span class="n">pytest</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="p">]</span> <span class="n">Ready</span><span class="p">:</span> <span class="n">deploy_create_users</span><span class="o">.</span><span class="n">py</span>
291+
292+
<span class="o">--&gt;</span> <span class="n">Detected</span> <span class="n">changes</span><span class="p">:</span>
293+
<span class="p">[</span><span class="nb">list</span> <span class="n">of</span> <span class="n">changes</span> <span class="n">here</span><span class="p">]</span>
294+
295+
<span class="o">--&gt;</span> <span class="n">Beginning</span> <span class="n">operation</span> <span class="n">run</span><span class="o">...</span>
296+
<span class="o">--&gt;</span> <span class="n">Starting</span> <span class="n">operation</span><span class="p">:</span> <span class="n">sshd_install</span><span class="o">.</span><span class="n">py</span> <span class="o">|</span> <span class="n">Install</span> <span class="n">OpenSSH</span> <span class="n">server</span>
297+
<span class="p">[</span><span class="n">pytest</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span><span class="p">]</span> <span class="n">No</span> <span class="n">changes</span>
298+
</pre></div>
299+
</div>
300+
<p><code class="docutils literal notranslate"><span class="pre">run_shell_command</span></code>, <code class="docutils literal notranslate"><span class="pre">put_file</span></code>, <code class="docutils literal notranslate"><span class="pre">get_file</span></code> and <code class="docutils literal notranslate"><span class="pre">rsync</span></code> can be used to change behaviour of pyinfra as it performs operations.</p>
301+
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">--&gt;</span> <span class="n">Results</span><span class="p">:</span>
302+
<span class="n">Operation</span> <span class="n">Hosts</span> <span class="n">Success</span> <span class="n">Error</span> <span class="n">No</span> <span class="n">Change</span>
303+
<span class="o">--&gt;</span> <span class="n">Disconnecting</span> <span class="kn">from</span> <span class="nn">hosts...</span>
304+
</pre></div>
305+
</div>
306+
<p><code class="docutils literal notranslate"><span class="pre">disconnect</span></code> can be used after all operations complete to clean up any connection/s remaining to the hosts being managed.</p>
307+
</section>
270308
<section id="pyproject-toml">
271309
<h2>pyproject.toml<a class="headerlink" href="#pyproject-toml" title="Link to this heading"></a></h2>
272310
<p>In order for pyinfra to gain knowledge about your connector, you need to add the following snippet to your connector’s <code class="docutils literal notranslate"><span class="pre">pyproject.toml</span></code>:</p>

en/latest/objects.inv

27 Bytes
Binary file not shown.

en/latest/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

en/next/_sources/api/connectors.md.txt

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,6 @@ The connector can also be run using `pyinfra @[name of connector in pyinfra.conn
4545
(inventory/data requested for a single host), `make_names_data(_=None)` should be updated to `make_names_data(name)` and the `name == None` case
4646
handled in code separately from `name` being a valid string.
4747

48-
Its worth being aware up front that due to `make_names_data` being a `staticmethod` it has no automatic access to the parent classes attributes.
49-
To work around this - eg to configure an API connector - configuration will have to happen outside the function and be imported in. Two examples
50-
(`getattr` and a function) are provided below.
51-
52-
```py
53-
def load_settings():
54-
settings = {}
55-
# logic here
56-
return settings
57-
58-
class InventoryConnector(BaseConnector):
59-
api_instance = external.ApiClient()
60-
...
61-
62-
@staticmethod
63-
def make_names_data(_=None)
64-
api_client = getattr(InventoryConnector, 'api_instance')
65-
api_settings = load_settings()
66-
...
67-
```
68-
6948
## Executing Connector
7049

7150
A connector that implements execution requires a few more methods:
@@ -136,6 +115,71 @@ class LocalConnector(BaseConnector):
136115
"""
137116
```
138117

118+
## Where to make changes
119+
120+
Connectors enable pyinfra to expand work done `in its 5 stages <deploy-process.html#how-pyinfra-works>`_ by providing methods which can be called at
121+
appropriate times.
122+
123+
To hook in to to the various steps with the methods outlined below.
124+
```
125+
--> Loading config...
126+
--> Loading inventory...
127+
```
128+
`make_names_data` is used to supply inventory data about a host while at 'Loading inventory' stage.
129+
130+
Its worth being aware up front that due to `make_names_data` being a `staticmethod` it has no automatic access to the parent classes attributes.
131+
To work around this - eg to configure an API connector - configuration will have to happen outside the function and be imported in. Two examples
132+
(`getattr` and a function) are provided below.
133+
134+
```py
135+
def load_settings():
136+
settings = {}
137+
# logic here
138+
return settings
139+
140+
class InventoryConnector(BaseConnector):
141+
api_instance = external.ApiClient()
142+
...
143+
144+
@staticmethod
145+
def make_names_data(_=None)
146+
api_client = getattr(InventoryConnector, 'api_instance')
147+
api_settings = load_settings()
148+
...
149+
```
150+
151+
```
152+
--> Connecting to hosts...
153+
[pytest.example.com] Connected
154+
```
155+
`connect` can be used to check access to a host is possible. If the connection fails `ConnectError` should be raised with a message to display on
156+
screen.
157+
158+
```
159+
--> Preparing operations...
160+
--> Preparing Operations...
161+
Loading: deploy_create_users.py
162+
[pytest.example.com] Ready: deploy_create_users.py
163+
164+
--> Detected changes:
165+
[list of changes here]
166+
167+
--> Beginning operation run...
168+
--> Starting operation: sshd_install.py | Install OpenSSH server
169+
[pytest.example.com] No changes
170+
```
171+
172+
`run_shell_command`, `put_file`, `get_file` and `rsync` can be used to change behaviour of pyinfra as it performs operations.
173+
174+
```
175+
--> Results:
176+
Operation Hosts Success Error No Change
177+
--> Disconnecting from hosts...
178+
```
179+
180+
`disconnect` can be used after all operations complete to clean up any connection/s remaining to the hosts being managed.
181+
182+
139183
## pyproject.toml
140184

141185
In order for pyinfra to gain knowledge about your connector, you need to add the following snippet to your connector's `pyproject.toml`:

0 commit comments

Comments
 (0)