feat: switch agent to a periodic dump rather than long running process

This commit is contained in:
Aarnav Tale
2026-03-27 13:19:16 -04:00
parent 2c57187628
commit ee59a2d06d
17 changed files with 521 additions and 464 deletions
+16 -34
View File
@@ -1,22 +1,15 @@
package main
import (
"bufio"
"context"
"fmt"
"encoding/json"
"os"
"strings"
"github.com/tale/headplane/internal/config"
"github.com/tale/headplane/internal/tsnet"
"github.com/tale/headplane/internal/util"
)
type Register struct {
Type string
ID string
}
func main() {
log := util.GetLogger()
cfg, err := config.Load()
@@ -28,34 +21,23 @@ func main() {
agent := tsnet.NewAgent(cfg)
defer agent.Shutdown()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Bytes()
directive := strings.TrimSpace(string(line))
log.Debug("Received directive: %s", directive)
agent.Connect()
switch directive {
case "START":
agent.Connect()
fmt.Printf("READY %s\n", agent.ID)
case "SHUTDOWN":
agent.Shutdown()
os.Exit(0)
case "PING":
fmt.Printf("PONG %s\n", agent.ID)
case "REFRESH":
err := agent.DispatchHostInfo(context.Background())
if err != nil {
fmt.Printf("ERROR %s\n", err)
}
}
hosts, err := agent.FetchAllHostInfo(context.Background())
if err != nil {
log.Fatal("Failed to fetch host info: %s", err)
}
if err := scanner.Err(); err != nil {
fmt.Printf("ERROR %s\n", err)
os.Exit(1)
output := struct {
Self string `json:"self"`
Hosts map[string]json.RawMessage `json:"hosts"`
}{
Self: agent.ID,
Hosts: hosts,
}
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(output); err != nil {
log.Fatal("Failed to encode result: %s", err)
}
}