Skip to content
Snippets Groups Projects
root.go 3.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
    Copyright © 2021 da/net research group <danet.fbi.h-da.de>
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software
       without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
    */
    
    package cmd
    
    import (
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"context"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"code.fbi.h-da.de/danet/gosdn"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	log "github.com/sirupsen/logrus"
    	"github.com/spf13/cobra"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"github.com/spf13/viper"
    )
    
    
    var cfgFile string
    
    var loglevel string
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    var grpcPort string
    
    var csbiOrchestrstor string
    
    
    // rootCmd represents the base command when called without any subcommands
    var rootCmd = &cobra.Command{
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	Use:   "gosdn",
    	Short: "starts the gosdn controller",
    
    	Long: `Starts the gosdn controller and listens on port 8080
    
    for REST API calls.`,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	RunE: func(cmd *cobra.Command, args []string) error {
    		ctx, cancel := context.WithCancel(context.Background())
    		defer cancel()
    
    		return gosdn.Run(ctx)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	},
    
    }
    
    // Execute adds all child commands to the root command and sets flags appropriately.
    // This is called by main.main(). It only needs to happen once to the rootCmd.
    func Execute() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if err := rootCmd.Execute(); err != nil {
    		log.WithFields(log.Fields{}).Error(err)
    		os.Exit(1)
    	}
    
    }
    
    func init() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	cobra.OnInitialize(initConfig)
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./configs/gosdn.toml)")
    	rootCmd.PersistentFlags().StringVarP(&loglevel, "log-level", "l", "", "log level 'debug' or 'trace'")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	rootCmd.Flags().StringVar(&grpcPort, "grpc-port", "55055", "port for gRPC NBI")
    
    	rootCmd.Flags().StringVar(&csbiOrchestrstor, "csbi-orchestrator", "localhost:55056", "csbi orchestrator address")
    
    }
    
    // initConfig reads in config file and ENV variables if set.
    func initConfig() {
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	if cfgFile != "" {
    		// Use config file from the flag.
    		viper.SetConfigFile(cfgFile)
    	} else {
    		viper.AddConfigPath("./configs")
    		viper.AddConfigPath("/usr/local/etc/gosdn/")
    		viper.SetConfigType("toml")
    		viper.SetConfigName("gosdn")
    	}
    
    	viper.AutomaticEnv() // read in environment variables that match
    
    	// If a config file is found, read it in.
    	if err := viper.ReadInConfig(); err == nil {
    		log.Debug("Using config file:", viper.ConfigFileUsed())
    	}
    
    
    	viper.SetDefault("socket", ":55055")
    	viper.SetDefault("csbi-orchestrator", "localhost:55056")
    
    	ll := viper.GetString("GOSDN_LOG")
    	if ll != "" {
    		loglevel = ll
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	log.SetReportCaller(true)
    	switch loglevel {
    	case "trace":
    		log.SetLevel(log.TraceLevel)
    	case "debug":
    		log.SetLevel(log.DebugLevel)
    	default:
    		log.SetLevel(log.InfoLevel)
    		log.SetFormatter(&log.JSONFormatter{})
    		log.SetReportCaller(false)
    	}