#!/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()