github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/main.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "os" 22 "runtime" 23 "strings" 24 25 "github.com/spf13/cobra" 26 "github.com/spf13/viper" 27 28 _ "net/http/pprof" 29 30 "github.com/hyperledger/fabric/common/flogging" 31 "github.com/hyperledger/fabric/core/config" 32 "github.com/hyperledger/fabric/peer/chaincode" 33 "github.com/hyperledger/fabric/peer/channel" 34 "github.com/hyperledger/fabric/peer/clilogging" 35 "github.com/hyperledger/fabric/peer/common" 36 "github.com/hyperledger/fabric/peer/node" 37 "github.com/hyperledger/fabric/peer/version" 38 ) 39 40 var logger = flogging.MustGetLogger("main") 41 var logOutput = os.Stderr 42 43 // Constants go here. 44 const cmdRoot = "core" 45 46 // The main command describes the service and 47 // defaults to printing the help message. 48 var mainCmd = &cobra.Command{ 49 Use: "peer", 50 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 51 // check for CORE_LOGGING_LEVEL environment variable, which should override 52 // all other log settings 53 loggingSpec := viper.GetString("logging_level") 54 55 if loggingSpec == "" { 56 // if CORE_LOGGING_LEVEL not set, use the value for 'peer' from core.yaml 57 loggingSpec = viper.GetString("logging.peer") 58 } 59 flogging.InitFromSpec(loggingSpec) 60 61 return nil 62 }, 63 Run: func(cmd *cobra.Command, args []string) { 64 if versionFlag { 65 fmt.Print(version.GetInfo()) 66 } else { 67 cmd.HelpFunc()(cmd, args) 68 } 69 }, 70 } 71 72 // Peer command version flag 73 var versionFlag bool 74 75 func main() { 76 // For environment variables. 77 viper.SetEnvPrefix(cmdRoot) 78 viper.AutomaticEnv() 79 replacer := strings.NewReplacer(".", "_") 80 viper.SetEnvKeyReplacer(replacer) 81 82 // Define command-line flags that are valid for all peer commands and 83 // subcommands. 84 mainFlags := mainCmd.PersistentFlags() 85 mainFlags.BoolVarP(&versionFlag, "version", "v", false, "Display current version of fabric peer server") 86 87 mainFlags.String("logging-level", "", "Default logging level and overrides, see core.yaml for full syntax") 88 viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level")) 89 testCoverProfile := "" 90 mainFlags.StringVarP(&testCoverProfile, "test.coverprofile", "", "coverage.cov", "Done") 91 92 err := common.InitConfig(cmdRoot) 93 if err != nil { // Handle errors reading the config file 94 panic(fmt.Errorf("Fatal error when initializing %s config : %s\n", cmdRoot, err)) 95 } 96 97 mainCmd.AddCommand(version.Cmd()) 98 mainCmd.AddCommand(node.Cmd()) 99 mainCmd.AddCommand(chaincode.Cmd(nil)) 100 mainCmd.AddCommand(clilogging.Cmd(nil)) 101 mainCmd.AddCommand(channel.Cmd(nil)) 102 103 runtime.GOMAXPROCS(viper.GetInt("peer.gomaxprocs")) 104 105 // setup system-wide logging backend based on settings from core.yaml 106 flogging.InitBackend(flogging.SetFormat(viper.GetString("logging.format")), logOutput) 107 108 // Init the MSP 109 var mspMgrConfigDir = config.GetPath("peer.mspConfigPath") 110 var mspID = viper.GetString("peer.localMspId") 111 err = common.InitCrypto(mspMgrConfigDir, mspID) 112 if err != nil { // Handle errors reading the config file 113 logger.Errorf("Cannot run peer because %s", err.Error()) 114 os.Exit(1) 115 } 116 // On failure Cobra prints the usage message and error string, so we only 117 // need to exit with a non-0 status 118 if mainCmd.Execute() != nil { 119 os.Exit(1) 120 } 121 logger.Info("Exiting.....") 122 }