pyinfra-pangolin-kit 0.1.0__tar.gz

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 (25) hide show
  1. pyinfra_pangolin_kit-0.1.0/.gitignore +14 -0
  2. pyinfra_pangolin_kit-0.1.0/LICENSE +21 -0
  3. pyinfra_pangolin_kit-0.1.0/PKG-INFO +540 -0
  4. pyinfra_pangolin_kit-0.1.0/README.md +507 -0
  5. pyinfra_pangolin_kit-0.1.0/pyinfra-metadata-schema-1.0.0.json +47 -0
  6. pyinfra_pangolin_kit-0.1.0/pyinfra-metadata.toml +29 -0
  7. pyinfra_pangolin_kit-0.1.0/pyproject.toml +95 -0
  8. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/__init__.py +111 -0
  9. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/compose.py +155 -0
  10. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/deploy.py +869 -0
  11. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/facts.py +144 -0
  12. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/models.py +85 -0
  13. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/newt.py +115 -0
  14. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/operations.py +912 -0
  15. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/py.typed +0 -0
  16. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/renderers.py +205 -0
  17. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/server.py +617 -0
  18. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/config/config.yml.j2 +29 -0
  19. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/config/crowdsec/acquis.d/appsec.yaml +9 -0
  20. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/config/crowdsec/acquis.d/traefik.yaml +7 -0
  21. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/config/crowdsec/profiles.yaml +27 -0
  22. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/config/traefik/dynamic_config.yml.j2 +101 -0
  23. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/config/traefik/traefik_config.yml.j2 +73 -0
  24. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/templates/docker-compose.yml.j2 +88 -0
  25. pyinfra_pangolin_kit-0.1.0/src/pyinfra_pangolin_kit/versions.py +272 -0
@@ -0,0 +1,14 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ build/
5
+ dist/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ node_modules/
11
+
12
+ # Local caches
13
+ .uv-cache/
14
+ .prek-cache/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Simon Maillard / Ogesta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,540 @@
1
+ Metadata-Version: 2.5
2
+ Name: pyinfra-pangolin-kit
3
+ Version: 0.1.0
4
+ Summary: Reusable pyinfra helpers to install and operate Pangolin servers, Newt agents, and Pangolin API resources
5
+ Project-URL: Homepage, https://github.com/maisim/pyinfra-pangolin-kit
6
+ Project-URL: Repository, https://github.com/maisim/pyinfra-pangolin-kit
7
+ Project-URL: Issues, https://github.com/maisim/pyinfra-pangolin-kit/issues
8
+ Project-URL: Changelog, https://github.com/maisim/pyinfra-pangolin-kit/releases
9
+ Author-email: Simon Maillard <simon@ogesta.fr>
10
+ License: MIT
11
+ License-File: LICENSE
12
+ Keywords: compose,deployment,docker,infrastructure-as-code,newt,pangolin,pyinfra
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Environment :: Console
15
+ Classifier: Intended Audience :: System Administrators
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: POSIX :: Linux
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: System :: Systems Administration
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: <4.0,>=3.11
26
+ Requires-Dist: httpx>=0.27
27
+ Requires-Dist: jinja2>=3.1
28
+ Requires-Dist: pangolin-client>=0.1.0
29
+ Requires-Dist: pyinfra<4,>=3.11
30
+ Requires-Dist: pyyaml<7,>=6.0.2
31
+ Requires-Dist: ruamel-yaml>=0.18
32
+ Description-Content-Type: text/markdown
33
+
34
+ # pyinfra-pangolin-kit
35
+
36
+ > **⚠️ ALPHA RELEASE**: This project is currently in the **Alpha** stage. The API, operation arguments, and features are subject to change at any time without backward compatibility. Do not use in production environments.
37
+
38
+ > **Packaging status**: this repository is the source of truth and is public. Install the latest
39
+ > release from PyPI; while the project is alpha, pin the version you use.
40
+
41
+ [![CI](https://github.com/maisim/pyinfra-pangolin-kit/actions/workflows/python-publish.yml/badge.svg)](https://github.com/maisim/pyinfra-pangolin-kit/actions/workflows/python-publish.yml)
42
+ [![PyPI](https://img.shields.io/pypi/v/pyinfra-pangolin-kit)](https://pypi.org/project/pyinfra-pangolin-kit/)
43
+ [![Python versions](https://img.shields.io/pypi/pyversions/pyinfra-pangolin-kit)](https://pypi.org/project/pyinfra-pangolin-kit/)
44
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
45
+
46
+ `pyinfra-pangolin-kit` provides reusable pyinfra building blocks for operating a
47
+ self-hosted [Pangolin](https://pangolin.net/) installation:
48
+
49
+ - **Newt agent (site side)**: install the binary, render its configuration and
50
+ blueprint, run it under systemd or as a container, or add it to an existing
51
+ Docker Compose stack.
52
+ - **Pangolin server (control plane)**: install the official four-file Docker
53
+ Compose layout, upgrade image tags and the Badger plugin, and optionally run
54
+ CrowdSec with its Traefik plugin and access logs.
55
+ - **Pangolin API (controller side)**: provision sites, domains, resources,
56
+ targets and blueprints through the Pangolin Integration API.
57
+
58
+ ## Execution model
59
+
60
+ The package has two kinds of side effects, and they behave differently:
61
+
62
+ - **Host convergence** is emitted as pyinfra operations. Files, packages,
63
+ services and `docker compose` commands run on the target host when pyinfra
64
+ executes the operation list.
65
+ - **Pangolin API calls** run in the pyinfra process (the controller). Read-only
66
+ lookups happen while the deploy is being assembled; mutations are emitted as
67
+ `FunctionCommand`s and execute with the operation.
68
+ - `pangolin_newt` is the exception: it creates or reads the site **during
69
+ assembly** because it needs the returned `newtId` and secret. A `--dry` run
70
+ can therefore still create a Pangolin site. Use it deliberately.
71
+ - `compose_newt` reads and merges the compose file during assembly; only the
72
+ resulting file write is a pyinfra command.
73
+
74
+ ## Convergence model
75
+
76
+ Server-side objects converge through the Integration API operations:
77
+ `pangolin_site`, `pangolin_domain`, `pangolin_resource`, `pangolin_target` and
78
+ the composite `pangolin_http_resource`. Callers that prefer a declarative
79
+ document can apply a YAML blueprint with `pangolin_blueprint`; site-local
80
+ resource policy normally travels in the blueprint file that Newt reapplies
81
+ continuously (`write_blueprint`).
82
+
83
+ The official `pangolin` CLI (`fosrl/cli`) is deliberately not used for
84
+ convergence. `pangolin apply blueprint` is a YAML/glob/environment wrapper
85
+ around the same `PUT /v1/org/{org}/blueprint` endpoint, with the same
86
+ server-side semantics (no pruning, positional targets, labels replaced), and
87
+ the CLI exposes no site or domain lifecycle commands, so it cannot replace the
88
+ API operations. Its value is at runtime: `pangolin up site` and
89
+ `pangolin service install site` embed Newt. The package keeps its own Newt
90
+ runtime (`ensure_newt_runtime`) rather than adding a second one.
91
+
92
+ ## Scope
93
+
94
+ **In scope:**
95
+
96
+ - Newt binary installation, env/config/blueprint rendering, systemd and
97
+ container runtimes, compose integration, API-provisioned sites.
98
+ - Pangolin server first install and upgrades: `docker-compose.yml`,
99
+ `config/config.yml`, `config/traefik/traefik_config.yml`,
100
+ `config/traefik/dynamic_config.yml`, Badger, optional CrowdSec.
101
+ - Pangolin API operations: `pangolin_site`, `pangolin_resource`,
102
+ `pangolin_http_resource`, `pangolin_target`, `pangolin_domain`,
103
+ `pangolin_blueprint`.
104
+
105
+ **Out of scope:**
106
+
107
+ - `pangctl` and business-specific naming or blueprint policy; let the calling
108
+ layer (for example `odoo-foundry`) compute those values.
109
+ - PostgreSQL, Redis and HA compose layouts. Updates refuse any service outside
110
+ `{pangolin, gerbil, traefik, crowdsec}`.
111
+ - Adding CrowdSec to an already-installed stack.
112
+ - Moving a resource between hostnames: the API cannot do it, and the operation
113
+ raises instead of silently keeping the old one.
114
+ - `compose_newt` creating external Docker networks or running `docker compose`.
115
+ - Inventing release tags that the discovery APIs did not return during a
116
+ stepwise upgrade.
117
+
118
+ ## Requirements
119
+
120
+ **Controller** (where pyinfra runs):
121
+
122
+ - Python >= 3.11 and `pyinfra >= 3.11`.
123
+ - `pangolin-client` (installed as a dependency) and an HTTPS route to Docker
124
+ Hub, GitHub Releases and the Pangolin Integration API.
125
+ - A Pangolin API URL and key for the API operations and `pangolin_newt`.
126
+
127
+ **Pangolin server host:**
128
+
129
+ - Docker Engine with the Compose plugin (`docker compose`, with a
130
+ `docker-compose` v1 fallback).
131
+ - `python3` for the version facts, plus `curl`, `tar`, `sed`, `grep`, `date` and
132
+ `mktemp`.
133
+ - Root or sudo access to `/opt/pangolin` (or the configured project directory).
134
+ - `logrotate` when CrowdSec is enabled, and systemd only for Newt systemd mode.
135
+
136
+ **Newt host:**
137
+
138
+ - Linux on `amd64`, `arm64`, `arm32`, `armv6` or `riscv64`; `curl`, `install`
139
+ and `mktemp`.
140
+ - Docker for container mode, systemd for systemd mode.
141
+
142
+ ## Installation
143
+
144
+ ```bash
145
+ pip install pyinfra-pangolin-kit
146
+ uv add pyinfra-pangolin-kit
147
+ ```
148
+
149
+ Development checkout:
150
+
151
+ ```bash
152
+ uv sync --group dev
153
+ ```
154
+
155
+ The package depends on `pangolin-client`, `httpx`, `jinja2`, `pyyaml` and
156
+ `ruamel.yaml`, resolved from PyPI.
157
+
158
+ ## Server: first install (`pangolin_stack`)
159
+
160
+ The first run writes the four files from the official manual and starts the
161
+ stack. Values come from the arguments or from `host.data["pangolin_*"]`.
162
+
163
+ ```python
164
+ from pyinfra.api import HiddenValue
165
+ from pyinfra_pangolin_kit import pangolin_stack
166
+
167
+ pangolin_stack(
168
+ project_dir="/opt/pangolin",
169
+ backup_dir="/srv/backups",
170
+ base_domain="example.com",
171
+ dashboard_domain="pangolin.example.com",
172
+ letsencrypt_email="ops@example.com",
173
+ server_secret=HiddenValue("<server-secret>"), # openssl rand -hex 32
174
+ pangolin_version="1.26.0",
175
+ gerbil_version="1.6.0",
176
+ traefik_version="v3.7.16",
177
+ badger_version="v1.8.0",
178
+ crowdsec=True,
179
+ crowdsec_version="1.8.2",
180
+ )
181
+ ```
182
+
183
+ What the deploy does:
184
+
185
+ 1. Runs the Docker preflight. It fails before writing anything when Docker or
186
+ the Compose plugin is missing.
187
+ 2. Creates `config/{db,letsencrypt,traefik/logs}` and, with CrowdSec,
188
+ `config/crowdsec/{db,acquis.d}`.
189
+ 3. Renders the four core files (plus the CrowdSec files and
190
+ `/etc/logrotate.d/pangolin-traefik` when enabled) and writes them. The
191
+ compose template carries a `Generated by pyinfra-pangolin-kit` header.
192
+ 4. Runs `docker compose pull` and `up -d`.
193
+ 5. With CrowdSec and no `crowdsec_lapi_key`, generates the Traefik bouncer key
194
+ after CrowdSec is up, writes it into `dynamic_config.yml` and restarts
195
+ Traefik. The key is preserved on subsequent runs.
196
+ 6. Polls `docker compose ps -a` until no container is stopped, restarting,
197
+ starting or unhealthy.
198
+
199
+ The first admin is created manually in the UI with the setup token printed by
200
+ the container logs:
201
+
202
+ ```bash
203
+ docker compose -f /opt/pangolin/docker-compose.yml logs pangolin
204
+ # then open https://<dashboard-domain>/auth/initial-setup
205
+ ```
206
+
207
+ The official manual lives at
208
+ <https://docs.pangolin.net/self-host/manual/docker-compose>.
209
+
210
+ ## Server: updates (`update_pangolin_stack`)
211
+
212
+ ```python
213
+ from pyinfra_pangolin_kit import update_pangolin_stack
214
+
215
+ update_pangolin_stack(
216
+ project_dir="/opt/pangolin",
217
+ backup_dir="/srv/backups",
218
+ target_versions={
219
+ "pangolin": "1.27.0",
220
+ "gerbil": "1.6.0",
221
+ "traefik": "v3.7.17",
222
+ "badger": "v1.9.0",
223
+ "crowdsec": "1.8.2",
224
+ },
225
+ )
226
+ ```
227
+
228
+ - **Backups**: a tarball of `docker-compose.yml` and `config/` is written only
229
+ when at least one upgrade round exists. A no-op update and a fresh install
230
+ write none. Without `backup_dir`, the tarball lands in the project directory.
231
+ - **Stepwise**: with `stepwise=True` (default) each service walks the release
232
+ tags it can discover, one minor band at a time. If discovery fails, or an
233
+ intermediate tag is missing, it falls back to a direct jump to the target.
234
+ `stepwise=False` jumps directly.
235
+ - **Compose is re-rendered**: the template is the source of truth, so manual
236
+ edits to `docker-compose.yml` are lost on update. The generated header says
237
+ so.
238
+ - **Managed services**: a compose file containing anything outside
239
+ `{pangolin, gerbil, traefik, crowdsec}` fails with `OperationError` rather
240
+ than silently dropping the extra service.
241
+ - **Preserved**: `config/config.yml`, `config/db/`, `config/letsencrypt/` and an
242
+ existing CrowdSec bouncer key are never overwritten. The Badger `version:` in
243
+ `config/traefik/traefik_config.yml` is updated in place.
244
+ - `latest` tags are skipped: no upgrade path can be computed for a floating tag.
245
+
246
+ `pangolin_stack` reuses `update_pangolin_stack` on an already-installed host,
247
+ but only with explicitly requested versions. The package defaults are for a
248
+ fresh install; they never silently bump an existing stack.
249
+
250
+ ## CrowdSec
251
+
252
+ CrowdSec is enabled on a fresh install by `crowdsec=True`, or by setting
253
+ `pangolin_crowdsec_version` alone. It writes:
254
+
255
+ - the `crowdsec` service in `docker-compose.yml`, publishing port `6060`;
256
+ - the Traefik plugin, JSON access logs and `crowdsec@file` middleware;
257
+ - `config/crowdsec/profiles.yaml`,
258
+ `config/crowdsec/acquis.d/traefik.yaml` and
259
+ `config/crowdsec/acquis.d/appsec.yaml`;
260
+ - `/etc/logrotate.d/pangolin-traefik`.
261
+
262
+ The image, GID `1000`, collections/parsers, port, AppSec host and limits, trust
263
+ ranges and the plugin version are fixed in the templates. `pangolin_crowdsec`
264
+ and `pangolin_crowdsec_lapi_key` are fresh-install only: an existing stack does
265
+ not gain CrowdSec, and an existing bouncer key is never regenerated. Port `6060`
266
+ is published on all interfaces; review that before exposing the host.
267
+
268
+ ## Newt agent
269
+
270
+ ### `ensure_newt_runtime`
271
+
272
+ ```python
273
+ from pyinfra.api import HiddenValue
274
+ from pyinfra_pangolin_kit import ensure_newt_runtime
275
+
276
+ ensure_newt_runtime(
277
+ pangolin_endpoint="wss://pangolin.example.com/tunnel",
278
+ newt_id="site-123",
279
+ newt_secret=HiddenValue("<newt-secret>"),
280
+ newt_mode="container",
281
+ newt_networks=["app_default"],
282
+ newt_workdir="/etc/newt/acme-dev",
283
+ newt_env_path="/etc/newt/acme-dev/newt.env",
284
+ newt_config_path="/etc/newt/acme-dev/config.json",
285
+ newt_blueprint_path="/etc/newt/acme-dev/blueprint.yaml",
286
+ newt_compose_path="/etc/newt/acme-dev/docker-compose.yml",
287
+ newt_binary_state_dir="/var/lib/newt/acme-dev",
288
+ blueprint_contents="public-resources: {}\nprivate-resources: {}\n",
289
+ )
290
+ ```
291
+
292
+ - All `newt_*` settings can be passed as arguments or through `host.data`; an
293
+ explicit argument wins, then `host.data`, then the package default.
294
+ - Container mode requires at least one of `newt_networks` or
295
+ `newt_docker_socket`. Missing external networks are created before the
296
+ container starts.
297
+ - Systemd mode installs the Newt binary and writes a systemd unit.
298
+ - `newt_workdir` is the managed directory; it does **not** move
299
+ `newt_env_path`, `newt_config_path` or `newt_blueprint_path`, which keep their
300
+ own defaults unless set.
301
+ - `newt_version` is the binary version in systemd mode and the image tag in
302
+ container mode.
303
+
304
+ ### `pangolin_newt`
305
+
306
+ `pangolin_newt` looks a site up by display name, creates it if missing, then
307
+ delegates the host work to `ensure_newt_runtime`.
308
+
309
+ ```python
310
+ from pyinfra.api import HiddenValue
311
+ from pyinfra_pangolin_kit import pangolin_newt
312
+
313
+ pangolin_newt(
314
+ pangolin_endpoint="wss://pangolin.example.com/tunnel",
315
+ pangolin_api_url="https://pangolin.example.com/api/v1",
316
+ pangolin_api_key=HiddenValue("<api-key>"),
317
+ org_id="acme",
318
+ site_name="acme-dev",
319
+ newt_mode="systemd",
320
+ blueprint_contents="public-resources: {}\nprivate-resources: {}\n",
321
+ )
322
+ ```
323
+
324
+ - `nice_id` is accepted but **not persisted**: the Pangolin API generates the
325
+ site niceId. The lookup is by `site_name`.
326
+ - An existing site's secret is not returned by the API. The deploy reads it from
327
+ `newt_config_path` (resolved through `host.data`) and raises
328
+ `OperationError` when it cannot recover one, rather than recreating the site
329
+ and orphaning its targets and resources.
330
+ - `present=False` removes the env/config/blueprint/workdir and, depending on
331
+ the mode, the unit or compose file. The installed binary and
332
+ `/var/lib/newt/...` state directory are kept.
333
+
334
+ ### `compose_newt`
335
+
336
+ `compose_newt` adds the Newt service to an existing `docker-compose.yml`. It
337
+ preserves comments, anchors and key order through `ruamel.yaml`.
338
+
339
+ ```python
340
+ from pyinfra.api import HiddenValue
341
+ from pyinfra_pangolin_kit import compose_newt
342
+
343
+ compose_newt(
344
+ "/srv/immich/docker-compose.yml",
345
+ pangolin_endpoint="wss://pangolin.example.com/tunnel",
346
+ newt_id="site-123",
347
+ newt_secret=HiddenValue("<newt-secret>"),
348
+ newt_networks=["immich_default"],
349
+ )
350
+ ```
351
+
352
+ - An existing service with the same container name is overwritten.
353
+ - `newt_networks` are declared `external: true`; unlike `ensure_newt_runtime`,
354
+ `compose_newt` does **not** create them.
355
+ - `newt_target_network` creates a network inside the compose file and attaches
356
+ the listed `newt_target_services`; an unknown service name fails.
357
+ - `newt_enforce_network_validation` only has an effect when a Docker socket is
358
+ configured.
359
+ - A bare socket path is normalized to `unix://...`; only `unix://` produces a
360
+ container mount. `tcp://...` is passed through the env file but not mounted.
361
+ - The env, config and blueprint files are written under `newt_workdir` (modes
362
+ 0600/0640), but the operation does **not** run `docker compose`. The owning
363
+ stack converges it.
364
+
365
+ ### Other Newt helpers
366
+
367
+ - `write_blueprint(dest=..., blueprint_contents=.../blueprint_src=...)` writes
368
+ the local blueprint (mode 0640).
369
+ - `install_newt_binary(version=..., binary_path=..., state_dir=...)` installs
370
+ the binary and returns its path.
371
+ - `fix_newt_permissions(config=None, **kwargs)` creates the managed directories.
372
+
373
+ ## Pangolin API operations
374
+
375
+ The API operations need `api_url`, `api_key`, `org_id` and a key with the
376
+ relevant actions. Pass secrets as `HiddenValue`; use `unmask` to unwrap one.
377
+
378
+ | Operation | Lookup key and limits |
379
+ | --- | --- |
380
+ | `pangolin_site` | display name (first 100 sites). `nice_id` is accepted but not sent. `present=False` deletes the site. |
381
+ | `pangolin_resource` | `nice_id` hint, then the full domain (resolved from `domain_id`), then display name + subdomain. `present=False` deletes the found resource. |
382
+ | `pangolin_http_resource` | domain by `base_domain`, site by `site_name`, then resource by full domain/name. Creates the resource and its target. A subdomain change raises rather than moving it. `present=False` deletes the target then the resource. |
383
+ | `pangolin_target` | identity `(site_id, ip, port)`. `method` is create-only: changing it is not reconciled. `present=False` removes the first match. |
384
+ | `pangolin_domain` | `base_domain`. `type` is create-only. `present=False` deletes it. |
385
+ | `pangolin_blueprint` | translates target `site` names to niceIds and skips resources whose `full-domain` already exists. Empty blueprints are not sent. |
386
+
387
+ **Destructive change:** `pangolin_resource(present=False)` and
388
+ `pangolin_http_resource(present=False)` now find resources by full domain, so
389
+ they delete resources created by earlier versions that a niceId lookup missed.
390
+ Review teardown runs before upgrading.
391
+
392
+ ## Configuration contract
393
+
394
+ Precedence is: explicit argument, then `host.data[key]`, then the package
395
+ default.
396
+
397
+ **Server:**
398
+
399
+ | Key | Argument | Consumed by | `host.data` honoured |
400
+ | --- | --- | --- | --- |
401
+ | `pangolin_project_dir` | `project_dir` | `pangolin_stack`, `update_pangolin_stack` | yes |
402
+ | `pangolin_backup_dir` | `backup_dir` | `pangolin_stack`, `update_pangolin_stack` | yes |
403
+ | `pangolin_base_domain` | `base_domain` | `pangolin_stack` (fresh install) | yes |
404
+ | `pangolin_dashboard_domain` | `dashboard_domain` | `pangolin_stack` (fresh install) | yes |
405
+ | `pangolin_letsencrypt_email` | `letsencrypt_email` | `pangolin_stack` (fresh install) | yes |
406
+ | `pangolin_server_secret` | `server_secret` | `pangolin_stack` (fresh install) | yes |
407
+ | `pangolin_version` | `pangolin_version` | `pangolin_stack` (explicit upgrade) | yes |
408
+ | `pangolin_gerbil_version` | `gerbil_version` | `pangolin_stack` (explicit upgrade) | yes |
409
+ | `pangolin_traefik_version` | `traefik_version` | `pangolin_stack` (explicit upgrade) | yes |
410
+ | `pangolin_badger_version` | `badger_version` | `pangolin_stack` (explicit upgrade) | yes |
411
+ | `pangolin_crowdsec` | `crowdsec` | `pangolin_stack` (fresh install) | yes |
412
+ | `pangolin_crowdsec_version` | `crowdsec_version` | `pangolin_stack` (fresh install) | yes |
413
+ | `pangolin_crowdsec_lapi_key` | `crowdsec_lapi_key` | `pangolin_stack` (fresh install) | yes |
414
+
415
+ `update_pangolin_stack` only reads `pangolin_project_dir`/`pangolin_backup_dir`
416
+ from `host.data`; versions must be passed explicitly in `target_versions`.
417
+
418
+ **Newt:**
419
+
420
+ | Argument / `host.data` key | Consumed by |
421
+ | --- | --- |
422
+ | `pangolin_endpoint` | `ensure_newt_runtime`, `pangolin_newt` |
423
+ | `newt_id`, `newt_secret` | `ensure_newt_runtime`, `pangolin_newt` |
424
+ | `newt_mode`, `newt_version` | both |
425
+ | `newt_binary_path`, `newt_env_path`, `newt_config_path`, `newt_blueprint_path`, `newt_compose_path` | both |
426
+ | `newt_container_name`, `newt_workdir`, `newt_service_name`, `newt_binary_state_dir` | both |
427
+ | `newt_log_level`, `newt_docker_socket`, `newt_enforce_network_validation`, `newt_health_file`, `newt_updown_script`, `newt_networks` | both |
428
+
429
+ `compose_newt` accepts only keyword arguments; it does not read `host.data`.
430
+
431
+ Example inventory data:
432
+
433
+ ```python
434
+ host.data.update(
435
+ {
436
+ "pangolin_project_dir": "/opt/pangolin",
437
+ "pangolin_base_domain": "example.com",
438
+ "pangolin_dashboard_domain": "pangolin.example.com",
439
+ "pangolin_letsencrypt_email": "ops@example.com",
440
+ "newt_mode": "systemd",
441
+ "newt_workdir": "/etc/newt/acme-dev",
442
+ }
443
+ )
444
+ ```
445
+
446
+ ## Secrets and `HiddenValue`
447
+
448
+ `newt_secret`, `pangolin_api_key`, `server_secret` and `crowdsec_lapi_key`
449
+ accept a `HiddenValue`. The package unmasks it internally and keeps it out of
450
+ pyinfra's operation arguments and command reprs. `unmask` is exported from the
451
+ package root for callers that need the real value in their own code.
452
+
453
+ `HiddenValue` does not protect data at rest: the secret is still written to the
454
+ target host. `config/config.yml` is written mode 0640, Newt env/config files
455
+ mode 0600, and `config/traefik/dynamic_config.yml` (which contains the CrowdSec
456
+ bouncer key) mode 0644 by the template. Do not pass a `HiddenValue` to a
457
+ renderer; unmask it first.
458
+
459
+ ## Security defaults
460
+
461
+ The templates inherit the official manual's defaults. Review them before
462
+ exposing a server:
463
+
464
+ - A Docker socket mounted into Newt is root-equivalent on the host.
465
+ - Traefik enables `api.insecure: true` and `serversTransport.insecureSkipVerify:
466
+ true`.
467
+ - Telemetry uses `anonymous_usage: true`.
468
+ - Pangolin sets `allow_raw_resources: true`.
469
+ - CrowdSec publishes `6060` on all interfaces and trusts `0.0.0.0/0` for
470
+ forwarded headers.
471
+ - Gerbil ports, Pangolin log level/flags/CORS, Traefik entrypoints and ACME, and
472
+ CrowdSec GID/collections/trust ranges are hardcoded in the templates.
473
+
474
+ The compose file is re-rendered on update, so compose changes must go into the
475
+ template. `config/config.yml` and the Traefik configs are only written on a
476
+ fresh install; editing them on a host is preserved.
477
+
478
+ ## Rendering helpers
479
+
480
+ For callers that need the text without running pyinfra:
481
+
482
+ - `render_compose(...)` renders `docker-compose.yml`; it raises `ValueError`
483
+ when `crowdsec=True` without a `crowdsec_version`.
484
+ - `render_config_files(...)` returns the four core config paths (plus the
485
+ CrowdSec files) keyed relative to the project directory.
486
+ - `render_newt_env`, `render_newt_config_json`, `render_newt_systemd_unit`,
487
+ `render_newt_service_yaml` and `render_newt_compose_yaml` render the Newt
488
+ files. `render_newt_config_json` returns the secret in plaintext; the caller
489
+ is responsible for writing it mode 0600.
490
+ - `NewtRuntimeConfig` is the frozen dataclass those renderers consume.
491
+ `DEFAULTS` is the fallback map in `deploy.py`.
492
+
493
+ ## Modules
494
+
495
+ **Newt agent:**
496
+
497
+ - `pyinfra_pangolin_kit.deploy` — `ensure_newt_runtime`, `pangolin_newt`,
498
+ `ensure_newt_systemd_service`, `ensure_newt_container_service`,
499
+ `install_newt_binary`, `fix_newt_permissions`, `write_blueprint`, `DEFAULTS`
500
+ and the `_build_*` helpers (private).
501
+ - `pyinfra_pangolin_kit.operations` — `newt_binary`, `compose_newt`,
502
+ `pangolin_site`, `pangolin_resource`, `pangolin_http_resource`,
503
+ `pangolin_target`, `pangolin_domain`, `pangolin_blueprint`.
504
+ - `pyinfra_pangolin_kit.compose` — round-trip compose helpers
505
+ (`load_compose`, `merge_newt`, `dump_compose`).
506
+ - `pyinfra_pangolin_kit.renderers` — pure Newt renderers.
507
+ - `pyinfra_pangolin_kit.models` — `NewtRuntimeConfig`, defaults, `unmask`.
508
+
509
+ **Server:**
510
+
511
+ - `pyinfra_pangolin_kit.facts` — `NewtVersion`, `PangolinComposeVersions`,
512
+ `PangolinBadgerVersion`.
513
+ - `pyinfra_pangolin_kit.server` — `pangolin_stack`, `update_pangolin_stack`
514
+ (both `@operation`), `render_compose`, `render_config_files`.
515
+ - `pyinfra_pangolin_kit.versions` — `Version`, `compute_upgrade_path`,
516
+ `fetch_docker_hub_versions`, `fetch_github_release_versions`,
517
+ `resolve_available_versions`.
518
+
519
+ `pyinfra_pangolin_kit.newt` is a compatibility facade re-exporting the public
520
+ surface. Private `_build_*` helpers are not exported from the package root;
521
+ import them from `deploy`/`operations` when needed.
522
+
523
+ ## Development
524
+
525
+ ```bash
526
+ uv sync --group dev
527
+ uv run pytest -q
528
+ uv run ruff check .
529
+ uv run ruff format --check .
530
+ uv run mypy src
531
+ uv run prek run --all-files
532
+ uv build
533
+ ```
534
+
535
+ CI publishes a `.dev<CI_PIPELINE_IID>` prerelease from the default branch and
536
+ literal versions from tags.
537
+
538
+ ## License
539
+
540
+ MIT. See [LICENSE](LICENSE).