Skip to content

Commit 6aeb59c

Browse files
lehorsmastersingh24
authored andcommitted
[FAB-10523] Fix peer command
This CR does two main things: 1) Patch error msg when core.yaml is not found 2) Restore the use of version and help commands in case of config error. The version of Viper currently used gives a confusing error message when the config file is not found: $ ./peer version 2018-06-02 21:00:42.397 EDT [main] main -> ERRO 001 Fatal error when initializing core config : error when reading core config file: Unsupported Config Type "" With this update the error clearly spells out what the problem is: $ .build/bin/peer node start 2018-07-17 09:17:54.148 CEST [main] main -> ERRO 001 Fatal error when initializing core config : Could not find config file. Please make sure that FABRIC_CFG_PATH or --configPath is set to a path which contains core.yaml In addition, if really only the version is requeted this works even if the config file isn't found. Same is true with help. $ ./peer version peer: Version: 1.3.0 Commit SHA: 27cda7a1a Go version: go1.10.3 OS/Arch: darwin/amd64 Experimental features: true Chaincode: Base Image Version: 0.4.10 Base Docker Namespace: hyperledger Base Docker Label: org.hyperledger.fabric Docker Namespace: hyperledger Patch-set 2: limit change to merely what's needed to address the problem Patch-set 3: fix error msg displayed by Fabric when config file is not found, limit init config to commands that need it, plus minor fix on format of crypto config not found error msg. Patch-set 4: fix unit test depending on modified error msg. Patch-set 5: some clean up to fix a few more unit tests Patch-set 6: more clean up Change-Id: I7c84c121c1493c48ea92c83b62ecd99c887ca316 Signed-off-by: Arnaud J Le Hors <lehors@us.ibm.com>
1 parent 3d866a7 commit 6aeb59c

File tree

9 files changed

+98
-66
lines changed

9 files changed

+98
-66
lines changed

Diff for: peer/chaincode/chaincode.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ var (
8787
)
8888

8989
var chaincodeCmd = &cobra.Command{
90-
Use: chainFuncName,
91-
Short: fmt.Sprint(chainCmdDes),
92-
Long: fmt.Sprint(chainCmdDes),
93-
PersistentPreRun: common.SetOrdererEnv,
90+
Use: chainFuncName,
91+
Short: fmt.Sprint(chainCmdDes),
92+
Long: fmt.Sprint(chainCmdDes),
93+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
94+
common.InitCmd(cmd, args)
95+
common.SetOrdererEnv(cmd, args)
96+
},
9497
}
9598

9699
var flags *pflag.FlagSet

Diff for: peer/chaincode/flags_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package chaincode
99
import (
1010
"testing"
1111

12+
"github.com/hyperledger/fabric/peer/common"
1213
"github.com/spf13/cobra"
1314
"github.com/spf13/viper"
1415
"github.com/stretchr/testify/assert"
@@ -34,7 +35,11 @@ func TestOrdererFlags(t *testing.T) {
3435
assert.Equal(t, sn, viper.GetString("orderer.tls.serverhostoverride"))
3536
assert.Equal(t, true, viper.GetBool("orderer.tls.enabled"))
3637
assert.Equal(t, true, viper.GetBool("orderer.tls.clientAuthRequired"))
37-
}}
38+
},
39+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
40+
common.SetOrdererEnv(cmd, args)
41+
},
42+
}
3843

3944
runCmd := Cmd(nil)
4045

Diff for: peer/channel/channel.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ func attachFlags(cmd *cobra.Command, names []string) {
9191
}
9292

9393
var channelCmd = &cobra.Command{
94-
Use: "channel",
95-
Short: "Operate a channel: create|fetch|join|list|update|signconfigtx|getinfo.",
96-
Long: "Operate a channel: create|fetch|join|list|update|signconfigtx|getinfo.",
97-
PersistentPreRun: common.SetOrdererEnv,
94+
Use: "channel",
95+
Short: "Operate a channel: create|fetch|join|list|update|signconfigtx|getinfo.",
96+
Long: "Operate a channel: create|fetch|join|list|update|signconfigtx|getinfo.",
97+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
98+
common.InitCmd(cmd, args)
99+
common.SetOrdererEnv(cmd, args)
100+
},
98101
}
99102

