pyinfra-pangolin-kit 0.1.0__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.
@@ -0,0 +1,111 @@
1
+ """
2
+ Public surface of pyinfra-pangolin-kit.
3
+
4
+ The package offers three families of building blocks: Newt agent management, Pangolin
5
+ server install/update, and controller-side Pangolin API provisioning.
6
+ """
7
+
8
+ from pyinfra_pangolin_kit.newt import (
9
+ DEFAULT_NEWT_BINARY_PATH,
10
+ DEFAULT_NEWT_BINARY_STATE_DIR,
11
+ DEFAULT_NEWT_BLUEPRINT_PATH,
12
+ DEFAULT_NEWT_COMPOSE_PATH,
13
+ DEFAULT_NEWT_CONFIG_PATH,
14
+ DEFAULT_NEWT_CONTAINER_NAME,
15
+ DEFAULT_NEWT_ENV_PATH,
16
+ DEFAULT_NEWT_IMAGE,
17
+ DEFAULT_NEWT_LOG_LEVEL,
18
+ DEFAULT_NEWT_MODE,
19
+ DEFAULT_NEWT_SERVICE_NAME,
20
+ DEFAULT_NEWT_VERSION,
21
+ DEFAULT_NEWT_WORKDIR,
22
+ DEFAULT_PANGOLIN_PROJECT_DIR,
23
+ DEFAULTS,
24
+ MINIMAL_BLUEPRINT,
25
+ NewtRuntimeConfig,
26
+ NewtVersion,
27
+ PangolinBadgerVersion,
28
+ PangolinComposeVersions,
29
+ Version,
30
+ compose_newt,
31
+ compute_upgrade_path,
32
+ ensure_newt_container_service,
33
+ ensure_newt_runtime,
34
+ ensure_newt_systemd_service,
35
+ fetch_docker_hub_versions,
36
+ fetch_github_release_versions,
37
+ fix_newt_permissions,
38
+ install_newt_binary,
39
+ newt_binary,
40
+ pangolin_blueprint,
41
+ pangolin_domain,
42
+ pangolin_http_resource,
43
+ pangolin_newt,
44
+ pangolin_resource,
45
+ pangolin_site,
46
+ pangolin_stack,
47
+ pangolin_target,
48
+ render_newt_compose_yaml,
49
+ render_newt_config_json,
50
+ render_newt_env,
51
+ render_newt_service_yaml,
52
+ render_newt_systemd_unit,
53
+ resolve_available_versions,
54
+ unmask,
55
+ update_pangolin_stack,
56
+ write_blueprint,
57
+ )
58
+ from pyinfra_pangolin_kit.server import render_compose, render_config_files
59
+
60
+ __all__ = [
61
+ "DEFAULTS",
62
+ "DEFAULT_PANGOLIN_PROJECT_DIR",
63
+ "DEFAULT_NEWT_BINARY_PATH",
64
+ "DEFAULT_NEWT_BINARY_STATE_DIR",
65
+ "DEFAULT_NEWT_BLUEPRINT_PATH",
66
+ "DEFAULT_NEWT_COMPOSE_PATH",
67
+ "DEFAULT_NEWT_CONFIG_PATH",
68
+ "DEFAULT_NEWT_CONTAINER_NAME",
69
+ "DEFAULT_NEWT_ENV_PATH",
70
+ "DEFAULT_NEWT_IMAGE",
71
+ "DEFAULT_NEWT_LOG_LEVEL",
72
+ "DEFAULT_NEWT_MODE",
73
+ "DEFAULT_NEWT_SERVICE_NAME",
74
+ "DEFAULT_NEWT_VERSION",
75
+ "DEFAULT_NEWT_WORKDIR",
76
+ "MINIMAL_BLUEPRINT",
77
+ "NewtVersion",
78
+ "NewtRuntimeConfig",
79
+ "PangolinBadgerVersion",
80
+ "PangolinComposeVersions",
81
+ "Version",
82
+ "unmask",
83
+ "compose_newt",
84
+ "compute_upgrade_path",
85
+ "ensure_newt_runtime",
86
+ "ensure_newt_container_service",
87
+ "ensure_newt_systemd_service",
88
+ "pangolin_newt",
89
+ "fetch_docker_hub_versions",
90
+ "fetch_github_release_versions",
91
+ "fix_newt_permissions",
92
+ "install_newt_binary",
93
+ "newt_binary",
94
+ "pangolin_site",
95
+ "pangolin_resource",
96
+ "pangolin_http_resource",
97
+ "pangolin_target",
98
+ "pangolin_domain",
99
+ "pangolin_blueprint",
100
+ "render_newt_compose_yaml",
101
+ "render_newt_config_json",
102
+ "render_newt_env",
103
+ "render_newt_service_yaml",
104
+ "render_newt_systemd_unit",
105
+ "resolve_available_versions",
106
+ "pangolin_stack",
107
+ "update_pangolin_stack",
108
+ "render_compose",
109
+ "render_config_files",
110
+ "write_blueprint",
111
+ ]
@@ -0,0 +1,155 @@
1
+ """
2
+ Patch an existing Docker Compose file with the Newt service.
3
+
4
+ The compose file belongs to the user: it usually carries comments, and often anchors. The
5
+ merge therefore uses ruamel.yaml's round-trip loader and dumper, which preserve both,
6
+ instead of PyYAML, which would silently rewrite the file.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from collections.abc import Iterable, MutableMapping, MutableSequence
12
+ from io import StringIO
13
+ from typing import Any
14
+
15
+ from pyinfra.api import OperationError
16
+ from ruamel.yaml import YAML
17
+
18
+ # ruamel's round-trip representer keeps mapping order by design ("no sorting"),
19
+ # so there is no sort_keys to disable: setting one would be a silent no-op.
20
+ _YAML = YAML()
21
+ _YAML.preserve_quotes = True
22
+ _YAML.explicit_start = False
23
+ _YAML.explicit_end = False
24
+
25
+
26
+ def load_compose(text: str, path: str) -> MutableMapping[str, Any]:
27
+ """
28
+ Load *text* as a compose mapping, or fail with the file name.
29
+
30
+ + text: the compose file contents.
31
+ + path: the compose path, used in error messages.
32
+ """
33
+ try:
34
+ data = _YAML.load(text)
35
+ except Exception as exc: # noqa: BLE001 - surface the parser error with the path
36
+ raise OperationError(f"Cannot parse {path}: {exc}") from exc
37
+ if not isinstance(data, MutableMapping):
38
+ raise OperationError(f"Compose file {path} is empty or is not a YAML mapping")
39
+ return data
40
+
41
+
42
+ def load_service(fragment: str, path: str) -> MutableMapping[str, Any]:
43
+ """
44
+ Load the rendered Newt service fragment.
45
+
46
+ + fragment: YAML emitted by :func:`render_newt_service_yaml`.
47
+ + path: the compose path, used in error messages.
48
+ """
49
+ try:
50
+ data = _YAML.load(fragment)
51
+ except Exception as exc: # noqa: BLE001 - surface the parser error with the path
52
+ raise OperationError(f"Cannot parse the Newt service for {path}: {exc}") from exc
53
+ if not isinstance(data, MutableMapping):
54
+ raise OperationError(f"Could not parse the Newt service for {path}")
55
+ return data
56
+
57
+
58
+ def dump_compose(data: MutableMapping[str, Any]) -> str:
59
+ """
60
+ Render *data* while preserving comments, anchors and key order.
61
+ """
62
+ buffer = StringIO()
63
+ _YAML.dump(data, buffer)
64
+ return buffer.getvalue()
65
+
66
+
67
+ def attach_network(
68
+ service: MutableMapping[str, Any],
69
+ network: str,
70
+ *,
71
+ service_name: str,
72
+ path: str,
73
+ ) -> None:
74
+ """
75
+ Attach *service* to *network*, accepting both Compose network syntaxes.
76
+
77
+ ``networks`` is either a list of names or a mapping of names to options
78
+ (aliases, static addresses, ...). Both are valid; appending to the mapping
79
+ would raise, and ``network_mode`` is mutually exclusive with ``networks``.
80
+
81
+ + service: the service mapping, modified in place.
82
+ + network: the network name.
83
+ + service_name: the service name, used in error messages.
84
+ + path: the compose path, used in error messages.
85
+ """
86
+ if not isinstance(service, MutableMapping):
87
+ raise OperationError(f"Service {service_name!r} in {path} is not a YAML mapping")
88
+ if service.get("network_mode") is not None:
89
+ raise OperationError(
90
+ f"Service {service_name!r} in {path} uses network_mode; it cannot also join the {network!r} network"
91
+ )
92
+
93
+ networks = service.get("networks")
94
+ if networks is None:
95
+ service["networks"] = [network]
96
+ elif isinstance(networks, MutableSequence):
97
+ if network not in networks:
98
+ networks.append(network)
99
+ elif isinstance(networks, MutableMapping):
100
+ networks.setdefault(network, {})
101
+ else:
102
+ raise OperationError(f"Service {service_name!r} in {path} has an unsupported networks value")
103
+
104
+
105
+ def merge_newt(
106
+ compose_data: MutableMapping[str, Any],
107
+ service_data: MutableMapping[str, Any],
108
+ *,
109
+ newt_name: str,
110
+ target_network: str | None,
111
+ target_services: Iterable[str],
112
+ extra_networks: Iterable[str],
113
+ path: str,
114
+ ) -> None:
115
+ """
116
+ Merge the Newt service and its networks into *compose_data*, in place.
117
+
118
+ An existing service with the same name is overwritten. ``target_network`` is
119
+ created inside the compose mapping and attached to Newt and its target services
120
+ (every name must exist); ``extra_networks`` are only declared ``external: true``.
121
+
122
+ + compose_data: the loaded compose mapping, modified in place.
123
+ + service_data: the rendered Newt service mapping.
124
+ + newt_name: the service key and container name of Newt.
125
+ + target_network: a dedicated network to create and attach, or ``None``.
126
+ + target_services: existing services to attach to *target_network*.
127
+ + extra_networks: external networks to declare and attach to Newt.
128
+ + path: the compose path, used in error messages.
129
+ """
130
+ services = compose_data.setdefault("services", {})
131
+ if not isinstance(services, MutableMapping):
132
+ raise OperationError(f"'services' in {path} is not a mapping")
133
+ for name, config in service_data.items():
134
+ services[name] = config
135
+
136
+ extra = tuple(extra_networks)
137
+ if target_network is None and not extra:
138
+ return
139
+
140
+ networks = compose_data.setdefault("networks", {})
141
+ if not isinstance(networks, MutableMapping):
142
+ raise OperationError(f"'networks' in {path} is not a mapping")
143
+
144
+ if target_network is not None:
145
+ networks.setdefault(target_network, {})
146
+ attach_network(services[newt_name], target_network, service_name=newt_name, path=path)
147
+
148
+ missing = [name for name in target_services if name not in services]
149
+ if missing:
150
+ raise OperationError(f"Target service(s) not found in {path}: {', '.join(sorted(missing))}")
151
+ for name in target_services:
152
+ attach_network(services[name], target_network, service_name=name, path=path)
153
+
154
+ for network in extra:
155
+ networks.setdefault(network, {"external": True, "name": network})