github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/peer/chaincode/install.go (about)

     1  /*
     2  Copyright IBM Corp. 2016-2017 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 chaincode
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"golang.org/x/net/context"
    23  
    24  	"github.com/hyperledger/fabric/core/common/ccprovider"
    25  	"github.com/hyperledger/fabric/peer/common"
    26  	pb "github.com/hyperledger/fabric/protos/peer"
    27  	"github.com/hyperledger/fabric/protos/utils"
    28  
    29  	"github.com/spf13/cobra"
    30  )
    31  
    32  var chaincodeInstallCmd *cobra.Command
    33  
    34  const install_cmdname = "install"
    35  
    36  const install_desc = "Package the specified chaincode into a deployment spec and save it on the peer's path."
    37  
    38  // installCmd returns the cobra command for Chaincode Deploy
    39  func installCmd(cf *ChaincodeCmdFactory) *cobra.Command {
    40  	chaincodeInstallCmd = &cobra.Command{
    41  		Use:       "install",
    42  		Short:     fmt.Sprintf(install_desc),
    43  		Long:      fmt.Sprintf(install_desc),
    44  		ValidArgs: []string{"1"},
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return chaincodeInstall(cmd, args, cf)
    47  		},
    48  	}
    49  
    50  	return chaincodeInstallCmd
    51  }
    52  
    53  //install the depspec to "peer.address"
    54  func install(chaincodeName string, chaincodeVersion string, cds *pb.ChaincodeDeploymentSpec, cf *ChaincodeCmdFactory) error {
    55  	creator, err := cf.Signer.Serialize()
    56  	if err != nil {
    57  		return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
    58  	}
    59  
    60  	prop, _, err := utils.CreateInstallProposalFromCDS(cds, creator)
    61  	if err != nil {
    62  		return fmt.Errorf("Error creating proposal  %s: %s", chainFuncName, err)
    63  	}
    64  
    65  	var signedProp *pb.SignedProposal
    66  	signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
    67  	if err != nil {
    68  		return fmt.Errorf("Error creating signed proposal  %s: %s", chainFuncName, err)
    69  	}
    70  
    71  	proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
    72  	if err != nil {
    73  		return fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
    74  	}
    75  
    76  	if proposalResponse != nil {
    77  		logger.Debug("Installed remotely %v", proposalResponse)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  // chaincodeInstall installs the chaincode. If remoteinstall, does it via a lccc call
    84  func chaincodeInstall(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error {
    85  	if chaincodePath == common.UndefinedParamValue || chaincodeVersion == common.UndefinedParamValue {
    86  		return fmt.Errorf("Must supply value for %s path and version parameters.", chainFuncName)
    87  	}
    88  
    89  	var err error
    90  	if cf == nil {
    91  		cf, err = InitCmdFactory()
    92  		if err != nil {
    93  			return err
    94  		}
    95  	}
    96  
    97  	tmppkg, _ := ccprovider.GetChaincodePackage(chaincodeName, chaincodeVersion)
    98  	if tmppkg != nil {
    99  		return fmt.Errorf("chaincode %s:%s exists", chaincodeName, chaincodeVersion)
   100  	}
   101  
   102  	spec, err := getChaincodeSpecification(cmd)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	cds, err := getChaincodeBytes(spec, true)
   108  	if err != nil {
   109  		return fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err)
   110  	}
   111  
   112  	err = install(chaincodeName, chaincodeVersion, cds, cf)
   113  
   114  	return err
   115  }