Add port management skill and CLI commands

- Add lib/port_manager.py for server port allocation
- Rules: dedicated ports, no +1 increment, kill same service on conflict
- Add 'luzia port' CLI commands (list/check/allocate/release/suggest)
- Add .gitignore for __pycache__

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
admin
2026-01-14 12:33:33 -03:00
parent b2a0dec79b
commit 45ec8828a1
3 changed files with 536 additions and 0 deletions

View File

@@ -4970,6 +4970,8 @@ class Router:
(self._match_telegram, self._route_telegram, "Telegram notifications"),
# Service management (for cockpits)
(self._match_service, self._route_service, "Service management"),
# Port management
(self._match_port, self._route_port, "Port allocation"),
# Watchdog (Task monitoring)
(self._match_watchdog, self._route_watchdog, "Task watchdog"),
(self._match_project_task, route_project_task, "Project task"),
@@ -5394,6 +5396,91 @@ class Router:
print(f"Unknown service command: {cmd}")
return 1
def _match_port(self, args: list) -> Optional[list]:
if args and args[0] == "port":
return args[1:]
return None
def _route_port(self, config: dict, args: list, kwargs: dict) -> int:
"""Handler: luzia port [list|check|allocate|release|suggest]"""
try:
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
from port_manager import (
list_allocations, check_port, allocate_port,
release_port, suggest_port, PORT_RANGES
)
except ImportError as e:
print(f"Error: Port manager not available: {e}")
return 1
if not args:
print("Port Management Commands:")
print(" luzia port list - List all port allocations")
print(" luzia port check <port> - Check what's using a port")
print(" luzia port allocate <service> [-p PORT] - Allocate port to service")
print(" luzia port release <service> - Release port allocation")
print(" luzia port suggest <type> - Suggest available port")
print(f" Types: {', '.join(PORT_RANGES.keys())}")
return 0
cmd = args[0]
if cmd == "list":
print(list_allocations())
return 0
elif cmd == "check":
if len(args) < 2:
print("Usage: luzia port check <port>")
return 1
try:
port = int(args[1])
print(check_port(port))
return 0
except ValueError:
print(f"Invalid port number: {args[1]}")
return 1
elif cmd == "allocate":
if len(args) < 2:
print("Usage: luzia port allocate <service> [--port PORT]")
return 1
service = args[1]
preferred_port = None
if len(args) >= 4 and args[2] in ("-p", "--port"):
try:
preferred_port = int(args[3])
except ValueError:
print(f"Invalid port number: {args[3]}")
return 1
port, msg = allocate_port(service, preferred_port)
print(msg)
return 0 if port > 0 else 1
elif cmd == "release":
if len(args) < 2:
print("Usage: luzia port release <service>")
return 1
print(release_port(args[1]))
return 0
elif cmd == "suggest":
if len(args) < 2:
print(f"Usage: luzia port suggest <type>")
print(f"Types: {', '.join(PORT_RANGES.keys())}")
return 1
port = suggest_port(args[1])
if port > 0:
print(f"Suggested port for {args[1]}: {port}")
return 0
else:
print(f"No available ports in {args[1]} range")
return 1
else:
print(f"Unknown port command: {cmd}")
return 1
def _route_exec(self, config: dict, args: list, kwargs: dict) -> int:
"""Handler: luzia --exec <project> <command>"""
if len(args) < 2: