This repository was archived by the owner on Jul 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmachine_handlers.py
More file actions
111 lines (90 loc) · 3.38 KB
/
Copy pathmachine_handlers.py
File metadata and controls
111 lines (90 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import logging
import os
from typing import Dict, Optional, Any
from ssh_utils import establish_ssh_connection, cleanup_ssh_key
import config
logger = logging.getLogger(__name__)
async def check_machine_discoverable(
machine_spec: Dict[str, Any],
body: Optional[Dict[str, Any]] = None,
machine_name: Optional[str] = None,
namespace: Optional[str] = None,
) -> bool:
"""Check machine availability via SSH with support for key, password, and no authentication"""
conn, ssh_key_temp_file = await establish_ssh_connection(
machine_spec, body, machine_name, namespace
)
if not conn:
return False
try:
# Simple command to check availability
result = await conn.run('echo "machine_available"', check=True)
return result.stdout.strip() == "machine_available"
except Exception as e:
logger.warning(
f"Machine {machine_spec.get('hostname')} availability check failed: {e}"
)
return False
finally:
conn.close()
await conn.wait_closed()
cleanup_ssh_key(ssh_key_temp_file)
async def scan_machine_hardware(
machine_spec: Dict[str, Any],
body: Optional[Dict[str, Any]] = None,
machine_name: Optional[str] = None,
namespace: Optional[str] = None,
) -> Dict[str, Any]:
"""Scan machine hardware and return facts"""
conn, ssh_key_temp_file = await establish_ssh_connection(
machine_spec, body, machine_name, namespace
)
if not conn:
logger.warning(
f"Failed to connect to machine {machine_spec.get('hostname')} for hardware scan"
)
return {}
try:
# Transfer scan script via SCP
scanner_path = os.path.join(
os.path.dirname(__file__), "scripts", "hardware_scanner.sh"
)
if not os.path.exists(scanner_path):
logger.error(f"Hardware scanner script not found at {scanner_path}")
return {}
# Read script content
with open(scanner_path, "r") as f:
scanner_content = f.read()
# Create temporary file on remote machine
remote_script_path = config.REMOTE_HARDWARE_SCRIPT_PATH
# Transfer script via SCP
async with conn.start_sftp_client() as sftp:
async with sftp.open(remote_script_path, "w") as remote_file:
await remote_file.write(scanner_content)
# Make script executable and run it
await conn.run(f"chmod +x {remote_script_path}", check=True)
result = await conn.run(f"{remote_script_path}", check=True)
# Get raw scanner output
facts_output = result.stdout.strip()
if not facts_output:
logger.warning("Hardware scanner returned empty output")
return {}
# Parse result locally
from scripts.facts_parser import parse_facts
# Split output into lines and parse
lines = facts_output.split("\n")
hardware_facts = parse_facts(lines)
logger.info(
f"Successfully scanned hardware for machine {machine_spec['hostname']}"
)
return hardware_facts
except Exception as e:
logger.warning(
f"Failed to scan hardware for machine {machine_spec.get('hostname')}: {e}"
)
return {}
finally:
conn.close()
await conn.wait_closed()
cleanup_ssh_key(ssh_key_temp_file)