mirror of
https://github.com/0x1d/rcond.git
synced 2025-12-14 18:25:21 +01:00
feat: apply configuration on start
This commit is contained in:
@@ -69,19 +69,19 @@ func (a *Agent) Event(event ClusterEvent) error {
|
||||
|
||||
// Members returns a list of members in the Serf cluster.
|
||||
func (a *Agent) Members() ([]serf.Member, error) {
|
||||
log.Printf("Getting members of the cluster")
|
||||
log.Printf("[INFO] Getting members of the cluster")
|
||||
return a.Serf.Members(), nil
|
||||
}
|
||||
|
||||
// Join attempts to join the Serf cluster with the given addresses, optionally ignoring old nodes.
|
||||
func (a *Agent) Join(addrs []string, ignoreOld bool) (int, error) {
|
||||
log.Printf("Joining nodes in the cluster: %v", addrs)
|
||||
log.Printf("[INFO] Joining nodes in the cluster: %v", addrs)
|
||||
n, err := a.Serf.Join(addrs, ignoreOld)
|
||||
if err != nil {
|
||||
log.Printf("Failed to join nodes in the cluster: %v", err)
|
||||
log.Printf("[ERROR] Failed to join nodes in the cluster: %v", err)
|
||||
return 0, err
|
||||
}
|
||||
log.Printf("Joined %d nodes in the cluster", n)
|
||||
log.Printf("[INFO] Joined %d nodes in the cluster", n)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -92,24 +92,24 @@ func (a *Agent) Leave() error {
|
||||
|
||||
// Shutdown shuts down the Serf cluster agent.
|
||||
func (a *Agent) Shutdown() error {
|
||||
log.Printf("Shutting down cluster agent")
|
||||
log.Printf("[INFO] Shutting down cluster agent")
|
||||
return a.Serf.Shutdown()
|
||||
}
|
||||
|
||||
// handleEvents handles Serf events received on the event channel.
|
||||
func handleEvents(eventCh chan serf.Event, clusterEvents map[string]func([]byte)) {
|
||||
eventHandlers := clusterEvents
|
||||
for event := range eventCh {
|
||||
switch event.EventType() {
|
||||
case serf.EventUser:
|
||||
userEvent := event.(serf.UserEvent)
|
||||
eventHandlers := clusterEvents
|
||||
if handler, ok := eventHandlers[userEvent.Name]; ok {
|
||||
handler(userEvent.Payload)
|
||||
} else {
|
||||
log.Printf("No event handler found for event: %s", userEvent.Name)
|
||||
log.Printf("[INFO] No event handler found for event: %s", userEvent.Name)
|
||||
}
|
||||
default:
|
||||
log.Printf("Received event: %s\n", event.EventType())
|
||||
log.Printf("[INFO] Received event: %s\n", event.EventType())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,18 +17,18 @@ func ClusterEventsMap() map[string]func([]byte) {
|
||||
|
||||
func restart(payload []byte) {
|
||||
if err := system.Restart(); err != nil {
|
||||
log.Printf("(ClusterEvent:restart) failed: %s", err)
|
||||
log.Printf("[ERROR] (ClusterEvent:restart) failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func shutdown(payload []byte) {
|
||||
if err := system.Shutdown(); err != nil {
|
||||
log.Printf("(ClusterEvent:shutdown) failed: %s", err)
|
||||
log.Printf("[ERROR] (ClusterEvent:shutdown) failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// just a sample function to test event functionality
|
||||
func printHostname(payload []byte) {
|
||||
hostname, _ := network.GetHostname()
|
||||
log.Printf("(ClusterEvent:printHostname): %s", hostname)
|
||||
log.Printf("[INFO] (ClusterEvent:printHostname): %s", hostname)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Rcond RcondConfig `yaml:"rcond"`
|
||||
Cluster ClusterConfig `yaml:"cluster"`
|
||||
Hostname string `yaml:"hostname"`
|
||||
Rcond RcondConfig `yaml:"rcond"`
|
||||
Network NetworkConfig `yaml:"network"`
|
||||
Cluster ClusterConfig `yaml:"cluster"`
|
||||
}
|
||||
|
||||
type RcondConfig struct {
|
||||
@@ -16,6 +18,25 @@ type RcondConfig struct {
|
||||
ApiToken string `yaml:"api_token"`
|
||||
}
|
||||
|
||||
type NetworkConfig struct {
|
||||
Connections []ConnectionConfig `yaml:"connections"`
|
||||
}
|
||||
|
||||
type ConnectionConfig struct {
|
||||
Type string `yaml:"type,omitempty"`
|
||||
UUID string `yaml:"uuid,omitempty"`
|
||||
ID string `yaml:"id,omitempty"`
|
||||
AutoConnect bool `yaml:"autoconnect,omitempty"`
|
||||
SSID string `yaml:"ssid,omitempty"`
|
||||
Mode string `yaml:"mode,omitempty"`
|
||||
Band string `yaml:"band,omitempty"`
|
||||
Channel uint32 `yaml:"channel,omitempty"`
|
||||
KeyMgmt string `yaml:"keymgmt,omitempty"`
|
||||
PSK string `yaml:"psk,omitempty"`
|
||||
IPv4Method string `yaml:"ipv4method,omitempty"`
|
||||
IPv6Method string `yaml:"ipv6method,omitempty"`
|
||||
}
|
||||
|
||||
type ClusterConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
NodeName string `yaml:"node_name"`
|
||||
|
||||
@@ -11,11 +11,11 @@ import (
|
||||
func WithDbus(fn func(*dbus.Conn) error) error {
|
||||
conn, err := dbus.SystemBus()
|
||||
if err != nil {
|
||||
log.Printf("Failed to connect to system bus: %v", err)
|
||||
log.Printf("[ERROR] Failed to connect to system bus: %v", err)
|
||||
return err
|
||||
}
|
||||
if err := fn(conn); err != nil {
|
||||
log.Print(err)
|
||||
log.Printf("[ERROR] Failed to execute D-Bus function: %s", err)
|
||||
return err
|
||||
}
|
||||
conn.Close()
|
||||
|
||||
Reference in New Issue
Block a user