feat(slice-5): implement extended exporters (linux-connector, generic yaml, xml, ini) and --sync mode
This commit is contained in:
1 parent
14a2032c42
commit
869fd241e7
7 files changed
+390
-17
No files matched your search
+66
-16
@@ -1,12 +1,46 @@
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from inventory_collector import __version__
|
||||
from inventory_collector.models import HostRecord, InventoryData
|
||||
from inventory_collector.collectors.static_file import StaticFileCollector
|
||||
from inventory_collector.collectors.proxmox import ProxmoxCollector
|
||||
from inventory_collector.engine import DeduplicationEngine
|
||||
from inventory_collector.exporters.base import BaseExporter
|
||||
from inventory_collector.exporters.ansible_yaml import AnsibleYamlExporter
|
||||
from inventory_collector.exporters.ansible_json import AnsibleJsonExporter
|
||||
from inventory_collector.exporters.linux_connector import LinuxConnectorExporter
|
||||
from inventory_collector.exporters.generic_yaml import GenericYamlExporter
|
||||
from inventory_collector.exporters.xml_exporter import XmlExporter
|
||||
from inventory_collector.exporters.ini_exporter import IniExporter
|
||||
|
||||
def get_exporter(fmt: str) -> BaseExporter:
|
||||
fmt_clean = fmt.lower().strip()
|
||||
if fmt_clean in ("ansible-yaml", "ansible"):
|
||||
return AnsibleYamlExporter()
|
||||
elif fmt_clean in ("ansible-json", "json"):
|
||||
return AnsibleJsonExporter()
|
||||
elif fmt_clean in ("linux-connector", "lc"):
|
||||
return LinuxConnectorExporter()
|
||||
elif fmt_clean in ("yaml", "generic-yaml"):
|
||||
return GenericYamlExporter()
|
||||
elif fmt_clean == "xml":
|
||||
return XmlExporter()
|
||||
elif fmt_clean == "ini":
|
||||
return IniExporter()
|
||||
return AnsibleYamlExporter()
|
||||
|
||||
def infer_format_from_path(path: Path) -> str:
|
||||
ext = path.suffix.lower()
|
||||
if ext == ".xml":
|
||||
return "xml"
|
||||
elif ext == ".ini":
|
||||
return "ini"
|
||||
elif ext == ".json":
|
||||
return "ansible-json"
|
||||
return "ansible-yaml"
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -33,7 +67,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
parser.add_argument(
|
||||
"-f", "--format",
|
||||
choices=["ansible-yaml", "ansible-json", "linux-connector", "yaml", "xml", "ini"],
|
||||
default="ansible-yaml",
|
||||
default=None,
|
||||
help="Export format. Inferred from output file extension if omitted."
|
||||
)
|
||||
parser.add_argument(
|
||||
@@ -63,20 +97,15 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
inv = InventoryData()
|
||||
|
||||
raw_records: List[HostRecord] = []
|
||||
|
||||
# Load from sources if specified
|
||||
# 1. Load from static sources if specified
|
||||
if args.sources:
|
||||
for src in args.sources:
|
||||
collector = StaticFileCollector(src)
|
||||
raw_records.extend(collector.collect())
|
||||
|
||||
# Check for Proxmox environment variables
|
||||
import os
|
||||
from inventory_collector.collectors.proxmox import ProxmoxCollector
|
||||
|
||||
# 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")
|
||||
@@ -92,7 +121,7 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
)
|
||||
raw_records.extend(pve_collector.collect())
|
||||
|
||||
# Tracer bullet fallback if no hosts loaded
|
||||
# 3. Fallback tracer records if completely empty
|
||||
if not raw_records:
|
||||
raw_records.append(HostRecord(
|
||||
name="k3s-master-0",
|
||||
@@ -111,13 +140,14 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
source="tracer"
|
||||
))
|
||||
|
||||
from inventory_collector.engine import DeduplicationEngine
|
||||
# 4. Deduplicate and merge records in memory
|
||||
engine = DeduplicationEngine()
|
||||
inv, warnings = engine.process(raw_records)
|
||||
|
||||
for w in warnings:
|
||||
sys.stderr.write(f"[WARN] {w}\n")
|
||||
|
||||
# 5. Inspection / Dry-run Mode
|
||||
if args.dry_run:
|
||||
sys.stderr.write(f"[INFO] Discovered {len(inv.hosts)} hosts across {len(inv.groups)} groups.\n")
|
||||
print("NAME IP ADDRESS SOURCE GROUPS")
|
||||
@@ -126,17 +156,37 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
print(f"{h.name:<17} {h.ansible_host:<15} {h.source:<9} {groups_str}")
|
||||
return 0
|
||||
|
||||
# Ansible Dynamic Inventory Mode (--list)
|
||||
if args.list or args.format == "ansible-json" or (args.output and args.output.suffix.lower() == ".json"):
|
||||
exporter = AnsibleJsonExporter()
|
||||
else:
|
||||
exporter = AnsibleYamlExporter()
|
||||
# 6. Ansible Dynamic Inventory Mode (--list)
|
||||
if args.list:
|
||||
sys.stdout.write(AnsibleJsonExporter().export(inv))
|
||||
return 0
|
||||
|
||||
# 7. Simultaneous Synchronization Mode (--sync)
|
||||
if args.sync:
|
||||
ansible_dest = Path("inventory/hosts.yaml")
|
||||
lc_dest = 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")
|
||||
|
||||
LinuxConnectorExporter().write_to_file(inv, lc_dest)
|
||||
sys.stderr.write(f"[SUCCESS] Updated linux-connector inventory: {lc_dest}\n")
|
||||
return 0
|
||||
|
||||
# 8. Standard Format Export
|
||||
if args.format:
|
||||
fmt = args.format
|
||||
elif args.output:
|
||||
fmt = infer_format_from_path(args.output)
|
||||
else:
|
||||
fmt = "ansible-yaml"
|
||||
|
||||
exporter = get_exporter(fmt)
|
||||
rendered = exporter.export(inv)
|
||||
|
||||
if args.output:
|
||||
exporter.write_to_file(inv, args.output)
|
||||
sys.stderr.write(f"[SUCCESS] Written inventory to {args.output}\n")
|
||||
sys.stderr.write(f"[SUCCESS] Written {fmt} inventory to {args.output}\n")
|
||||
else:
|
||||
sys.stdout.write(rendered)
|
||||
|
||||
|
||||
Reference in new issue
Block a user