Removing WROLPi admin functionality that is now in Controller.
Some checks failed
CodeQL / Analyze (push) Has been cancelled
Controller Tests / controller-tests (push) Has been cancelled
Jest Tests / jest-tests (push) Has been cancelled

This commit is contained in:
Roland 2025-12-19 17:40:43 -07:00
parent 7183cf5e85
commit 78251b067e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
15 changed files with 159 additions and 1332 deletions

View file

@ -9,7 +9,10 @@ import enum
import subprocess
from pathlib import Path
from controller.lib.config import get_config, is_docker_mode
from controller.lib.config import is_docker_mode, get_config_value
# Constants - use controller's own config instead of wrolpi module
DEFAULT_CPU_FREQUENCY = get_config_value('throttle.default_governor', 'ondemand')
class HotspotStatus(enum.Enum):
@ -28,11 +31,13 @@ class GovernorStatus(enum.Enum):
unknown = enum.auto()
def get_current_ssid(interface: str = 'wlan0') -> str | None:
def get_current_ssid(interface: str = None) -> str | None:
"""
Returns the name of the SSID that is currently connected.
Returns None if Hotspot is active, or no Wi-Fi network is being used.
"""
if interface is None:
interface = get_config_value('hotspot.device', 'wlan0')
try:
result = subprocess.run(
['iwgetid', interface, '--raw'],
@ -62,9 +67,7 @@ def get_hotspot_status() -> HotspotStatus:
if is_docker_mode():
return HotspotStatus.unknown
config = get_config()
hotspot_config = config.get("hotspot", {})
device = hotspot_config.get("device", "wlan0")
device = get_config_value('hotspot.device', 'wlan0')
# Check if device is connected to a Wi-Fi network (not hotspot)
if get_current_ssid(device):
@ -111,9 +114,7 @@ def get_hotspot_status_dict() -> dict:
ssid: Optional[str] - Hotspot SSID when enabled
device: Optional[str] - WiFi device name
"""
config = get_config()
hotspot_config = config.get("hotspot", {})
device = hotspot_config.get("device", "wlan0")
device = get_config_value('hotspot.device', 'wlan0')
status = get_hotspot_status()
# Map enum status to enabled/available flags
@ -132,7 +133,7 @@ def get_hotspot_status_dict() -> dict:
"enabled": enabled,
"available": available,
"reason": reason,
"ssid": hotspot_config.get("ssid", "WROLPi") if enabled else None,
"ssid": get_config_value('hotspot.ssid', 'WROLPi') if enabled else None,
"device": device,
}
@ -147,12 +148,9 @@ def enable_hotspot() -> dict:
if is_docker_mode():
return {"success": False, "error": "Not available in Docker mode"}
config = get_config()
hotspot_config = config.get("hotspot", {})
device = hotspot_config.get("device", "wlan0")
ssid = hotspot_config.get("ssid", "WROLPi")
password = hotspot_config.get("password", "wrolpi hotspot")
device = get_config_value('hotspot.device', 'wlan0')
ssid = get_config_value('hotspot.ssid', 'WROLPi')
password = get_config_value('hotspot.password', 'wrolpi hotspot')
try:
# First ensure radio is on
@ -348,8 +346,7 @@ def disable_throttle() -> dict:
if is_docker_mode():
return {"success": False, "error": "Not available in Docker mode"}
config = get_config()
default_governor = config.get("throttle", {}).get("default_governor", "ondemand")
default_governor = DEFAULT_CPU_FREQUENCY
try:
result = subprocess.run(
@ -378,40 +375,68 @@ def disable_throttle() -> dict:
return {"success": False, "error": str(e)}
def shutdown_system() -> dict:
def shutdown_system(delay: int = 10) -> dict:
"""
Shut down the system.
Shut down the system after a delay.
Args:
delay: Seconds to wait before shutdown (default 10). Allows API to return response.
Returns:
dict with success status
"""
import shutil
if is_docker_mode():
return {"success": False, "error": "Cannot shutdown from Docker container"}
try:
subprocess.Popen(["shutdown", "-h", "now"])
return {"success": True, "message": "System shutting down"}
except FileNotFoundError:
# Check if shutdown command exists before starting thread
if not shutil.which("shutdown"):
return {"success": False, "error": "shutdown command not found"}
def _delayed_shutdown():
import time
time.sleep(delay)
subprocess.Popen(["shutdown", "-h", "now"])
try:
import threading
thread = threading.Thread(target=_delayed_shutdown, daemon=True)
thread.start()
return {"success": True, "message": f"System shutting down in {delay} seconds"}
except Exception as e:
return {"success": False, "error": str(e)}
def reboot_system() -> dict:
def reboot_system(delay: int = 10) -> dict:
"""
Reboot the system.
Reboot the system after a delay.
Args:
delay: Seconds to wait before reboot (default 10). Allows API to return response.
Returns:
dict with success status
"""
import shutil
if is_docker_mode():
return {"success": False, "error": "Cannot reboot from Docker container"}
try:
subprocess.Popen(["reboot"])
return {"success": True, "message": "System rebooting"}
except FileNotFoundError:
# Check if reboot command exists before starting thread
if not shutil.which("reboot"):
return {"success": False, "error": "reboot command not found"}
def _delayed_reboot():
import time
time.sleep(delay)
subprocess.Popen(["reboot"])
try:
import threading
thread = threading.Thread(target=_delayed_reboot, daemon=True)
thread.start()
return {"success": True, "message": f"System rebooting in {delay} seconds"}
except Exception as e:
return {"success": False, "error": str(e)}

View file

@ -20,6 +20,7 @@ pyyaml>=6.0
# Docker management (optional, for Docker mode)
docker>=6.0.0
aiohttp==3.12.14
# Testing
pytest>=7.0.0

View file

@ -66,11 +66,38 @@ class TestEnableHotspot:
def test_handles_nmcli_not_found(self):
"""Should handle nmcli not being available."""
with mock.patch("controller.lib.admin.is_docker_mode", return_value=False):
with mock.patch("controller.lib.admin.get_config", return_value={}):
with mock.patch("subprocess.run", side_effect=FileNotFoundError()):
result = enable_hotspot()
assert result["success"] is False
assert "nmcli" in result.get("error", "").lower()
with mock.patch("subprocess.run", side_effect=FileNotFoundError()):
result = enable_hotspot()
assert result["success"] is False
assert "nmcli" in result.get("error", "").lower()
def test_uses_config_values(self):
"""Should use get_config_value for device, ssid, and password."""
with mock.patch("controller.lib.admin.is_docker_mode", return_value=False):
# Mock get_config_value to return custom values
def mock_get_config(key, default=None):
config_map = {
'hotspot.device': 'wlan1',
'hotspot.ssid': 'TestSSID',
'hotspot.password': 'testpassword123',
}
return config_map.get(key, default)
with mock.patch("controller.lib.admin.get_config_value", side_effect=mock_get_config):
mock_run = mock.Mock()
mock_run.returncode = 0
with mock.patch("subprocess.run", return_value=mock_run) as mock_subprocess:
enable_hotspot()
# Verify the hotspot was created with config values
calls = mock_subprocess.call_args_list
# Find the hotspot creation call (nmcli device wifi hotspot)
hotspot_call = [c for c in calls if len(c[0][0]) > 2 and 'hotspot' in c[0][0]]
if hotspot_call:
cmd_args = hotspot_call[0][0][0]
assert "wlan1" in cmd_args
assert "TestSSID" in cmd_args
assert "testpassword123" in cmd_args
class TestDisableHotspot:
@ -165,19 +192,25 @@ class TestShutdownSystem:
def test_handles_shutdown_not_found(self):
"""Should handle shutdown command not being available."""
with mock.patch("controller.lib.admin.is_docker_mode", return_value=False):
with mock.patch("subprocess.Popen", side_effect=FileNotFoundError()):
with mock.patch("shutil.which", return_value=None):
result = shutdown_system()
assert result["success"] is False
assert "not found" in result.get("error", "").lower()
def test_calls_shutdown_command(self):
"""Should call shutdown command with correct args."""
"""Should call shutdown command with correct args after delay."""
with mock.patch("controller.lib.admin.is_docker_mode", return_value=False):
mock_popen = mock.Mock()
with mock.patch("subprocess.Popen", mock_popen):
result = shutdown_system()
mock_popen.assert_called_once_with(["shutdown", "-h", "now"])
assert result["success"] is True
with mock.patch("shutil.which", return_value="/sbin/shutdown"):
mock_popen = mock.Mock()
with mock.patch("subprocess.Popen", mock_popen):
# Use delay=0 for testing to avoid waiting
result = shutdown_system(delay=0)
assert result["success"] is True
assert "shutting down" in result["message"].lower()
# Give thread time to execute
import time
time.sleep(0.1)
mock_popen.assert_called_once_with(["shutdown", "-h", "now"])
class TestRebootSystem:
@ -193,19 +226,25 @@ class TestRebootSystem:
def test_handles_reboot_not_found(self):
"""Should handle reboot command not being available."""
with mock.patch("controller.lib.admin.is_docker_mode", return_value=False):
with mock.patch("subprocess.Popen", side_effect=FileNotFoundError()):
with mock.patch("shutil.which", return_value=None):
result = reboot_system()
assert result["success"] is False
assert "not found" in result.get("error", "").lower()
def test_calls_reboot_command(self):
"""Should call reboot command."""
"""Should call reboot command after delay."""
with mock.patch("controller.lib.admin.is_docker_mode", return_value=False):
mock_popen = mock.Mock()
with mock.patch("subprocess.Popen", mock_popen):
result = reboot_system()
mock_popen.assert_called_once_with(["reboot"])
assert result["success"] is True
with mock.patch("shutil.which", return_value="/sbin/reboot"):
mock_popen = mock.Mock()
with mock.patch("subprocess.Popen", mock_popen):
# Use delay=0 for testing to avoid waiting
result = reboot_system(delay=0)
assert result["success"] is True
assert "rebooting" in result["message"].lower()
# Give thread time to execute
import time
time.sleep(0.1)
mock_popen.assert_called_once_with(["reboot"])
class TestRestartAllServices: