netbox-opennebula-sync 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. netbox_opennebula_sync/__init__.py +27 -0
  2. netbox_opennebula_sync/bridge.py +49 -0
  3. netbox_opennebula_sync/cli.py +127 -0
  4. netbox_opennebula_sync/config.py +200 -0
  5. netbox_opennebula_sync/crypto_util.py +29 -0
  6. netbox_opennebula_sync/django_store.py +419 -0
  7. netbox_opennebula_sync/forms.py +67 -0
  8. netbox_opennebula_sync/inventory.py +71 -0
  9. netbox_opennebula_sync/jobs.py +61 -0
  10. netbox_opennebula_sync/management/__init__.py +0 -0
  11. netbox_opennebula_sync/management/commands/__init__.py +0 -0
  12. netbox_opennebula_sync/management/commands/opennebula_sync.py +48 -0
  13. netbox_opennebula_sync/migrations/0001_initial.py +62 -0
  14. netbox_opennebula_sync/migrations/0002_site_cluster_fk.py +57 -0
  15. netbox_opennebula_sync/migrations/0003_remove_netbox_http_creds.py +26 -0
  16. netbox_opennebula_sync/migrations/__init__.py +0 -0
  17. netbox_opennebula_sync/models.py +115 -0
  18. netbox_opennebula_sync/navigation.py +23 -0
  19. netbox_opennebula_sync/netbox.py +213 -0
  20. netbox_opennebula_sync/opennebula.py +341 -0
  21. netbox_opennebula_sync/reconciler.py +734 -0
  22. netbox_opennebula_sync/rest_store.py +7 -0
  23. netbox_opennebula_sync/snmp.py +107 -0
  24. netbox_opennebula_sync/status.py +80 -0
  25. netbox_opennebula_sync/store.py +46 -0
  26. netbox_opennebula_sync/templates/netbox_opennebula_sync/settings.html +38 -0
  27. netbox_opennebula_sync/templates/netbox_opennebula_sync/status.html +74 -0
  28. netbox_opennebula_sync/urls.py +8 -0
  29. netbox_opennebula_sync/views.py +81 -0
  30. netbox_opennebula_sync-0.3.0.dist-info/METADATA +141 -0
  31. netbox_opennebula_sync-0.3.0.dist-info/RECORD +35 -0
  32. netbox_opennebula_sync-0.3.0.dist-info/WHEEL +5 -0
  33. netbox_opennebula_sync-0.3.0.dist-info/entry_points.txt +2 -0
  34. netbox_opennebula_sync-0.3.0.dist-info/licenses/LICENSE +190 -0
  35. netbox_opennebula_sync-0.3.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,27 @@
