merge new gateway configs from dev build
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
OpenClaw config helper for Home Assistant add-on.
|
||||
Safely reads/writes openclaw.json without corrupting it.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
CONFIG_PATH = Path(os.environ.get("OPENCLAW_CONFIG_PATH", "/config/.openclaw/openclaw.json"))
|
||||
|
||||
|
||||
|
||||
def read_config():
|
||||
"""Read and parse openclaw.json."""
|
||||
if not CONFIG_PATH.exists():
|
||||
return None
|
||||
try:
|
||||
return json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
|
||||
except (json.JSONDecodeError, IOError) as e:
|
||||
print(f"ERROR: Failed to read config: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
|
||||
def write_config(cfg):
|
||||
"""Write config back to file with nice formatting."""
|
||||
try:
|
||||
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
CONFIG_PATH.write_text(json.dumps(cfg, indent=2) + "\n", encoding="utf-8")
|
||||
return True
|
||||
except IOError as e:
|
||||
print(f"ERROR: Failed to write config: {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def get_gateway_setting(key, default=None):
|
||||
"""Get a gateway setting from config."""
|
||||
cfg = read_config()
|
||||
if cfg is None:
|
||||
return default
|
||||
return cfg.get("gateway", {}).get(key, default)
|
||||
|
||||
|
||||
def set_gateway_setting(key, value):
|
||||
"""Set a gateway setting, preserving other config."""
|
||||
cfg = read_config()
|
||||
if cfg is None:
|
||||
cfg = {}
|
||||
|
||||
if "gateway" not in cfg:
|
||||
cfg["gateway"] = {}
|
||||
|
||||
cfg["gateway"][key] = value
|
||||
return write_config(cfg)
|
||||
|
||||
|
||||
def apply_bind_mode_settings(bind_mode: str, port: int):
|
||||
"""
|
||||
Apply bind mode settings to OpenClaw config.
|
||||
|
||||
Args:
|
||||
bind_mode: "loopback" or "lan"
|
||||
port: Port number to listen on (must be 1-65535)
|
||||
"""
|
||||
# Validate bind mode
|
||||
if bind_mode not in ["loopback", "lan"]:
|
||||
print(f"ERROR: Invalid bind_mode '{bind_mode}'. Must be 'loopback' or 'lan'")
|
||||
return False
|
||||
|
||||
# Validate port range
|
||||
if port < 1 or port > 65535:
|
||||
print(f"ERROR: Invalid port {port}. Must be between 1 and 65535")
|
||||
return False
|
||||
|
||||
cfg = read_config()
|
||||
if cfg is None:
|
||||
cfg = {}
|
||||
|
||||
if "gateway" not in cfg:
|
||||
cfg["gateway"] = {}
|
||||
|
||||
gateway = cfg["gateway"]
|
||||
|
||||
current_bind = gateway.get("bind", "")
|
||||
current_port = gateway.get("port", 18789)
|
||||
|
||||
changes = []
|
||||
|
||||
if current_bind != bind_mode:
|
||||
gateway["bind"] = bind_mode
|
||||
changes.append(f"bind: {current_bind} -> {bind_mode}")
|
||||
|
||||
# Always update port when it differs from the desired value
|
||||
if current_port != port:
|
||||
gateway["port"] = port
|
||||
changes.append(f"port: {current_port} -> {port}")
|
||||
|
||||
if changes:
|
||||
if write_config(cfg):
|
||||
print(f"INFO: Updated gateway settings: {', '.join(changes)}")
|
||||
return True
|
||||
else:
|
||||
print("ERROR: Failed to write config")
|
||||
return False
|
||||
else:
|
||||
print(f"INFO: Gateway settings already correct (bind={bind_mode}, port={port})")
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
"""CLI entry point for use by run.sh"""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: oc_config_helper.py <command> [args...]")
|
||||
sys.exit(1)
|
||||
|
||||
cmd = sys.argv[1]
|
||||
|
||||
if cmd == "apply-bind-mode":
|
||||
if len(sys.argv) != 4:
|
||||
print("Usage: oc_config_helper.py apply-bind-mode <loopback|lan> <port>")
|
||||
sys.exit(1)
|
||||
bind_mode = sys.argv[2]
|
||||
port = int(sys.argv[3])
|
||||
success = apply_bind_mode_settings(bind_mode, port)
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
elif cmd == "get":
|
||||
if len(sys.argv) != 3:
|
||||
print("Usage: oc_config_helper.py get <key>")
|
||||
sys.exit(1)
|
||||
key = sys.argv[2]
|
||||
value = get_gateway_setting(key)
|
||||
if value is not None:
|
||||
print(value)
|
||||
sys.exit(0)
|
||||
|
||||
elif cmd == "set":
|
||||
if len(sys.argv) != 4:
|
||||
print("Usage: oc_config_helper.py set <key> <value>")
|
||||
sys.exit(1)
|
||||
key = sys.argv[2]
|
||||
value = sys.argv[3]
|
||||
# Try to convert to int if it looks like a number
|
||||
try:
|
||||
value = int(value)
|
||||
except ValueError:
|
||||
pass
|
||||
success = set_gateway_setting(key, value)
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
else:
|
||||
print(f"Unknown command: {cmd}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user