feat(mesh): integrate MeshPort/EasyTier P2P mesh network with NetworkManager auto-connect
Offline project checks / check (push) Canceled after 0s
Offline project checks / check (push) Canceled after 0s
- Add platina-mesh CLI (scripts/phone-mesh.py) for status inspection, control, and JSON telemetry. - Add OpenRC service (scripts/phone-mesh.openrc) for managing easytier-core daemon. - Add NetworkManager dispatcher hook (scripts/phone-mesh-dispatcher.sh) to automatically start mesh when external default route is active and stop when disconnected. - Add default mesh configuration (scripts/easytier.conf) with node identity Platina (192.168.20.10). - Integrate mesh runtime into image overlay builder (scripts/image-runtime.py). - Add unit tests for mesh control and image runtime integration (142 tests passing).
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Platina Linux EasyTier P2P Mesh Network Configuration
|
||||
hostname = "Platina"
|
||||
instance_name = "platina-phone"
|
||||
ipv4 = "192.168.20.10/24"
|
||||
dhcp = false
|
||||
listeners = [
|
||||
"tcp://0.0.0.0:52015",
|
||||
"udp://0.0.0.0:52015",
|
||||
]
|
||||
|
||||
[network_identity]
|
||||
network_name = "UzeaThJooQn1ylehvb5"
|
||||
network_secret = "2NWf05N815cL1zKp09%"
|
||||
|
||||
[[peer]]
|
||||
uri = "tcp://easytier.slcydia.fun:52015"
|
||||
|
||||
[flags]
|
||||
latency_first = true
|
||||
@@ -27,9 +27,13 @@ HELPERS = {
|
||||
'scripts/phone-usb-power.py': 'usr/local/sbin/platina-usb-power',
|
||||
'scripts/phone-usb-power-dispatcher.sh': 'etc/NetworkManager/dispatcher.d/90-platina-usb-power',
|
||||
'scripts/phone-cellular-setup.py': 'usr/local/sbin/platina-cellular',
|
||||
'scripts/phone-mesh.py': 'usr/local/sbin/platina-mesh',
|
||||
'scripts/phone-mesh.openrc': 'etc/init.d/easytier',
|
||||
'scripts/phone-mesh-dispatcher.sh': 'etc/NetworkManager/dispatcher.d/95-platina-mesh',
|
||||
'scripts/image-runtime.py': 'usr/local/libexec/platina-image-runtime',
|
||||
}
|
||||
CONFIG = 'etc/conf.d/platina-power-chord'
|
||||
MESH_CONFIG = 'etc/easytier/easytier.conf'
|
||||
|
||||
|
||||
def layout(version):
|
||||
@@ -38,18 +42,21 @@ def layout(version):
|
||||
base = 'usr/local/share/platina/releases/' + version
|
||||
modes = {name: 0o755 for name in HELPERS.values()}
|
||||
modes[CONFIG] = 0o644
|
||||
modes[MESH_CONFIG] = 0o644
|
||||
for sub in ('', 'poweroff/'):
|
||||
for name in ('release.json', 'version', 'frame.bgrx.gz'):
|
||||
modes[base + '/' + sub + name] = 0o644
|
||||
links = {'usr/local/share/platina/current': 'releases/' + version,
|
||||
'etc/platina-release': '../usr/local/share/platina/current/version',
|
||||
'etc/runlevels/default/platina-power-chord': '../../init.d/platina-power-chord',
|
||||
'etc/runlevels/default/platina-battery-guard': '../../init.d/platina-battery-guard'}
|
||||
'etc/runlevels/default/platina-battery-guard': '../../init.d/platina-battery-guard',
|
||||
'etc/runlevels/default/easytier': '../../init.d/easytier'}
|
||||
return base, modes, links
|
||||
|
||||
|
||||
def source_inputs():
|
||||
names = list(HELPERS) + ['scripts/phone-power-chord.conf',
|
||||
'scripts/easytier.conf',
|
||||
'scripts/platina-release.py', 'project/release.json',
|
||||
'project/branding/platina-linux-logo.png',
|
||||
'project/branding/provenance.json']
|
||||
@@ -138,6 +145,9 @@ def prepare(output):
|
||||
dest = stage / CONFIG
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_bytes(conf.replace(b'dry_run=yes\n', b'dry_run=no\n'))
|
||||
dest_mesh = stage / MESH_CONFIG
|
||||
dest_mesh.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(ROOT / 'scripts/easytier.conf', dest_mesh)
|
||||
for sub in ('', 'poweroff/'):
|
||||
for name in ('release.json', 'version', 'frame.bgrx.gz'):
|
||||
dest = stage / base / sub / name
|
||||
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/bin/sh
|
||||
# Platina Linux MeshPort / EasyTier auto-connect dispatcher
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
export PATH
|
||||
|
||||
interface="${1:-}"
|
||||
action="${2:-}"
|
||||
|
||||
# Ignore virtual tun/tap/dummy devices created by mesh networking
|
||||
case "$interface" in
|
||||
lo|tun*|tap*|dummy*) exit 0 ;;
|
||||
esac
|
||||
|
||||
case "$action" in
|
||||
up|dhcp4-change|reapply)
|
||||
# Check if an active default route to the external network exists
|
||||
if ip route show default 2>/dev/null | grep -q 'default via'; then
|
||||
if [ -x /usr/local/sbin/platina-mesh ]; then
|
||||
/usr/local/sbin/platina-mesh --auto-up >/dev/null 2>&1 || true
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
down)
|
||||
# If no default route remains, stop mesh service to conserve battery
|
||||
if ! ip route show default 2>/dev/null | grep -q 'default via'; then
|
||||
if [ -x /usr/local/sbin/platina-mesh ]; then
|
||||
/usr/local/sbin/platina-mesh --auto-down >/dev/null 2>&1 || true
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/sbin/openrc-run
|
||||
description="EasyTier P2P Mesh Network Service"
|
||||
|
||||
command="/usr/local/bin/easytier-core"
|
||||
command_args="-c /etc/easytier/easytier.conf"
|
||||
command_background="yes"
|
||||
pidfile="/run/easytier.pid"
|
||||
output_log="/var/log/easytier.log"
|
||||
error_log="/var/log/easytier.log"
|
||||
|
||||
depend() {
|
||||
need localmount
|
||||
after net
|
||||
}
|
||||
|
||||
start_pre() {
|
||||
if [ ! -c /dev/net/tun ]; then
|
||||
modprobe tun >/dev/null 2>&1 || true
|
||||
fi
|
||||
if [ ! -r /etc/easytier/easytier.conf ]; then
|
||||
eerror "EasyTier configuration file /etc/easytier/easytier.conf not found"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
Executable
+148
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Platina Linux EasyTier P2P Mesh Network Management."""
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
CONFIG_FILE = '/etc/easytier/easytier.conf'
|
||||
SERVICE_NAME = 'easytier'
|
||||
|
||||
|
||||
def is_service_running(executor=subprocess.run):
|
||||
"""Check if easytier service is currently running."""
|
||||
try:
|
||||
proc = executor(['rc-service', SERVICE_NAME, 'status'],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
return proc.returncode == 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def get_mesh_status(executor=subprocess.run, config_file=CONFIG_FILE, cli_path=None):
|
||||
"""Retrieve full status of mesh node and peers via easytier-cli."""
|
||||
running = is_service_running(executor=executor)
|
||||
status = {
|
||||
'service_running': running,
|
||||
'config_present': Path(config_file).is_file(),
|
||||
'node': None,
|
||||
'peers': [],
|
||||
'error': None
|
||||
}
|
||||
if not running:
|
||||
return status
|
||||
|
||||
resolved_cli = cli_path or shutil.which('easytier-cli') or '/usr/local/bin/easytier-cli'
|
||||
if executor is subprocess.run and not Path(resolved_cli).is_file():
|
||||
status['error'] = 'easytier-cli binary not found'
|
||||
return status
|
||||
|
||||
# Query node details
|
||||
try:
|
||||
proc_node = executor([resolved_cli, '-o', 'json', 'node'],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=5)
|
||||
if proc_node.returncode == 0:
|
||||
status['node'] = json.loads(proc_node.stdout)
|
||||
except Exception as exc:
|
||||
status['error'] = f'Failed to query node info: {exc}'
|
||||
|
||||
# Query peers list
|
||||
try:
|
||||
proc_peer = executor([resolved_cli, '-o', 'json', 'peer'],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=5)
|
||||
if proc_peer.returncode == 0:
|
||||
status['peers'] = json.loads(proc_peer.stdout)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def toggle_service(action, executor=subprocess.run):
|
||||
"""Execute rc-service action (start/stop/restart)."""
|
||||
cmd = ['rc-service', SERVICE_NAME, action]
|
||||
proc = executor(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
return proc.returncode == 0
|
||||
|
||||
|
||||
def auto_up(executor=subprocess.run, checker=is_service_running, config_file=CONFIG_FILE):
|
||||
"""Invoked on network interface up with default route: start if not running."""
|
||||
if not checker(executor=executor):
|
||||
if Path(config_file).is_file():
|
||||
return toggle_service('start', executor=executor)
|
||||
return True
|
||||
|
||||
|
||||
def auto_down(executor=subprocess.run, checker=is_service_running):
|
||||
"""Invoked on network interface down when no default route: stop service."""
|
||||
if checker(executor=executor):
|
||||
return toggle_service('stop', executor=executor)
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--status', action='store_true', help='Show mesh network status')
|
||||
parser.add_argument('--up', action='store_true', help='Start mesh networking service')
|
||||
parser.add_argument('--down', action='store_true', help='Stop mesh networking service')
|
||||
parser.add_argument('--restart', action='store_true', help='Restart mesh networking service')
|
||||
parser.add_argument('--auto-up', action='store_true', help='Auto-start when default network is up')
|
||||
parser.add_argument('--auto-down', action='store_true', help='Auto-stop when default network is down')
|
||||
parser.add_argument('--json', action='store_true', help='Output in JSON format')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.auto_up:
|
||||
success = auto_up()
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
if args.auto_down:
|
||||
success = auto_down()
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
if args.up:
|
||||
success = toggle_service('start')
|
||||
print("Mesh service started" if success else "Failed to start mesh service")
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
if args.down:
|
||||
success = toggle_service('stop')
|
||||
print("Mesh service stopped" if success else "Failed to stop mesh service")
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
if args.restart:
|
||||
success = toggle_service('restart')
|
||||
print("Mesh service restarted" if success else "Failed to restart mesh service")
|
||||
sys.exit(0 if success else 1)
|
||||
|
||||
# Default to status if no other action
|
||||
status = get_mesh_status()
|
||||
if args.json:
|
||||
print(json.dumps(status, indent=2))
|
||||
else:
|
||||
if not status['service_running']:
|
||||
print("Mesh Status: Service is STOPPED")
|
||||
else:
|
||||
node = status.get('node') or {}
|
||||
vip = node.get('ipv4_addr', 'N/A')
|
||||
hostname = node.get('hostname', 'N/A')
|
||||
peer_id = node.get('peer_id', 'N/A')
|
||||
print("Mesh Status: RUNNING")
|
||||
print(f"Hostname: {hostname}")
|
||||
print(f"Virtual IP: {vip}")
|
||||
print(f"Peer ID: {peer_id}")
|
||||
peers = status.get('peers') or []
|
||||
print(f"Peers Count: {len(peers)}")
|
||||
for p in peers:
|
||||
p_host = p.get('hostname', 'unknown')
|
||||
p_ip = p.get('ipv4', 'unknown')
|
||||
p_tun = p.get('tunnel', 'unknown')
|
||||
p_cost = p.get('cost', 'unknown')
|
||||
print(f" * {p_host:<12} {p_ip:<18} [{p_tun}, {p_cost}]")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -46,7 +46,12 @@ class ImageRuntimeTests(unittest.TestCase):
|
||||
files = {str(p.relative_to(self.root)) for p in self.root.rglob('*') if p.is_file()}
|
||||
self.assertFalse(any('usb-off' in name or name.endswith('.ko') for name in files))
|
||||
self.assertTrue((self.root / 'etc/NetworkManager/dispatcher.d/90-platina-usb-power').is_file())
|
||||
self.assertTrue((self.root / 'etc/NetworkManager/dispatcher.d/95-platina-mesh').is_file())
|
||||
self.assertTrue((self.root / 'usr/local/sbin/platina-cellular').is_file())
|
||||
self.assertTrue((self.root / 'usr/local/sbin/platina-mesh').is_file())
|
||||
self.assertTrue((self.root / 'etc/init.d/easytier').is_file())
|
||||
self.assertTrue((self.root / 'etc/easytier/easytier.conf').is_file())
|
||||
self.assertTrue((self.root / 'etc/runlevels/default/easytier').is_file())
|
||||
self.assertFalse((self.root / 'etc/platina/usb-power.json').exists())
|
||||
|
||||
def test_missing_shutdown_helper_is_rejected(self):
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
"""Verify Platina Linux MeshPort / EasyTier auto-connect and control logic."""
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
spec = importlib.util.spec_from_file_location('mesh_setup', ROOT / 'scripts/phone-mesh.py')
|
||||
mesh = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mesh)
|
||||
|
||||
SAMPLE_NODE_JSON = {
|
||||
"peer_id": 1697707457,
|
||||
"ipv4_addr": "192.168.20.10/24",
|
||||
"hostname": "Platina"
|
||||
}
|
||||
|
||||
SAMPLE_PEER_JSON = [
|
||||
{
|
||||
"ipv4": "192.168.20.1",
|
||||
"hostname": "DIG-US",
|
||||
"cost": "p2p",
|
||||
"tunnel": "tcp"
|
||||
},
|
||||
{
|
||||
"ipv4": "192.168.20.2",
|
||||
"hostname": "ORA-JP",
|
||||
"cost": "p2p",
|
||||
"tunnel": "tcp"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
class MeshSetupTests(unittest.TestCase):
|
||||
def test_is_service_running(self):
|
||||
def mock_running(cmd, **kwargs):
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout='started\n', stderr='')
|
||||
|
||||
def mock_stopped(cmd, **kwargs):
|
||||
return subprocess.CompletedProcess(cmd, 1, stdout='stopped\n', stderr='')
|
||||
|
||||
self.assertTrue(mesh.is_service_running(executor=mock_running))
|
||||
self.assertFalse(mesh.is_service_running(executor=mock_stopped))
|
||||
|
||||
def test_get_mesh_status_stopped(self):
|
||||
def mock_stopped(cmd, **kwargs):
|
||||
return subprocess.CompletedProcess(cmd, 1)
|
||||
|
||||
status = mesh.get_mesh_status(executor=mock_stopped)
|
||||
self.assertFalse(status['service_running'])
|
||||
self.assertIsNone(status['node'])
|
||||
self.assertEqual(status['peers'], [])
|
||||
|
||||
def test_get_mesh_status_running(self):
|
||||
def mock_executor(cmd, **kwargs):
|
||||
cmd_str = ' '.join(cmd)
|
||||
if 'status' in cmd_str:
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
if 'node' in cmd_str:
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(SAMPLE_NODE_JSON), stderr='')
|
||||
if 'peer' in cmd_str:
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(SAMPLE_PEER_JSON), stderr='')
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
with tempfile.NamedTemporaryFile() as tmp_conf:
|
||||
status = mesh.get_mesh_status(executor=mock_executor, config_file=tmp_conf.name)
|
||||
self.assertTrue(status['service_running'])
|
||||
self.assertTrue(status['config_present'])
|
||||
self.assertEqual(status['node']['hostname'], 'Platina')
|
||||
self.assertEqual(status['node']['ipv4_addr'], '192.168.20.10/24')
|
||||
self.assertEqual(len(status['peers']), 2)
|
||||
|
||||
def test_toggle_service(self):
|
||||
recorded = []
|
||||
|
||||
def mock_executor(cmd, **kwargs):
|
||||
recorded.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
self.assertTrue(mesh.toggle_service('start', executor=mock_executor))
|
||||
self.assertEqual(recorded[-1], ['rc-service', 'easytier', 'start'])
|
||||
|
||||
self.assertTrue(mesh.toggle_service('stop', executor=mock_executor))
|
||||
self.assertEqual(recorded[-1], ['rc-service', 'easytier', 'stop'])
|
||||
|
||||
def test_auto_up_starts_service_when_stopped(self):
|
||||
recorded = []
|
||||
|
||||
def mock_executor(cmd, **kwargs):
|
||||
recorded.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
with tempfile.NamedTemporaryFile() as tmp_conf:
|
||||
res = mesh.auto_up(executor=mock_executor, checker=lambda **kw: False, config_file=tmp_conf.name)
|
||||
self.assertTrue(res)
|
||||
self.assertIn(['rc-service', 'easytier', 'start'], recorded)
|
||||
|
||||
def test_auto_up_skips_when_already_running(self):
|
||||
recorded = []
|
||||
|
||||
def mock_executor(cmd, **kwargs):
|
||||
recorded.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
with tempfile.NamedTemporaryFile() as tmp_conf:
|
||||
res = mesh.auto_up(executor=mock_executor, checker=lambda **kw: True, config_file=tmp_conf.name)
|
||||
self.assertTrue(res)
|
||||
self.assertEqual(recorded, [])
|
||||
|
||||
def test_auto_down_stops_service_when_running(self):
|
||||
recorded = []
|
||||
|
||||
def mock_executor(cmd, **kwargs):
|
||||
recorded.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
res = mesh.auto_down(executor=mock_executor, checker=lambda **kw: True)
|
||||
self.assertTrue(res)
|
||||
self.assertIn(['rc-service', 'easytier', 'stop'], recorded)
|
||||
|
||||
def test_auto_down_skips_when_already_stopped(self):
|
||||
recorded = []
|
||||
|
||||
def mock_executor(cmd, **kwargs):
|
||||
recorded.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
res = mesh.auto_down(executor=mock_executor, checker=lambda **kw: False)
|
||||
self.assertTrue(res)
|
||||
self.assertEqual(recorded, [])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user