lobby2/refresher/main.go

180 lines
3.5 KiB
Go
Raw Permalink Normal View History

2024-12-08 01:30:07 +00:00
package refresher
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
2024-12-09 01:00:38 +00:00
"path/filepath"
2024-12-08 01:30:07 +00:00
"time"
"gitea.ceperka.net/rosti/lobby2/nodes"
)
const refreshIntervalSeconds = 15
// Refresher loads local node info and sends them to the master node.
type Refresher struct {
2024-12-09 01:00:38 +00:00
configPath string
nodePath string
2024-12-08 01:30:07 +00:00
}
2024-12-09 01:00:38 +00:00
func NewRefresher(nodePath string, configPath string) *Refresher {
2024-12-08 01:30:07 +00:00
return &Refresher{
2024-12-09 01:00:38 +00:00
nodePath: nodePath,
configPath: configPath,
2024-12-08 01:30:07 +00:00
}
}
// Run loop the process that updates node in the master node.
func (r *Refresher) Loop() {
log.Println("Start refresher loop")
for {
log.Println("Refreshing node")
err := r.Refresh()
if err != nil {
log.Printf("failed to refresh node: %v\n", err)
}
time.Sleep(time.Second * refreshIntervalSeconds)
}
}
// Refresh loads labels from the local filesystem and sends them to the master node.
func (r *Refresher) Refresh() error {
// Load config
cfg, err := r.getConfig()
if err != nil {
return err
}
// Load labels
node, err := r.loadNode()
if err != nil {
return err
}
nodeBytes, err := json.Marshal(node)
if err != nil {
return fmt.Errorf("failed to marshal labels: %w", err)
}
// Send labels to master
req, err := http.NewRequest("POST", fmt.Sprintf("%s://%s:%d/nodes/%s", cfg.MasterProto, cfg.MasterHost, cfg.MasterPort, node.HostName), bytes.NewBuffer(nodeBytes))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+cfg.MasterToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected status: %s", resp.Status)
}
return nil
}
// Returns the node config.
func (r *Refresher) getConfig() (NodeConfig, error) {
cfg := NodeConfig{}
content, err := os.ReadFile(r.configPath)
if err != nil {
return cfg, fmt.Errorf("failed to read config file: %w", err)
}
err = json.Unmarshal(content, &cfg)
if err != nil {
return cfg, fmt.Errorf("failed to unmarshal config: %w", err)
}
if cfg.MasterProto == "" {
cfg.MasterProto = "http"
}
if cfg.MasterPort == 0 {
cfg.MasterPort = 1352
}
return cfg, nil
}
// TODO: rewrite this to load Node structure
func (r *Refresher) loadNode() (*nodes.Node, error) {
2024-12-09 01:00:38 +00:00
filePath := r.nodePath
2024-12-08 01:30:07 +00:00
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
err = r.initNodeFile()
if err != nil {
return nil, err
}
}
2024-12-09 01:00:38 +00:00
content, err := os.ReadFile(r.nodePath)
2024-12-08 01:30:07 +00:00
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
node := &nodes.Node{}
err = json.Unmarshal(content, &node)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal node: %w", err)
}
return node, err
}
func (r *Refresher) initNodeFile() error {
err := r.createNodePath()
if err != nil {
return fmt.Errorf("failed to create node path: %w", err)
}
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to get hostname: %w", err)
}
node := &nodes.Node{
HostName: hostname,
}
nodeBytes, err := json.MarshalIndent(node, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal node: %w", err)
}
2024-12-09 01:00:38 +00:00
err = os.WriteFile(r.nodePath, nodeBytes, 0640)
2024-12-08 01:30:07 +00:00
if err != nil {
return fmt.Errorf("failed to write node file: %w", err)
}
return nil
}
func (r *Refresher) createNodePath() error {
2024-12-09 01:00:38 +00:00
d := filepath.Dir(r.nodePath)
_, err := os.Stat(d)
2024-12-08 01:30:07 +00:00
if os.IsNotExist(err) {
2024-12-09 01:00:38 +00:00
err = os.MkdirAll(d, 0755)
2024-12-08 01:30:07 +00:00
if err != nil {
return fmt.Errorf("failed to create node dir: %w", err)
}
}
return nil
}