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:
19
tools.py
19
tools.py
@@ -8,6 +8,7 @@ import json
|
||||
import os
|
||||
from typing import Dict, Any, List, Optional
|
||||
from pathlib import Path
|
||||
from command_patterns import transform_ssh_command
|
||||
|
||||
|
||||
class SysadminTools:
|
||||
@@ -268,21 +269,9 @@ class SysadminTools:
|
||||
"allowed_commands": self.allowed_commands
|
||||
}
|
||||
|
||||
# Automatically configure SSH commands to use macha user on remote systems
|
||||
# Transform: ssh hostname cmd -> ssh -i /var/lib/macha/.ssh/id_ed25519 macha@hostname sudo cmd
|
||||
if command.strip().startswith('ssh ') and '@' not in command.split()[1]:
|
||||
parts = command.split(maxsplit=2)
|
||||
if len(parts) >= 2:
|
||||
hostname = parts[1]
|
||||
remaining = ' '.join(parts[2:]) if len(parts) > 2 else ''
|
||||
# Always use explicit SSH key path
|
||||
ssh_key = "/var/lib/macha/.ssh/id_ed25519"
|
||||
ssh_opts = f"-i {ssh_key} -o StrictHostKeyChecking=no"
|
||||
# If there's a command to run remotely, prefix it with sudo
|
||||
if remaining:
|
||||
command = f"ssh {ssh_opts} macha@{hostname} sudo {remaining}".strip()
|
||||
else:
|
||||
command = f"ssh {ssh_opts} macha@{hostname}".strip()
|
||||
# Automatically configure SSH commands using centralized command_patterns
|
||||
# See command_patterns.py for the single source of truth
|
||||
command = transform_ssh_command(command)
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
|
||||
Reference in New Issue
Block a user