2021-08-31 14:26:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-02 00:13:12 +00:00
|
|
|
"context"
|
2021-08-31 14:26:09 +00:00
|
|
|
"log"
|
2021-09-02 00:13:12 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-09-01 21:18:52 +00:00
|
|
|
"strconv"
|
2021-09-02 00:13:12 +00:00
|
|
|
"syscall"
|
2021-08-31 14:26:09 +00:00
|
|
|
"time"
|
|
|
|
|
2021-09-06 22:46:48 +00:00
|
|
|
"github.com/by-cx/lobby/common"
|
|
|
|
"github.com/by-cx/lobby/nats_driver"
|
|
|
|
"github.com/by-cx/lobby/redis_driver"
|
|
|
|
"github.com/by-cx/lobby/server"
|
2021-08-31 14:26:09 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
var discoveryStorage server.Discoveries = server.Discoveries{}
|
2021-09-02 17:22:39 +00:00
|
|
|
var driver common.Driver
|
2021-09-04 20:17:56 +00:00
|
|
|
var localHost server.LocalHost
|
2021-09-15 14:58:09 +00:00
|
|
|
var lastLocalHostname string
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
var config Config
|
|
|
|
|
2021-09-02 00:13:12 +00:00
|
|
|
var shuttingDown bool
|
2021-09-09 16:25:22 +00:00
|
|
|
var sendDiscoveryPacketTrigger chan bool = make(chan bool)
|
2021-09-02 00:13:12 +00:00
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
func init() {
|
2021-09-02 17:22:39 +00:00
|
|
|
// Load config from environment variables
|
|
|
|
config = *GetConfig()
|
|
|
|
|
|
|
|
// Setup discovery storage
|
2021-08-31 14:26:09 +00:00
|
|
|
discoveryStorage.LogChannel = make(chan string)
|
2021-09-02 17:22:39 +00:00
|
|
|
discoveryStorage.TTL = config.TTL
|
|
|
|
|
2021-09-04 20:17:56 +00:00
|
|
|
// localhost initization
|
|
|
|
localHost = server.LocalHost{
|
|
|
|
LabelsPath: config.LabelsPath,
|
|
|
|
HostnameOverride: config.HostName,
|
|
|
|
InitialLabels: config.Labels,
|
|
|
|
RuntimeLabelsFilename: config.RuntimeLabelsFilename,
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
// Setup driver
|
2021-09-06 22:46:48 +00:00
|
|
|
if config.Driver == "NATS" {
|
|
|
|
driver = &nats_driver.Driver{
|
|
|
|
NATSUrl: config.NATSURL,
|
|
|
|
NATSDiscoveryChannel: config.NATSDiscoveryChannel,
|
2021-09-02 17:22:39 +00:00
|
|
|
|
2021-09-06 22:46:48 +00:00
|
|
|
LogChannel: discoveryStorage.LogChannel,
|
|
|
|
}
|
|
|
|
} else if config.Driver == "Redis" {
|
|
|
|
driver = &redis_driver.Driver{
|
|
|
|
Host: config.RedisHost,
|
|
|
|
Port: uint(config.RedisPort),
|
|
|
|
Password: config.RedisPassword,
|
|
|
|
Channel: config.RedisChannel,
|
|
|
|
DB: uint(config.RedisDB),
|
|
|
|
|
|
|
|
LogChannel: discoveryStorage.LogChannel,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Fatalf("unsupported driver %s", config.Driver)
|
2021-09-02 17:22:39 +00:00
|
|
|
}
|
2021-09-06 22:46:48 +00:00
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanDiscoveryPool clears the local server map and keeps only the alive servers
|
|
|
|
func cleanDiscoveryPool() {
|
|
|
|
for {
|
|
|
|
discoveryStorage.Clean()
|
2021-09-01 21:18:52 +00:00
|
|
|
time.Sleep(time.Duration(config.CleanEvery) * time.Second)
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:13:12 +00:00
|
|
|
// sendGoodbyePacket is almost same as sendDiscoveryPacket but it's not running in loop
|
|
|
|
// and it adds goodbye message so other nodes know this node is gonna die.
|
2021-09-02 17:22:39 +00:00
|
|
|
func sendGoodbyePacket() {
|
2021-09-04 20:17:56 +00:00
|
|
|
discovery, err := localHost.GetIdentification()
|
2021-09-02 00:13:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("sending discovery identification error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
err = driver.SendGoodbyePacket(discovery)
|
2021-09-02 00:13:12 +00:00
|
|
|
if err != nil {
|
2021-09-02 17:22:39 +00:00
|
|
|
log.Println(err)
|
2021-09-02 00:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:25:22 +00:00
|
|
|
// sendDiscoveryPacket sends a single discovery packet out
|
2021-09-02 17:22:39 +00:00
|
|
|
func sendDiscoveryPacket() {
|
2021-09-09 16:25:22 +00:00
|
|
|
sendDiscoveryPacketTrigger <- true
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
|
2021-09-09 16:25:22 +00:00
|
|
|
// sendDisoveryPacket sends discovery packet to the driver which passes it to the
|
|
|
|
// other nodes. By this it propagates any change that happens in the local discovery struct.
|
|
|
|
// Every tune trigger is triggered it sends one message.
|
|
|
|
func sendDiscoveryPacketTask(trigger chan bool) {
|
|
|
|
for {
|
|
|
|
// We are waiting for the trigger
|
|
|
|
<-trigger
|
|
|
|
|
|
|
|
if !shuttingDown {
|
2021-09-15 14:58:09 +00:00
|
|
|
// Get info about local machine and send to the exchange point
|
2021-09-09 16:25:22 +00:00
|
|
|
discovery, err := localHost.GetIdentification()
|
|
|
|
if err != nil {
|
2021-09-09 16:35:32 +00:00
|
|
|
log.Printf("sending discovery identification error: %v\n", err)
|
2021-09-09 16:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = driver.SendDiscoveryPacket(discovery)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2021-09-15 14:58:09 +00:00
|
|
|
|
|
|
|
// If local hostname changes, we will deregister it
|
|
|
|
if discovery.Hostname != lastLocalHostname && lastLocalHostname != "" {
|
|
|
|
log.Println("Hostname change detected, deregistering the old one")
|
|
|
|
err = driver.SendGoodbyePacket(server.Discovery{
|
|
|
|
Hostname: lastLocalHostname,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastLocalHostname = discovery.Hostname
|
2021-09-09 16:35:32 +00:00
|
|
|
} else {
|
2021-09-02 00:13:12 +00:00
|
|
|
break
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print logs acquired from disovery storage
|
|
|
|
func printDiscoveryLogs() {
|
|
|
|
for {
|
|
|
|
logMessage := <-discoveryStorage.LogChannel
|
|
|
|
log.Println(logMessage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Closing the logging channel
|
|
|
|
defer close(discoveryStorage.LogChannel)
|
2021-09-02 17:22:39 +00:00
|
|
|
defer driver.Close()
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Server discovering stuff
|
|
|
|
// ------------------------
|
|
|
|
|
2021-09-11 09:58:27 +00:00
|
|
|
// Setup callback function to register and unregister discovery packets from other servers
|
2021-09-02 17:22:39 +00:00
|
|
|
driver.RegisterSubscribeFunction(func(d server.Discovery) {
|
2021-09-15 14:58:09 +00:00
|
|
|
// Check if the local version and the new version are somehowe changed
|
2021-09-11 09:58:27 +00:00
|
|
|
localVersion := discoveryStorage.Get(d.Hostname)
|
|
|
|
exists := discoveryStorage.Exist(d.Hostname)
|
|
|
|
changed := server.Compare(localVersion, d)
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
discoveryStorage.Add(d)
|
2021-09-11 09:58:27 +00:00
|
|
|
|
|
|
|
if changed {
|
|
|
|
// Print this only if the server is already registered
|
|
|
|
if exists {
|
|
|
|
log.Printf("%s has been updated", d.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err = discoveryChange(d)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("discovery changed error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
})
|
|
|
|
driver.RegisterUnsubscribeFunction(func(d server.Discovery) {
|
|
|
|
discoveryStorage.Delete(d.Hostname)
|
2021-09-11 09:58:27 +00:00
|
|
|
go func() {
|
|
|
|
err = discoveryChange(d)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("discovery changed error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-09-02 17:22:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err = driver.Init()
|
2021-08-31 14:26:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go printDiscoveryLogs()
|
|
|
|
go cleanDiscoveryPool()
|
2021-09-16 23:36:20 +00:00
|
|
|
|
|
|
|
if len(config.Callback) > 0 {
|
|
|
|
go discoveryChangeLoop()
|
|
|
|
go changeCatcherLoop()
|
|
|
|
}
|
2021-09-15 14:58:09 +00:00
|
|
|
|
|
|
|
// When the daemon boots up we trigger discovery change event so the config can be setup via callback script if there is any
|
|
|
|
err = discoveryChange(server.Discovery{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("discovery changed error: %v", err)
|
|
|
|
}
|
2021-09-02 17:22:39 +00:00
|
|
|
// If config.Register is false this instance won't be registered with other nodes
|
|
|
|
if config.Register {
|
2021-09-09 16:25:22 +00:00
|
|
|
// This is background process that sends the message
|
|
|
|
go sendDiscoveryPacketTask(sendDiscoveryPacketTrigger)
|
|
|
|
|
|
|
|
// This triggers the process
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
sendDiscoveryPacket()
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(config.KeepAlive) * time.Second)
|
|
|
|
|
|
|
|
if shuttingDown {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-09-02 17:22:39 +00:00
|
|
|
} else {
|
|
|
|
log.Println("standalone mode, I won't register myself")
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
// --------
|
|
|
|
// REST API
|
|
|
|
// --------
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middleware
|
2021-09-01 21:18:52 +00:00
|
|
|
if len(config.Token) > 0 {
|
|
|
|
e.Use(TokenMiddleware)
|
|
|
|
}
|
2021-08-31 14:26:09 +00:00
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
// Routes
|
2021-09-06 23:18:17 +00:00
|
|
|
if !config.DisableAPI {
|
2021-09-06 22:46:48 +00:00
|
|
|
e.GET("/", listHandler)
|
2021-09-20 14:10:52 +00:00
|
|
|
e.GET("/v1/resolve", resolveHandler)
|
2021-09-06 22:46:48 +00:00
|
|
|
e.GET("/v1/discovery", getIdentificationHandler)
|
|
|
|
e.GET("/v1/discoveries", listHandler)
|
|
|
|
e.POST("/v1/labels", addLabelsHandler)
|
|
|
|
e.DELETE("/v1/labels", deleteLabelsHandler)
|
|
|
|
e.GET("/v1/prometheus/:name", prometheusHandler)
|
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
|
2021-09-02 00:13:12 +00:00
|
|
|
// ------------------------------
|
|
|
|
// Termination signals processing
|
|
|
|
// ------------------------------
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
2021-09-02 17:22:39 +00:00
|
|
|
go func(e *echo.Echo, config Config) {
|
2021-09-02 00:13:12 +00:00
|
|
|
sig := <-signals
|
|
|
|
shuttingDown = true
|
2021-09-02 17:22:39 +00:00
|
|
|
if config.Register {
|
|
|
|
log.Printf("%s signal received, sending goodbye packet\n", sig.String())
|
|
|
|
sendGoodbyePacket()
|
2021-09-06 16:34:38 +00:00
|
|
|
time.Sleep(1 * time.Second) // we wait for a few seconds to let background jobs to finish their job
|
2021-09-02 17:22:39 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("%s signal received", sig.String())
|
|
|
|
}
|
2021-09-02 00:13:12 +00:00
|
|
|
e.Shutdown(context.TODO())
|
2021-09-02 17:22:39 +00:00
|
|
|
}(e, config)
|
2021-08-31 14:26:09 +00:00
|
|
|
// Start server
|
2021-09-06 16:34:38 +00:00
|
|
|
// In most cases this will end expectedly so it doesn't make sense to use the echo's approach to treat this message as an error.
|
|
|
|
e.Logger.Info(e.Start(config.Host + ":" + strconv.Itoa(int(config.Port))))
|
2021-08-31 14:26:09 +00:00
|
|
|
}
|