Refactor: Centralize command patterns in single source of truth
CRITICAL: Prevents inconsistent sudo/SSH patterns across codebase. Created command_patterns.py with: - Single source of truth for ALL command execution patterns - SSH key path constant: /var/lib/macha/.ssh/id_ed25519 - Remote user constant: macha - sudo prefix for all remote commands - Helper functions: build_ssh_command(), transform_ssh_command() - Self-validation tests Updated files to use centralized patterns: - tools.py: Uses transform_ssh_command() - remote_monitor.py: Uses build_ssh_command() - system_discovery.py: Uses build_ssh_command() - DESIGN.md: Documents centralized approach Benefits: - Impossible to have inconsistent patterns - Single place to update if needed - Self-documenting with validation tests - Prevents future refactoring errors DO NOT duplicate these patterns in other files - always import.
This commit is contained in:
@@ -9,6 +9,7 @@ import re
|
||||
from typing import Dict, List, Set, Optional, Any
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from command_patterns import build_ssh_command
|
||||
|
||||
|
||||
class SystemDiscovery:
|
||||
@@ -65,12 +66,10 @@ class SystemDiscovery:
|
||||
def detect_os_type(self, hostname: str) -> str:
|
||||
"""Detect the operating system of a remote host via SSH"""
|
||||
try:
|
||||
# Use explicit SSH key path
|
||||
ssh_key = "/var/lib/macha/.ssh/id_ed25519"
|
||||
# Try to detect OS via SSH
|
||||
# Use centralized command pattern - see command_patterns.py
|
||||
ssh_cmd = build_ssh_command(hostname, "cat /etc/os-release", timeout=10)
|
||||
result = subprocess.run(
|
||||
["ssh", "-i", ssh_key, "-o", "ConnectTimeout=5", "-o", "StrictHostKeyChecking=no",
|
||||
hostname, "cat /etc/os-release"],
|
||||
ssh_cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
@@ -96,9 +95,9 @@ class SystemDiscovery:
|
||||
return 'alpine'
|
||||
|
||||
# Try uname for other systems
|
||||
ssh_cmd = build_ssh_command(hostname, "uname -s", timeout=10)
|
||||
result = subprocess.run(
|
||||
["ssh", "-i", ssh_key, "-o", "ConnectTimeout=5", "-o", "StrictHostKeyChecking=no",
|
||||
hostname, "uname -s"],
|
||||
ssh_cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
|
||||
Reference in New Issue
Block a user