Добавлена инициализация источников в БД

This commit is contained in:
Ilgiz Kalmetyev 2026-04-11 23:11:26 +05:00
parent ed418563b4
commit dbcff49e22
7 changed files with 135 additions and 8 deletions

View File

@ -0,0 +1,98 @@
BEGIN IMMEDIATE;
INSERT INTO source_registry (
id,
source_key,
source_kind,
display_name,
is_enabled,
metadata_json,
created_at,
updated_at
) VALUES
(
'11111111-1111-1111-1111-111111111111',
'journal-main',
'journal',
'Journald source',
1,
'{}',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
),
(
'22222222-2222-2222-2222-222222222222',
'dbus-main',
'dbus',
'wpa_supplicant D-Bus source',
1,
'{}',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
),
(
'33333333-3333-3333-3333-333333333333',
'navigation-main',
'navigation',
'Navigation source',
1,
'{}',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
)
ON CONFLICT(source_key) DO UPDATE SET
source_kind = excluded.source_kind,
display_name = excluded.display_name,
is_enabled = excluded.is_enabled,
metadata_json = excluded.metadata_json,
updated_at = excluded.updated_at;
INSERT INTO source_health (
source_id,
status,
last_seen_at,
last_heartbeat_at,
last_error,
details_json,
created_at,
updated_at
) VALUES
(
'11111111-1111-1111-1111-111111111111',
'disconnected',
NULL,
NULL,
NULL,
'{}',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
),
(
'22222222-2222-2222-2222-222222222222',
'disconnected',
NULL,
NULL,
NULL,
'{}',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
),
(
'33333333-3333-3333-3333-333333333333',
'disconnected',
NULL,
NULL,
NULL,
'{}',
strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
)
ON CONFLICT(source_id) DO UPDATE SET
status = excluded.status,
last_seen_at = excluded.last_seen_at,
last_heartbeat_at = excluded.last_heartbeat_at,
last_error = excluded.last_error,
details_json = excluded.details_json,
updated_at = excluded.updated_at;
COMMIT;

View File

@ -7,10 +7,21 @@ This document describes the day-to-day procedures used by operators and maintain
1. Install or update the application under `/opt/wf-agent`, or adjust `WorkingDirectory` and `ExecStart` in `deployment/systemd/wf-agent.service` if you use another path.
2. Copy `deployment/config/wf-agent.toml.example` to `/etc/wf-agent/wf-agent.toml`.
3. Ensure `wf-agent.toml` contains `app_env`, `database_dsn`, `log_level`, `wifi_interface_name`, `cache_ttl_seconds`, and the `[acl]` section.
4. Copy `deployment/systemd/wf-agent.service` to `/etc/systemd/system/wf-agent.service`.
5. Run `systemctl daemon-reload`.
6. Run `systemctl enable --now wf-agent`.
4. Apply migrations to the target SQLite database.
5. Seed the standard source registry and baseline health rows with `sqlite3 /var/lib/wf-agent/wf-agent.sqlite3 < deployment/sqlite3/seed-sources.sql`.
6. Copy `deployment/systemd/wf-agent.service` to `/etc/systemd/system/wf-agent.service`.
7. Run `systemctl daemon-reload`.
8. Run `systemctl enable --now wf-agent`.
## Bootstrap Sources
After migrations, initialize the standard source registry and baseline health rows:
```bash
sqlite3 /var/lib/wf-agent/wf-agent.sqlite3 < deployment/sqlite3/seed-sources.sql
```
If your database lives at a different path, substitute the target SQLite file in the command above.
## CLI Utility
Use the CLI for local inspection and cache maintenance. It reads the same TOML configuration as the service.
@ -29,6 +40,7 @@ wf-agent --config-file /etc/wf-agent/wf-agent.toml cache clear
- `inspect health` shows source health snapshots.
- `inspect cache` shows active cached events with TTL and ACL marks.
- `cache clear` removes local cached events from the canonical cache table.
- If `inspect sources` is empty after deploy, run `deployment/sqlite3/seed-sources.sql` against the target database to seed registry and health rows.
- If `--config-file` is omitted, the CLI uses `/etc/wf-agent/wf-agent.toml`.
## Restart

View File

