github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/hyperledger/fabric/bccsp/factory"
    25  	"github.com/hyperledger/fabric/common/flogging"
    26  	"github.com/hyperledger/fabric/core/errors"
    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  	"github.com/spf13/viper"
    32  )
    33  
    34  // UndefinedParamValue defines what undefined parameters in the command line will initialise to
    35  const UndefinedParamValue = ""
    36  
    37  //InitConfig initializes viper config
    38  func InitConfig(cmdRoot string) error {
    39  	var alternativeCfgPath = os.Getenv("PEER_CFG_PATH")
    40  	if alternativeCfgPath != "" {
    41  		viper.AddConfigPath(alternativeCfgPath) // Path to look for the config file in
    42  	} else {
    43  		viper.AddConfigPath("./") // Path to look for the config file in
    44  		// Path to look for the config file in based on GOPATH
    45  		gopath := os.Getenv("GOPATH")
    46  		for _, p := range filepath.SplitList(gopath) {
    47  			peerpath := filepath.Join(p, "src/github.com/hyperledger/fabric/peer")
    48  			viper.AddConfigPath(peerpath)
    49  		}
    50  	}
    51  
    52  	// Now set the configuration file.
    53  	viper.SetConfigName(cmdRoot) // Name of config file (without extension)
    54  
    55  	err := viper.ReadInConfig() // Find and read the config file
    56  	if err != nil {             // Handle errors reading the config file
    57  		return fmt.Errorf("Fatal error when reading %s config file: %s\n", cmdRoot, err)
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  //InitCrypto initializes crypto for this peer
    64  func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
    65  	// Init the BCCSP
    66  	var bccspConfig *factory.FactoryOpts
    67  	err := viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
    68  	if err != nil {
    69  		return fmt.Errorf("Could not parse YAML config [%s]", err)
    70  	}
    71  
    72  	err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
    73  	if err != nil {
    74  		return fmt.Errorf("Fatal error when setting up MSP from directory %s: err %s\n", mspMgrConfigDir, err)
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  // GetEndorserClient returns a new endorser client connection for this peer
    81  func GetEndorserClient() (pb.EndorserClient, error) {
    82  	clientConn, err := peer.NewPeerClientConnection()
    83  	if err != nil {
    84  		err = errors.ErrorWithCallstack("Peer", "ConnectionError", "Error trying to connect to local peer: %s", err.Error())
    85  		return nil, err
    86  	}
    87  	endorserClient := pb.NewEndorserClient(clientConn)
    88  	return endorserClient, nil
    89  }
    90  
    91  // GetAdminClient returns a new admin client connection for this peer
    92  func GetAdminClient() (pb.AdminClient, error) {
    93  	clientConn, err := peer.NewPeerClientConnection()
    94  	if err != nil {
    95  		err = errors.ErrorWithCallstack("Peer", "ConnectionError", "Error trying to connect to local peer: %s", err.Error())
    96  		return nil, err
    97  	}
    98  	adminClient := pb.NewAdminClient(clientConn)
    99  	return adminClient, nil
   100  }
   101  
   102  // SetLogLevelFromViper sets the log level for 'module' logger to the value in
   103  // core.yaml
   104  func SetLogLevelFromViper(module string) error {
   105  	var err error
   106  	if module != "" {
   107  		logLevelFromViper := viper.GetString("logging." + module)
   108  		_, err = flogging.SetModuleLevel(module, logLevelFromViper)
   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  }