Fix agent plugin configuration handling
This commit is contained in:
+38
-10
@@ -250,6 +250,7 @@ update_netbox_configuration() {
|
||||
CONFIG_BEGIN_VALUE="$CONFIG_BEGIN" \
|
||||
CONFIG_END_VALUE="$CONFIG_END" \
|
||||
"$NETBOX_PYTHON" - <<'PY'
|
||||
import ast
|
||||
import os
|
||||
import re
|
||||
import stat
|
||||
@@ -266,9 +267,6 @@ block = "\n".join([
|
||||
start_marker,
|
||||
"from netbox.store_plugins import STORE_PLUGINS",
|
||||
"",
|
||||
"PLUGINS = list(globals().get(\"PLUGINS\", []))",
|
||||
"if \"netbox_plugin_store\" not in PLUGINS:",
|
||||
" PLUGINS.append(\"netbox_plugin_store\")",
|
||||
"for _store_plugin in STORE_PLUGINS:",
|
||||
" if _store_plugin not in PLUGINS:",
|
||||
" PLUGINS.append(_store_plugin)",
|
||||
@@ -291,21 +289,51 @@ block = "\n".join([
|
||||
end_marker,
|
||||
])
|
||||
original = path.read_text(encoding="utf-8")
|
||||
original = re.sub(
|
||||
r"(?m)^from store_plugins import STORE_PLUGINS\s*$",
|
||||
"from netbox.store_plugins import STORE_PLUGINS",
|
||||
original,
|
||||
)
|
||||
start_count = original.count(start_marker)
|
||||
end_count = original.count(end_marker)
|
||||
if (start_count, end_count) == (0, 0):
|
||||
candidate = original.rstrip() + "\n\n" + block + "\n"
|
||||
base = original
|
||||
elif (start_count, end_count) == (1, 1):
|
||||
before, remainder = original.split(start_marker, 1)
|
||||
_, after = remainder.split(end_marker, 1)
|
||||
candidate = before.rstrip() + "\n\n" + block + after.rstrip() + "\n"
|
||||
base = before.rstrip() + "\n" + after.lstrip("\r\n")
|
||||
else:
|
||||
raise SystemExit("Der verwaltete configuration.py-Block ist unvollständig oder doppelt vorhanden.")
|
||||
base = re.sub(
|
||||
r"(?m)^from store_plugins import STORE_PLUGINS\s*$",
|
||||
"from netbox.store_plugins import STORE_PLUGINS",
|
||||
base,
|
||||
)
|
||||
tree = ast.parse(base)
|
||||
assignments = []
|
||||
for node in tree.body:
|
||||
if isinstance(node, ast.Assign) and any(
|
||||
isinstance(target, ast.Name) and target.id == "PLUGINS" for target in node.targets
|
||||
):
|
||||
assignments.append(node)
|
||||
elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) and node.target.id == "PLUGINS":
|
||||
assignments.append(node)
|
||||
if len(assignments) != 1:
|
||||
raise SystemExit("configuration.py muss genau eine statische PLUGINS-Zuweisung enthalten.")
|
||||
assignment = assignments[0]
|
||||
try:
|
||||
plugins = ast.literal_eval(assignment.value)
|
||||
except (ValueError, TypeError, SyntaxError) as exc:
|
||||
raise SystemExit("PLUGINS muss eine statische Liste oder ein Tupel sein.") from exc
|
||||
if not isinstance(plugins, (list, tuple)) or any(not isinstance(item, str) for item in plugins):
|
||||
raise SystemExit("PLUGINS muss ausschließlich Plugin-Importnamen enthalten.")
|
||||
plugins = list(plugins)
|
||||
if "netbox_plugin_store" not in plugins:
|
||||
plugins.append("netbox_plugin_store")
|
||||
newline = "\r\n" if "\r\n" in base else "\n"
|
||||
lines = base.splitlines(keepends=True)
|
||||
if assignment.lineno == assignment.end_lineno and ";" in lines[assignment.lineno - 1]:
|
||||
raise SystemExit("Die PLUGINS-Zuweisung teilt sich eine Zeile und kann nicht sicher geändert werden.")
|
||||
replacement = [f"PLUGINS = [{newline}"]
|
||||
replacement.extend(f" {plugin!r},{newline}" for plugin in plugins)
|
||||
replacement.append(f"]{newline}")
|
||||
base = "".join(lines[: assignment.lineno - 1] + replacement + lines[assignment.end_lineno :])
|
||||
candidate = base.rstrip() + "\n\n" + block + "\n"
|
||||
compile(candidate, str(path), "exec")
|
||||
metadata = path.stat()
|
||||
fd, temporary_name = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
|
||||
|
||||
Reference in New Issue
Block a user