100103
type BroadcastClientFactory func() (common.BroadcastClient, error)

Diff for: peer/channel/flags_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package channel
99
import (
1010
"testing"
1111

12+
"github.com/hyperledger/fabric/peer/common"
1213
"github.com/spf13/cobra"
1314
"github.com/spf13/viper"
1415
"github.com/stretchr/testify/assert"
@@ -35,7 +36,11 @@ func TestOrdererFlags(t *testing.T) {
3536
assert.Equal(t, sn, viper.GetString("orderer.tls.serverhostoverride"))
3637
assert.Equal(t, true, viper.GetBool("orderer.tls.enabled"))
3738
assert.Equal(t, true, viper.GetBool("orderer.tls.clientAuthRequired"))
38-
}}
39+
},
40+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
41+
common.SetOrdererEnv(cmd, args)
42+
},
43+
}
3944

4045
runCmd := Cmd(nil)
4146

Diff for: peer/clilogging/logging.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/hyperledger/fabric/common/flogging"
23+
"github.com/hyperledger/fabric/peer/common"
2324

2425
"github.com/spf13/cobra"
2526
)
@@ -41,7 +42,8 @@ func Cmd(cf *LoggingCmdFactory) *cobra.Command {
4142
}
4243

4344
var loggingCmd = &cobra.Command{
44-
Use: loggingFuncName,
45-
Short: fmt.Sprint(loggingCmdDes),
46-
Long: fmt.Sprint(loggingCmdDes),
45+
Use: loggingFuncName,
46+
Short: fmt.Sprint(loggingCmdDes),
47+
Long: fmt.Sprint(loggingCmdDes),
48+
PersistentPreRun: common.InitCmd,
4749
}

Diff for: peer/common/common.go

