headplane/internal/hpagent/handler.go

56 lines
1.3 KiB
Go
Raw Normal View History

package hpagent
import (
"bufio"
"context"
"fmt"
2025-06-16 11:38:13 -04:00
"os"
2025-04-08 14:51:28 -04:00
2025-06-16 11:38:13 -04:00
"github.com/tale/headplane/internal/tsnet"
"github.com/tale/headplane/internal/util"
)
// Starts listening for messages from stdin
func FollowMaster(agent *tsnet.TSAgent) {
2025-04-08 14:51:28 -04:00
log := util.GetLogger()
scanner := bufio.NewScanner(os.Stdin)
2025-05-30 19:35:47 -04:00
log.Info("Listening for messages from Headplane master on stdin")
2025-04-08 14:51:28 -04:00
for scanner.Scan() {
line := scanner.Bytes()
directive := string(line)
log.Debug("Received directive from master: %s", directive)
switch directive {
case "SHUTDOWN":
log.Debug("Received SHUTDOWN directive from master, shutting down agent")
agent.Shutdown()
return
case "START":
log.Debug("Received START directive from master, starting agent")
// TODO: Start the agent here instead of in main
fmt.Println("READY " + agent.ID)
continue
case "PING":
log.Debug("Received PING directive from master, responding with PONG")
fmt.Println("PONG " + agent.ID)
2025-06-08 08:59:33 -04:00
continue
2025-06-16 11:38:13 -04:00
case "REFRESH":
log.Debug("Received REFRESH directive from master, refreshing status for all nodes")
err := agent.DispatchHostInfo(context.Background())
if err != nil {
log.Error("Error refreshing host info: %s", err)
fmt.Println("ERR " + err.Error())
}
}
}
2025-06-16 11:38:13 -04:00
if err := scanner.Err(); err != nil {
log.Fatal("Error reading from stdin: %s", err)
}
}