refactor: improve config and ssh core and add iac integration tests
This commit is contained in:
parent
5d61ef8116
commit
2d9e1bc06e
3 changed files with 94 additions and 29 deletions
|
|
@ -9,17 +9,23 @@ class Config:
|
|||
self.data = self._load()
|
||||
|
||||
def _load(self):
|
||||
if not os.path.exists(self.path):
|
||||
# Fallback to local config if exists
|
||||
if os.path.exists("config.yaml"):
|
||||
self.path = os.path.abspath("config.yaml")
|
||||
else:
|
||||
raise FileNotFoundError(f"Config file not found at {self.path}. Please create it based on config.yaml.example")
|
||||
data = {}
|
||||
if os.path.exists(self.path):
|
||||
with open(self.path, 'r') as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
elif os.path.exists("config.yaml"):
|
||||
self.path = os.path.abspath("config.yaml")
|
||||
with open(self.path, 'r') as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
|
||||
with open(self.path, 'r') as f:
|
||||
return yaml.safe_load(f)
|
||||
return data
|
||||
|
||||
def get(self, key, default=None):
|
||||
# Support ENV overrides: INFRA_PROXMOX_USER -> proxmox.user
|
||||
env_key = "INFRA_" + key.upper().replace('.', '_')
|
||||
if env_key in os.environ:
|
||||
return os.environ[env_key]
|
||||
|
||||
parts = key.split('.')
|
||||
val = self.data
|
||||
for part in parts:
|
||||
|
|
@ -30,18 +36,7 @@ class Config:
|
|||
return val
|
||||
|
||||
def get_node(self, node_name):
|
||||
"""Helper to get proxmox node details by name or default to first if none provided"""
|
||||
nodes = self.get('proxmox.nodes', {})
|
||||
if not nodes:
|
||||
# Fallback for old single-host config if present
|
||||
host = self.get('proxmox.host')
|
||||
if host:
|
||||
return {"host": host, "pass": self.get('proxmox.password')}
|
||||
return None
|
||||
|
||||
if not node_name:
|
||||
# Default to first node found
|
||||
return next(iter(nodes.values()))
|
||||
|
||||
return nodes.get(node_name)
|
||||
|
||||
return nodes.get(node_name)
|
||||
Loading…
Add table
Add a link
Reference in a new issue