+59-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ package common
99
import (
1010
"crypto/tls"
1111
"fmt"
12+
"io"
1213
"io/ioutil"
1314
"os"
15+
"runtime"
1416
"strings"
1517
"time"
1618

@@ -29,12 +31,14 @@ import (
2931
putils "github.com/hyperledger/fabric/protos/utils"
3032
"github.com/op/go-logging"
3133
"github.com/pkg/errors"
34+
"github.com/spf13/cobra"
3235
"github.com/spf13/viper"
3336
"golang.org/x/net/context"
3437
)
3538

3639
// UndefinedParamValue defines what undefined parameters in the command line will initialise to
3740
const UndefinedParamValue = ""
41+
const CmdRoot = "core"
3842

3943
var (
4044
defaultConnTimeout = 3 * time.Second
@@ -71,6 +75,9 @@ var (
7175

7276
// GetCertificateFnc is a function that returns the client TLS certificate
7377
GetCertificateFnc func() (tls.Certificate, error)
78+
79+
mainLogger *logging.Logger
80+
logOutput io.Writer
7481
)
7582

7683
type commonClient struct {
@@ -87,18 +94,29 @@ func init() {
8794
GetDeliverClientFnc = GetDeliverClient
8895
GetPeerDeliverClientFnc = GetPeerDeliverClient
8996
GetCertificateFnc = GetCertificate
97+
mainLogger = flogging.MustGetLogger("main")
98+
logOutput = os.Stderr
9099
}
91100

92101
// InitConfig initializes viper config
93102
func InitConfig(cmdRoot string) error {
103+
94104
err := config.InitViper(nil, cmdRoot)
95105
if err != nil {
96106
return err
97107
}
98108

99109
err = viper.ReadInConfig() // Find and read the config file
100110
if err != nil { // Handle errors reading the config file
101-
return errors.WithMessage(err, fmt.Sprintf("error when reading %s config file", cmdRoot))
111+
// The version of Viper we use claims the config type isn't supported when in fact the file hasn't been found
112+
// Display a more helpful message to avoid confusing the user.
113+
if strings.Contains(fmt.Sprint(err), "Unsupported Config Type") {
114+
return errors.New(fmt.Sprintf("Could not find config file. "+
115+
"Please make sure that FABRIC_CFG_PATH or --configPath is set to a path "+
116+
"which contains %s.yaml", cmdRoot))
117+
} else {
118+
return errors.WithMessage(err, fmt.Sprintf("error when reading %s config file", cmdRoot))
119+
}
102120
}
103121

104122
return nil
@@ -111,7 +129,7 @@ func InitCrypto(mspMgrConfigDir, localMSPID, localMSPType string) error {
111129
fi, err := os.Stat(mspMgrConfigDir)
112130
if os.IsNotExist(err) || !fi.IsDir() {
113131
// No need to try to load MSP from folder which is not available
114-
return errors.Errorf("cannot init crypto, missing %s folder", mspMgrConfigDir)
132+
return errors.Errorf("cannot init crypto, folder \"%s\" does not exist", mspMgrConfigDir)
115133
}
116134
// Check whether localMSPID exists
117135
if localMSPID == "" {
@@ -277,3 +295,42 @@ func configFromEnv(prefix string) (address, override string, clientConfig comm.C
277295
clientConfig.SecOpts = secOpts
278296
return
279297
}
298+
299+
func InitCmd(cmd *cobra.Command, args []string) {
300+
301+
err := InitConfig(CmdRoot)
302+
if err != nil { // Handle errors reading the config file
303+
mainLogger.Errorf("Fatal error when initializing %s config : %s", CmdRoot, err)
304+
os.Exit(1)
305+
}
306+
307+
// setup system-wide logging backend based on settings from core.yaml
308+
flogging.InitBackend(flogging.SetFormat(viper.GetString("logging.format")), logOutput)
309+
310+
// check for --logging-level pflag first, which should override all other
311+
// log settings. if --logging-level is not set, use CORE_LOGGING_LEVEL
312+
// (environment variable takes priority; otherwise, the value set in
313+
// core.yaml)
314+
var loggingSpec string
315+
if viper.GetString("logging_level") != "" {
316+
loggingSpec = viper.GetString("logging_level")
317+
} else {
318+
loggingSpec = viper.GetString("logging.level")
319+
}
320+
flogging.InitFromSpec(loggingSpec)
321+
322+
// Init the MSP
323+
var mspMgrConfigDir = config.GetPath("peer.mspConfigPath")
324+
var mspID = viper.GetString("peer.localMspId")
325+
var mspType = viper.GetString("peer.localMspType")
326+
if mspType == "" {
327+
mspType = msp.ProviderTypeToString(msp.FABRIC)
328+
}
329+
err = InitCrypto(mspMgrConfigDir, mspID, mspType)
330+
if err != nil { // Handle errors reading the config file
331+
mainLogger.Errorf("Cannot run peer because %s", err.Error())
332+
os.Exit(1)
333+
}
334+
335+
runtime.GOMAXPROCS(viper.GetInt("peer.gomaxprocs"))
336+
}

Diff for: peer/common/common_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestInitCryptoMissingDir(t *testing.T) {
6161
dir := os.TempDir() + "/" + util.GenerateUUID()
6262
err := common.InitCrypto(dir, "SampleOrg", msp.ProviderTypeToString(msp.FABRIC))
6363
assert.Error(t, err, "Should be able to initialize crypto with non-existing directory")
64-
assert.Contains(t, err.Error(), fmt.Sprintf("missing %s folder", dir))
64+
assert.Contains(t, err.Error(), fmt.Sprintf("folder \"%s\" does not exist", dir))
6565
}
6666

6767
func TestInitCrypto(t *testing.T) {

Diff for: peer/main.go

+2-47
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ package main
99
import (
1010
_ "net/http/pprof"
1111
"os"
12-
"runtime"
1312
"strings"
1413

15-
"github.com/hyperledger/fabric/common/flogging"
16-
"github.com/hyperledger/fabric/core/config"
17-
"github.com/hyperledger/fabric/msp"
1814
"github.com/hyperledger/fabric/peer/chaincode"
1915
"github.com/hyperledger/fabric/peer/channel"
2016
"github.com/hyperledger/fabric/peer/clilogging"
@@ -25,20 +21,15 @@ import (
2521
"github.com/spf13/viper"
2622
)
2723

28-
var logger = flogging.MustGetLogger("main")
29-
var logOutput = os.Stderr
30-
31-
// Constants go here.
32-
const cmdRoot = "core"
33-
3424
// The main command describes the service and
3525
// defaults to printing the help message.
3626
var mainCmd = &cobra.Command{
3727
Use: "peer"}
3828

3929
func main() {
30+
4031
// For environment variables.
41-
viper.SetEnvPrefix(cmdRoot)
32+
viper.SetEnvPrefix(common.CmdRoot)
4233
viper.AutomaticEnv()
4334
replacer := strings.NewReplacer(".", "_")
4435
viper.SetEnvKeyReplacer(replacer)
@@ -56,42 +47,6 @@ func main() {
5647
mainCmd.AddCommand(clilogging.Cmd(nil))
5748
mainCmd.AddCommand(channel.Cmd(nil))
5849

59-
err := common.InitConfig(cmdRoot)
60-
if err != nil { // Handle errors reading the config file
61-
logger.Errorf("Fatal error when initializing %s config : %s", cmdRoot, err)
62-
os.Exit(1)
63-
}
64-
65-
// setup system-wide logging backend based on settings from core.yaml
66-
flogging.InitBackend(flogging.SetFormat(viper.GetString("logging.format")), logOutput)
67-
68-
// check for --logging-level pflag first, which should override all other
69-
// log settings. if --logging-level is not set, use CORE_LOGGING_LEVEL
70-
// (environment variable takes priority; otherwise, the value set in
71-
// core.yaml)
72-
var loggingSpec string
73-
if viper.GetString("logging_level") != "" {
74-
loggingSpec = viper.GetString("logging_level")
75-
} else {
76-
loggingSpec = viper.GetString("logging.level")
77-
}
78-
flogging.InitFromSpec(loggingSpec)
79-
80-
// Init the MSP
81-
var mspMgrConfigDir = config.GetPath("peer.mspConfigPath")
82-
var mspID = viper.GetString("peer.localMspId")
83-
var mspType = viper.GetString("peer.localMspType")
84-
if mspType == "" {
85-
mspType = msp.ProviderTypeToString(msp.FABRIC)
86-
}
87-
err = common.InitCrypto(mspMgrConfigDir, mspID, mspType)
88-
if err != nil { // Handle errors reading the config file
89-
logger.Errorf("Cannot run peer because %s", err.Error())
90-
os.Exit(1)
91-
}
92-
93-
runtime.GOMAXPROCS(viper.GetInt("peer.gomaxprocs"))
94-
9550
// On failure Cobra prints the usage message and error string, so we only
9651
// need to exit with a non-0 status
9752
if mainCmd.Execute() != nil {

Diff for: peer/node/node.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/hyperledger/fabric/common/flogging"
23+
"github.com/hyperledger/fabric/peer/common"
2324
"github.com/spf13/cobra"
2425
)
2526

@@ -39,7 +40,8 @@ func Cmd() *cobra.Command {
3940
}
4041

4142
var nodeCmd = &cobra.Command{
42-
Use: nodeFuncName,
43-
Short: fmt.Sprint(nodeCmdDes),
44-
Long: fmt.Sprint(nodeCmdDes),
43+
Use: nodeFuncName,
44+
Short: fmt.Sprint(nodeCmdDes),
45+
Long: fmt.Sprint(nodeCmdDes),
46+
PersistentPreRun: common.InitCmd,
4547
}

0 commit comments

Comments
 (0)