feat(plugins): fold first-party plugins in-tree; bundled + external roots

First-party plugins (host_agent, http, snmp, traefik, unifi, docker) are now
tracked under plugins/ and baked into the image, so they version atomically
with core — ending the cross-repo import drift the roundtable->steward rename
exposed. History for these files is preserved in the archived Roundtable-plugins
repo.

Plugin discovery becomes multi-root: PLUGIN_DIR (single) -> PLUGIN_DIRS
(bundled first, then external) + PLUGIN_INSTALL_DIR. Bundled ships in the image;
third-party plugins still mount at runtime into the external root
(STEWARD_PLUGIN_DIR, default /data/plugins) and downloads/installs land there.
Bundled shadows external on a name collision.

- config.py: load_bootstrap returns plugin_dirs + plugin_install_dir
- app.py: iterate PLUGIN_DIRS at the migration + load sites
- migration_runner.py: discover_all_in() unions every plugin root
- plugin_manager.py: resolve_plugin_path() (pure, first-root-wins); load /
  install / hot-reload span all roots; installs target the external root
- settings/routes.py: _discover_plugins scans all roots, dedup bundled-first
- Dockerfile: COPY plugins/ ; docker-compose: drop host bind, document external
- tests/test_plugin_dirs.py: resolution, multi-root discovery, bootstrap split

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-01 08:37:24 -04:00
parent 88ab5b917e
commit a7a281cb11
99 changed files with 7931 additions and 52 deletions
+72
View File
@@ -0,0 +1,72 @@
# plugins/unifi/migrations/env.py
"""Alembic env.py for the UniFi plugin (standalone dev use).
At app startup, migration_runner.py uses the core env.py with version_locations.
"""
import asyncio
import os
import sys
from pathlib import Path
from logging.config import fileConfig
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from fabledscryer.models.base import Base
import fabledscryer.models # noqa: F401
from plugins.unifi.models import UnifiWanStat, UnifiClientSnapshot, UnifiDevice # noqa: F401
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def _get_url() -> str:
import yaml
cfg_path = os.environ.get("FABLEDSCRYER_CONFIG", "config.yaml")
try:
with open(cfg_path) as f:
cfg = yaml.safe_load(f) or {}
url = cfg.get("database", {}).get("url", "")
except FileNotFoundError:
url = ""
return os.environ.get("FABLEDSCRYER_DATABASE__URL", url)
def run_migrations_offline() -> None:
context.configure(
url=_get_url(), target_metadata=target_metadata,
literal_binds=True, dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
cfg = config.get_section(config.config_ini_section, {})
cfg["sqlalchemy.url"] = _get_url()
connectable = async_engine_from_config(cfg, prefix="sqlalchemy.", poolclass=pool.NullPool)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
@@ -0,0 +1,62 @@
"""UniFi plugin initial tables
Revision ID: unifi_001_initial
Revises: (none — branch off core via depends_on)
Create Date: 2026-03-20
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "unifi_001_initial"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = "unifi"
depends_on: Union[str, Sequence[str], None] = ("0004_app_settings",)
def upgrade() -> None:
op.create_table(
"unifi_wan_stats",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("is_up", sa.Boolean, nullable=False, server_default="false"),
sa.Column("wan_ip", sa.String(64), nullable=True),
sa.Column("latency_ms", sa.Float, nullable=True),
sa.Column("rx_bytes_rate", sa.Float, nullable=True),
sa.Column("tx_bytes_rate", sa.Float, nullable=True),
sa.Column("uptime_seconds", sa.Integer, nullable=True),
)
op.create_index("ix_unifi_wan_stats_scraped", "unifi_wan_stats", ["scraped_at"])
op.create_table(
"unifi_client_snapshots",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("total_clients", sa.Integer, nullable=False, server_default="0"),
sa.Column("wireless_clients", sa.Integer, nullable=False, server_default="0"),
sa.Column("wired_clients", sa.Integer, nullable=False, server_default="0"),
sa.Column("guest_clients", sa.Integer, nullable=False, server_default="0"),
sa.Column("top_clients_json", sa.Text, nullable=False, server_default="[]"),
)
op.create_index("ix_unifi_client_snapshots_scraped", "unifi_client_snapshots", ["scraped_at"])
op.create_table(
"unifi_devices",
sa.Column("mac", sa.String(32), primary_key=True),
sa.Column("name", sa.String(255), nullable=True),
sa.Column("model", sa.String(64), nullable=True),
sa.Column("device_type", sa.String(16), nullable=True),
sa.Column("ip", sa.String(64), nullable=True),
sa.Column("state", sa.Integer, nullable=False, server_default="0"),
sa.Column("uptime_seconds", sa.Integer, nullable=True),
sa.Column("last_seen", sa.DateTime(timezone=True), nullable=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
)
def downgrade() -> None:
op.drop_table("unifi_devices")
op.drop_index("ix_unifi_client_snapshots_scraped", "unifi_client_snapshots")
op.drop_table("unifi_client_snapshots")
op.drop_index("ix_unifi_wan_stats_scraped", "unifi_wan_stats")
op.drop_table("unifi_wan_stats")
@@ -0,0 +1,156 @@
"""UniFi expanded tables: wlan stats, dpi, events, alarms, known clients,
speedtest results, networks, port forwards, firewall rules.
Also adds missing 'version' column to unifi_devices.
Revision ID: unifi_002_expanded
Revises: unifi_001_initial
Create Date: 2026-03-20
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "unifi_002_expanded"
down_revision: Union[str, None] = "unifi_001_initial"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ── Patch unifi_devices to add missing version column ─────────────────────
op.add_column("unifi_devices", sa.Column("version", sa.String(64), nullable=True))
# ── WLAN stats (time-series per SSID+radio) ───────────────────────────────
op.create_table(
"unifi_wlan_stats",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("ssid", sa.String(255), nullable=False),
sa.Column("radio", sa.String(8), nullable=True),
sa.Column("num_sta", sa.Integer, nullable=False, server_default="0"),
sa.Column("tx_bytes", sa.Float, nullable=True),
sa.Column("rx_bytes", sa.Float, nullable=True),
sa.Column("channel", sa.Integer, nullable=True),
sa.Column("satisfaction", sa.Integer, nullable=True),
)
op.create_index("ix_unifi_wlan_stats_scraped", "unifi_wlan_stats", ["scraped_at"])
# ── DPI snapshots (site-level category totals) ────────────────────────────
op.create_table(
"unifi_dpi_snapshots",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("by_category_json", sa.Text, nullable=False, server_default="[]"),
sa.Column("by_client_json", sa.Text, nullable=False, server_default="[]"),
)
op.create_index("ix_unifi_dpi_scraped", "unifi_dpi_snapshots", ["scraped_at"])
# ── Known clients (upserted by MAC) ───────────────────────────────────────
op.create_table(
"unifi_known_clients",
sa.Column("mac", sa.String(32), primary_key=True),
sa.Column("hostname", sa.String(255), nullable=True),
sa.Column("name", sa.String(255), nullable=True),
sa.Column("ip", sa.String(64), nullable=True),
sa.Column("mac_vendor", sa.String(128), nullable=True),
sa.Column("note", sa.Text, nullable=True),
sa.Column("is_blocked", sa.Boolean, nullable=False, server_default="false"),
sa.Column("first_seen", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_seen", sa.DateTime(timezone=True), nullable=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
)
# ── Events (upserted by UniFi event ID) ───────────────────────────────────
op.create_table(
"unifi_events",
sa.Column("unifi_id", sa.String(64), primary_key=True),
sa.Column("event_time", sa.DateTime(timezone=True), nullable=False),
sa.Column("event_key", sa.String(64), nullable=False),
sa.Column("category", sa.String(16), nullable=False),
sa.Column("message", sa.Text, nullable=False),
sa.Column("source_mac", sa.String(32), nullable=True),
sa.Column("source_ip", sa.String(64), nullable=True),
sa.Column("details_json", sa.Text, nullable=False, server_default="{}"),
)
op.create_index("ix_unifi_events_time", "unifi_events", ["event_time"])
# ── Alarms (upserted by UniFi alarm ID) ───────────────────────────────────
op.create_table(
"unifi_alarms",
sa.Column("unifi_id", sa.String(64), primary_key=True),
sa.Column("alarm_time", sa.DateTime(timezone=True), nullable=False),
sa.Column("alarm_key", sa.String(64), nullable=False),
sa.Column("message", sa.Text, nullable=False),
sa.Column("archived", sa.Boolean, nullable=False, server_default="false"),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_unifi_alarms_time", "unifi_alarms", ["alarm_time"])
# ── Speedtest results (upserted by run timestamp) ─────────────────────────
op.create_table(
"unifi_speedtest_results",
sa.Column("run_at", sa.DateTime(timezone=True), primary_key=True),
sa.Column("download_mbps", sa.Float, nullable=True),
sa.Column("upload_mbps", sa.Float, nullable=True),
sa.Column("latency_ms", sa.Float, nullable=True),
sa.Column("server_name", sa.String(255), nullable=True),
)
# ── Networks/VLANs (upserted by UniFi ID) ────────────────────────────────
op.create_table(
"unifi_networks",
sa.Column("unifi_id", sa.String(64), primary_key=True),
sa.Column("name", sa.String(255), nullable=False),
sa.Column("purpose", sa.String(32), nullable=True),
sa.Column("vlan", sa.Integer, nullable=True),
sa.Column("ip_subnet", sa.String(64), nullable=True),
sa.Column("dhcp_enabled", sa.Boolean, nullable=False, server_default="false"),
sa.Column("is_guest", sa.Boolean, nullable=False, server_default="false"),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
)
# ── Port forwards (upserted by UniFi ID) ─────────────────────────────────
op.create_table(
"unifi_port_forwards",
sa.Column("unifi_id", sa.String(64), primary_key=True),
sa.Column("name", sa.String(255), nullable=True),
sa.Column("proto", sa.String(8), nullable=False, server_default="tcp"),
sa.Column("dst_port", sa.String(32), nullable=False),
sa.Column("fwd_ip", sa.String(64), nullable=False),
sa.Column("fwd_port", sa.String(32), nullable=False),
sa.Column("src_filter", sa.String(255), nullable=True),
sa.Column("enabled", sa.Boolean, nullable=False, server_default="true"),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
)
# ── Firewall rules (upserted by UniFi ID) ────────────────────────────────
op.create_table(
"unifi_fw_rules",
sa.Column("unifi_id", sa.String(64), primary_key=True),
sa.Column("name", sa.String(255), nullable=False),
sa.Column("ruleset", sa.String(16), nullable=False),
sa.Column("rule_index", sa.Integer, nullable=True),
sa.Column("action", sa.String(16), nullable=False),
sa.Column("protocol", sa.String(16), nullable=True),
sa.Column("enabled", sa.Boolean, nullable=False, server_default="true"),
sa.Column("src_address", sa.String(255), nullable=True),
sa.Column("dst_address", sa.String(255), nullable=True),
sa.Column("scraped_at", sa.DateTime(timezone=True), nullable=False),
)
def downgrade() -> None:
op.drop_table("unifi_fw_rules")
op.drop_table("unifi_port_forwards")
op.drop_table("unifi_networks")
op.drop_table("unifi_speedtest_results")
op.drop_index("ix_unifi_alarms_time", "unifi_alarms")
op.drop_table("unifi_alarms")
op.drop_index("ix_unifi_events_time", "unifi_events")
op.drop_table("unifi_events")
op.drop_table("unifi_known_clients")
op.drop_index("ix_unifi_dpi_scraped", "unifi_dpi_snapshots")
op.drop_table("unifi_dpi_snapshots")
op.drop_index("ix_unifi_wlan_stats_scraped", "unifi_wlan_stats")
op.drop_table("unifi_wlan_stats")
op.drop_column("unifi_devices", "version")