Source code for fleetmesh_node.app.config

"""The node's configuration: the control-socket table, the defaults, and the
file that carries them.

Separate from the node so `meshnode init`, `peer` and `trust` can read and
write config.json without constructing a NodeApp.
"""
from __future__ import annotations

import json
import os
from typing import Any, Mapping, Sequence

from fleetmesh_core.identity.did import node_of_did
from ..config import ConfigError, NodeConfig, Problems
from .commands import CONTROL, NODE_COMMANDS, NODE_COMMANDS_BY_KEY

__all__ = [
    'CONTROL',
    'ConfigError',
    'DEFAULTS',
    'NODE_COMMANDS',
    'NODE_COMMANDS_BY_KEY',
    'NodeConfig',
    'Problems',
    'config_from_manifests',
    'defaults',
    'load_config',
    'save_config',
    'typed_config',
    'validate_config',
]

# The file's defaults are the file shape of NodeConfig(): one source. The
# comments that used to sit beside each key are on the dataclass fields in
# fleetmesh_node/config/. Only the node's own keys are written; a compartment's block
# is absent until the operator asks for it, because presence has meaning.
DEFAULTS: Mapping[str, Any] = NodeConfig.defaults_dict()


[docs]def defaults() -> Mapping[str, Any]: """A fresh copy, nested values included. DEFAULTS holds dicts and lists, and dict(DEFAULTS) copies only the top level: two nodes built from it shared one `face` dict, so the first to record its port set it for every node after. Anything that hands out defaults hands out copies. """ return json.loads(json.dumps(DEFAULTS))
[docs]def validate_config(cfg: Any) -> list[str]: """Every problem with a config, as a list of sentences; empty if none. The file is the interface, so it is checked the way a message is: at the door, with reasons. NodeConfig.check does the checking; this is the list the CLI and the tests read. """ p = Problems() NodeConfig.check(cfg, p) return list(p)
[docs]def typed_config(cfg: Mapping[str, Any]) -> NodeConfig: """The typed view of a config that validate_config passed.""" return NodeConfig.from_dict(cfg)
[docs]def load_config(directory: str) -> Mapping[str, Any]: with open(os.path.join(directory, 'config.json'), encoding='utf-8') as f: cfg = json.load(f) out = dict(defaults()) if not isinstance(cfg, dict): raise ConfigError('config.json is not an object') out.update(cfg) problems = validate_config(out) if problems: raise ConfigError('config.json: ' + '; '.join(problems)) return out
def _blob_storage_cap(cfg: Mapping[str, Any]) -> int | None: """Byte cap for the Blossom blob store, or None for unbounded. FLEETMESH_BLOB_MAX_BYTES wins, then config ``blobs.max_storage_bytes``, then the MeshNode manifest's ``storage.maxBytes`` (config_from_manifests copies it to ``cfg['storage']``). Before this nothing passed a cap, so the LRU eviction in BlobStore.put never ran and a node's disk filled without bound. 0 or a negative value means unbounded. """ raw: Any = os.environ.get('FLEETMESH_BLOB_MAX_BYTES') if raw in (None, ''): raw = (cfg.get('blobs') or {}).get('max_storage_bytes') if raw in (None, ''): raw = (cfg.get('storage') or {}).get('maxBytes') try: cap = int(raw) if raw not in (None, '') else 0 except (TypeError, ValueError): raise ConfigError(f'blob storage cap is not an integer: {raw!r}') return cap if cap > 0 else None
[docs]def save_config(directory: str, cfg: Mapping[str, Any]) -> None: os.makedirs(directory, mode=0o700, exist_ok=True) path = os.path.join(directory, 'config.json') tmp = path + '.tmp' with open(tmp, 'w', encoding='utf-8') as f: json.dump(cfg, f, indent=2, sort_keys=True) f.write('\n') os.replace(tmp, path)
[docs]def config_from_manifests(meshnode: Mapping[str, Any], peerings: Sequence[Mapping[str, Any]] = ()) -> Mapping[str, Any]: """A config from a MeshNode manifest and any Peering manifests (parsed YAML or JSON).""" spec = meshnode['spec'] cfg = dict(defaults()) cfg['class'] = spec['class'] cfg['operator'] = spec['operator']['pubkey'] cfg['endpoints'] = [{'transport': e['transport'], 'address': e.get('address') or e.get('ticket'), 'priority': e.get('priority', 50)} for e in spec.get('endpoints', [])] caps = spec.get('capabilities', []) cfg['capabilities'] = [c for c in caps if not c.startswith('https://')] cfg['protocols'] = sorted(set(DEFAULTS['protocols']) | {c for c in caps if c.startswith('https://')}) cfg['storage'] = spec.get('storage', {}) cfg['peers'] = [] for pm in peerings: ps = pm['spec'] cfg['peers'].append({'pubkey': node_of_did(ps['peer']), 'shard': pm['metadata']['name'], 'filter': ps['ingress']['filter'], 'pull_every': 600, 'window': ps.get('budget', {}).get('window')}) return cfg