implement host info update hook
This commit is contained in:
parent
1af0aacf96
commit
76f6a43a10
1 changed files with 59 additions and 0 deletions
59
main.go
59
main.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"lux/crypto"
|
"lux/crypto"
|
||||||
"lux/host"
|
"lux/host"
|
||||||
"lux/node"
|
"lux/node"
|
||||||
|
|
@ -13,6 +14,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
@ -34,6 +36,12 @@ type LogConfig struct {
|
||||||
LogPath string `xml:",innerxml"`
|
LogPath string `xml:",innerxml"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateHookConfig struct {
|
||||||
|
XMLName xml.Name `xml:"hook"`
|
||||||
|
HostID string `xml:"id"`
|
||||||
|
Script string `xml:"script"`
|
||||||
|
}
|
||||||
|
|
||||||
type NodeConfig struct {
|
type NodeConfig struct {
|
||||||
XMLName xml.Name `xml:"node"`
|
XMLName xml.Name `xml:"node"`
|
||||||
KeyStore string `xml:"keystore"`
|
KeyStore string `xml:"keystore"`
|
||||||
|
|
@ -53,6 +61,8 @@ type NodeConfig struct {
|
||||||
DNS []string `xml:"dns"`
|
DNS []string `xml:"dns"`
|
||||||
|
|
||||||
Log LogConfig `xml:"log"`
|
Log LogConfig `xml:"log"`
|
||||||
|
|
||||||
|
Hooks []UpdateHookConfig `xml:"hook"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogging(log LogConfig) {
|
func setupLogging(log LogConfig) {
|
||||||
|
|
@ -124,6 +134,40 @@ func bootstrapNode() {
|
||||||
|
|
||||||
var log = logging.MustGetLogger("main")
|
var log = logging.MustGetLogger("main")
|
||||||
|
|
||||||
|
type hookUpdateSubscriber struct {
|
||||||
|
Hooks map[proto.LuxID]UpdateHookConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (subscriber *hookUpdateSubscriber) HandleStateUpdate(state node.LuxHostState) {
|
||||||
|
hook, ok := subscriber.Hooks[state.HostId]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// spawn executable and pipe xml host state into it's stdin
|
||||||
|
xmlBytes, err := xml.Marshal(&rpc.LuxRpcHost{
|
||||||
|
HostID: state.HostId.String(),
|
||||||
|
Hostname: state.State.Hostname,
|
||||||
|
State: state.State.IntoRpc(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to marshal host update for hook: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer := bytes.Buffer{}
|
||||||
|
buffer.Write(xmlBytes)
|
||||||
|
|
||||||
|
cmd := exec.Command(hook.Script)
|
||||||
|
cmd.Stdin = &buffer
|
||||||
|
cmd.Stdout = io.Discard
|
||||||
|
cmd.Stderr = io.Discard
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
log.Warningf("failed to execute script %s: %v\n", hook.Script, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func nodeMain() {
|
func nodeMain() {
|
||||||
xmlBytes, err := os.ReadFile(configPath)
|
xmlBytes, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -234,6 +278,21 @@ func nodeMain() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add update hooks
|
||||||
|
hook := hookUpdateSubscriber{
|
||||||
|
Hooks: make(map[proto.LuxID]UpdateHookConfig),
|
||||||
|
}
|
||||||
|
for _, item := range config.Hooks {
|
||||||
|
id, err := proto.ParseLuxID(item.HostID)
|
||||||
|
if err != nil {
|
||||||
|
log.Criticalf("failed to parse hook host id %s: %v\n", item.HostID, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
hook.Hooks[id] = item
|
||||||
|
}
|
||||||
|
node.AddSubscriber(&hook)
|
||||||
|
|
||||||
// start node
|
// start node
|
||||||
node.Start()
|
node.Start()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue