pyinfra-alertmanager 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.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Adrien Mannocci
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,196 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyinfra-alertmanager
3
+ Version: 0.1.0
4
+ Summary: Install and uninstall Prometheus alertmanager with pyinfra.
5
+ Keywords: pyinfra,alertmanager,prometheus,alerting,monitoring,deploy,systemd,infrastructure-as-code
6
+ Author: Adrien Mannocci
7
+ Author-email: Adrien Mannocci <adrien.mannocci@gmail.com>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: System Administrators
13
+ Classifier: Operating System :: POSIX :: Linux
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Programming Language :: Python :: 3.15
19
+ Classifier: Topic :: System :: Monitoring
20
+ Classifier: Topic :: System :: Systems Administration
21
+ Classifier: Typing :: Typed
22
+ Requires-Dist: pyinfra>=3
23
+ Requires-Python: >=3.12
24
+ Project-URL: Repository, https://github.com/techcode-io/pyinfra-alertmanager
25
+ Project-URL: Homepage, https://github.com/techcode-io/pyinfra-alertmanager
26
+ Project-URL: Documentation, https://github.com/techcode-io/pyinfra-alertmanager
27
+ Project-URL: Changelog, https://github.com/techcode-io/pyinfra-alertmanager/blob/main/CHANGELOG.md
28
+ Project-URL: Tracker, https://github.com/techcode-io/pyinfra-alertmanager/issues
29
+ Description-Content-Type: text/markdown
30
+
31
+ <h1 align="center">Pyinfra Alertmanager</h1>
32
+
33
+ <p align="center">
34
+ <i align="center">Install and uninstall Prometheus alertmanager with pyinfra.</i>
35
+ </p>
36
+
37
+ <h4 align="center">
38
+ <a href="https://pypi.org/project/pyinfra-alertmanager/">
39
+ <img src="https://img.shields.io/pypi/v/pyinfra-alertmanager?style=flat-square" alt="pypi version" style="height: 20px;">
40
+ </a>
41
+ <a href="https://pypi.org/project/pyinfra-alertmanager/">
42
+ <img src="https://img.shields.io/pypi/pyversions/pyinfra-alertmanager?style=flat-square" alt="python versions" style="height: 20px;">
43
+ </a>
44
+ <a href="https://github.com/techcode-io/pyinfra-alertmanager/actions/workflows/ci.yml">
45
+ <img src="https://img.shields.io/github/actions/workflow/status/techcode-io/pyinfra-alertmanager/ci.yml?branch=main&label=ci&style=flat-square" alt="continuous integration" style="height: 20px;">
46
+ </a>
47
+ <a href="https://github.com/techcode-io/pyinfra-alertmanager/graphs/contributors">
48
+ <img src="https://img.shields.io/github/contributors-anon/techcode-io/pyinfra-alertmanager?color=yellow&style=flat-square" alt="contributors" style="height: 20px;">
49
+ </a>
50
+ <a href="https://opensource.org/licenses/Apache-2.0">
51
+ <img src="https://img.shields.io/badge/apache%202.0-blue.svg?style=flat-square&label=license" alt="license" style="height: 20px;">
52
+ </a>
53
+ <br>
54
+ </h4>
55
+
56
+ - [Source](https://github.com/techcode-io/pyinfra-alertmanager)
57
+ - [Changelog](https://github.com/techcode-io/pyinfra-alertmanager/blob/main/CHANGELOG.md)
58
+ - [Issues](https://github.com/techcode-io/pyinfra-alertmanager/issues)
59
+ - [Contact](mailto:adrien.mannocci@gmail.com)
60
+ - [Maintained by techcode.io](https://techcode.io)
61
+
62
+ ## :package: Prerequisites
63
+
64
+ - Python 3.12 or newer and [pyinfra](https://pyinfra.com) 3.x on the machine running the deploy.
65
+ - `linux-amd64` target hosts running systemd.
66
+
67
+ ## :sparkles: Features
68
+
69
+ - Idempotent `install()`: system user/group, storage/config directories, alerting config, binary,
70
+ systemd unit and running service in one call.
71
+ - Skips re-downloading the binary when the installed version already matches, using a pyinfra
72
+ fact.
73
+ - `uninstall()` reverses everything: service, unit file, binaries, config/storage directories,
74
+ user and group.
75
+ - Deploy functions only, no CLI: import it into any [pyinfra](https://pyinfra.com) project.
76
+
77
+ ## :dart: Motivation
78
+
79
+ - We needed to manage `alertmanager` the same way across every server we operate.
80
+ - The solution should be reusable across pyinfra projects instead of copy-pasted between deploy
81
+ scripts.
82
+ - The solution should be idempotent and skip work that has already been done.
83
+
84
+ ## 📖 Usage
85
+
86
+ ### How it works
87
+
88
+ - `install()` and `uninstall()` are [pyinfra](https://pyinfra.com) deploy functions, wrapped with
89
+ `@deploy(...)` from `pyinfra.api`.
90
+ - They take explicit keyword arguments instead of reading `host.data`, so any inventory can use
91
+ them.
92
+ - `install()` creates the system user/group, the storage (`/var/lib/alertmanager`) and config
93
+ (`/etc/alertmanager`) directories, renders the alerting configuration from a Jinja2 template,
94
+ downloads the `alertmanager`/`amtool` release binaries, renders the systemd unit from a bundled
95
+ template, then enables and starts the service.
96
+ - Before downloading, it checks the currently installed version using a pyinfra fact and skips the
97
+ download entirely if it already matches.
98
+ - `uninstall()` stops and disables the service, then removes the unit file, binaries, config and
99
+ storage directories, user and group.
100
+
101
+ ### How to install alertmanager
102
+
103
+ - Add it to your pyinfra project from [PyPI](https://pypi.org/project/pyinfra-alertmanager/).
104
+
105
+ ```bash
106
+ uv add pyinfra-alertmanager
107
+ # or
108
+ pip install pyinfra-alertmanager
109
+ ```
110
+
111
+ - Then call `install()` from a deploy script.
112
+
113
+ ```python
114
+ from pyinfra_alertmanager import install
115
+
116
+ install()
117
+ ```
118
+
119
+ ### How to uninstall alertmanager
120
+
121
+ - Call `uninstall()` from a deploy script.
122
+
123
+ ```python
124
+ from pyinfra_alertmanager import uninstall
125
+
126
+ uninstall()
127
+ ```
128
+
129
+ ### How to customize the install
130
+
131
+ - All functions accept keyword arguments; defaults match the upstream alertmanager release layout
132
+ for `linux-amd64`.
133
+ - `config_template` is the path to a local Jinja2 template rendered to
134
+ `/etc/alertmanager/config.yml` (via pyinfra's `files.template`); `config_context` supplies the
135
+ variables used inside it. The bundled `DEFAULT_CONFIG_TEMPLATE` is a minimal valid config with a
136
+ no-op `default` receiver, so pass your own template (and context) to actually deliver alerts —
137
+ this lets you use Jinja conditionals, loops, or secrets pulled from your own vault/templating
138
+ setup, same as the systemd unit is rendered.
139
+
140
+ ```python
141
+ from pyinfra_alertmanager import DEFAULT_SERVICE_ARGS, install
142
+
143
+ install(
144
+ version="0.35.1",
145
+ system_user="alertmanager",
146
+ system_group="alertmanager",
147
+ service_args={
148
+ **DEFAULT_SERVICE_ARGS,
149
+ "web.listen-address": "0.0.0.0:9093",
150
+ },
151
+ config_template="files/alertmanager/config.yml.j2",
152
+ config_context={
153
+ "discord_webhook_url": "https://discord.com/api/webhooks/...",
154
+ },
155
+ )
156
+ ```
157
+
158
+ - `files/alertmanager/config.yml.j2` (relative to your deploy script, like any other pyinfra
159
+ template):
160
+
161
+ ```yaml
162
+ route:
163
+ receiver: discord
164
+
165
+ receivers:
166
+ - name: discord
167
+ discord_configs:
168
+ - webhook_url: {{ discord_webhook_url }}
169
+ ```
170
+
171
+ | Function | Parameter | Default | Description |
172
+ |------------------------|---------------------|-----------------------------|-----------------------------------------------------------------------------|
173
+ | `install`, `uninstall` | `system_user` | `alertmanager` | System user running the service |
174
+ | `install`, `uninstall` | `system_group` | `alertmanager` | System group running the service |
175
+ | `install` | `version` | `0.35.1` | alertmanager release version to download |
176
+ | `install` | `service_args` | `DEFAULT_SERVICE_ARGS` | Dict of `--flag: value` (or `None` for a bare flag) passed to `alertmanager` |
177
+ | `install` | `config_template` | `DEFAULT_CONFIG_TEMPLATE` | Path to a Jinja2 template rendered to `/etc/alertmanager/config.yml` |
178
+ | `install` | `config_context` | `None` | Dict of variables passed to `config_template` when rendering |
179
+
180
+ ## :heart: Contributing
181
+
182
+ If you find this project useful here's how you can help, please click the :eye: **Watch** button
183
+ to avoid missing notifications about new versions, and give it a :star2: **GitHub Star**!
184
+
185
+ You can also contribute by:
186
+
187
+ - Reading the [contributing guide](CONTRIBUTING.md) to set up the project, lint, format and test.
188
+ - Sending a [Pull Request](https://github.com/techcode-io/pyinfra-alertmanager/pulls) with your
189
+ awesome new features and bug fixed.
190
+ - Be part of the community and help resolve
191
+ [Issues](https://github.com/techcode-io/pyinfra-alertmanager/issues).
192
+
193
+ ## 🧾 License
194
+
195
+ The `pyinfra-alertmanager` project is free and open-source software licensed under the Apache-2.0
196
+ license.
@@ -0,0 +1,166 @@
1
+ <h1 align="center">Pyinfra Alertmanager</h1>
2
+
3
+ <p align="center">
4
+ <i align="center">Install and uninstall Prometheus alertmanager with pyinfra.</i>
5
+ </p>
6
+
7
+ <h4 align="center">
8
+ <a href="https://pypi.org/project/pyinfra-alertmanager/">
9
+ <img src="https://img.shields.io/pypi/v/pyinfra-alertmanager?style=flat-square" alt="pypi version" style="height: 20px;">
10
+ </a>
11
+ <a href="https://pypi.org/project/pyinfra-alertmanager/">
12
+ <img src="https://img.shields.io/pypi/pyversions/pyinfra-alertmanager?style=flat-square" alt="python versions" style="height: 20px;">
13
+ </a>
14
+ <a href="https://github.com/techcode-io/pyinfra-alertmanager/actions/workflows/ci.yml">
15
+ <img src="https://img.shields.io/github/actions/workflow/status/techcode-io/pyinfra-alertmanager/ci.yml?branch=main&label=ci&style=flat-square" alt="continuous integration" style="height: 20px;">
16
+ </a>
17
+ <a href="https://github.com/techcode-io/pyinfra-alertmanager/graphs/contributors">
18
+ <img src="https://img.shields.io/github/contributors-anon/techcode-io/pyinfra-alertmanager?color=yellow&style=flat-square" alt="contributors" style="height: 20px;">
19
+ </a>
20
+ <a href="https://opensource.org/licenses/Apache-2.0">
21
+ <img src="https://img.shields.io/badge/apache%202.0-blue.svg?style=flat-square&label=license" alt="license" style="height: 20px;">
22
+ </a>
23
+ <br>
24
+ </h4>
25
+
26
+ - [Source](https://github.com/techcode-io/pyinfra-alertmanager)
27
+ - [Changelog](https://github.com/techcode-io/pyinfra-alertmanager/blob/main/CHANGELOG.md)
28
+ - [Issues](https://github.com/techcode-io/pyinfra-alertmanager/issues)
29
+ - [Contact](mailto:adrien.mannocci@gmail.com)
30
+ - [Maintained by techcode.io](https://techcode.io)
31
+
32
+ ## :package: Prerequisites
33
+
34
+ - Python 3.12 or newer and [pyinfra](https://pyinfra.com) 3.x on the machine running the deploy.
35
+ - `linux-amd64` target hosts running systemd.
36
+
37
+ ## :sparkles: Features
38
+
39
+ - Idempotent `install()`: system user/group, storage/config directories, alerting config, binary,
40
+ systemd unit and running service in one call.
41
+ - Skips re-downloading the binary when the installed version already matches, using a pyinfra
42
+ fact.
43
+ - `uninstall()` reverses everything: service, unit file, binaries, config/storage directories,
44
+ user and group.
45
+ - Deploy functions only, no CLI: import it into any [pyinfra](https://pyinfra.com) project.
46
+
47
+ ## :dart: Motivation
48
+
49
+ - We needed to manage `alertmanager` the same way across every server we operate.
50
+ - The solution should be reusable across pyinfra projects instead of copy-pasted between deploy
51
+ scripts.
52
+ - The solution should be idempotent and skip work that has already been done.
53
+
54
+ ## 📖 Usage
55
+
56
+ ### How it works
57
+
58
+ - `install()` and `uninstall()` are [pyinfra](https://pyinfra.com) deploy functions, wrapped with
59
+ `@deploy(...)` from `pyinfra.api`.
60
+ - They take explicit keyword arguments instead of reading `host.data`, so any inventory can use
61
+ them.
62
+ - `install()` creates the system user/group, the storage (`/var/lib/alertmanager`) and config
63
+ (`/etc/alertmanager`) directories, renders the alerting configuration from a Jinja2 template,
64
+ downloads the `alertmanager`/`amtool` release binaries, renders the systemd unit from a bundled
65
+ template, then enables and starts the service.
66
+ - Before downloading, it checks the currently installed version using a pyinfra fact and skips the
67
+ download entirely if it already matches.
68
+ - `uninstall()` stops and disables the service, then removes the unit file, binaries, config and
69
+ storage directories, user and group.
70
+
71
+ ### How to install alertmanager
72
+
73
+ - Add it to your pyinfra project from [PyPI](https://pypi.org/project/pyinfra-alertmanager/).
74
+
75
+ ```bash
76
+ uv add pyinfra-alertmanager
77
+ # or
78
+ pip install pyinfra-alertmanager
79
+ ```
80
+
81
+ - Then call `install()` from a deploy script.
82
+
83
+ ```python
84
+ from pyinfra_alertmanager import install
85
+
86
+ install()
87
+ ```
88
+
89
+ ### How to uninstall alertmanager
90
+
91
+ - Call `uninstall()` from a deploy script.
92
+
93
+ ```python
94
+ from pyinfra_alertmanager import uninstall
95
+
96
+ uninstall()
97
+ ```
98
+
99
+ ### How to customize the install
100
+
101
+ - All functions accept keyword arguments; defaults match the upstream alertmanager release layout
102
+ for `linux-amd64`.
103
+ - `config_template` is the path to a local Jinja2 template rendered to
104
+ `/etc/alertmanager/config.yml` (via pyinfra's `files.template`); `config_context` supplies the
105
+ variables used inside it. The bundled `DEFAULT_CONFIG_TEMPLATE` is a minimal valid config with a
106
+ no-op `default` receiver, so pass your own template (and context) to actually deliver alerts —
107
+ this lets you use Jinja conditionals, loops, or secrets pulled from your own vault/templating
108
+ setup, same as the systemd unit is rendered.
109
+
110
+ ```python
111
+ from pyinfra_alertmanager import DEFAULT_SERVICE_ARGS, install
112
+
113
+ install(
114
+ version="0.35.1",
115
+ system_user="alertmanager",
116
+ system_group="alertmanager",
117
+ service_args={
118
+ **DEFAULT_SERVICE_ARGS,
119
+ "web.listen-address": "0.0.0.0:9093",
120
+ },
121
+ config_template="files/alertmanager/config.yml.j2",
122
+ config_context={
123
+ "discord_webhook_url": "https://discord.com/api/webhooks/...",
124
+ },
125
+ )
126
+ ```
127
+
128
+ - `files/alertmanager/config.yml.j2` (relative to your deploy script, like any other pyinfra
129
+ template):
130
+
131
+ ```yaml
132
+ route:
133
+ receiver: discord
134
+
135
+ receivers:
136
+ - name: discord
137
+ discord_configs:
138
+ - webhook_url: {{ discord_webhook_url }}
139
+ ```
140
+
141
+ | Function | Parameter | Default | Description |
142
+ |------------------------|---------------------|-----------------------------|-----------------------------------------------------------------------------|
143
+ | `install`, `uninstall` | `system_user` | `alertmanager` | System user running the service |
144
+ | `install`, `uninstall` | `system_group` | `alertmanager` | System group running the service |
145
+ | `install` | `version` | `0.35.1` | alertmanager release version to download |
146
+ | `install` | `service_args` | `DEFAULT_SERVICE_ARGS` | Dict of `--flag: value` (or `None` for a bare flag) passed to `alertmanager` |
147
+ | `install` | `config_template` | `DEFAULT_CONFIG_TEMPLATE` | Path to a Jinja2 template rendered to `/etc/alertmanager/config.yml` |
148
+ | `install` | `config_context` | `None` | Dict of variables passed to `config_template` when rendering |
149
+
150
+ ## :heart: Contributing
151
+
152
+ If you find this project useful here's how you can help, please click the :eye: **Watch** button
153
+ to avoid missing notifications about new versions, and give it a :star2: **GitHub Star**!
154
+
155
+ You can also contribute by:
156
+
157
+ - Reading the [contributing guide](CONTRIBUTING.md) to set up the project, lint, format and test.
158
+ - Sending a [Pull Request](https://github.com/techcode-io/pyinfra-alertmanager/pulls) with your
159
+ awesome new features and bug fixed.
160
+ - Be part of the community and help resolve
161
+ [Issues](https://github.com/techcode-io/pyinfra-alertmanager/issues).
162
+
163
+ ## 🧾 License
164
+
165
+ The `pyinfra-alertmanager` project is free and open-source software licensed under the Apache-2.0
166
+ license.
@@ -0,0 +1,99 @@
1
+ [project]
2
+ name = "pyinfra-alertmanager"
3
+ version = "0.1.0"
4
+ description = "Install and uninstall Prometheus alertmanager with pyinfra."
5
+ license = "Apache-2.0"
6
+ license-files = ["LICENSE"]
7
+ readme = "README.md"
8
+ keywords = [
9
+ "pyinfra",
10
+ "alertmanager",
11
+ "prometheus",
12
+ "alerting",
13
+ "monitoring",
14
+ "deploy",
15
+ "systemd",
16
+ "infrastructure-as-code",
17
+ ]
18
+ classifiers = [
19
+ "Development Status :: 4 - Beta",
20
+ "Intended Audience :: Developers",
21
+ "Intended Audience :: System Administrators",
22
+ "Operating System :: POSIX :: Linux",
23
+ "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
26
+ "Programming Language :: Python :: 3.14",
27
+ "Programming Language :: Python :: 3.15",
28
+ "Topic :: System :: Monitoring",
29
+ "Topic :: System :: Systems Administration",
30
+ "Typing :: Typed",
31
+ ]
32
+ requires-python = ">=3.12"
33
+ dependencies = ["pyinfra>=3"]
34
+
35
+ [[project.authors]]
36
+ name = "Adrien Mannocci"
37
+ email = "adrien.mannocci@gmail.com"
38
+
39
+ [project.urls]
40
+ Repository = "https://github.com/techcode-io/pyinfra-alertmanager"
41
+ Homepage = "https://github.com/techcode-io/pyinfra-alertmanager"
42
+ Documentation = "https://github.com/techcode-io/pyinfra-alertmanager"
43
+ Changelog = "https://github.com/techcode-io/pyinfra-alertmanager/blob/main/CHANGELOG.md"
44
+ Tracker = "https://github.com/techcode-io/pyinfra-alertmanager/issues"
45
+
46
+ [build-system]
47
+ requires = ["uv_build>=0.11.28,<0.13.0"]
48
+ build-backend = "uv_build"
49
+
50
+ [dependency-groups]
51
+ dev = [
52
+ "poethepoet>=0.48.0",
53
+ "pre-commit>=4.6.0",
54
+ "proclet>=0.1.2",
55
+ "pytest>=9.1.1",
56
+ "ruff>=0.15.21",
57
+ "urllib3>=2.6.0",
58
+ ]
59
+
60
+ [tool.poe.tasks."env:configure"]
61
+ help = "Setup project environment."
62
+ cmd = "pre-commit install"
63
+
64
+ [tool.poe.tasks.lint]
65
+ help = "Lint code project."
66
+ cmd = "ruff check src"
67
+
68
+ [tool.poe.tasks.fmt]
69
+ help = "Format code project."
70
+
71
+ [[tool.poe.tasks.fmt.sequence]]
72
+ cmd = "ruff check --select I --fix"
73
+
74
+ [[tool.poe.tasks.fmt.sequence]]
75
+ cmd = "ruff format"
76
+
77
+ [tool.poe.tasks.test]
78
+ help = "Run all tests."
79
+ cmd = "pytest tests"
80
+
81
+ [tool.poe.tasks."project:upgrade"]
82
+ help = "Bump DEFAULT_VERSION to the latest upstream alertmanager release and .python-version to its latest patch."
83
+ script = "scripts.project:upgrade"
84
+
85
+ [tool.poe.tasks."project:upgrade_ci"]
86
+ help = "Like project:upgrade, but also writes the diff as GitHub Actions step outputs (used by Dependabot++)."
87
+ script = "scripts.project:upgrade_ci"
88
+
89
+ [tool.poe.tasks.release]
90
+ help = "Create a new pyinfra-alertmanager release."
91
+ script = "scripts.release:run"
92
+
93
+ [tool.poe.tasks."release:pre"]
94
+ help = "Create a PR with changes for release."
95
+ script = "scripts.release:pre"
96
+
97
+ [tool.poe.tasks."release:post"]
98
+ help = "Create a PR preparing the next iteration."
99
+ script = "scripts.release:post"
@@ -0,0 +1,98 @@
1
+ [project]
2
+ name = "pyinfra-alertmanager"
3
+ version = "0.1.0"
4
+ description = "Install and uninstall Prometheus alertmanager with pyinfra."
5
+ authors = [
6
+ { name = "Adrien Mannocci", email = "adrien.mannocci@gmail.com" }
7
+ ]
8
+ license = "Apache-2.0"
9
+ license-files = ["LICENSE"]
10
+ readme = "README.md"
11
+ keywords = [
12
+ "pyinfra",
13
+ "alertmanager",
14
+ "prometheus",
15
+ "alerting",
16
+ "monitoring",
17
+ "deploy",
18
+ "systemd",
19
+ "infrastructure-as-code",
20
+ ]
21
+ classifiers = [
22
+ "Development Status :: 4 - Beta",
23
+ "Intended Audience :: Developers",
24
+ "Intended Audience :: System Administrators",
25
+ "Operating System :: POSIX :: Linux",
26
+ "Programming Language :: Python :: 3",
27
+ "Programming Language :: Python :: 3.12",
28
+ "Programming Language :: Python :: 3.13",
29
+ "Programming Language :: Python :: 3.14",
30
+ "Programming Language :: Python :: 3.15",
31
+ "Topic :: System :: Monitoring",
32
+ "Topic :: System :: Systems Administration",
33
+ "Typing :: Typed",
34
+ ]
35
+ requires-python = ">=3.12"
36
+ dependencies = [
37
+ "pyinfra>=3", # MIT
38
+ ]
39
+
40
+ [project.urls]
41
+ Repository = "https://github.com/techcode-io/pyinfra-alertmanager"
42
+ Homepage = "https://github.com/techcode-io/pyinfra-alertmanager"
43
+ Documentation = "https://github.com/techcode-io/pyinfra-alertmanager"
44
+ Changelog = "https://github.com/techcode-io/pyinfra-alertmanager/blob/main/CHANGELOG.md"
45
+ Tracker = "https://github.com/techcode-io/pyinfra-alertmanager/issues"
46
+
47
+ [build-system]
48
+ requires = ["uv_build>=0.11.28,<0.13.0"]
49
+ build-backend = "uv_build"
50
+
51
+ [dependency-groups]
52
+ dev = [
53
+ "poethepoet>=0.48.0", # MIT
54
+ "pre-commit>=4.6.0", # MIT
55
+ "proclet>=0.1.2", # Apache-2.0
56
+ "pytest>=9.1.1", # MIT
57
+ "ruff>=0.15.21", # MIT
58
+ "urllib3>=2.6.0", # MIT
59
+ ]
60
+
61
+ [tool.poe.tasks."env:configure"]
62
+ help = "Setup project environment."
63
+ cmd = "pre-commit install"
64
+
65
+ [tool.poe.tasks.lint]
66
+ help = "Lint code project."
67
+ cmd = "ruff check src"
68
+
69
+ [tool.poe.tasks.fmt]
70
+ help = "Format code project."
71
+ sequence = [
72
+ { cmd = "ruff check --select I --fix" },
73
+ { cmd = "ruff format" },
74
+ ]
75
+
76
+ [tool.poe.tasks.test]
77
+ help = "Run all tests."
78
+ cmd = "pytest tests"
79
+
80
+ [tool.poe.tasks."project:upgrade"]
81
+ help = "Bump DEFAULT_VERSION to the latest upstream alertmanager release and .python-version to its latest patch."
82
+ script = "scripts.project:upgrade"
83
+
84
+ [tool.poe.tasks."project:upgrade_ci"]
85
+ help = "Like project:upgrade, but also writes the diff as GitHub Actions step outputs (used by Dependabot++)."
86
+ script = "scripts.project:upgrade_ci"
87
+
88
+ [tool.poe.tasks.release]
89
+ help = "Create a new pyinfra-alertmanager release."
90
+ script = "scripts.release:run"
91
+
92
+ [tool.poe.tasks."release:pre"]
93
+ help = "Create a PR with changes for release."
94
+ script = "scripts.release:pre"
95
+
96
+ [tool.poe.tasks."release:post"]
97
+ help = "Create a PR preparing the next iteration."
98
+ script = "scripts.release:post"
@@ -0,0 +1,21 @@
1
+ from pyinfra_alertmanager.facts import AlertManagerVersion
2
+ from pyinfra_alertmanager.tasks import (
3
+ DEFAULT_CONFIG_TEMPLATE,
4
+ DEFAULT_SERVICE_ARGS,
5
+ DEFAULT_SYSTEM_GROUP,
6
+ DEFAULT_SYSTEM_USER,
7
+ DEFAULT_VERSION,
8
+ install,
9
+ uninstall,
10
+ )
11
+
12
+ __all__ = [
13
+ "DEFAULT_CONFIG_TEMPLATE",
14
+ "DEFAULT_SERVICE_ARGS",
15
+ "DEFAULT_SYSTEM_GROUP",
16
+ "DEFAULT_SYSTEM_USER",
17
+ "DEFAULT_VERSION",
18
+ "AlertManagerVersion",
19
+ "install",
20
+ "uninstall",
21
+ ]
@@ -0,0 +1,28 @@
1
+ import re
2
+ from typing import Final
3
+
4
+ from pyinfra.api import FactBase
5
+
6
+ BINARY_PATH: Final[str] = "/usr/local/bin/alertmanager"
7
+
8
+ _VERSION_MATCHER: Final[re.Pattern] = re.compile(
9
+ r"alertmanager,\s+version\s+(?P<version>\S+)"
10
+ )
11
+
12
+
13
+ class AlertManagerVersion(FactBase):
14
+ """
15
+ Returns the currently installed alertmanager version (eg ``0.30.1``), or ``None`` if
16
+ alertmanager is not installed.
17
+ """
18
+
19
+ @staticmethod
20
+ def command() -> str:
21
+ return f"{BINARY_PATH} --version 2>&1"
22
+
23
+ def requires_command(self) -> str:
24
+ return BINARY_PATH
25
+
26
+ def process(self, output) -> str | None:
27
+ match = _VERSION_MATCHER.search("\n".join(output))
28
+ return match.group("version") if match else None
@@ -0,0 +1,212 @@
1
+ import shlex
2
+ from collections.abc import Mapping
3
+ from importlib import resources
4
+ from pathlib import Path
5
+ from types import MappingProxyType
6
+ from typing import Any, Final
7
+
8
+ from pyinfra.api import deploy
9
+ from pyinfra.context import host
10
+ from pyinfra.operations import files, server, systemd
11
+
12
+ from pyinfra_alertmanager.facts import BINARY_PATH, AlertManagerVersion
13
+
14
+ UNIT_PATH: Final[str] = "/etc/systemd/system/alertmanager.service"
15
+ AMTOOL_BINARY_PATH: Final[str] = "/usr/local/bin/amtool"
16
+ CONFIG_DIR: Final[str] = "/etc/alertmanager"
17
+ CONFIG_PATH: Final[str] = f"{CONFIG_DIR}/config.yml"
18
+ STORAGE_DIR: Final[str] = "/var/lib/alertmanager"
19
+ DOWNLOAD_DIR: Final[str] = "/tmp/alertmanager"
20
+
21
+ DEFAULT_VERSION: Final[str] = "0.35.1"
22
+ DEFAULT_SYSTEM_USER: Final[str] = "alertmanager"
23
+ DEFAULT_SYSTEM_GROUP: Final[str] = "alertmanager"
24
+ DEFAULT_SERVICE_ARGS: Final[Mapping[str, str]] = MappingProxyType(
25
+ {
26
+ "web.listen-address": "127.0.0.1:9093",
27
+ "config.file": CONFIG_PATH,
28
+ "storage.path": STORAGE_DIR,
29
+ }
30
+ )
31
+ DEFAULT_CONFIG_TEMPLATE: Final[str] = str(
32
+ resources.files("pyinfra_alertmanager") / "templates" / "config.yml.j2"
33
+ )
34
+
35
+ _TEMPLATE: Final[str] = str(
36
+ resources.files("pyinfra_alertmanager") / "templates" / "alertmanager.service.j2"
37
+ )
38
+
39
+
40
+ @deploy("Install alertmanager")
41
+ def install(
42
+ version: str = DEFAULT_VERSION,
43
+ system_user: str = DEFAULT_SYSTEM_USER,
44
+ system_group: str = DEFAULT_SYSTEM_GROUP,
45
+ service_args: Mapping[str, str] | None = None,
46
+ config_template: str | Path = DEFAULT_CONFIG_TEMPLATE,
47
+ config_context: dict[str, Any] | None = None,
48
+ ):
49
+ server.group(
50
+ name="Create alertmanager system group",
51
+ group=system_group,
52
+ )
53
+
54
+ server.user(
55
+ name="Create alertmanager system user",
56
+ user=system_user,
57
+ group=system_group,
58
+ system=True,
59
+ create_home=False,
60
+ shell="/usr/sbin/nologin",
61
+ )
62
+
63
+ files.directory(
64
+ name="Create alertmanager storage directory",
65
+ path=STORAGE_DIR,
66
+ user=system_user,
67
+ group=system_group,
68
+ mode=755,
69
+ present=True,
70
+ )
71
+
72
+ files.directory(
73
+ name="Create alertmanager configuration directory",
74
+ path=CONFIG_DIR,
75
+ user=system_user,
76
+ group=system_group,
77
+ mode=755,
78
+ present=True,
79
+ )
80
+
81
+ config = files.template(
82
+ name="Render alertmanager configuration",
83
+ src=str(config_template),
84
+ dest=CONFIG_PATH,
85
+ user=system_user,
86
+ group=system_group,
87
+ **(config_context or {}),
88
+ )
89
+
90
+ binary_changed = host.get_fact(AlertManagerVersion) != version
91
+ if binary_changed:
92
+ files.directory(
93
+ name="Prepare local download path",
94
+ path=DOWNLOAD_DIR,
95
+ mode=755,
96
+ present=True,
97
+ )
98
+
99
+ archive = f"alertmanager-{version}.linux-amd64.tar.gz"
100
+ archive_path = f"{DOWNLOAD_DIR}/{archive}"
101
+
102
+ files.download(
103
+ name="Download alertmanager release binary",
104
+ src=f"https://github.com/prometheus/alertmanager/releases/download/v{version}/{archive}",
105
+ dest=archive_path,
106
+ )
107
+
108
+ release_dir = f"/tmp/alertmanager-{version}.linux-amd64"
109
+
110
+ # files.unarchive/files.move hard-fail if their src/dest doesn't already
111
+ # exist per a fact check, and that check runs when pyinfra builds its
112
+ # change-detection preview (i.e. before any operation's commands have
113
+ # actually executed) unless the deploy is run with `-y`. That makes them
114
+ # unsafe here: the archive/binaries are created earlier in this very run.
115
+ # server.shell has no such pre-check, so use raw commands instead.
116
+ server.shell(
117
+ name="Unarchive alertmanager release binary",
118
+ commands=[f"tar -xzf {shlex.quote(archive_path)} -C {shlex.quote('/tmp')}"],
119
+ )
120
+
121
+ def move_binary(binary_name: str) -> str:
122
+ src = shlex.quote(f"{release_dir}/{binary_name}")
123
+ dest = shlex.quote(f"/usr/local/bin/{binary_name}")
124
+ return f"mv {src} {dest}"
125
+
126
+ server.shell(
127
+ name="Move alertmanager and amtool binaries",
128
+ commands=[move_binary("alertmanager"), move_binary("amtool")],
129
+ )
130
+
131
+ files.directory(name="Clear download path", path=DOWNLOAD_DIR, present=False)
132
+ files.directory(
133
+ name="Clear extracted release directory", path=release_dir, present=False
134
+ )
135
+
136
+ unit = files.template(
137
+ name="Copy alertmanager systemd unit file",
138
+ src=_TEMPLATE,
139
+ dest=UNIT_PATH,
140
+ alertmanager_system_user=system_user, # type: ignore[arg-type]
141
+ alertmanager_system_group=system_group, # type: ignore[arg-type]
142
+ alertmanager_service_args=service_args or DEFAULT_SERVICE_ARGS, # type: ignore[arg-type]
143
+ )
144
+
145
+ if unit.will_change:
146
+ systemd.daemon_reload(name="Reload systemd daemon")
147
+
148
+ systemd.service(
149
+ name="Restart and enable the alertmanager service",
150
+ service="alertmanager.service",
151
+ running=True,
152
+ restarted=config.will_change or binary_changed or unit.will_change,
153
+ enabled=True,
154
+ )
155
+
156
+
157
+ @deploy("Uninstall alertmanager")
158
+ def uninstall(
159
+ system_user: str = DEFAULT_SYSTEM_USER,
160
+ system_group: str = DEFAULT_SYSTEM_GROUP,
161
+ ):
162
+ systemd.service(
163
+ name="Stop and disable the alertmanager service",
164
+ service="alertmanager.service",
165
+ running=False,
166
+ enabled=False,
167
+ )
168
+
169
+ unit = files.file(
170
+ name="Remove alertmanager systemd unit file",
171
+ path=UNIT_PATH,
172
+ present=False,
173
+ )
174
+
175
+ if unit.will_change:
176
+ systemd.daemon_reload(name="Reload systemd daemon")
177
+
178
+ files.file(
179
+ name="Remove alertmanager binary",
180
+ path=BINARY_PATH,
181
+ present=False,
182
+ )
183
+
184
+ files.file(
185
+ name="Remove amtool binary",
186
+ path=AMTOOL_BINARY_PATH,
187
+ present=False,
188
+ )
189
+
190
+ files.directory(
191
+ name="Remove alertmanager configuration directory",
192
+ path=CONFIG_DIR,
193
+ present=False,
194
+ )
195
+
196
+ files.directory(
197
+ name="Remove alertmanager storage directory",
198
+ path=STORAGE_DIR,
199
+ present=False,
200
+ )
201
+
202
+ server.user(
203
+ name="Remove alertmanager system user",
204
+ user=system_user,
205
+ present=False,
206
+ )
207
+
208
+ server.group(
209
+ name="Remove alertmanager system group",
210
+ group=system_group,
211
+ present=False,
212
+ )
@@ -0,0 +1,24 @@
1
+ [Unit]
2
+ Description=Alertmanager service
3
+ After=network.target
4
+
5
+ [Service]
6
+ Type=simple
7
+ User={{ alertmanager_system_user }}
8
+ Group={{ alertmanager_system_group }}
9
+ ExecStart=/usr/local/bin/alertmanager {% for flag, flag_value in alertmanager_service_args.items() %}--{{ flag }}{% if flag_value is not none %}={{ flag_value }}{% endif %} {% endfor %}
10
+
11
+ SyslogIdentifier=alertmanager
12
+ Restart=always
13
+
14
+ # Sandboxing: restrict the process's access to the rest of the system.
15
+ PrivateTmp=yes
16
+ ProtectHome=yes
17
+ NoNewPrivileges=yes
18
+ ProtectSystem=full
19
+ ProtectControlGroups=true
20
+ ProtectKernelModules=true
21
+ ProtectKernelTunables=yes
22
+
23
+ [Install]
24
+ WantedBy=multi-user.target
@@ -0,0 +1,5 @@
1
+ route:
2
+ receiver: default
3
+
4
+ receivers:
5
+ - name: default