github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/common/common.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 common
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/bccsp/factory"
    23  	"github.com/hyperledger/fabric/common/errors"
    24  	"github.com/hyperledger/fabric/common/flogging"
    25  	"github.com/hyperledger/fabric/common/viperutil"
    26  	"github.com/hyperledger/fabric/core/config"
    27  	"github.com/hyperledger/fabric/core/peer"
    28  	"github.com/hyperledger/fabric/msp"
    29  	mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
    30  	pb "github.com/hyperledger/fabric/protos/peer"
    31  	logging "github.com/op/go-logging"
    32  	"github.com/spf13/viper"
    33  )
    34  
    35  // UndefinedParamValue defines what undefined parameters in the command line will initialise to
    36  const UndefinedParamValue = ""
    37  
    38  //InitConfig initializes viper config
    39  func InitConfig(cmdRoot string) error {
    40  	config.InitViper(nil, cmdRoot)
    41  
    42  	err := viper.ReadInConfig() // Find and read the config file
    43  	if err != nil {             // Handle errors reading the config file
    44  		return fmt.Errorf("Fatal error when reading %s config file: %s\n", cmdRoot, err)
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  //InitCrypto initializes crypto for this peer
    51  func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
    52  	// Init the BCCSP
    53  	var bccspConfig *factory.FactoryOpts
    54  	err := viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
    55  	if err != nil {
    56  		return fmt.Errorf("Could not parse YAML config [%s]", err)
    57  	}
    58  
    59  	err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
    60  	if err != nil {
    61  		return fmt.Errorf("Fatal error when setting up MSP from directory %s: err %s\n", mspMgrConfigDir, err)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  // GetEndorserClient returns a new endorser client connection for this peer
    68  func GetEndorserClient() (pb.EndorserClient, error) {
    69  	clientConn, err := peer.NewPeerClientConnection()
    70  	if err != nil {
    71  		err = errors.ErrorWithCallstack("PER", "404", "Error trying to connect to local peer").WrapError(err)
    72  		return nil, err
    73  	}
    74  	endorserClient := pb.NewEndorserClient(clientConn)
    75  	return endorserClient, nil
    76  }
    77  
    78  // GetAdminClient returns a new admin client connection for this peer
    79  func GetAdminClient() (pb.AdminClient, error) {
    80  	clientConn, err := peer.NewPeerClientConnection()
    81  	if err != nil {
    82  		err = errors.ErrorWithCallstack("PER", "404", "Error trying to connect to local peer").WrapError(err)
    83  		return nil, err
    84  	}
    85  	adminClient := pb.NewAdminClient(clientConn)
    86  	return adminClient, nil
    87  }
    88  
    89  // SetLogLevelFromViper sets the log level for 'module' logger to the value in
    90  // core.yaml
    91  func SetLogLevelFromViper(module string) error {
    92  	var err error
    93  	if module != "" {
    94  		logLevelFromViper := viper.GetString("logging." + module)
    95  		err = CheckLogLevel(logLevelFromViper)
    96  		if err != nil {
    97  			return err
    98  		}
    99  		_, err = flogging.SetModuleLevel(module, logLevelFromViper)
   100  	}
   101  	return err
   102  }
   103  
   104  // CheckLogLevel checks that a given log level string is valid
   105  func CheckLogLevel(level string) error {
   106  	_, err := logging.LogLevel(level)
   107  	if err != nil {
   108  		err = errors.ErrorWithCallstack("LOG", "400", "Invalid log level provided - %s", level)
   109  	}
   110  	return err
   111  }
   112  
   113  // GetDefaultSigner return a default Signer(Default/PERR) for cli
   114  func GetDefaultSigner() (msp.SigningIdentity, error) {
   115  	signer, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
   116  	if err != nil {
   117  		return nil, fmt.Errorf("Error obtaining the default signing identity, err %s", err)
   118  	}
   119  
   120  	return signer, err
   121  }