netherframe.backends.consul 0.2.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.
- netherframe/backends/consul/__init__.py +43 -0
- netherframe/backends/consul/consul.py +313 -0
- netherframe/backends/consul/consul_backend_bootstrap.py +102 -0
- netherframe/backends/consul/py.typed +0 -0
- netherframe_backends_consul-0.2.0.dist-info/METADATA +80 -0
- netherframe_backends_consul-0.2.0.dist-info/RECORD +12 -0
- netherframe_backends_consul-0.2.0.dist-info/WHEEL +4 -0
- netherframe_backends_consul-0.2.0.dist-info/licenses/LICENSE +21 -0
- nf/backends/consul/__init__.py +4 -0
- nf/backends/consul/consul.py +4 -0
- nf/backends/consul/consul_backend_bootstrap.py +4 -0
- nf/backends/consul/py.typed +0 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Consul backend implementations for Netherframe."""
|
|
2
|
+
|
|
3
|
+
from netherframe.backends.consul.consul import ConsulBackend, ConsulStateBackend
|
|
4
|
+
from netherframe.backends.consul.consul_backend_bootstrap import (
|
|
5
|
+
ConsulBackendBootstrapper,
|
|
6
|
+
ConsulBackendResources,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"ConsulBackend",
|
|
11
|
+
"ConsulStateBackend",
|
|
12
|
+
"ConsulBackendBootstrapper",
|
|
13
|
+
"ConsulBackendResources",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
from netherframe.core.backends.backend_bootstrap import BackendBootstrapManager
|
|
17
|
+
from netherframe.core.backends.backend_registry import BackendRegistry
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _register_core_components() -> None:
|
|
21
|
+
"""Register Consul backend constructs and state backends."""
|
|
22
|
+
BackendBootstrapManager.register_bootstrapper(
|
|
23
|
+
"consul",
|
|
24
|
+
ConsulBackendBootstrapper,
|
|
25
|
+
)
|
|
26
|
+
BackendRegistry.register_backend_construct(
|
|
27
|
+
"consul",
|
|
28
|
+
ConsulBackend,
|
|
29
|
+
"netherframe.backends.consul",
|
|
30
|
+
)
|
|
31
|
+
BackendRegistry.register_state_backend(
|
|
32
|
+
"consul",
|
|
33
|
+
ConsulStateBackend,
|
|
34
|
+
"netherframe.backends.consul",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
_register_core_components()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def register_backends() -> None:
|
|
42
|
+
"""Register Consul backends. Called by entry points."""
|
|
43
|
+
_register_core_components()
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"""Consul backend implementation for Terraform and native execution modes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from netherframe.core.serialization import orjson_compat as json
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
7
|
+
|
|
8
|
+
from netherframe.core.backends.lock_heartbeat import LockHeartbeat
|
|
9
|
+
from netherframe.core.backends.document_state_backend import ABSENT, DocumentChangedError, DocumentStateBackend
|
|
10
|
+
from netherframe.core.constructs.backend_base import Backend
|
|
11
|
+
from netherframe.core.exceptions import StateLockError
|
|
12
|
+
from netherframe.core.logging import get_logger
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from netherframe.core.backends.backend_bootstrap import BackendBootstrapper
|
|
16
|
+
from netherframe.core.constructs.app_stack import Stack
|
|
17
|
+
from netherframe.core.constructs.framework_settings import BackendSettings
|
|
18
|
+
|
|
19
|
+
logger = get_logger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ConsulBackend(Backend):
|
|
23
|
+
"""Consul backend for Terraform state."""
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def backend_type(self) -> str:
|
|
27
|
+
"""Get the backend type name."""
|
|
28
|
+
return "consul"
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
nf_scope: Stack,
|
|
33
|
+
*,
|
|
34
|
+
address: str,
|
|
35
|
+
path: str,
|
|
36
|
+
scheme: str = "http",
|
|
37
|
+
nf_backend_settings: BackendSettings | None = None,
|
|
38
|
+
**kwargs: Any,
|
|
39
|
+
) -> None:
|
|
40
|
+
"""Initialize Consul backend."""
|
|
41
|
+
super().__init__(
|
|
42
|
+
nf_scope=nf_scope,
|
|
43
|
+
nf_id="backend",
|
|
44
|
+
nf_backend_settings=nf_backend_settings,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
self.address = address
|
|
48
|
+
self.consul_path = path
|
|
49
|
+
self.scheme = scheme
|
|
50
|
+
self.additional_config = kwargs
|
|
51
|
+
|
|
52
|
+
def _build_config(self) -> dict[str, Any]:
|
|
53
|
+
"""Build Terraform backend configuration."""
|
|
54
|
+
config: dict[str, Any] = {
|
|
55
|
+
"address": self.address,
|
|
56
|
+
"path": self.consul_path,
|
|
57
|
+
"scheme": self.scheme,
|
|
58
|
+
}
|
|
59
|
+
config.update(self.additional_config)
|
|
60
|
+
return config
|
|
61
|
+
|
|
62
|
+
def _to_config(self) -> dict[str, Any]:
|
|
63
|
+
"""Convert to Terraform backend configuration."""
|
|
64
|
+
return {"consul": self._build_config()}
|
|
65
|
+
|
|
66
|
+
# Consul backend could implement get_state_resources() using Consul HTTP API
|
|
67
|
+
|
|
68
|
+
def get_bootstrapper(self) -> BackendBootstrapper | None:
|
|
69
|
+
"""Return a Consul backend bootstrapper if possible."""
|
|
70
|
+
from netherframe.backends.consul.consul_backend_bootstrap import (
|
|
71
|
+
ConsulBackendBootstrapper,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
additional = getattr(self, "additional_config", {})
|
|
75
|
+
return ConsulBackendBootstrapper(
|
|
76
|
+
address=self.address,
|
|
77
|
+
path=self.consul_path,
|
|
78
|
+
scheme=self.scheme,
|
|
79
|
+
access_token=additional.get("access_token") or additional.get("token"),
|
|
80
|
+
datacenter=additional.get("datacenter"),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class ConsulStateBackend(DocumentStateBackend):
|
|
85
|
+
"""Consul-based state backend.
|
|
86
|
+
|
|
87
|
+
Compatible with Terraform's consul backend.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
storage_name = "Consul"
|
|
91
|
+
state_logger = logger
|
|
92
|
+
|
|
93
|
+
def __init__(
|
|
94
|
+
self,
|
|
95
|
+
address: str,
|
|
96
|
+
path: str,
|
|
97
|
+
access_token: str | None = None,
|
|
98
|
+
scheme: str = "http",
|
|
99
|
+
datacenter: str | None = None,
|
|
100
|
+
) -> None:
|
|
101
|
+
"""Initialize Consul state backend.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
address: Consul address
|
|
105
|
+
path: KV path for state
|
|
106
|
+
access_token: Consul ACL token
|
|
107
|
+
scheme: URL scheme (http/https)
|
|
108
|
+
datacenter: Consul datacenter
|
|
109
|
+
|
|
110
|
+
"""
|
|
111
|
+
self.address = address
|
|
112
|
+
self.path = path
|
|
113
|
+
self.access_token = access_token
|
|
114
|
+
self.scheme = scheme
|
|
115
|
+
self.datacenter = datacenter
|
|
116
|
+
|
|
117
|
+
# Import consul
|
|
118
|
+
try:
|
|
119
|
+
import consul
|
|
120
|
+
|
|
121
|
+
host, port = self._parse_address(address)
|
|
122
|
+
self.consul = consul.Consul(
|
|
123
|
+
host=host,
|
|
124
|
+
port=port,
|
|
125
|
+
token=access_token,
|
|
126
|
+
scheme=scheme,
|
|
127
|
+
dc=datacenter,
|
|
128
|
+
)
|
|
129
|
+
except ImportError as e:
|
|
130
|
+
raise RuntimeError(
|
|
131
|
+
"py-consul is required for consul backend. Install with: pip install py-consul",
|
|
132
|
+
) from e
|
|
133
|
+
|
|
134
|
+
@staticmethod
|
|
135
|
+
def _parse_address(address: str) -> tuple[str, int]:
|
|
136
|
+
"""Parse a Consul host:port address."""
|
|
137
|
+
host, port = address.split(":", maxsplit=1) if ":" in address else (address, "8500")
|
|
138
|
+
return host, int(port)
|
|
139
|
+
|
|
140
|
+
def _lock_path(self) -> str:
|
|
141
|
+
"""Return the KV path used for state locking."""
|
|
142
|
+
return f"{self.path}.lock"
|
|
143
|
+
|
|
144
|
+
def _state_location(self) -> str:
|
|
145
|
+
"""Return the KV path of the state."""
|
|
146
|
+
return self.path
|
|
147
|
+
|
|
148
|
+
def state_identity(self) -> dict[str, Any]:
|
|
149
|
+
return {"backend": "consul", "scheme": self.scheme, "address": self.address, "datacenter": self.datacenter, "path": self.path}
|
|
150
|
+
|
|
151
|
+
def _read_document(self) -> tuple[bytes | None, Any]:
|
|
152
|
+
"""Read the state document from Consul KV with its ModifyIndex; (None, ABSENT) if the key is absent."""
|
|
153
|
+
_index, data = self.consul.kv.get(self.path)
|
|
154
|
+
if not data:
|
|
155
|
+
return None, ABSENT
|
|
156
|
+
return data.get("Value"), data.get("ModifyIndex")
|
|
157
|
+
|
|
158
|
+
def _write_document(self, data: bytes, if_token: Any = None) -> Any:
|
|
159
|
+
"""Write the state document to Consul KV; with a token, as a check-and-set on that ModifyIndex."""
|
|
160
|
+
if if_token is None:
|
|
161
|
+
if not self.consul.kv.put(self.path, data):
|
|
162
|
+
raise RuntimeError(f"Consul rejected the state write to {self.path}")
|
|
163
|
+
return None
|
|
164
|
+
# cas=0 writes only a key that does not exist
|
|
165
|
+
if not self.consul.kv.put(self.path, data, cas=0 if if_token is ABSENT else if_token):
|
|
166
|
+
raise DocumentChangedError(f"{self.path} changed since ModifyIndex {if_token}")
|
|
167
|
+
# A put does not say the key's new ModifyIndex: the next conditional write reads it
|
|
168
|
+
return None
|
|
169
|
+
|
|
170
|
+
def _checkpoint_key(self, name: str) -> str:
|
|
171
|
+
return f"{self.path}.checkpoints/{name}"
|
|
172
|
+
|
|
173
|
+
def _checkpoint_location(self, name: str) -> str:
|
|
174
|
+
return f"consul://{self.address}/{self._checkpoint_key(name)}"
|
|
175
|
+
|
|
176
|
+
def _write_checkpoint(self, name: str, data: bytes) -> None:
|
|
177
|
+
if not self.consul.kv.put(self._checkpoint_key(name), data):
|
|
178
|
+
raise RuntimeError(f"Consul rejected the checkpoint write to {self._checkpoint_key(name)}")
|
|
179
|
+
|
|
180
|
+
def _read_checkpoint(self, name: str) -> bytes | None:
|
|
181
|
+
_index, data = self.consul.kv.get(self._checkpoint_key(name))
|
|
182
|
+
return data.get("Value") if data else None
|
|
183
|
+
|
|
184
|
+
def _checkpoint_names(self) -> list[str]:
|
|
185
|
+
prefix = self._checkpoint_key("")
|
|
186
|
+
_index, keys = self.consul.kv.get(prefix, keys=True)
|
|
187
|
+
return [key.removeprefix(prefix) for key in keys or []]
|
|
188
|
+
|
|
189
|
+
def _delete_checkpoint(self, name: str) -> None:
|
|
190
|
+
self.consul.kv.delete(self._checkpoint_key(name))
|
|
191
|
+
|
|
192
|
+
SESSION_TTL_SECONDS = 30
|
|
193
|
+
SESSION_RENEW_SECONDS: float = 10
|
|
194
|
+
|
|
195
|
+
def lock(self, info: dict[str, Any] | object) -> bool:
|
|
196
|
+
"""Acquire state lock using a Consul session, renewed until unlock()."""
|
|
197
|
+
try:
|
|
198
|
+
# Create session
|
|
199
|
+
session_id = self.consul.session.create(
|
|
200
|
+
name=f"nf-state-lock-{self.path}",
|
|
201
|
+
ttl=self.SESSION_TTL_SECONDS,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
# Try to acquire lock
|
|
205
|
+
to_dict = getattr(info, "to_dict", None)
|
|
206
|
+
lock_info = to_dict() if callable(to_dict) else info
|
|
207
|
+
success = self.consul.kv.put(
|
|
208
|
+
self._lock_path(),
|
|
209
|
+
json.dumps(lock_info),
|
|
210
|
+
acquire=session_id,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
if success:
|
|
214
|
+
self._session_id = session_id
|
|
215
|
+
# The session (and so the lock) lapses after its TTL unless renewed
|
|
216
|
+
self._heartbeat = LockHeartbeat(
|
|
217
|
+
lambda: self.consul.session.renew(session_id),
|
|
218
|
+
self.SESSION_RENEW_SECONDS,
|
|
219
|
+
name=f"consul:{self.path}",
|
|
220
|
+
).start()
|
|
221
|
+
logger.info("State lock acquired (Session: %s)", session_id)
|
|
222
|
+
return True
|
|
223
|
+
logger.error("Failed to acquire state lock")
|
|
224
|
+
# Destroy session
|
|
225
|
+
self.consul.session.destroy(session_id)
|
|
226
|
+
return False
|
|
227
|
+
|
|
228
|
+
except Exception as e:
|
|
229
|
+
logger.error("Failed to acquire lock: %s", e)
|
|
230
|
+
return False
|
|
231
|
+
|
|
232
|
+
def unlock(self) -> None:
|
|
233
|
+
"""Release state lock."""
|
|
234
|
+
heartbeat = getattr(self, "_heartbeat", None)
|
|
235
|
+
if heartbeat is not None:
|
|
236
|
+
heartbeat.stop()
|
|
237
|
+
self._heartbeat = None
|
|
238
|
+
if hasattr(self, "_session_id"):
|
|
239
|
+
try:
|
|
240
|
+
# Release lock
|
|
241
|
+
self.consul.kv.put(
|
|
242
|
+
self._lock_path(),
|
|
243
|
+
None,
|
|
244
|
+
release=self._session_id,
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
# Destroy session
|
|
248
|
+
self.consul.session.destroy(self._session_id)
|
|
249
|
+
|
|
250
|
+
logger.info(f"State lock released (Session: {self._session_id})")
|
|
251
|
+
del self._session_id
|
|
252
|
+
|
|
253
|
+
except Exception as e:
|
|
254
|
+
logger.error("Failed to release lock: %s", e)
|
|
255
|
+
|
|
256
|
+
can_force_unlock = True
|
|
257
|
+
|
|
258
|
+
def _held_lock(self) -> tuple[dict[str, Any], str] | None:
|
|
259
|
+
"""The info of the lock held on this state and the session holding it, or None."""
|
|
260
|
+
_index, data = self.consul.kv.get(self._lock_path())
|
|
261
|
+
if not data or not data.get("Session"):
|
|
262
|
+
return None
|
|
263
|
+
value = data.get("Value")
|
|
264
|
+
return (json.loads(value) if value else {"ID": None}), data["Session"]
|
|
265
|
+
|
|
266
|
+
def current_lock(self) -> dict[str, Any] | None:
|
|
267
|
+
"""The info of the lock held on this state, or None."""
|
|
268
|
+
held = self._held_lock()
|
|
269
|
+
return held[0] if held is not None else None
|
|
270
|
+
|
|
271
|
+
def force_unlock(self, lock_id: str) -> None:
|
|
272
|
+
"""Release the lock key and end the session holding it, only while it is the lock ``lock_id``."""
|
|
273
|
+
held = self._held_lock()
|
|
274
|
+
if held is None:
|
|
275
|
+
raise StateLockError(f"State at {self.path} is not locked")
|
|
276
|
+
info, session_id = held
|
|
277
|
+
if str(info.get("ID")) != lock_id:
|
|
278
|
+
raise StateLockError(f"State at {self.path} is locked by lock {info.get('ID')}, not {lock_id}")
|
|
279
|
+
if not self.consul.kv.put(self._lock_path(), None, release=session_id):
|
|
280
|
+
raise StateLockError(f"State at {self.path} is no longer locked by lock {lock_id}")
|
|
281
|
+
# Ended, so its holder, if it still runs, can neither renew nor use it
|
|
282
|
+
self.consul.session.destroy(session_id)
|
|
283
|
+
logger.warning("Released state lock %s of %s", lock_id, info)
|
|
284
|
+
|
|
285
|
+
def get_backend_type(self) -> str:
|
|
286
|
+
"""Get the backend type identifier.
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
"consul" as the backend type
|
|
290
|
+
|
|
291
|
+
"""
|
|
292
|
+
return "consul"
|
|
293
|
+
|
|
294
|
+
def get_config(self) -> dict[str, Any]:
|
|
295
|
+
"""Get the backend configuration.
|
|
296
|
+
|
|
297
|
+
Returns:
|
|
298
|
+
Configuration dictionary with all parameters needed to recreate this backend
|
|
299
|
+
|
|
300
|
+
"""
|
|
301
|
+
config = {
|
|
302
|
+
"address": self.address,
|
|
303
|
+
"path": self.path,
|
|
304
|
+
"scheme": self.scheme,
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if self.access_token:
|
|
308
|
+
config["access_token"] = self.access_token
|
|
309
|
+
|
|
310
|
+
if self.datacenter:
|
|
311
|
+
config["datacenter"] = self.datacenter
|
|
312
|
+
|
|
313
|
+
return config
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Consul backend bootstrapping implementation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any, Protocol
|
|
7
|
+
from uuid import uuid4
|
|
8
|
+
|
|
9
|
+
from netherframe.core.backends.backend_bootstrap import BackendBootstrapper, BackendResources
|
|
10
|
+
from netherframe.core.logging import get_logger
|
|
11
|
+
from netherframe.core.serialization import orjson_compat as json
|
|
12
|
+
|
|
13
|
+
logger = get_logger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ConsulKvClient(Protocol):
|
|
17
|
+
"""Minimal Consul KV API used by the bootstrapper."""
|
|
18
|
+
|
|
19
|
+
def get(self, key: str) -> tuple[Any, Any]: ...
|
|
20
|
+
def put(self, key: str, value: str) -> Any: ...
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ConsulClient(Protocol):
|
|
24
|
+
"""Minimal Consul client interface used by the bootstrapper."""
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def kv(self) -> ConsulKvClient: ...
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class ConsulBackendResources(BackendResources):
|
|
32
|
+
"""Consul backend resources information."""
|
|
33
|
+
|
|
34
|
+
address: str | None = None
|
|
35
|
+
path: str | None = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ConsulBackendBootstrapper(BackendBootstrapper):
|
|
39
|
+
"""Bootstrap Consul backend resources (KV path)."""
|
|
40
|
+
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
*,
|
|
44
|
+
address: str,
|
|
45
|
+
path: str,
|
|
46
|
+
scheme: str = "http",
|
|
47
|
+
access_token: str | None = None,
|
|
48
|
+
datacenter: str | None = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
self.address = address
|
|
51
|
+
self.path = path
|
|
52
|
+
self.scheme = scheme
|
|
53
|
+
self.access_token = access_token
|
|
54
|
+
self.datacenter = datacenter
|
|
55
|
+
self._resources = ConsulBackendResources()
|
|
56
|
+
|
|
57
|
+
def _create_client(self) -> ConsulClient:
|
|
58
|
+
try:
|
|
59
|
+
import consul
|
|
60
|
+
except ImportError as exc: # pragma: no cover - optional deps
|
|
61
|
+
raise RuntimeError(
|
|
62
|
+
"py-consul is required for consul bootstrapping. Install with: pip install py-consul",
|
|
63
|
+
) from exc
|
|
64
|
+
|
|
65
|
+
host, port = self.address.split(":", maxsplit=1) if ":" in self.address else (self.address, "8500")
|
|
66
|
+
return consul.Consul(
|
|
67
|
+
host=host,
|
|
68
|
+
port=int(port),
|
|
69
|
+
token=self.access_token,
|
|
70
|
+
scheme=self.scheme,
|
|
71
|
+
dc=self.datacenter,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def validate(self) -> bool:
|
|
75
|
+
"""Validate that the KV path exists."""
|
|
76
|
+
client = self._create_client()
|
|
77
|
+
_index, data = client.kv.get(self.path)
|
|
78
|
+
if not data:
|
|
79
|
+
return False
|
|
80
|
+
self._resources.address = self.address
|
|
81
|
+
self._resources.path = self.path
|
|
82
|
+
return True
|
|
83
|
+
|
|
84
|
+
def bootstrap(self) -> BackendResources:
|
|
85
|
+
"""Create the KV path if missing with a minimal state payload."""
|
|
86
|
+
client = self._create_client()
|
|
87
|
+
_index, data = client.kv.get(self.path)
|
|
88
|
+
if not data:
|
|
89
|
+
logger.info("Creating Consul KV path: %s", self.path)
|
|
90
|
+
state = {
|
|
91
|
+
"version": 4,
|
|
92
|
+
"terraform_version": "1.6.0",
|
|
93
|
+
"serial": 1,
|
|
94
|
+
"lineage": str(uuid4()),
|
|
95
|
+
"outputs": {},
|
|
96
|
+
"resources": [],
|
|
97
|
+
}
|
|
98
|
+
client.kv.put(self.path, json.dumps(state))
|
|
99
|
+
|
|
100
|
+
self._resources.address = self.address
|
|
101
|
+
self._resources.path = self.path
|
|
102
|
+
return self._resources
|
|
File without changes
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: netherframe.backends.consul
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Consul backend implementations for Netherframe
|
|
5
|
+
Author: Sourabh Cheedella
|
|
6
|
+
Author-email: Sourabh Cheedella <cheedella.sourabh@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
13
|
+
Classifier: Topic :: System :: Systems Administration
|
|
14
|
+
Requires-Dist: netherframe-core>=0.9.0
|
|
15
|
+
Requires-Dist: py-consul>=1.8.0
|
|
16
|
+
Requires-Dist: pytest>=9.2.0 ; extra == 'dev'
|
|
17
|
+
Requires-Python: >=3.14
|
|
18
|
+
Project-URL: Homepage, https://netherframe.arsenal.sh
|
|
19
|
+
Project-URL: Repository, https://github.com/arsenal-sh/netherframe
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# nf.backends.consul
|
|
24
|
+
|
|
25
|
+
Consul backend implementations for Netherframe Infrastructure as Code framework.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install netherframe.backends.consul
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The backend uses `py-consul`. If upgrading an environment that has `python-consul`
|
|
34
|
+
installed, remove the old distribution first: both packages install the `consul`
|
|
35
|
+
Python module. Reinstalling the backend restores any shared files removed with it:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
python -m pip uninstall python-consul
|
|
39
|
+
python -m pip install --upgrade --force-reinstall netherframe.backends.consul
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
In this workspace, `uv sync` removes the old distribution and installs the replacement
|
|
43
|
+
from the updated lock file.
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from nf.backends.consul import ConsulBackend, ConsulStateBackend
|
|
49
|
+
|
|
50
|
+
# For Terraform synthesis mode
|
|
51
|
+
backend = ConsulBackend(nf_scope=stack, address="consul.example.com:8500", path="terraform/myapp/state", scheme="https")
|
|
52
|
+
|
|
53
|
+
# For native execution mode
|
|
54
|
+
state_backend = ConsulStateBackend(address="consul.example.com:8500", path="terraform/myapp/state", access_token="your-consul-token")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Features
|
|
58
|
+
|
|
59
|
+
- Consul KV-based state storage
|
|
60
|
+
- Compatible with Terraform's consul backend
|
|
61
|
+
- Support for ACL tokens
|
|
62
|
+
- State locking using Consul sessions
|
|
63
|
+
- Automatic session renewal and TTL management, and force-unlock by lock ID (`nf state force-unlock`)
|
|
64
|
+
- Conditional writes: a write replaces only the state version its run read (check-and-set on the ModifyIndex)
|
|
65
|
+
- Pre-apply checkpoints kept beside the state (`<path>.checkpoints/`)
|
|
66
|
+
|
|
67
|
+
## Real-server tests
|
|
68
|
+
|
|
69
|
+
The integration contract tests use a unique KV prefix per test and remove it afterward.
|
|
70
|
+
Point them at a test Consul server with KV and session permissions:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
NF_CONSUL_TEST_ADDRESS=127.0.0.1:8500 \
|
|
74
|
+
uv run pytest backends/netherframe.backends.consul/tests/test_consul_real_integration.py -n 0
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
For an ACL-protected HTTPS endpoint, also set `NF_CONSUL_TEST_SCHEME=https`,
|
|
78
|
+
`NF_CONSUL_TEST_TOKEN_FILE` to a file containing the ACL token, and
|
|
79
|
+
`REQUESTS_CA_BUNDLE` to the trusted CA certificate file. The migration was validated
|
|
80
|
+
against Consul 1.22 over HTTP and HTTPS with ACL enforcement.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
netherframe/backends/consul/__init__.py,sha256=yTQ4Hh8TmZAFy9GWxSbTM-u59JlmVPxIkZIs08aECCY,1185
|
|
2
|
+
netherframe/backends/consul/consul.py,sha256=WmAttm0RiXuQhOzBKyCsr_uMoYMgE5qTPNyXLYhsQpQ,11271
|
|
3
|
+
netherframe/backends/consul/consul_backend_bootstrap.py,sha256=VUVHgLM3Ms-dqQJuSun8Ub_rRQOR0Yxv18CD2pdSJMc,3128
|
|
4
|
+
netherframe/backends/consul/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
nf/backends/consul/__init__.py,sha256=y-T55CIgmfifa0oaT0aTKwZ3FqhjFaCM2RjnTXVUmYE,247
|
|
6
|
+
nf/backends/consul/consul.py,sha256=cbOvKWVFEKbZWRjKm0FYnHbiWOs_I4S4XgcTQy8nObs,268
|
|
7
|
+
nf/backends/consul/consul_backend_bootstrap.py,sha256=lKbHDO8Df41k3OkVQ6zWvbrUVCrMOsSMLe5XQVby3QY,322
|
|
8
|
+
nf/backends/consul/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
netherframe_backends_consul-0.2.0.dist-info/licenses/LICENSE,sha256=CmCIBOHbWRqBeux1f3l5G7rXwtrrtVCZwymYiSoAYoE,1068
|
|
10
|
+
netherframe_backends_consul-0.2.0.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
|
|
11
|
+
netherframe_backends_consul-0.2.0.dist-info/METADATA,sha256=cMJ4OhB8P3Of6gbspE8yNaF551PUr5vP0Ln7_eYVqe8,2978
|
|
12
|
+
netherframe_backends_consul-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Netherframe
|
|
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,4 @@
|
|
|
1
|
+
# Generated nf.* alias shim (netherframe.generation.nf_alias_shims); do not edit.
|
|
2
|
+
"""Type-checker view of ``nf.backends.consul.consul_backend_bootstrap``; at runtime it is ``netherframe.backends.consul.consul_backend_bootstrap`` itself."""
|
|
3
|
+
|
|
4
|
+
from netherframe.backends.consul.consul_backend_bootstrap import * # noqa: F403
|
|
File without changes
|