pyinfra-gitlab-kit 0.1.1__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,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 CI caches
13
+ .uv-cache/
14
+ .prek-cache/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Simon Maillard
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,173 @@
1
+ Metadata-Version: 2.5
2
+ Name: pyinfra-gitlab-kit
3
+ Version: 0.1.1
4
+ Summary: GitLab automation with pyinfra — runners, CI variables, protected branches, deploy tokens
5
+ Project-URL: Homepage, https://github.com/maisim/pyinfra-gitlab-kit
6
+ Project-URL: Repository, https://github.com/maisim/pyinfra-gitlab-kit
7
+ Project-URL: Issues, https://github.com/maisim/pyinfra-gitlab-kit/issues
8
+ Project-URL: Changelog, https://github.com/maisim/pyinfra-gitlab-kit/releases
9
+ Author-email: Simon Maillard <simon@ogesta.fr>
10
+ License: MIT
11
+ License-File: LICENSE
12
+ Keywords: ci,gitlab,infrastructure,pyinfra,runner
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
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.10
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
+ Requires-Python: <4.0,>=3.10
24
+ Requires-Dist: pyinfra<4,>=3.11
25
+ Requires-Dist: typing-extensions>=4.6
26
+ Description-Content-Type: text/markdown
27
+
28
+ # pyinfra-gitlab-kit
29
+
30
+ > **⚠️ 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.
31
+
32
+ > **Packaging status**: this repository is the source of truth and is public. Install the latest
33
+ > release from PyPI; while the project is alpha, pin the version you use.
34
+
35
+ GitLab automation operations, facts, and deploys for [pyinfra](https://pyinfra.com/): install and
36
+ register GitLab Runner, manage project and group resources through the GitLab API, and install the
37
+ `glab` CLI.
38
+
39
+ [![CI](https://github.com/maisim/pyinfra-gitlab-kit/actions/workflows/python-publish.yml/badge.svg)](https://github.com/maisim/pyinfra-gitlab-kit/actions/workflows/python-publish.yml)
40
+ [![PyPI](https://img.shields.io/pypi/v/pyinfra-gitlab-kit)](https://pypi.org/project/pyinfra-gitlab-kit/)
41
+ [![Python versions](https://img.shields.io/pypi/pyversions/pyinfra-gitlab-kit)](https://pypi.org/project/pyinfra-gitlab-kit/)
42
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
43
+
44
+ ## Install
45
+
46
+ ```bash
47
+ pip install pyinfra-gitlab-kit
48
+ uv add pyinfra-gitlab-kit
49
+ ```
50
+
51
+ Requires pyinfra 3.11+ (`pyinfra>=3.11,<4`).
52
+
53
+ ## Package structure
54
+
55
+ ```
56
+ pyinfra_gitlab_kit/
57
+ operations/
58
+ glab.py — glab CLI install / uninstall
59
+ instance.py — GitLab API resources (projects, variables, protected refs, deploy tokens)
60
+ runner.py — GitLab Runner package, registration, service, concurrency
61
+ facts/
62
+ glab.py — GlabVersion
63
+ instance.py — SopsSecret (SOPS-decrypted values)
64
+ runner.py — GitlabRunnerRegistrations, GitlabRunnerConcurrency, OsReleaseCodename
65
+ deploys/
66
+ glab.py — install_glab (data-driven glab install)
67
+ runner.py — docker_kernel_tuning (sysctl for Docker executor)
68
+ ```
69
+
70
+ ## Operations
71
+
72
+ ### GitLab API (`operations.instance`)
73
+
74
+ All API operations target `GITLAB_HOST` and authenticate with `GITLAB_TOKEN`
75
+ (environment variables, or pyinfra `config.ENV`). They are intended for `@local`.
76
+
77
+ | Operation | Description |
78
+ | ------------------------- | ----------------------------------------------------------------------------- |
79
+ | `gitlab_project` | Ensure a project under a group, with the requested visibility. |
80
+ | `gitlab_project_variable` | Ensure a project-level CI variable (masked, protected, raw, file-type). |
81
+ | `gitlab_group_variable` | Ensure a group-level CI variable. |
82
+ | `gitlab_protected_branch` | Ensure a branch protection rule. |
83
+ | `gitlab_protected_tag` | Ensure a protected-tag rule (for immutable, content-addressed tags). |
84
+ | `gitlab_deploy_token` | Ensure a project deploy token with the given scopes. |
85
+ | `runner_token` | Manage a GitLab runner authentication token stored in SOPS. |
86
+
87
+ These wrap internal `_fn_ensure_*` helpers callable outside pyinfra's operation system (for example
88
+ from other Python code). Refer to the docstrings for full signatures.
89
+
90
+ ### GitLab Runner (`operations.runner`)
91
+
92
+ | Operation | Description |
93
+ | --------------- | -------------------------------------------------------- |
94
+ | `package` | APT install of GitLab Runner (Debian / Ubuntu). |
95
+ | `registration` | Register a runner via `gitlab-runner register`. |
96
+ | `service` | Enable / start the systemd service. |
97
+ | `concurrency` | Set the global `concurrent` job limit in `config.toml`. |
98
+ | `gitlab_runner` | Composite: `package` → `registration` → `service`. |
99
+
100
+ ### glab CLI (`operations.glab`)
101
+
102
+ | Operation | Description |
103
+ | ---------- | --------------------------------------------------------- |
104
+ | `glab_cli` | Install or remove the `glab` binary from GitHub releases. |
105
+
106
+ ## Facts
107
+
108
+ | Fact | Returns | Description |
109
+ | --------------------------- | ------------- | ---------------------------------------------------------- |
110
+ | `GlabVersion` | `str \| None` | Installed `glab` version. |
111
+ | `SopsSecret` | `str \| None` | Decrypted value of a SOPS-encrypted file. |
112
+ | `GitlabRunnerRegistrations` | `list[str]` | Runner names from `/etc/gitlab-runner/config.toml`. |
113
+ | `GitlabRunnerConcurrency` | `int` | Global `concurrent` limit (defaults to `1` when absent). |
114
+ | `OsReleaseCodename` | `str` | Debian / Ubuntu codename from `/etc/os-release`. |
115
+
116
+ ## Deploys
117
+
118
+ | Deploy | Description |
119
+ | ---------------------- | ------------------------------------------------------------------ |
120
+ | `docker_kernel_tuning` | Sysctl `kernel.keys.maxkeys=2000` for the Docker executor. |
121
+ | `install_glab` | Install the `glab` CLI, versioned from host data (`glab_version`). |
122
+
123
+ ## Usage
124
+
125
+ ### Via the pyinfra CLI
126
+
127
+ ```python
128
+ # runner_deploy.py
129
+ from pyinfra import host
130
+ from pyinfra_gitlab_kit import gitlab_runner
131
+
132
+ gitlab_runner(
133
+ url=host.data.get("gitlab_url"),
134
+ token=host.data.get("gitlab_runner_token"),
135
+ description=host.data.get("gitlab_runner_description"),
136
+ )
137
+ ```
138
+
139
+ ```bash
140
+ pyinfra -y @docker/ubuntu deploy.py
141
+ ```
142
+
143
+ ### As a Python API
144
+
145
+ ```python
146
+ from pyinfra_gitlab_kit.operations.instance import (
147
+ _fn_ensure_project_variable,
148
+ _fn_ensure_protected_branch,
149
+ _fn_ensure_deploy_token,
150
+ )
151
+
152
+ # Set GITLAB_HOST and GITLAB_TOKEN before calling.
153
+ _fn_ensure_project_variable(
154
+ project="acme/odoo",
155
+ key="SOPS_AGE_KEY",
156
+ value="<value from your secret store>",
157
+ masked=False,
158
+ protected=True,
159
+ variable_type="file",
160
+ raw=True,
161
+ environment_scope="*",
162
+ present=True,
163
+ )
164
+ ```
165
+
166
+ ## Contributing
167
+
168
+ Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup,
169
+ tests, and commit conventions.
170
+
171
+ ## License
172
+
173
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,146 @@
1
+ # pyinfra-gitlab-kit
2
+
3
+ > **⚠️ 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.
4
+
5
+ > **Packaging status**: this repository is the source of truth and is public. Install the latest
6
+ > release from PyPI; while the project is alpha, pin the version you use.
7
+
8
+ GitLab automation operations, facts, and deploys for [pyinfra](https://pyinfra.com/): install and
9
+ register GitLab Runner, manage project and group resources through the GitLab API, and install the
10
+ `glab` CLI.
11
+
12
+ [![CI](https://github.com/maisim/pyinfra-gitlab-kit/actions/workflows/python-publish.yml/badge.svg)](https://github.com/maisim/pyinfra-gitlab-kit/actions/workflows/python-publish.yml)
13
+ [![PyPI](https://img.shields.io/pypi/v/pyinfra-gitlab-kit)](https://pypi.org/project/pyinfra-gitlab-kit/)
14
+ [![Python versions](https://img.shields.io/pypi/pyversions/pyinfra-gitlab-kit)](https://pypi.org/project/pyinfra-gitlab-kit/)
15
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ pip install pyinfra-gitlab-kit
21
+ uv add pyinfra-gitlab-kit
22
+ ```
23
+
24
+ Requires pyinfra 3.11+ (`pyinfra>=3.11,<4`).
25
+
26
+ ## Package structure
27
+
28
+ ```
29
+ pyinfra_gitlab_kit/
30
+ operations/
31
+ glab.py — glab CLI install / uninstall
32
+ instance.py — GitLab API resources (projects, variables, protected refs, deploy tokens)
33
+ runner.py — GitLab Runner package, registration, service, concurrency
34
+ facts/
35
+ glab.py — GlabVersion
36
+ instance.py — SopsSecret (SOPS-decrypted values)
37
+ runner.py — GitlabRunnerRegistrations, GitlabRunnerConcurrency, OsReleaseCodename
38
+ deploys/
39
+ glab.py — install_glab (data-driven glab install)
40
+ runner.py — docker_kernel_tuning (sysctl for Docker executor)
41
+ ```
42
+
43
+ ## Operations
44
+
45
+ ### GitLab API (`operations.instance`)
46
+
47
+ All API operations target `GITLAB_HOST` and authenticate with `GITLAB_TOKEN`
48
+ (environment variables, or pyinfra `config.ENV`). They are intended for `@local`.
49
+
50
+ | Operation | Description |
51
+ | ------------------------- | ----------------------------------------------------------------------------- |
52
+ | `gitlab_project` | Ensure a project under a group, with the requested visibility. |
53
+ | `gitlab_project_variable` | Ensure a project-level CI variable (masked, protected, raw, file-type). |
54
+ | `gitlab_group_variable` | Ensure a group-level CI variable. |
55
+ | `gitlab_protected_branch` | Ensure a branch protection rule. |
56
+ | `gitlab_protected_tag` | Ensure a protected-tag rule (for immutable, content-addressed tags). |
57
+ | `gitlab_deploy_token` | Ensure a project deploy token with the given scopes. |
58
+ | `runner_token` | Manage a GitLab runner authentication token stored in SOPS. |
59
+
60
+ These wrap internal `_fn_ensure_*` helpers callable outside pyinfra's operation system (for example
61
+ from other Python code). Refer to the docstrings for full signatures.
62
+
63
+ ### GitLab Runner (`operations.runner`)
64
+
65
+ | Operation | Description |
66
+ | --------------- | -------------------------------------------------------- |
67
+ | `package` | APT install of GitLab Runner (Debian / Ubuntu). |
68
+ | `registration` | Register a runner via `gitlab-runner register`. |
69
+ | `service` | Enable / start the systemd service. |
70
+ | `concurrency` | Set the global `concurrent` job limit in `config.toml`. |
71
+ | `gitlab_runner` | Composite: `package` → `registration` → `service`. |
72
+
73
+ ### glab CLI (`operations.glab`)
74
+
75
+ | Operation | Description |
76
+ | ---------- | --------------------------------------------------------- |
77
+ | `glab_cli` | Install or remove the `glab` binary from GitHub releases. |
78
+
79
+ ## Facts
80
+
81
+ | Fact | Returns | Description |
82
+ | --------------------------- | ------------- | ---------------------------------------------------------- |
83
+ | `GlabVersion` | `str \| None` | Installed `glab` version. |
84
+ | `SopsSecret` | `str \| None` | Decrypted value of a SOPS-encrypted file. |
85
+ | `GitlabRunnerRegistrations` | `list[str]` | Runner names from `/etc/gitlab-runner/config.toml`. |
86
+ | `GitlabRunnerConcurrency` | `int` | Global `concurrent` limit (defaults to `1` when absent). |
87
+ | `OsReleaseCodename` | `str` | Debian / Ubuntu codename from `/etc/os-release`. |
88
+
89
+ ## Deploys
90
+
91
+ | Deploy | Description |
92
+ | ---------------------- | ------------------------------------------------------------------ |
93
+ | `docker_kernel_tuning` | Sysctl `kernel.keys.maxkeys=2000` for the Docker executor. |
94
+ | `install_glab` | Install the `glab` CLI, versioned from host data (`glab_version`). |
95
+
96
+ ## Usage
97
+
98
+ ### Via the pyinfra CLI
99
+
100
+ ```python
101
+ # runner_deploy.py
102
+ from pyinfra import host
103
+ from pyinfra_gitlab_kit import gitlab_runner
104
+
105
+ gitlab_runner(
106
+ url=host.data.get("gitlab_url"),
107
+ token=host.data.get("gitlab_runner_token"),
108
+ description=host.data.get("gitlab_runner_description"),
109
+ )
110
+ ```
111
+
112
+ ```bash
113
+ pyinfra -y @docker/ubuntu deploy.py
114
+ ```
115
+
116
+ ### As a Python API
117
+
118
+ ```python
119
+ from pyinfra_gitlab_kit.operations.instance import (
120
+ _fn_ensure_project_variable,
121
+ _fn_ensure_protected_branch,
122
+ _fn_ensure_deploy_token,
123
+ )
124
+
125
+ # Set GITLAB_HOST and GITLAB_TOKEN before calling.
126
+ _fn_ensure_project_variable(
127
+ project="acme/odoo",
128
+ key="SOPS_AGE_KEY",
129
+ value="<value from your secret store>",
130
+ masked=False,
131
+ protected=True,
132
+ variable_type="file",
133
+ raw=True,
134
+ environment_scope="*",
135
+ present=True,
136
+ )
137
+ ```
138
+
139
+ ## Contributing
140
+
141
+ Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup,
142
+ tests, and commit conventions.
143
+
144
+ ## License
145
+
146
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,83 @@
1
+ [project]
2
+ name = "pyinfra-gitlab-kit"
3
+ version = "0.1.1"
4
+ description = "GitLab automation with pyinfra — runners, CI variables, protected branches, deploy tokens"
5
+ authors = [
6
+ { name = "Simon Maillard", email = "simon@ogesta.fr" }
7
+ ]
8
+ dependencies = [
9
+ "pyinfra>=3.11,<4",
10
+ # Imported directly for @override on the Python 3.10 floor.
11
+ "typing_extensions>=4.6",
12
+ ]
13
+ readme = "README.md"
14
+ requires-python = ">=3.10,<4.0"
15
+ license = { text = "MIT" }
16
+
17
+ keywords = ["pyinfra", "gitlab", "ci", "runner", "infrastructure"]
18
+ classifiers = [
19
+ "Development Status :: 3 - Alpha",
20
+ "Intended Audience :: Developers",
21
+ "Intended Audience :: System Administrators",
22
+ "License :: OSI Approved :: MIT License",
23
+ "Operating System :: POSIX :: Linux",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Programming Language :: Python :: 3.13",
28
+ "Programming Language :: Python :: 3.14",
29
+ ]
30
+
31
+ [project.urls]
32
+ Homepage = "https://github.com/maisim/pyinfra-gitlab-kit"
33
+ Repository = "https://github.com/maisim/pyinfra-gitlab-kit"
34
+ Issues = "https://github.com/maisim/pyinfra-gitlab-kit/issues"
35
+ Changelog = "https://github.com/maisim/pyinfra-gitlab-kit/releases"
36
+
37
+ [build-system]
38
+ requires = ["hatchling"]
39
+ build-backend = "hatchling.build"
40
+
41
+ [tool.hatch.build.targets.wheel]
42
+ packages = ["src/pyinfra_gitlab_kit"]
43
+
44
+ [tool.hatch.build.targets.sdist]
45
+ include = ["src/pyinfra_gitlab_kit", "README.md", "LICENSE", "pyproject.toml"]
46
+
47
+ [tool.coverage.run]
48
+ branch = true
49
+ omit = ["*/tests/*"]
50
+
51
+ [tool.coverage.report]
52
+ show_missing = true
53
+
54
+ [tool.coverage.html]
55
+ directory = "htmlcov"
56
+
57
+ [tool.ruff]
58
+ line-length = 120
59
+ target-version = "py310"
60
+
61
+ [tool.ruff.lint]
62
+ select = ["E", "F", "W", "I", "N"]
63
+
64
+ [tool.mypy]
65
+ python_version = "3.10"
66
+ ignore_missing_imports = true
67
+ check_untyped_defs = true
68
+
69
+ [dependency-groups]
70
+ dev = [
71
+ "coverage>=7.16.2",
72
+ "detect-secrets>=1.4.0",
73
+ # 1.7.8 rather than 1.7.5: 1.7.5 pulls untokenize, whose sdist does not build on Python 3.14.
74
+ "docformatter==1.7.8",
75
+ "mypy>=2.3.1",
76
+ "prek>=0.5.4",
77
+ "pytest>=9.1.1",
78
+ "pytest-cov>=7.1.0",
79
+ "ruff>=0.16.9",
80
+ ]
81
+
82
+ [tool.pytest.ini_options]
83
+ testpaths = ["tests"]
@@ -0,0 +1,61 @@
1
+ """
2
+ Public API for the ``pyinfra_gitlab_kit`` package.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from . import deploys, facts, operations
8
+ from .deploys.glab import install_glab
9
+ from .deploys.runner import docker_kernel_tuning
10
+ from .facts.glab import GlabVersion
11
+ from .facts.instance import SopsSecret
12
+ from .facts.runner import GitlabRunnerConcurrency, GitlabRunnerRegistrations, OsReleaseCodename
13
+ from .operations.glab import glab_cli
14
+ from .operations.instance import (
15
+ gitlab_deploy_token,
16
+ gitlab_group_variable,
17
+ gitlab_project,
18
+ gitlab_project_variable,
19
+ gitlab_protected_branch,
20
+ gitlab_protected_tag,
21
+ runner_token,
22
+ )
23
+ from .operations.runner import (
24
+ concurrency,
25
+ gitlab_runner,
26
+ package,
27
+ registration,
28
+ service,
29
+ )
30
+
31
+ __all__ = [
32
+ # facts
33
+ "GitlabRunnerConcurrency",
34
+ "GitlabRunnerRegistrations",
35
+ "GlabVersion",
36
+ "OsReleaseCodename",
37
+ "SopsSecret",
38
+ # operations — glab
39
+ "glab_cli",
40
+ # operations — instance (GitLab API)
41
+ "gitlab_deploy_token",
42
+ "gitlab_group_variable",
43
+ "gitlab_project",
44
+ "gitlab_project_variable",
45
+ "gitlab_protected_branch",
46
+ "gitlab_protected_tag",
47
+ "runner_token",
48
+ # operations — runner
49
+ "concurrency",
50
+ "gitlab_runner",
51
+ "package",
52
+ "registration",
53
+ "service",
54
+ # deploys
55
+ "docker_kernel_tuning",
56
+ "install_glab",
57
+ # subpackages
58
+ "deploys",
59
+ "facts",
60
+ "operations",
61
+ ]
@@ -0,0 +1,41 @@
1
+ from __future__ import annotations
2
+
3
+ from pyinfra import host
4
+ from pyinfra.api import deploy
5
+
6
+ from ..operations.glab import glab_cli
7
+
8
+
9
+ @deploy(
10
+ "Deploy glab",
11
+ data_defaults={
12
+ "glab_version": "latest",
13
+ "glab_dest": "/usr/local/bin/glab",
14
+ },
15
+ )
16
+ def install_glab(
17
+ version: str | None = None,
18
+ dest: str | None = None,
19
+ present: bool = True,
20
+ ):
21
+ """
22
+ Install (or remove) the GitLab CLI, ``glab``.
23
+
24
+ A data-driven entry point over the ``glab_cli`` operation: called with no arguments, the
25
+ version and the destination come from host data, so an inventory can pin
26
+ ``glab_version``/``glab_dest`` per host or per group and call this deploy bare.
27
+
28
+ + version: version to install (default: host data ``glab_version``, ``"latest"`` when unset,
29
+ which resolves the newest stable release at deploy time)
30
+ + dest: destination path for the binary (default: host data ``glab_dest``,
31
+ ``"/usr/local/bin/glab"`` when unset)
32
+ + present: when ``False``, removes the binary and the telemetry snippet
33
+
34
+ An install writes ``dest`` and ``/etc/profile.d/glab-no-telemetry.sh``, so pass
35
+ ``_sudo=True`` at the call site when the connecting user is not root.
36
+ """
37
+ glab_cli(
38
+ version=version or host.data.glab_version,
39
+ dest=dest or host.data.glab_dest,
40
+ present=present,
41
+ )
@@ -0,0 +1,38 @@
1
+ from __future__ import annotations
2
+
3
+ from pyinfra.api import deploy
4
+ from pyinfra.operations import server
5
+
6
+
7
+ @deploy()
8
+ def docker_kernel_tuning(
9
+ maxkeys: int = 50000,
10
+ maxbytes: int = 2000000,
11
+ persist_file: str = "/etc/sysctl.d/99-gitlab-runner.conf",
12
+ ):
13
+ """
14
+ Raise per-user kernel keyring limits to prevent ``unable to join session
15
+ keyring: disk quota exceeded`` errors when the Docker executor accumulates
16
+ container/keyring entries.
17
+
18
+ + maxkeys: ``kernel.keys.maxkeys`` value (default: ``50000``)
19
+ + maxbytes: ``kernel.keys.maxbytes`` value (default: ``2000000``)
20
+ + persist_file: sysctl drop-in to write (default:
21
+ ``"/etc/sysctl.d/99-gitlab-runner.conf"``)
22
+
23
+ Only relevant when the runner uses the Docker executor. The defaults match
24
+ the values empirically required to keep a busy runner from exhausting the
25
+ keyring quota.
26
+ """
27
+ server.sysctl(
28
+ key="kernel.keys.maxkeys",
29
+ value=maxkeys,
30
+ persist=True,
31
+ persist_file=persist_file,
32
+ )
33
+ server.sysctl(
34
+ key="kernel.keys.maxbytes",
35
+ value=maxbytes,
36
+ persist=True,
37
+ persist_file=persist_file,
38
+ )
@@ -0,0 +1,18 @@
1
+ from __future__ import annotations
2
+
3
+ import importlib
4
+ from glob import glob
5
+ from os import path
6
+
7
+ _module_filenames = glob(path.join(path.dirname(__file__), "*.py"))
8
+ __all__ = sorted(set(path.basename(name)[:-3] for name in _module_filenames if not name.endswith("__init__.py")))
9
+
10
+
11
+ def __getattr__(name):
12
+ if name in __all__:
13
+ return importlib.import_module(f".{name}", __package__)
14
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
15
+
16
+
17
+ def __dir__():
18
+ return __all__
@@ -0,0 +1,28 @@
1
+ from __future__ import annotations
2
+
3
+ import re
4
+
5
+ from pyinfra.api import FactBase
6
+ from typing_extensions import override
7
+
8
+
9
+ class GlabVersion(FactBase[str | None]):
10
+ """
11
+ Returns the installed ``glab`` version string (e.g. ``"1.50.0"``), or ``None`` if
12
+ ``glab`` is not installed.
13
+
14
+ .. code:: python
15
+
16
+ "1.50.0"
17
+ """
18
+
19
+ command: str = "glab version 2>/dev/null | head -1 || true" # type: ignore[assignment]
20
+ default = lambda self: None # type: ignore[assignment,misc] # noqa: E731
21
+
22
+ @override
23
+ def process(self, output: list[str]) -> str | None:
24
+ if not output or not output[0].strip():
25
+ return None
26
+ # "glab version 1.50.0 (2024-01-01)"
27
+ match = re.search(r"(\d+\.\d+\.\d+)", output[0])
28
+ return match.group(1) if match else None
@@ -0,0 +1,32 @@
1
+ from __future__ import annotations
2
+
3
+ from pyinfra.api import FactBase
4
+ from typing_extensions import override
5
+
6
+
7
+ class SopsSecret(FactBase[str | None]):
8
+ """
9
+ Returns the decrypted value of a key from a SOPS-encrypted YAML file, or ``None`` if
10
+ the key does not exist or the file is not accessible.
11
+
12
+ Requires ``sops`` to be installed and a valid age key available
13
+ (either ``~/.config/sops/age/keys.txt`` or the ``SOPS_AGE_KEY`` env var).
14
+
15
+ Designed for use on ``@local`` only — the ``sops`` binary and age key are
16
+ not expected to be present on remote hosts.
17
+
18
+ .. code:: python
19
+
20
+ "a value from the SOPS file"
21
+ """
22
+
23
+ default: str | None = None # type: ignore[assignment]
24
+
25
+ @override
26
+ def command(self, secrets_file: str, key: str) -> str:
27
+ return f'sops --decrypt --extract \'["{key}"]\' "{secrets_file}" 2>/dev/null || true'
28
+
29
+ @override
30
+ def process(self, output: list[str]) -> str | None:
31
+ value = "".join(output).strip()
32
+ return value if value else None