import hashlib import os from datetime import datetime, timezone from typing import Dict from .utils import load_json, save_json DEVICE_STATE_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "infra", "device_state.json") BOOTSTRAP_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "infra", "bootstrap.json") def _timestamp() -> str: return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") def _new_hash(device: str, timestamp: str) -> str: return hashlib.sha256(f"bootstrap-{device}-{timestamp}".encode()).hexdigest()[:8] def oob_seed_device(device: str) -> Dict: device_state = load_json(DEVICE_STATE_PATH) bootstrap_state = load_json(BOOTSTRAP_PATH) now = _timestamp() cfg_hash = _new_hash(device, now) device_state[device] = { "role": device_state.get(device, {}).get("role", "unknown"), "pod": device_state.get(device, {}).get("pod", "unknown"), "status": "healthy", "last_config_hash": cfg_hash, "last_update": now, } save_json(DEVICE_STATE_PATH, device_state) bootstrap_state[device] = { "bootstrapped": True, "bootstrap_time": now, "method": "oob_seed_device", "initial_config_hash": cfg_hash, } save_json(BOOTSTRAP_PATH, bootstrap_state) return { "device": device, "bootstrapped": True, "bootstrap_time": now, "method": "oob_seed_device", "config_hash": cfg_hash, "message": "Device bootstrapped (simulated).", }