mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-12 08:04:25 +08:00
Merge pull request #1546 from gravitl/bugfix_v0.15.2_pub_message_for_connect_disconnect
publish message after node connect/disconnect
This commit is contained in:
commit
44d0ef6565
2 changed files with 41 additions and 4 deletions
|
@ -24,9 +24,14 @@ func Connect(network string) error {
|
||||||
if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil {
|
if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = config.ModNodeConfig(&cfg.Node)
|
if err := setupMQTTSingleton(cfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := PublishNodeUpdate(cfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
daemon.Restart()
|
daemon.Restart()
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect - attempts to disconnect a node on given network
|
// Disconnect - attempts to disconnect a node on given network
|
||||||
|
@ -44,7 +49,12 @@ func Disconnect(network string) error {
|
||||||
if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil {
|
if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = config.ModNodeConfig(&cfg.Node)
|
if err := setupMQTTSingleton(cfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := PublishNodeUpdate(cfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
daemon.Restart()
|
daemon.Restart()
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,6 +232,33 @@ func NewTLSConfig(server string) (*tls.Config, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func setMQTTSingenton creates a connection to broker for single use (ie to publish a message)
|
||||||
|
// only to be called from cli (eg. connect/disconnect, join, leave) and not from daemon ---
|
||||||
|
func setupMQTTSingleton(cfg *config.ClientConfig) error {
|
||||||
|
opts := mqtt.NewClientOptions()
|
||||||
|
server := cfg.Server.Server
|
||||||
|
port := cfg.Server.MQPort
|
||||||
|
opts.AddBroker("ssl://" + server + ":" + port)
|
||||||
|
tlsConfig, err := NewTLSConfig(server)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(0, "failed to get TLS config for", server, err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
opts.SetTLSConfig(tlsConfig)
|
||||||
|
mqclient = mqtt.NewClient(opts)
|
||||||
|
var connecterr error
|
||||||
|
opts.SetClientID(ncutils.MakeRandomString(23))
|
||||||
|
if token := mqclient.Connect(); !token.WaitTimeout(30*time.Second) || token.Error() != nil {
|
||||||
|
logger.Log(0, "unable to connect to broker, retrying ...")
|
||||||
|
if token.Error() == nil {
|
||||||
|
connecterr = errors.New("connect timeout")
|
||||||
|
} else {
|
||||||
|
connecterr = token.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return connecterr
|
||||||
|
}
|
||||||
|
|
||||||
// setupMQTT creates a connection to broker and returns client
|
// setupMQTT creates a connection to broker and returns client
|
||||||
// this function is primarily used to create a connection to publish to the broker
|
// this function is primarily used to create a connection to publish to the broker
|
||||||
func setupMQTT(cfg *config.ClientConfig) error {
|
func setupMQTT(cfg *config.ClientConfig) error {
|
||||||
|
|
Loading…
Add table
Reference in a new issue