forked from nginx/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (75 loc) · 2.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"context"
"os"
"strconv"
"github.com/nginx/agent/sdk/v2/agent/events"
sdkGRPC "github.com/nginx/agent/sdk/v2/grpc"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
"github.com/nginx/agent/v2/src/core/logger"
"github.com/nginx/agent/v2/src/plugins"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
// set at buildtime
commit = ""
version = ""
env = &core.EnvironmentType{}
)
func main() {
config.InitFlags(version, commit)
config.RegisterRunner(func(cmd *cobra.Command, _ []string) {
config.InitConfigurationFiles()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
loadedConfig, err := config.GetConfig(env.GetSystemUUID())
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
eventMeta := events.NewAgentEventMeta(
config.MODULE,
version,
strconv.Itoa(os.Getpid()),
env.GetHostname(),
env.GetSystemUUID(),
loadedConfig.InstanceGroup,
loadedConfig.Tags)
logger.SetLogLevel(loadedConfig.Log.Level)
logFile := logger.SetLogFile(loadedConfig.Log.Path)
if logFile != nil {
defer logFile.Close()
}
if config.MigratedEnv {
log.Warnf("The environment variable prefix 'NMS' is deprecated. Prefix has been migrated to 'NGINX_AGENT'. Please update your configuration to use the new prefix.")
}
log.Tracef("Config loaded from disk, %v", loadedConfig)
if loadedConfig.DisplayName == "" {
loadedConfig.DisplayName = env.GetHostname()
log.Infof("setting displayName to %s", loadedConfig.DisplayName)
}
log.Infof("NGINX Agent %s at %s with pid %d, clientID=%s name=%s features=%v",
version, commit, os.Getpid(), loadedConfig.ClientID, loadedConfig.DisplayName, loadedConfig.Features)
sdkGRPC.InitMeta(loadedConfig.ClientID, loadedConfig.CloudAccountID)
controller, commander, reporter := core.CreateGrpcClients(ctx, loadedConfig)
if controller != nil {
go controller.Connect()
}
binary := core.NewNginxBinary(env, loadedConfig)
corePlugins, extensionPlugins := plugins.LoadPlugins(commander, binary, env, reporter, loadedConfig, eventMeta)
pipe := core.InitializePipe(ctx, corePlugins, extensionPlugins, loadedConfig.QueueSize)
pipe.Process(core.NewMessage(core.AgentStarted, eventMeta))
core.HandleSignals(ctx, commander, loadedConfig, env, pipe, cancel, controller)
pipe.Run()
})
if err := config.Execute(); err != nil {
log.Fatal(err)
}
}