netmaker/main.go

211 lines
5.5 KiB
Go
Raw Normal View History

2022-09-14 03:25:56 +08:00
// -build ee
2021-03-26 00:17:52 +08:00
package main
import (
"context"
"flag"
2021-08-10 02:56:27 +08:00
"fmt"
2021-07-21 05:18:45 +08:00
"os"
"os/signal"
"path/filepath"
2021-11-17 10:19:13 +08:00
"runtime/debug"
2021-07-21 05:18:45 +08:00
"sync"
"syscall"
2021-10-22 03:28:58 +08:00
"github.com/gravitl/netmaker/auth"
"github.com/gravitl/netmaker/config"
2021-07-21 05:18:45 +08:00
controller "github.com/gravitl/netmaker/controllers"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/functions"
2021-12-07 04:31:08 +08:00
"github.com/gravitl/netmaker/logger"
2021-10-22 03:28:58 +08:00
"github.com/gravitl/netmaker/logic"
2022-09-14 03:25:56 +08:00
"github.com/gravitl/netmaker/logic/pro"
2023-05-05 23:03:59 +08:00
"github.com/gravitl/netmaker/migrate"
2021-08-10 02:56:27 +08:00
"github.com/gravitl/netmaker/models"
2022-01-13 05:23:34 +08:00
"github.com/gravitl/netmaker/mq"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-07-21 05:18:45 +08:00
"github.com/gravitl/netmaker/servercfg"
"github.com/gravitl/netmaker/serverctl"
"golang.org/x/exp/slog"
2021-03-26 00:17:52 +08:00
)
2023-08-01 14:15:17 +08:00
var version = "v0.20.6"
2021-08-31 03:58:23 +08:00
// Start DB Connection and start API Request Handler
2021-03-26 00:17:52 +08:00
func main() {
absoluteConfigPath := flag.String("c", "", "absolute path to configuration file")
flag.Parse()
setupConfig(*absoluteConfigPath)
servercfg.SetVersion(version)
2021-08-10 02:56:27 +08:00
fmt.Println(models.RetrieveLogo()) // print the logo
initialize() // initial db and acls
2021-11-17 10:19:13 +08:00
setGarbageCollection()
setVerbosity()
if servercfg.DeployedByOperator() && !servercfg.Is_EE {
logic.SetFreeTierLimits()
}
defer database.CloseDB()
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
var waitGroup sync.WaitGroup
startControllers(&waitGroup, ctx) // start the api endpoint and mq and stun
<-ctx.Done()
waitGroup.Wait()
2021-07-21 05:18:45 +08:00
}
func setupConfig(absoluteConfigPath string) {
if len(absoluteConfigPath) > 0 {
cfg, err := config.ReadConfig(absoluteConfigPath)
if err != nil {
logger.Log(0, fmt.Sprintf("failed parsing config at: %s", absoluteConfigPath))
return
}
config.Config = cfg
}
}
2021-07-29 04:40:13 +08:00
func initialize() { // Client Mode Prereq Check
2021-07-21 05:18:45 +08:00
var err error
if servercfg.GetMasterKey() == "" {
logger.Log(0, "warning: MASTER_KEY not set, this could make account recovery difficult")
}
2022-02-01 22:59:23 +08:00
if servercfg.GetNodeID() == "" {
2022-02-15 05:19:19 +08:00
logger.FatalLog("error: must set NODE_ID, currently blank")
2022-02-01 22:59:23 +08:00
}
2021-07-29 04:40:13 +08:00
if err = database.InitializeDatabase(); err != nil {
2022-11-19 17:52:08 +08:00
logger.FatalLog("Error connecting to database: ", err.Error())
2021-07-29 04:40:13 +08:00
}
2021-12-07 04:31:08 +08:00
logger.Log(0, "database successfully connected")
2023-05-05 23:03:59 +08:00
migrate.Run()
2022-09-14 03:25:56 +08:00
logic.SetJWTSecret()
if err = pro.InitializeGroups(); err != nil {
logger.Log(0, "could not initialize default user group, \"*\"")
}
2022-01-26 00:00:56 +08:00
err = logic.TimerCheckpoint()
2022-01-21 06:50:42 +08:00
if err != nil {
2022-01-26 00:00:56 +08:00
logger.Log(1, "Timer error occurred: ", err.Error())
2022-01-21 06:50:42 +08:00
}
2022-09-15 01:26:31 +08:00
logic.EnterpriseCheck()
2022-09-14 03:25:56 +08:00
2021-10-22 03:28:58 +08:00
var authProvider = auth.InitializeAuthProvider()
if authProvider != "" {
2021-12-29 01:07:50 +08:00
logger.Log(0, "OAuth provider,", authProvider+",", "initialized")
2021-10-22 21:47:29 +08:00
} else {
2021-12-07 04:31:08 +08:00
logger.Log(0, "no OAuth provider found or not configured, continuing without OAuth")
2021-10-22 03:28:58 +08:00
}
2022-08-31 23:38:29 +08:00
err = serverctl.SetDefaults()
2022-03-14 09:35:22 +08:00
if err != nil {
2022-08-31 23:38:29 +08:00
logger.FatalLog("error setting defaults: ", err.Error())
2022-03-14 09:35:22 +08:00
}
2021-07-21 05:18:45 +08:00
if servercfg.IsDNSMode() {
err := functions.SetDNSDir()
2021-05-06 04:42:17 +08:00
if err != nil {
2021-12-07 04:31:08 +08:00
logger.FatalLog(err.Error())
2021-05-06 04:42:17 +08:00
}
}
if servercfg.IsMessageQueueBackend() {
if err = mq.ServerStartNotify(); err != nil {
logger.Log(0, "error occurred when notifying nodes of startup", err.Error())
}
}
2021-07-21 05:18:45 +08:00
}
2021-05-06 04:42:17 +08:00
func startControllers(wg *sync.WaitGroup, ctx context.Context) {
2021-07-21 05:18:45 +08:00
if servercfg.IsDNSMode() {
2021-10-27 00:27:29 +08:00
err := logic.SetDNS()
2021-07-21 05:18:45 +08:00
if err != nil {
2021-12-07 04:31:08 +08:00
logger.Log(0, "error occurred initializing DNS: ", err.Error())
2021-07-21 05:18:45 +08:00
}
}
2022-09-26 19:27:10 +08:00
2021-05-06 04:42:17 +08:00
//Run Rest Server
if servercfg.IsRestBackend() {
2021-07-21 05:18:45 +08:00
if !servercfg.DisableRemoteIPCheck() && servercfg.GetAPIHost() == "127.0.0.1" {
err := servercfg.SetHost()
if err != nil {
2021-12-07 04:31:08 +08:00
logger.FatalLog("Unable to Set host. Exiting...", err.Error())
2021-07-21 05:18:45 +08:00
}
}
wg.Add(1)
go controller.HandleRESTRequests(wg, ctx)
2021-03-26 00:17:52 +08:00
}
2022-01-13 05:23:34 +08:00
//Run MessageQueue
if servercfg.IsMessageQueueBackend() {
wg.Add(1)
go runMessageQueue(wg, ctx)
2022-01-13 05:23:34 +08:00
}
if !servercfg.IsRestBackend() && !servercfg.IsMessageQueueBackend() {
logger.Log(0, "No Server Mode selected, so nothing is being served! Set Rest mode (REST_BACKEND) or MessageQueue (MESSAGEQUEUE_BACKEND) to 'true'.")
2021-03-26 00:17:52 +08:00
}
2022-11-20 12:05:29 +08:00
wg.Add(1)
go logic.StartHookManager(ctx, wg)
}
2022-01-13 05:23:34 +08:00
// Should we be using a context vice a waitgroup????????????
func runMessageQueue(wg *sync.WaitGroup, ctx context.Context) {
2022-01-13 05:23:34 +08:00
defer wg.Done()
2023-02-28 03:52:50 +08:00
brokerHost, _ := servercfg.GetMessageQueueEndpoint()
logger.Log(0, "connecting to mq broker at", brokerHost)
2022-08-30 02:08:01 +08:00
mq.SetupMQTT()
2023-02-28 03:52:50 +08:00
if mq.IsConnected() {
logger.Log(0, "connected to MQ Broker")
} else {
logger.FatalLog("error connecting to MQ Broker")
}
defer mq.CloseClient()
go mq.Keepalive(ctx)
2023-01-27 21:27:54 +08:00
go func() {
peerUpdate := make(chan *models.Node)
go logic.ManageZombies(ctx, peerUpdate)
for nodeUpdate := range peerUpdate {
if err := mq.NodeUpdate(nodeUpdate); err != nil {
2023-01-27 21:34:54 +08:00
logger.Log(0, "failed to send peer update for deleted node: ", nodeUpdate.ID.String(), err.Error())
2023-01-27 21:27:54 +08:00
}
}
}()
<-ctx.Done()
logger.Log(0, "Message Queue shutting down")
2022-01-13 05:23:34 +08:00
}
func setVerbosity() {
verbose := int(servercfg.GetVerbosity())
logger.Verbosity = verbose
logLevel := &slog.LevelVar{}
replace := func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
a.Value = slog.StringValue(filepath.Base(a.Value.String()))
}
return a
}
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{AddSource: true, ReplaceAttr: replace, Level: logLevel}))
slog.SetDefault(logger)
switch verbose {
case 4:
logLevel.Set(slog.LevelDebug)
case 3:
logLevel.Set(slog.LevelInfo)
case 2:
logLevel.Set(slog.LevelWarn)
default:
logLevel.Set(slog.LevelError)
}
}
2021-11-17 10:19:13 +08:00
func setGarbageCollection() {
_, gcset := os.LookupEnv("GOGC")
if !gcset {
debug.SetGCPercent(ncutils.DEFAULT_GC_PERCENT)
}
}