diff --git a/hosts/glyph/services/agentsview.nix b/hosts/glyph/services/agentsview.nix new file mode 100644 index 00000000..a83c098e --- /dev/null +++ b/hosts/glyph/services/agentsview.nix @@ -0,0 +1,72 @@ +{ + config, + pkgs, + ... +}: let + port = 8095; + configFile = pkgs.writeText "agentsview-config.toml" '' + [pg] + url = "postgres://agentsview@localhost:5432/agentsview?sslmode=disable" + machine_name = "glyph" + ''; + + # pg push starts a watcher after syncing; wrap it to exit after sync + pgPushScript = pkgs.writeShellScript "agentsview-pg-push" '' + ${pkgs.agentsview}/bin/agentsview pg push & + PID=$! + # Wait for sync to complete, then kill the watcher + sleep 15 + kill $PID 2>/dev/null || true + ''; +in { + # Serve the aggregated team dashboard from PostgreSQL + systemd.services.agentsview = { + description = "Agentsview team dashboard"; + after = ["postgresql.service"]; + requires = ["postgresql.service"]; + wantedBy = ["multi-user.target"]; + serviceConfig = { + ExecStart = "${pkgs.agentsview}/bin/agentsview pg serve -host 127.0.0.1 -port ${toString port}"; + DynamicUser = true; + User = "agentsview"; + Group = "agentsview"; + StateDirectory = "agentsview"; + RuntimeDirectory = "agentsview"; + Environment = "HOME=/var/lib/agentsview"; + ExecStartPre = let + script = pkgs.writeShellScript "agentsview-setup" '' + mkdir -p /var/lib/agentsview/.agentsview + cp ${configFile} /var/lib/agentsview/.agentsview/config.toml + chown -R agentsview:agentsview /var/lib/agentsview/.agentsview + ''; + in "+${script}"; + Restart = "on-failure"; + RestartSec = 5; + }; + }; + + # Push local glyph sessions to PostgreSQL every 10 minutes + systemd.timers.agentsview-pg-push = { + description = "Push agentsview sessions to PostgreSQL"; + wantedBy = ["timers.target"]; + timerConfig = { + OnBootSec = "2min"; + OnUnitActiveSec = "10min"; + Persistent = true; + }; + }; + + systemd.services.agentsview-pg-push = { + description = "Push agentsview sessions to PostgreSQL"; + after = ["postgresql.service"]; + requires = ["postgresql.service"]; + serviceConfig = { + Type = "oneshot"; + ExecStart = pgPushScript; + User = "mu"; + Group = "users"; + Environment = "HOME=/home/mu"; + TimeoutStartSec = 60; + }; + }; +} diff --git a/hosts/glyph/services/db.nix b/hosts/glyph/services/db.nix index bc35dfb5..37229061 100644 --- a/hosts/glyph/services/db.nix +++ b/hosts/glyph/services/db.nix @@ -12,12 +12,16 @@ port = 5432; max_connections = 150; }; - ensureDatabases = ["atticd" "grafana" "open-webui" "pocketid"]; + ensureDatabases = ["agentsview" "atticd" "grafana" "open-webui" "pocketid"]; ensureUsers = [ { name = "mu"; ensureClauses.superuser = true; } + { + name = "agentsview"; + ensureDBOwnership = true; + } { name = "atticd"; ensureDBOwnership = true; @@ -39,6 +43,6 @@ services.postgresqlBackup = { enable = true; - databases = ["atticd" "grafana" "open-webui" "pocketid"]; + databases = ["agentsview" "atticd" "grafana" "open-webui" "pocketid"]; }; } diff --git a/hosts/glyph/services/default.nix b/hosts/glyph/services/default.nix index 59bc0e8b..9fe13a73 100644 --- a/hosts/glyph/services/default.nix +++ b/hosts/glyph/services/default.nix @@ -4,6 +4,7 @@ ... }: { imports = [ + ./agentsview.nix ./attic.nix ./db.nix ./avahi.nix