feat(slice-6): implement config loader, CLI check mode, comprehensive documentation, and shell completions
This commit is contained in:
1 parent
869fd241e7
commit
e5784db650
9 files changed
+616
-29
No files matched your search
+33
-15
@@ -97,19 +97,35 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
from inventory_collector.config import ConfigLoader
|
||||
app_config = ConfigLoader.load(args.config)
|
||||
|
||||
raw_records: List[HostRecord] = []
|
||||
|
||||
# 1. Load from static sources if specified
|
||||
if args.sources:
|
||||
for src in args.sources:
|
||||
collector = StaticFileCollector(src)
|
||||
raw_records.extend(collector.collect())
|
||||
# 1. Load from static sources (CLI arguments take precedence, then config files)
|
||||
sources = list(args.sources or [])
|
||||
if not sources and app_config.static_files:
|
||||
for sf in app_config.static_files:
|
||||
sources.append(sf.path)
|
||||
|
||||
# 2. Check for Proxmox environment variables
|
||||
pve_url = os.getenv("PROXMOX_URL") or os.getenv("ANSIBLE_PROXMOX_URL")
|
||||
pve_token_id = os.getenv("PROXMOX_TOKEN_ID") or os.getenv("ANSIBLE_PROXMOX_TOKEN_ID")
|
||||
pve_token_secret = os.getenv("PROXMOX_TOKEN_SECRET") or os.getenv("ANSIBLE_PROXMOX_TOKEN_SECRET")
|
||||
verify_ssl = os.getenv("PROXMOX_VERIFY_SSL", "false").lower() in ("true", "1", "yes")
|
||||
for src in sources:
|
||||
collector = StaticFileCollector(
|
||||
src,
|
||||
default_user=app_config.default_ssh_user,
|
||||
default_port=app_config.default_ssh_port
|
||||
)
|
||||
raw_records.extend(collector.collect())
|
||||
|
||||
# 2. Check for Proxmox credentials (env vars take precedence, then config)
|
||||
pve_url = os.getenv("PROXMOX_URL") or os.getenv("ANSIBLE_PROXMOX_URL") or (app_config.proxmox.url if app_config.proxmox.enabled else "")
|
||||
pve_token_id = os.getenv("PROXMOX_TOKEN_ID") or os.getenv("ANSIBLE_PROXMOX_TOKEN_ID") or app_config.proxmox.token_id
|
||||
pve_token_secret = os.getenv("PROXMOX_TOKEN_SECRET") or os.getenv("ANSIBLE_PROXMOX_TOKEN_SECRET") or app_config.proxmox.token_secret
|
||||
|
||||
verify_ssl_env = os.getenv("PROXMOX_VERIFY_SSL")
|
||||
if verify_ssl_env is not None:
|
||||
verify_ssl = verify_ssl_env.lower() in ("true", "1", "yes")
|
||||
else:
|
||||
verify_ssl = app_config.proxmox.verify_ssl
|
||||
|
||||
if pve_url and pve_token_id and pve_token_secret:
|
||||
sys.stderr.write(f"[INFO] Querying Proxmox VE API at {pve_url}...\n")
|
||||
@@ -117,7 +133,9 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
url=pve_url,
|
||||
token_id=pve_token_id,
|
||||
token_secret=pve_token_secret,
|
||||
verify_ssl=verify_ssl
|
||||
verify_ssl=verify_ssl,
|
||||
timeout=app_config.proxmox.timeout,
|
||||
default_user=app_config.default_ssh_user
|
||||
)
|
||||
raw_records.extend(pve_collector.collect())
|
||||
|
||||
@@ -126,7 +144,7 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
raw_records.append(HostRecord(
|
||||
name="k3s-master-0",
|
||||
ansible_host="192.168.5.70",
|
||||
ansible_user="lain",
|
||||
ansible_user=app_config.default_ssh_user,
|
||||
groups=["k3s_master", "k3s_cluster"],
|
||||
tags=["control-plane", "proxmox"],
|
||||
source="tracer"
|
||||
@@ -141,7 +159,7 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
))
|
||||
|
||||
# 4. Deduplicate and merge records in memory
|
||||
engine = DeduplicationEngine()
|
||||
engine = DeduplicationEngine(conflict_policy=app_config.conflict_policy)
|
||||
inv, warnings = engine.process(raw_records)
|
||||
|
||||
for w in warnings:
|
||||
@@ -163,8 +181,8 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
|
||||
# 7. Simultaneous Synchronization Mode (--sync)
|
||||
if args.sync:
|
||||
ansible_dest = Path("inventory/hosts.yaml")
|
||||
lc_dest = Path("~/.config/linux-connector/hosts.yaml").expanduser()
|
||||
ansible_dest = app_config.output_ansible or Path("inventory/hosts.yaml")
|
||||
lc_dest = (app_config.output_linux_connector or Path("~/.config/linux-connector/hosts.yaml")).expanduser()
|
||||
|
||||
AnsibleYamlExporter().write_to_file(inv, ansible_dest)
|
||||
sys.stderr.write(f"[SUCCESS] Updated Ansible inventory: {ansible_dest}\n")
|
||||
|
||||
Reference in new issue
Block a user