-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
227 lines (190 loc) · 7.56 KB
/
root.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
cliHandler "github.com/TheThingsNetwork/go-utils/handlers/cli"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/go-utils/log/apex"
"github.com/TheThingsNetwork/go-utils/log/grpc"
promlog "github.com/TheThingsNetwork/go-utils/log/prometheus"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/core/band"
esHandler "github.com/TheThingsNetwork/ttn/utils/elasticsearch/handler"
"github.com/apex/log"
jsonHandler "github.com/apex/log/handlers/json"
levelHandler "github.com/apex/log/handlers/level"
multiHandler "github.com/apex/log/handlers/multi"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tj/go-elastic"
"google.golang.org/grpc/grpclog"
"gopkg.in/redis.v5"
)
var cfgFile string
var logFile *os.File
var ctx ttnlog.Interface
// RootCmd is executed when ttn is executed without a subcommand
var RootCmd = &cobra.Command{
Use: "ttn",
Short: "The Things Network's backend servers",
Long: `ttn launches The Things Network's backend servers`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var logLevel = log.InfoLevel
if viper.GetBool("debug") {
logLevel = log.DebugLevel
}
var logHandlers []log.Handler
if !viper.GetBool("no-cli-logs") {
logHandlers = append(logHandlers, levelHandler.New(cliHandler.New(os.Stdout), logLevel))
}
if logFileLocation := viper.GetString("log-file"); logFileLocation != "" {
absLogFileLocation, err := filepath.Abs(logFileLocation)
if err != nil {
panic(err)
}
logFile, err = os.OpenFile(absLogFileLocation, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
if err == nil {
logHandlers = append(logHandlers, levelHandler.New(jsonHandler.New(logFile), logLevel))
}
}
if esServer := viper.GetString("elasticsearch"); esServer != "" {
esClient := elastic.New(esServer)
esClient.HTTPClient = &http.Client{
Timeout: 5 * time.Second,
}
esUsername := viper.GetString("elasticsearch-username")
esPassword := viper.GetString("elasticsearch-password")
if esUsername != "" {
esClient.SetAuthCredentials(esUsername, esPassword)
}
esPrefix := cmd.Name()
if prefix := viper.GetString("elasticsearch-prefix"); prefix != "" {
esPrefix = fmt.Sprintf("%s-%s", prefix, esPrefix)
}
logHandlers = append(logHandlers, levelHandler.New(esHandler.New(&esHandler.Config{
Client: esClient,
Prefix: esPrefix,
BufferSize: 10,
}), logLevel))
}
// Set the API/gRPC logger
ctx = promlog.Wrap(apex.Wrap(&log.Logger{
Handler: multiHandler.New(logHandlers...),
}))
ttnlog.Set(ctx)
grpclog.SetLogger(grpc.Wrap(ttnlog.Get()))
if viper.GetBool("allow-insecure") {
api.AllowInsecureFallback = true
}
ctx.WithFields(ttnlog.Fields{
"ComponentID": viper.GetString("id"),
"Description": viper.GetString("description"),
"Discovery Server Address": viper.GetString("discovery-address"),
"Auth Servers": viper.GetStringMapString("auth-servers"),
"Monitors": viper.GetStringMapString("monitor-servers"),
}).Info("Initializing The Things Network")
band.InitializeTables()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if logFile != nil {
logFile.Close()
}
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default \"$HOME/.ttn.yml\")")
RootCmd.PersistentFlags().Bool("no-cli-logs", false, "Disable CLI logs")
RootCmd.PersistentFlags().String("log-file", "", "Location of the log file")
RootCmd.PersistentFlags().String("elasticsearch", "", "Location of Elasticsearch server for logging")
RootCmd.PersistentFlags().String("elasticsearch-prefix", "", "Prefix of the ES index for logging - changes the index from \"<component>-<date>\" to \"<prefix>-<component>-<date>\"")
RootCmd.PersistentFlags().String("elasticsearch-username", "", "Username used to connect to the Elasticsearch server")
RootCmd.PersistentFlags().String("elasticsearch-password", "", "Password used to connect to the Elasticsearch server")
RootCmd.PersistentFlags().String("id", "", "The id of this component")
RootCmd.PersistentFlags().String("description", "", "The description of this component")
RootCmd.PersistentFlags().Bool("public", false, "Announce this component as part of The Things Network (public community network)")
RootCmd.PersistentFlags().String("discovery-address", "discover.thethingsnetwork.org:1900", "The address of the Discovery server")
RootCmd.PersistentFlags().String("auth-token", "", "The JWT token to be used for the discovery server")
RootCmd.PersistentFlags().Int("health-port", 0, "The port number where the health server should be started")
viper.SetDefault("auth-servers", map[string]string{
"ttn-account-v2": "https://account.thethingsnetwork.org",
})
dir, err := homedir.Dir()
if err == nil {
dir, _ = homedir.Expand(dir)
}
if dir == "" {
dir, err = os.Getwd()
if err != nil {
panic(err)
}
}
RootCmd.PersistentFlags().Bool("tls", true, "Use TLS")
RootCmd.PersistentFlags().String("min-tls-version", "", "Minimum TLS version")
RootCmd.PersistentFlags().Bool("allow-insecure", false, "Allow insecure fallback if TLS unavailable")
RootCmd.PersistentFlags().String("key-dir", path.Clean(dir+"/.ttn/"), "The directory where public/private keys are stored")
RootCmd.PersistentFlags().Int("eu-rx2-dr", 3, "RX2 data rate for the EU band (SF12=0,SF9=3)")
RootCmd.PersistentFlags().Int("us-fsb", 1, "Frequency sub-band for the US band (0-indexed)")
RootCmd.PersistentFlags().Int("au-fsb", 1, "Frequency sub-band for the AU band (0-indexed)")
viper.BindPFlags(RootCmd.PersistentFlags())
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetConfigType("yaml")
viper.SetConfigName(".ttn") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.SetEnvPrefix("ttn") // set environment prefix
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv() // read in environment variables that match
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
}
viper.BindEnv("debug")
// If a config file is found, read it in.
err := viper.ReadInConfig()
if err != nil {
fmt.Println("Error when reading config file:", err)
os.Exit(1)
} else if err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
// RedisConnectRetries indicates how many times the Redis connection should be retried
var RedisConnectRetries = 10
// RedisConnectRetryDelay indicates the time between Redis connection retries
var RedisConnectRetryDelay = 1 * time.Second
func connectRedis(client *redis.Client) error {
var err error
for retries := 0; retries < RedisConnectRetries; retries++ {
_, err = client.Ping().Result()
if err == nil {
break
}
ctx.WithError(err).Warn("Could not connect to Redis. Retrying...")
<-time.After(RedisConnectRetryDelay)
}
if err != nil {
client.Close()
return err
}
return nil
}