cmd

package
v2.9.4+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2018 License: MIT Imports: 51 Imported by: 31

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RedisConnectRetries = 10

RedisConnectRetries indicates how many times the Redis connection should be retried

View Source
var RedisConnectRetryDelay = 1 * time.Second

RedisConnectRetryDelay indicates the time between Redis connection retries

View Source
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))
		}

		ctx = 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")
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		if logFile != nil {
			logFile.Close()
		}
	},
}

RootCmd is executed when ttn is executed without a subcommand

Functions

func Execute

func Execute()

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.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL