feat(bms): add read-only diagnostics and guarded operating switch commands
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""Node113 over qualified Node112; replace reviewed BMS/UI only."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import runpy
|
||||
import sys
|
||||
import tarfile
|
||||
from pathlib import Path
|
||||
|
||||
REPO = Path(__file__).resolve().parents[3]
|
||||
NODE = REPO / "apps/node-agent"
|
||||
VERSION = "0.8.53-113"
|
||||
BASE_SHA = "a67521b9647c3f8e02849cde5adb3bcb8d5f50370e61559da8c6aef3692b6e29"
|
||||
sys.path.insert(0, str(REPO / "scripts/packaging"))
|
||||
from debian import package # noqa: E402
|
||||
|
||||
|
||||
def build(binary, output):
|
||||
if output.exists():
|
||||
raise FileExistsError("Published releases are immutable")
|
||||
raw = (NODE / "build/power-112/mission-core-node_0.8.53-112_amd64.deb").read_bytes()
|
||||
if hashlib.sha256(raw).hexdigest() != BASE_SHA:
|
||||
raise ValueError("Qualified baseline changed")
|
||||
stream = io.BytesIO(raw)
|
||||
assert stream.read(8) == b"!<arch>\n"
|
||||
archives = {}
|
||||
while header := stream.read(60):
|
||||
size = int(header[48:58])
|
||||
archives[header[:16].decode().strip().rstrip("/")] = stream.read(size)
|
||||
if size % 2:
|
||||
stream.read(1)
|
||||
|
||||
def members(name):
|
||||
with tarfile.open(fileobj=io.BytesIO(archives[name]), mode="r:gz") as archive:
|
||||
return [(m.name, archive.extractfile(m).read(), m.mode) for m in archive if m.isfile()]
|
||||
|
||||
controls, files = members("control.tar.gz"), members("data.tar.gz")
|
||||
old = {n: d for n, d, _ in files}
|
||||
compiled = binary.read_bytes()
|
||||
assert compiled[:6] == b"\x7fELF\x02\x01" and compiled[18:20] == b"\x3e\x00"
|
||||
sources = {
|
||||
str(p.relative_to(REPO)): hashlib.sha256(p.read_bytes()).hexdigest()
|
||||
for root in (
|
||||
REPO / "plugins/jk-bms",
|
||||
NODE / "internal",
|
||||
NODE / "cmd",
|
||||
NODE / "web/dist",
|
||||
REPO / "packages/sensor-ui/src",
|
||||
)
|
||||
for p in sorted(root.rglob("*"))
|
||||
if p.is_file() and "__pycache__" not in p.parts
|
||||
}
|
||||
provenance = dict(
|
||||
schema="missioncore.node.packaging-revision/v1",
|
||||
version=VERSION,
|
||||
qualified_base_sha256=BASE_SHA,
|
||||
qualified_base_provenance=json.loads(
|
||||
old["usr/share/doc/mission-core-node/provenance.json"]
|
||||
),
|
||||
scope="Reviewed JK diagnostics and expiring three-switch controls; no numeric writes",
|
||||
binary_sha256=hashlib.sha256(compiled).hexdigest(),
|
||||
source_files=sources,
|
||||
)
|
||||
changes = {
|
||||
"usr/lib/mission-core-node/node-agent": compiled,
|
||||
"usr/share/doc/mission-core-node/provenance.json": (
|
||||
json.dumps(provenance, indent=2) + "\n"
|
||||
).encode(),
|
||||
}
|
||||
added = []
|
||||
for name, data, mode in runpy.run_path(str(Path(__file__).with_name("payload.py")))[
|
||||
"payload"
|
||||
]():
|
||||
if name in old:
|
||||
changes[name] = data
|
||||
else:
|
||||
added.append((name, data, mode))
|
||||
replaced = []
|
||||
for name, data, mode in controls:
|
||||
if name == "control":
|
||||
assert b"Version: 0.8.53-112\n" in data
|
||||
data = data.replace(b"Version: 0.8.53-112\n", f"Version: {VERSION}\n".encode())
|
||||
replaced.append((name, data, mode))
|
||||
files = [(name, changes.get(name, data), mode) for name, data, mode in files] + added
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
output.write_bytes(package(replaced, files))
|
||||
report = dict(
|
||||
version=VERSION,
|
||||
base_sha256=BASE_SHA,
|
||||
sha256=hashlib.sha256(output.read_bytes()).hexdigest(),
|
||||
bytes=output.stat().st_size,
|
||||
changed_controls=["control"],
|
||||
changed_payloads=[name for name, data in changes.items() if old.get(name) != data],
|
||||
added_payloads=[n for n, _, _ in added],
|
||||
retained_payload_count=sum(data == old.get(name) for name, data, _ in files),
|
||||
)
|
||||
output.with_suffix(".json").write_text(json.dumps(report, indent=2) + "\n")
|
||||
print(json.dumps(report))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--binary", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
build(args.binary, args.output)
|
||||
Reference in New Issue
Block a user