1
+ """Synchronize OpenNebula inventory into NetBox Community (plugin + CLI)."""
2
+
3
+ __version__ = "0.3.0"
4
+
5
+ try:
6
+ from netbox.plugins import PluginConfig
7
+ except ImportError: # Standalone CLI / unit tests without NetBox
8
+ config = None
9
+ else:
10
+
11
+ class OpenNebulaSyncConfig(PluginConfig):
12
+ name = "netbox_opennebula_sync"
13
+ verbose_name = "OpenNebula Sync"
14
+ description = "Discover and synchronize OpenNebula infrastructure into NetBox"
15
+ version = __version__
16
+ author = "NetBox OpenNebula Sync contributors"
17
+ author_email = "dev@localhost"
18
+ base_url = "opennebula"
19
+ min_version = "4.2"
20
+ default_settings = {}
21
+ required_settings = []
22
+
23
+ def ready(self):
24
+ super().ready()
25
+ from . import jobs # noqa: F401
26
+
27
+ config = OpenNebulaSyncConfig
@@ -0,0 +1,49 @@
1
+ """Build standalone AppConfig from the Django plugin config model."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from .config import (
6
+ DEFAULT_EXCLUDE_INTERFACES,
7
+ AppConfig,
8
+ NetBoxConfig,
9
+ OpenNebulaConfig,
10
+ SnmpConfig,
11
+ SyncConfig,
12
+ )
13
+ from .models import OpenNebulaSyncConfig
14
+
15
+
16
+ def app_config_from_db(cfg: OpenNebulaSyncConfig) -> AppConfig:
17
+ """Map plugin settings to AppConfig (no NetBox HTTP credentials)."""
18
+ one_token = cfg.get_one_token()
19
+ if not cfg.one_endpoint or not cfg.one_username or not one_token:
20
+ raise RuntimeError("OpenNebula endpoint/username/token are required")
21
+ ignore = cfg.ignore_hostnames_list()
22
+ return AppConfig(
23
+ netbox=NetBoxConfig(
24
+ site=(cfg.site.name if cfg.site_id else "default"),
25
+ site_id=cfg.site_id,
26
+ cluster_name=(cfg.cluster.name if cfg.cluster_id else None),
27
+ cluster_id=cfg.cluster_id,
28
+ create_missing_hosts=cfg.create_missing_hosts,
29
+ dns_suffix=(cfg.dns_suffix or "").lstrip("."),
30
+ timeout=float(cfg.one_timeout or 30),
31
+ ),
32
+ opennebula=OpenNebulaConfig(
33
+ endpoint=cfg.one_endpoint,
34
+ username=cfg.one_username,
35
+ token=one_token,
36
+ timeout=float(cfg.one_timeout or 30),
37
+ ),
38
+ snmp=SnmpConfig(
39
+ enabled=cfg.snmp_enabled,
40
+ community=cfg.snmp_community or "public",
41
+ ignore_hostnames=ignore,
42
+ exclude_interfaces=cfg.snmp_exclude_interfaces or DEFAULT_EXCLUDE_INTERFACES,
43
+ ),
44
+ sync=SyncConfig(
45
+ orphan_policy=cfg.orphan_policy or "mark_offline",
46
+ interval=int(cfg.sync_interval or 15) * 60,
47
+ dry_run=cfg.dry_run,
48
+ ),
49
+ )
@@ -0,0 +1,127 @@
1
+ """Command-line interface for netbox-opennebula-sync."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import logging
7
+ import signal
8
+ import sys
9
+ import time
10
+ from datetime import UTC
11
+ from pathlib import Path
12
+
13
+ from . import __version__
14
+ from .config import load_config
15
+ from .reconciler import run_bootstrap, run_sync
16
+
17
+
18
+ def _setup_logging(verbose: bool, json_logs: bool) -> None:
19
+ level = logging.DEBUG if verbose else logging.INFO
20
+ if json_logs:
21
+ class JsonFormatter(logging.Formatter):
22
+ def format(self, record: logging.LogRecord) -> str:
23
+ import json
24
+ from datetime import datetime
25
+
26
+ return json.dumps(
27
+ {
28
+ "ts": datetime.now(UTC).isoformat(),
29
+ "level": record.levelname,
30
+ "logger": record.name,
31
+ "msg": record.getMessage(),
32
+ },
33
+ ensure_ascii=False,
34
+ )
35
+
36
+ handler = logging.StreamHandler(sys.stdout)
37
+ handler.setFormatter(JsonFormatter())
38
+ logging.root.handlers = [handler]
39
+ logging.root.setLevel(level)
40
+ else:
41
+ logging.basicConfig(
42
+ level=level,
43
+ format="%(asctime)s %(levelname)s %(name)s: %(message)s",
44
+ )
45
+
46
+
47
+ def main(argv: list[str] | None = None) -> int:
48
+ parser = argparse.ArgumentParser(
49
+ prog="netbox-opennebula-sync",
50
+ description="Discover and synchronize OpenNebula inventory into NetBox Community",
51
+ )
52
+ parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
53
+ parser.add_argument(
54
+ "-c",
55
+ "--config",
56
+ help="Path to YAML configuration file",
57
+ default=None,
58
+ )
59
+ parser.add_argument("-v", "--verbose", action="store_true")
60
+ parser.add_argument("--json-logs", action="store_true", help="Emit structured JSON logs")
61
+
62
+ sub = parser.add_subparsers(dest="command", required=True)
63
+
64
+ p_boot = sub.add_parser("bootstrap", help="Create NetBox custom fields, tags and cluster type")
65
+ p_boot.add_argument("--dry-run", action="store_true")
66
+
67
+ p_sync = sub.add_parser("sync", help="Run a one-shot inventory synchronization")
68
+ p_sync.add_argument("--dry-run", action="store_true")
69
+
70
+ p_daemon = sub.add_parser("daemon", help="Run continuous synchronization loop")
71
+ p_daemon.add_argument("--dry-run", action="store_true")
72
+ p_daemon.add_argument(
73
+ "--interval",
74
+ type=int,
75
+ default=None,
76
+ help="Seconds between sync runs (default from config)",
77
+ )
78
+
79
+ args = parser.parse_args(argv)
80
+ _setup_logging(args.verbose, args.json_logs)
81
+
82
+ cfg_path = args.config
83
+ if cfg_path is None:
84
+ for candidate in (
85
+ Path("/etc/netbox-opennebula-sync/config.yaml"),
86
+ Path("config.yaml"),
87
+ ):
88
+ if candidate.is_file():
89
+ cfg_path = str(candidate)
90
+ break
91
+ cfg = load_config(cfg_path)
92
+ if getattr(args, "dry_run", False):
93
+ cfg.sync.dry_run = True
94
+
95
+ if args.command == "bootstrap":
96
+ return run_bootstrap(cfg)
97
+ if args.command == "sync":
98
+ return run_sync(cfg)
99
+ if args.command == "daemon":
100
+ interval = args.interval or cfg.sync.interval
101
+ stop = False
102
+
103
+ def _stop(*_args: object) -> None:
104
+ nonlocal stop
105
+ stop = True
106
+ logging.getLogger(__name__).info("shutdown requested")
107
+
108
+ signal.signal(signal.SIGTERM, _stop)
109
+ signal.signal(signal.SIGINT, _stop)
110
+ log = logging.getLogger(__name__)
111
+ while not stop:
112
+ try:
113
+ rc = run_sync(cfg)
114
+ if rc:
115
+ log.warning("sync finished with errors (rc=%s)", rc)
116
+ except Exception:
117
+ log.exception("sync failed")
118
+ for _ in range(interval):
119
+ if stop:
120
+ break
121
+ time.sleep(1)
122
+ return 0
123
+ return 2
124
+
125
+
126
+ if __name__ == "__main__":
127
+ raise SystemExit(main())
@@ -0,0 +1,200 @@
1
+ """Configuration loading for netbox-opennebula-sync."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import re
7
+ from dataclasses import dataclass, field
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ import yaml
12
+
13
+ DEFAULT_EXCLUDE_INTERFACES = (
14
+ r"^(lo|docker\d*|br-.*|veth.*|virbr\d*|cni\d*|flannel\..*|cali.*|tunl\d*)$"
15
+ )
16
+ DEFAULT_IGNORE_HOSTNAMES = ("localhost", "demo", "demo.local", "ubuntu", "debian")
17
+
18
+
19
+ @dataclass
20
+ class NetBoxConfig:
21
+ url: str = ""
22
+ token: str = ""
23
+ verify_ssl: bool | str = True
24
+ site: str = "default"
25
+ site_id: int | None = None
26
+ tenant: str | None = None
27
+ cluster_name: str | None = None
28
+ cluster_id: int | None = None
29
+ create_missing_hosts: bool = True
30
+ host_device_role: str = "Hypervisor"
31
+ host_manufacturer: str = "Generic"
32
+ host_device_type: str = "KVM Host"
33
+ dns_suffix: str = ""
34
+ socks_proxy: str | None = None
35
+ host_header: str | None = None
36
+ timeout: float = 30.0
37
+
38
+
39
+ @dataclass
40
+ class OpenNebulaConfig:
41
+ endpoint: str
42
+ username: str
43
+ token: str
44
+ timeout: float = 30.0
45
+
46
+ @property
47
+ def credentials(self) -> str:
48
+ return f"{self.username}:{self.token}"
49
+
50
+
51
+ @dataclass
52
+ class SnmpConfig:
53
+ enabled: bool = True
54
+ community: str = "public"
55
+ timeout: float = 1.0
56
+ retries: int = 0
57
+ exclude_interfaces: str = DEFAULT_EXCLUDE_INTERFACES
58
+ ignore_hostnames: list[str] = field(
59
+ default_factory=lambda: list(DEFAULT_IGNORE_HOSTNAMES)
60
+ )
61
+
62
+
63
+ @dataclass
64
+ class SyncConfig:
65
+ orphan_policy: str = "mark_offline" # mark_offline | delete | ignore
66
+ tags: list[str] = field(default_factory=lambda: ["opennebula", "discovered"])
67
+ interval: int = 900
68
+ dry_run: bool = False
69
+
70
+
71
+ @dataclass
72
+ class AppConfig:
73
+ netbox: NetBoxConfig
74
+ opennebula: OpenNebulaConfig
75
+ snmp: SnmpConfig = field(default_factory=SnmpConfig)
76
+ sync: SyncConfig = field(default_factory=SyncConfig)
77
+
78
+ def exclude_iface_re(self) -> re.Pattern[str]:
79
+ return re.compile(self.snmp.exclude_interfaces, re.IGNORECASE)
80
+
81
+
82
+ def _env(name: str, default: str | None = None) -> str | None:
83
+ value = os.environ.get(name)
84
+ if value is None or value == "":
85
+ return default
86
+ return value
87
+
88
+
89
+ def _as_bool(value: Any, default: bool = False) -> bool:
90
+ if value is None:
91
+ return default
92
+ if isinstance(value, bool):
93
+ return value
94
+ return str(value).strip().lower() in {"1", "true", "yes", "on"}
95
+
96
+
97
+ def load_config(path: str | Path | None = None) -> AppConfig:
98
+ """Load YAML config and overlay environment variables."""
99
+ data: dict[str, Any] = {}
100
+ if path:
101
+ raw = Path(path).read_text(encoding="utf-8")
102
+ loaded = yaml.safe_load(raw) or {}
103
+ if not isinstance(loaded, dict):
104
+ raise ValueError(f"config root must be a mapping: {path}")
105
+ data = loaded
106
+
107
+ nb = data.get("netbox") or {}
108
+ one = data.get("opennebula") or {}
109
+ snmp = data.get("snmp") or {}
110
+ sync = data.get("sync") or {}
111
+
112
+ netbox_url = _env("NETBOX_URL", nb.get("url"))
113
+ netbox_token = _env("NETBOX_TOKEN", nb.get("token"))
114
+ one_endpoint = _env("ONE_ENDPOINT", one.get("endpoint"))
115
+ one_user = _env("ONE_USERNAME", one.get("username"))
116
+ one_token = _env("ONE_TOKEN", one.get("token") or _env("ONE_PASSWORD"))
117
+ # ONE_CREDENTIALS=username:token overrides split username/token when set
118
+ one_creds = _env("ONE_CREDENTIALS")
119
+ if one_creds and ":" in one_creds:
120
+ one_user, one_token = one_creds.split(":", 1)
121
+
122
+ missing = [
123
+ name
124
+ for name, value in (
125
+ ("netbox.url / NETBOX_URL", netbox_url),
126
+ ("netbox.token / NETBOX_TOKEN", netbox_token),
127
+ ("opennebula.endpoint / ONE_ENDPOINT", one_endpoint),
128
+ ("opennebula.username / ONE_USERNAME", one_user),
129
+ ("opennebula.token / ONE_TOKEN", one_token),
130
+ )
131
+ if not value
132
+ ]
133
+ if missing:
134
+ raise SystemExit("Missing required configuration: " + ", ".join(missing))
135
+
136
+ verify = _env("NETBOX_VERIFY_SSL", nb.get("verify_ssl"))
137
+ if verify is None:
138
+ verify_ssl: bool | str = True
139
+ elif str(verify).lower() in {"false", "0", "no", "off"}:
140
+ verify_ssl = False
141
+ elif str(verify).lower() in {"true", "1", "yes", "on"}:
142
+ verify_ssl = True
143
+ else:
144
+ verify_ssl = str(verify)
145
+
146
+ socks = _env("ALL_PROXY") or _env("HTTPS_PROXY") or _env("NETBOX_SOCKS") or nb.get(
147
+ "socks_proxy"
148
+ )
149
+
150
+ ignore = snmp.get("ignore_hostnames") or list(DEFAULT_IGNORE_HOSTNAMES)
151
+ if isinstance(ignore, str):
152
+ ignore = [x.strip() for x in ignore.split(",") if x.strip()]
153
+
154
+ return AppConfig(
155
+ netbox=NetBoxConfig(
156
+ url=str(netbox_url).rstrip("/"),
157
+ token=str(netbox_token),
158
+ verify_ssl=verify_ssl,
159
+ site=str(_env("NETBOX_SITE", nb.get("site") or "default")),
160
+ tenant=_env("NETBOX_TENANT", nb.get("tenant")),
161
+ cluster_name=_env("NETBOX_CLUSTER_NAME", nb.get("cluster_name")),
162
+ create_missing_hosts=_as_bool(
163
+ _env("NETBOX_CREATE_MISSING_HOSTS", nb.get("create_missing_hosts")),
164
+ True,
165
+ ),
166
+ host_device_role=str(nb.get("host_device_role") or "Hypervisor"),
167
+ host_manufacturer=str(nb.get("host_manufacturer") or "Generic"),
168
+ host_device_type=str(nb.get("host_device_type") or "KVM Host"),
169
+ dns_suffix=str(_env("NETBOX_DNS_SUFFIX", nb.get("dns_suffix") or "")).lstrip(
170
+ "."
171
+ ),
172
+ socks_proxy=socks,
173
+ host_header=_env("NETBOX_HOST_HEADER", nb.get("host_header")),
174
+ timeout=float(_env("NETBOX_TIMEOUT", nb.get("timeout") or 30)),
175
+ ),
176
+ opennebula=OpenNebulaConfig(
177
+ endpoint=str(one_endpoint),
178
+ username=str(one_user),
179
+ token=str(one_token),
180
+ timeout=float(_env("ONE_TIMEOUT", one.get("timeout") or 30)),
181
+ ),
182
+ snmp=SnmpConfig(
183
+ enabled=_as_bool(_env("SNMP_ENABLED", snmp.get("enabled")), True),
184
+ community=str(_env("SNMP_COMMUNITY", snmp.get("community") or "public")),
185
+ timeout=float(_env("SNMP_TIMEOUT", snmp.get("timeout") or 1)),
186
+ retries=int(_env("SNMP_RETRIES", snmp.get("retries") or 0)),
187
+ exclude_interfaces=str(
188
+ snmp.get("exclude_interfaces") or DEFAULT_EXCLUDE_INTERFACES
189
+ ),
190
+ ignore_hostnames=[str(x).lower() for x in ignore],
191
+ ),
192
+ sync=SyncConfig(
193
+ orphan_policy=str(
194
+ _env("SYNC_ORPHAN_POLICY", sync.get("orphan_policy") or "mark_offline")
195
+ ),
196
+ tags=list(sync.get("tags") or ["opennebula", "discovered"]),
197
+ interval=int(_env("SYNC_INTERVAL", sync.get("interval") or 900)),
198
+ dry_run=_as_bool(_env("SYNC_DRY_RUN", sync.get("dry_run")), False),
199
+ ),
200
+ )
@@ -0,0 +1,29 @@
1
+ """Encrypt plugin secrets with a Fernet key derived from Django SECRET_KEY."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import hashlib
7
+
8
+ from cryptography.fernet import Fernet, InvalidToken
9
+ from django.conf import settings
10
+
11
+
12
+ def _fernet() -> Fernet:
13
+ digest = hashlib.sha256(settings.SECRET_KEY.encode("utf-8")).digest()
14
+ return Fernet(base64.urlsafe_b64encode(digest))
15
+
16
+
17
+ def encrypt_secret(plaintext: str) -> str:
18
+ if not plaintext:
19
+ return ""
20
+ return _fernet().encrypt(plaintext.encode("utf-8")).decode("ascii")
21
+
22
+
23
+ def decrypt_secret(ciphertext: str) -> str:
24
+ if not ciphertext:
25
+ return ""
26
+ try:
27
+ return _fernet().decrypt(ciphertext.encode("ascii")).decode("utf-8")
28
+ except (InvalidToken, ValueError):
29
+ return ""