pyinfra-gitlab-kit 0.1.1__py3-none-any.whl
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.
- pyinfra_gitlab_kit/__init__.py +61 -0
- pyinfra_gitlab_kit/deploys/__init__.py +0 -0
- pyinfra_gitlab_kit/deploys/glab.py +41 -0
- pyinfra_gitlab_kit/deploys/runner.py +38 -0
- pyinfra_gitlab_kit/facts/__init__.py +18 -0
- pyinfra_gitlab_kit/facts/glab.py +28 -0
- pyinfra_gitlab_kit/facts/instance.py +32 -0
- pyinfra_gitlab_kit/facts/runner.py +67 -0
- pyinfra_gitlab_kit/operations/__init__.py +18 -0
- pyinfra_gitlab_kit/operations/glab.py +105 -0
- pyinfra_gitlab_kit/operations/instance.py +922 -0
- pyinfra_gitlab_kit/operations/runner.py +254 -0
- pyinfra_gitlab_kit/py.typed +0 -0
- pyinfra_gitlab_kit-0.1.1.dist-info/METADATA +173 -0
- pyinfra_gitlab_kit-0.1.1.dist-info/RECORD +17 -0
- pyinfra_gitlab_kit-0.1.1.dist-info/WHEEL +4 -0
- pyinfra_gitlab_kit-0.1.1.dist-info/licenses/LICENSE +21 -0
|
@@ -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
|
+
]
|
|
File without changes
|
|
@@ -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
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pyinfra.api import FactBase
|
|
4
|
+
from typing_extensions import override
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class OsReleaseCodename(FactBase[str]):
|
|
8
|
+
"""
|
|
9
|
+
Returns the OS codename from ``/etc/os-release`` (e.g. ``"bookworm"``).
|
|
10
|
+
|
|
11
|
+
Preferred over ``LsbRelease`` because ``lsb-release`` is not installed by default in
|
|
12
|
+
minimal Debian/Ubuntu containers.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
command: str = ". /etc/os-release && echo ${VERSION_CODENAME:-${UBUNTU_CODENAME:-bookworm}}" # type: ignore[assignment]
|
|
16
|
+
default = lambda self: "bookworm" # type: ignore[assignment,misc] # noqa: E731
|
|
17
|
+
|
|
18
|
+
@override
|
|
19
|
+
def process(self, output: list[str]) -> str:
|
|
20
|
+
return output[0].strip() if output else "bookworm"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class GitlabRunnerConcurrency(FactBase[int]):
|
|
24
|
+
"""
|
|
25
|
+
Returns the runner's global ``concurrent`` job limit from ``/etc/gitlab-
|
|
26
|
+
runner/config.toml``.
|
|
27
|
+
|
|
28
|
+
Returns ``1`` when the runner is not installed yet, the value ``gitlab-runner
|
|
29
|
+
register`` writes by default.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
# Returns nothing if the file is absent (runner not installed yet).
|
|
33
|
+
command: str = "grep -E '^concurrent = [0-9]+$' /etc/gitlab-runner/config.toml 2>/dev/null || true" # type: ignore[assignment]
|
|
34
|
+
default: int = 1 # type: ignore[assignment]
|
|
35
|
+
|
|
36
|
+
@override
|
|
37
|
+
def process(self, output: list[str]) -> int:
|
|
38
|
+
for line in output:
|
|
39
|
+
stripped = line.strip()
|
|
40
|
+
if stripped.startswith("concurrent = "):
|
|
41
|
+
return int(stripped.split("=", 1)[1].strip())
|
|
42
|
+
return 1
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class GitlabRunnerRegistrations(FactBase[list[str]]):
|
|
46
|
+
"""
|
|
47
|
+
Returns the list of runner description names currently registered in ``/etc/gitlab-
|
|
48
|
+
runner/config.toml``.
|
|
49
|
+
|
|
50
|
+
.. code:: python
|
|
51
|
+
|
|
52
|
+
["lx4-infradev-shell", "another-runner"]
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
# Returns [] if the file is absent (runner not installed yet).
|
|
56
|
+
command: str = "cat /etc/gitlab-runner/config.toml 2>/dev/null || true" # type: ignore[assignment]
|
|
57
|
+
default: list[str] = list # type: ignore[assignment]
|
|
58
|
+
|
|
59
|
+
@override
|
|
60
|
+
def process(self, output: list[str]) -> list[str]:
|
|
61
|
+
names: list[str] = []
|
|
62
|
+
for line in output:
|
|
63
|
+
stripped = line.strip()
|
|
64
|
+
# TOML: name = "description-here" (inside a [[runners]] block)
|
|
65
|
+
if stripped.startswith('name = "') and stripped.endswith('"'):
|
|
66
|
+
names.append(stripped[8:-1])
|
|
67
|
+
return names
|
|
@@ -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,105 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
|
|
5
|
+
from pyinfra import host
|
|
6
|
+
from pyinfra.api import OperationError, operation
|
|
7
|
+
from pyinfra.facts.server import Arch
|
|
8
|
+
from pyinfra.operations import apt, files
|
|
9
|
+
|
|
10
|
+
from ..facts.glab import GlabVersion
|
|
11
|
+
|
|
12
|
+
# Architecture mapping: uname -m → glab release asset name
|
|
13
|
+
_GLAB_ARCH = {
|
|
14
|
+
"x86_64": "amd64",
|
|
15
|
+
"aarch64": "arm64",
|
|
16
|
+
"armv7l": "armv6",
|
|
17
|
+
"i686": "386",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
# Path to the telemetry suppression snippet installed alongside glab.
|
|
21
|
+
_GLAB_NO_TELEMETRY_PATH = "/etc/profile.d/glab-no-telemetry.sh"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _resolve_latest_glab_version() -> str:
|
|
25
|
+
"""
|
|
26
|
+
Return the latest stable ``glab`` version tag (e.g. ``"1.91.0"``).
|
|
27
|
+
|
|
28
|
+
Calls the GitLab releases API ``permalink/latest`` for ``gitlab-org/cli``.
|
|
29
|
+
"""
|
|
30
|
+
import json as _json
|
|
31
|
+
import urllib.parse as _urllib_parse
|
|
32
|
+
import urllib.request as _urllib_request
|
|
33
|
+
|
|
34
|
+
project = _urllib_parse.quote("gitlab-org/cli", safe="")
|
|
35
|
+
url = f"https://gitlab.com/api/v4/projects/{project}/releases/permalink/latest"
|
|
36
|
+
try:
|
|
37
|
+
with _urllib_request.urlopen(url, timeout=15) as resp:
|
|
38
|
+
release = _json.load(resp)
|
|
39
|
+
except Exception as exc:
|
|
40
|
+
raise OperationError(f"Failed to fetch latest glab version from GitLab API: {exc}") from exc
|
|
41
|
+
|
|
42
|
+
tag = release.get("tag_name", "")
|
|
43
|
+
if not tag:
|
|
44
|
+
raise OperationError("GitLab API response missing tag_name for latest glab release")
|
|
45
|
+
return tag.lstrip("v")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@operation()
|
|
49
|
+
def glab_cli(version: str = "latest", dest: str = "/usr/local/bin/glab", present: bool = True):
|
|
50
|
+
"""
|
|
51
|
+
Install or remove the ``glab`` GitLab CLI.
|
|
52
|
+
|
|
53
|
+
+ version: version to install, or ``"latest"`` to resolve the latest
|
|
54
|
+
stable release tag at deploy time (default: ``"latest"``).
|
|
55
|
+
+ dest: destination path for the binary (default: ``"/usr/local/bin/glab"``).
|
|
56
|
+
+ present: when ``False``, removes the binary and the telemetry snippet
|
|
57
|
+
if they exist.
|
|
58
|
+
|
|
59
|
+
Downloads the pre-built tarball from the GitLab releases page.
|
|
60
|
+
Idempotent: skips download when the requested version is already installed.
|
|
61
|
+
"""
|
|
62
|
+
if not present:
|
|
63
|
+
current = host.get_fact(GlabVersion)
|
|
64
|
+
if current:
|
|
65
|
+
yield from files.file._inner(path=dest, present=False)
|
|
66
|
+
yield from files.file._inner(path=_GLAB_NO_TELEMETRY_PATH, present=False)
|
|
67
|
+
else:
|
|
68
|
+
host.noop("glab is not installed")
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
if version == "latest":
|
|
72
|
+
version = _resolve_latest_glab_version()
|
|
73
|
+
|
|
74
|
+
current = host.get_fact(GlabVersion)
|
|
75
|
+
if current == version:
|
|
76
|
+
host.noop(f"glab {version} already installed")
|
|
77
|
+
else:
|
|
78
|
+
arch_raw = host.get_fact(Arch)
|
|
79
|
+
arch = _GLAB_ARCH.get(arch_raw)
|
|
80
|
+
if not arch:
|
|
81
|
+
raise OperationError(f"Unsupported architecture for glab: {arch_raw}")
|
|
82
|
+
|
|
83
|
+
tarball = f"glab_{version}_linux_{arch}.tar.gz"
|
|
84
|
+
url = f"https://gitlab.com/gitlab-org/cli/-/releases/v{version}/downloads/{tarball}"
|
|
85
|
+
tmp = f"/tmp/{tarball}"
|
|
86
|
+
|
|
87
|
+
# Prerequisites: curl + tar
|
|
88
|
+
yield from apt.packages._inner(packages=["curl", "tar"])
|
|
89
|
+
|
|
90
|
+
yield from files.download._inner(
|
|
91
|
+
src=url,
|
|
92
|
+
dest=tmp,
|
|
93
|
+
)
|
|
94
|
+
dest_dir = dest.rsplit("/", 1)[0]
|
|
95
|
+
yield f'rm -f "{dest}"'
|
|
96
|
+
yield f'tar -xzf "{tmp}" -C "{dest_dir}" --strip-components=1 bin/glab'
|
|
97
|
+
yield from files.file._inner(path=dest, mode="755")
|
|
98
|
+
yield f'rm -f "{tmp}"'
|
|
99
|
+
|
|
100
|
+
# Always ensure telemetry is disabled (glab sends usage data to gitlab.com).
|
|
101
|
+
yield from files.put._inner(
|
|
102
|
+
src=io.StringIO("export GLAB_SEND_TELEMETRY=false\n"),
|
|
103
|
+
dest=_GLAB_NO_TELEMETRY_PATH,
|
|
104
|
+
mode="644",
|
|
105
|
+
)
|