@ -26,9 +26,10 @@ This runbook describes the normal operating posture for `wf-agent` on a Linux ho
1. Install the application under `/opt/wf-agent`, or update `WorkingDirectory` and `ExecStart` in `deployment/systemd/wf-agent.service` to match the actual path.
2. Copy `deployment/config/wf-agent.toml.example` to `/etc/wf-agent/wf-agent.toml`.
3. Ensure `wf-agent.toml` contains `app_env`, `database_dsn`, `log_level`, `wifi_interface_name`, `cache_ttl_seconds`, and the `[acl]` section.
4. Enable and start `wf-agent.service`.
5. For local inspection, use `wf-agent --config-file /etc/wf-agent/wf-agent.toml inspect sources|outbox|health|cache`.
6. For D-Bus sources, confirm `wifi_interface_name` matches the target Wi-Fi interface on the host.
4. Apply migrations and seed `source_registry` and `source_health` with `deployment/sqlite3/seed-sources.sql` before the first start.
5. Enable and start `wf-agent.service`.
6. For local inspection, use `wf-agent --config-file /etc/wf-agent/wf-agent.toml inspect sources|outbox|health|cache`.
7. For D-Bus sources, confirm `wifi_interface_name` matches the target Wi-Fi interface on the host.
## Routine Checks

View File

@ -17,6 +17,7 @@ This guide collects the shortest path to useful operational information.
### Adapter looks stuck
- Check `/admin/sources` for `degraded`, `disconnected`, or `failed`.
- If `/admin/sources` is empty, confirm the deployment ran `deployment/sqlite3/seed-sources.sql` after migration to seed registry and health rows.
- Check structured logs for the adapter `source_key` and `source_id`.
- For D-Bus sources, verify `wifi_interface_name`, the interface path, and that `wpa_supplicant` is reachable on the system bus.
- For journal sources, confirm the persisted cursor is advancing.

View File

@ -42,6 +42,7 @@ package:
- app/adapters/journal/adapter.py
- app/adapters/dbus_wpa/adapter.py
- deployment/config/wf-agent.toml.example
- deployment/sqlite3/seed-sources.sql
- deployment/systemd/wf-agent.service
- pyproject.toml
- README.md

View File

@ -1,4 +1,4 @@
from __future__ import annotations
from __future__ import annotations
import hashlib
import tarfile
@ -38,6 +38,7 @@ def test_release_build_produces_clean_bundle(tmp_path: Path) -> None:
assert "app/__init__.py" in relative_members
assert "deployment/systemd/wf-agent.service" in relative_members
assert "deployment/config/wf-agent.toml.example" in relative_members
assert "deployment/sqlite3/seed-sources.sql" in relative_members
assert "deployment/env/wf-agent.env.example" not in relative_members
assert "release.manifest.yml" in relative_members
assert "docs/content-delivery.md" in relative_members

View File

@ -1,4 +1,4 @@
from __future__ import annotations
from __future__ import annotations
from pathlib import Path
@ -6,6 +6,7 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SERVICE_FILE = ROOT / "deployment" / "systemd" / "wf-agent.service"
CONFIG_FILE = ROOT / "deployment" / "config" / "wf-agent.toml.example"
SEED_FILE = ROOT / "deployment" / "sqlite3" / "seed-sources.sql"
README_FILE = ROOT / "README.md"
@ -16,6 +17,7 @@ def test_deployment_assets_reference_single_toml_config() -> None:
assert SERVICE_FILE.is_file()
assert CONFIG_FILE.is_file()
assert SEED_FILE.is_file()
assert "ExecStart=/opt/wf-agent/.venv/bin/uvicorn app.api.main:app" in service_text
assert "EnvironmentFile=" not in service_text
assert "wf_agent_" not in service_text
@ -27,6 +29,17 @@ def test_deployment_assets_reference_single_toml_config() -> None:
)
assert "wifi_interface_name = \"wlan0\"" in config_text
assert "[acl]" in config_text
seed_text = SEED_FILE.read_text(encoding="utf-8")
assert "BEGIN IMMEDIATE;" in seed_text
assert "INSERT INTO source_registry" in seed_text
assert "INSERT INTO source_health" in seed_text
assert "journal-main" in seed_text
assert "dbus-main" in seed_text
assert "navigation-main" in seed_text
assert "disconnected" in seed_text
assert "ON CONFLICT(source_key) DO UPDATE" in seed_text
assert "ON CONFLICT(source_id) DO UPDATE" in seed_text
assert "deployment/systemd/wf-agent.service" in readme_text
assert "deployment/config/wf-agent.toml.example" in readme_text
assert "deployment/env/wf-agent.env.example" not in readme_text