mirror of
https://github.com/tale/headplane.git
synced 2026-07-26 07:48:14 +00:00
feat: i guess we're undoing agent work
This commit is contained in:
+36
-15
@@ -1,15 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/tale/headplane/internal/config"
|
||||
"github.com/tale/headplane/internal/tsnet"
|
||||
"github.com/tale/headplane/internal/util"
|
||||
)
|
||||
|
||||
type output struct {
|
||||
Self string `json:"self"`
|
||||
Hosts map[string]json.RawMessage `json:"hosts"`
|
||||
}
|
||||
|
||||
type errorOutput struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
log := util.GetLogger()
|
||||
cfg, err := config.Load()
|
||||
@@ -23,21 +35,30 @@ func main() {
|
||||
|
||||
agent.Connect()
|
||||
|
||||
hosts, err := agent.FetchAllHostInfo(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal("Failed to fetch host info: %s", err)
|
||||
}
|
||||
|
||||
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)
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
// Shut down cleanly on signal or stdin close
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
go func() {
|
||||
<-sigCh
|
||||
agent.Shutdown()
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
// Each line on stdin triggers a sync. The line content is ignored.
|
||||
for scanner.Scan() {
|
||||
hosts, err := agent.FetchAllHostInfo(context.Background())
|
||||
if err != nil {
|
||||
enc.Encode(errorOutput{Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
|
||||
enc.Encode(output{
|
||||
Self: agent.ID,
|
||||
Hosts: hosts,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user