mirror of
https://github.com/tale/headplane.git
synced 2026-07-26 07:48:14 +00:00
feat: switch agent to a periodic dump rather than long running process
This commit is contained in:
+16
-34
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user