2024-12-30 13:48:49 +05:30
|
|
|
package hpagent
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-11 12:00:19 -04:00
|
|
|
"bufio"
|
2025-08-19 00:10:06 -04:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-06-16 11:38:13 -04:00
|
|
|
|
2025-04-11 12:00:19 -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"
|
2024-12-30 13:48:49 +05:30
|
|
|
)
|
|
|
|
|
|
2025-04-11 12:00:19 -04:00
|
|
|
// Starts listening for messages from stdin
|
|
|
|
|
func FollowMaster(agent *tsnet.TSAgent) {
|
2025-04-08 14:51:28 -04:00
|
|
|
log := util.GetLogger()
|
2025-04-11 12:00:19 -04:00
|
|
|
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
|
|
|
|
2025-04-11 12:00:19 -04:00
|
|
|
for scanner.Scan() {
|
|
|
|
|
line := scanner.Bytes()
|
2025-08-19 00:10:06 -04:00
|
|
|
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)
|
2025-05-31 09:45:47 -04:00
|
|
|
continue
|
2025-05-31 16:41:07 -04:00
|
|
|
|
2025-08-19 00:10:06 -04:00
|
|
|
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
|
|
|
|
2025-08-19 00:10:06 -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())
|
|
|
|
|
}
|
2024-12-30 13:48:49 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-16 11:38:13 -04:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
|
log.Fatal("Error reading from stdin: %s", err)
|
|
|
|
|
}
|
2024-12-30 13:48:49 +05:30
|
|
|
}
|