Add edge node registration script
Browse files- scripts/register_edge_node.py +123 -0
scripts/register_edge_node.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Register an edge node with the central SmartClass server.
|
| 4 |
+
|
| 5 |
+
This script registers a new edge device (e.g., Raspberry Pi 5) with the central
|
| 6 |
+
server, establishing its identity and section assignment.
|
| 7 |
+
|
| 8 |
+
Usage:
|
| 9 |
+
python scripts/register_edge_node.py --node-id rpi5-room301 --section AIML-3-A --ip 192.168.1.101
|
| 10 |
+
"""
|
| 11 |
+
import argparse
|
| 12 |
+
import json
|
| 13 |
+
import socket
|
| 14 |
+
import sys
|
| 15 |
+
import urllib.request
|
| 16 |
+
import urllib.error
|
| 17 |
+
from datetime import datetime
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_system_info() -> dict:
|
| 21 |
+
"""Gather system information for registration."""
|
| 22 |
+
import platform
|
| 23 |
+
|
| 24 |
+
return {
|
| 25 |
+
"hostname": socket.gethostname(),
|
| 26 |
+
"platform": platform.platform(),
|
| 27 |
+
"architecture": platform.machine(),
|
| 28 |
+
"python_version": platform.python_version(),
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def register_node(
|
| 33 |
+
api_url: str,
|
| 34 |
+
node_id: str,
|
| 35 |
+
section_key: str,
|
| 36 |
+
ip_address: str,
|
| 37 |
+
api_key: str = "",
|
| 38 |
+
) -> bool:
|
| 39 |
+
"""Register edge node with central API server."""
|
| 40 |
+
system_info = get_system_info()
|
| 41 |
+
|
| 42 |
+
payload = {
|
| 43 |
+
"node_id": node_id,
|
| 44 |
+
"section_key": section_key,
|
| 45 |
+
"ip_address": ip_address,
|
| 46 |
+
"hostname": system_info["hostname"],
|
| 47 |
+
"platform": system_info["platform"],
|
| 48 |
+
"architecture": system_info["architecture"],
|
| 49 |
+
"registered_at": datetime.now().isoformat(),
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
print(f"📡 Registering edge node: {node_id}")
|
| 53 |
+
print(f" Section: {section_key}")
|
| 54 |
+
print(f" IP: {ip_address}")
|
| 55 |
+
print(f" Hostname: {system_info['hostname']}")
|
| 56 |
+
|
| 57 |
+
headers = {
|
| 58 |
+
"Content-Type": "application/json",
|
| 59 |
+
}
|
| 60 |
+
if api_key:
|
| 61 |
+
headers["Authorization"] = f"Bearer {api_key}"
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
data = json.dumps(payload).encode("utf-8")
|
| 65 |
+
req = urllib.request.Request(
|
| 66 |
+
f"{api_url}/api/v1/edge-nodes/register",
|
| 67 |
+
data=data,
|
| 68 |
+
headers=headers,
|
| 69 |
+
method="POST",
|
| 70 |
+
)
|
| 71 |
+
with urllib.request.urlopen(req, timeout=15) as resp:
|
| 72 |
+
result = json.loads(resp.read().decode())
|
| 73 |
+
print(f"\n✅ Registration successful!")
|
| 74 |
+
print(f" Node ID: {result.get('node_id', node_id)}")
|
| 75 |
+
print(f" Status: {result.get('status', 'registered')}")
|
| 76 |
+
return True
|
| 77 |
+
|
| 78 |
+
except urllib.error.HTTPError as e:
|
| 79 |
+
body = e.read().decode() if e.fp else ""
|
| 80 |
+
print(f"\n❌ Registration failed: HTTP {e.code}")
|
| 81 |
+
print(f" Response: {body[:200]}")
|
| 82 |
+
return False
|
| 83 |
+
|
| 84 |
+
except urllib.error.URLError as e:
|
| 85 |
+
print(f"\n❌ Connection failed: {e.reason}")
|
| 86 |
+
print(f" Is the API server running at {api_url}?")
|
| 87 |
+
return False
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def main():
|
| 91 |
+
parser = argparse.ArgumentParser(description="Register SmartClass edge node")
|
| 92 |
+
parser.add_argument("--node-id", required=True, help="Unique node identifier")
|
| 93 |
+
parser.add_argument("--section", required=True, help="Section key (e.g., AIML-3-A)")
|
| 94 |
+
parser.add_argument("--ip", default="", help="Node IP address (auto-detected if empty)")
|
| 95 |
+
parser.add_argument("--api-url", default="http://localhost:8000", help="Central API URL")
|
| 96 |
+
parser.add_argument("--api-key", default="", help="API authentication key")
|
| 97 |
+
|
| 98 |
+
args = parser.parse_args()
|
| 99 |
+
|
| 100 |
+
# Auto-detect IP if not provided
|
| 101 |
+
ip_address = args.ip
|
| 102 |
+
if not ip_address:
|
| 103 |
+
try:
|
| 104 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 105 |
+
s.connect(("8.8.8.8", 80))
|
| 106 |
+
ip_address = s.getsockname()[0]
|
| 107 |
+
s.close()
|
| 108 |
+
except Exception:
|
| 109 |
+
ip_address = "127.0.0.1"
|
| 110 |
+
|
| 111 |
+
success = register_node(
|
| 112 |
+
api_url=args.api_url,
|
| 113 |
+
node_id=args.node_id,
|
| 114 |
+
section_key=args.section,
|
| 115 |
+
ip_address=ip_address,
|
| 116 |
+
api_key=args.api_key,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
sys.exit(0 if success else 1)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
main()
|