github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/regression/daily/chaincodeTests/fabricFeatureChaincodes/create_join_channel.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright IBM Corp. All Rights Reserved.
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  
     9  set +x
    10  set -e
    11  
    12  
    13  START_TIME=$(date +%s)
    14  ##### GLOBALS ######
    15  CHANNEL_NAME="$1"
    16  CHANNELS="$2"
    17  CHAINCODES="$3"
    18  ENDORSERS="$4"
    19  fun="$5"
    20  LOG_FILE="scripts1/logs.txt"
    21  
    22  ##### SET DEFAULT VALUES #####
    23  : ${CHANNEL_NAME:="mychannel"}
    24  : ${CHANNELS:="1"}
    25  : ${CHAINCODES:="1"}
    26  : ${ENDORSERS:="4"}
    27  : ${TIMEOUT:="60"}
    28  COUNTER=1
    29  MAX_RETRY=5
    30  ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
    31  
    32  echo "Channel name : $CHANNEL_NAME" >>$LOG_FILE
    33  echo "Channels: $CHANNELS" >>$LOG_FILE
    34  echo "Chaincodes : $CHAINCODES" >>$LOG_FILE
    35  echo "Endorsers : $ENDORSERS" >>$LOG_FILE
    36  echo "FUN : $fun" >>$LOG_FILE
    37  
    38  verifyResult () {
    39  	if [ $1 -ne 0 ] ; then
    40  		echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!" >>$LOG_FILE
    41                  echo "================== ERROR !!! FAILED to execute End-2-End Scenario ==================" >>$LOG_FILE
    42  		echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"
    43                  echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
    44  		echo
    45     		exit 1
    46  	fi
    47  }
    48  
    49  setGlobals () {
    50  	if [ $1 -eq 0 -o $1 -eq 1 ] ; then
    51  		export CORE_PEER_LOCALMSPID="Org1MSP"
    52  		export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
    53  		export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    54  		if [ $1 -eq 0 ]; then
    55  			export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
    56  		else
    57  			export CORE_PEER_ADDRESS=peer1.org1.example.com:7051
    58  		fi
    59  	else
    60  		export CORE_PEER_LOCALMSPID="Org2MSP"
    61  		export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
    62  		export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
    63  		if [ $1 -eq 2 ]; then
    64  			export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
    65  		else
    66  			export CORE_PEER_ADDRESS=peer1.org2.example.com:7051
    67  		fi
    68  	fi
    69  }
    70  
    71  createChannel() {
    72          echo "Inside create channel fun = $fun"
    73  	setGlobals 0
    74          CH_NUM=$1
    75  
    76          if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
    77  		peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME$CH_NUM -f ./channel-artifacts/channel$CH_NUM.tx >>$LOG_FILE
    78  	else
    79  		peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME$CH_NUM -f ./channel-artifacts/channel$CH_NUM.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >>$LOG_FILE
    80  	fi
    81  	res=$?
    82  	verifyResult $res "Channel creation failed"
    83  	echo "===================== Channel \"$CHANNEL_NAME$CH_NUM\" is created successfully ===================== " >>$LOG_FILE
    84  	echo "===================== Channel \"$CHANNEL_NAME$CH_NUM\" is created successfully ===================== "
    85  	echo
    86  }
    87  
    88  updateAnchorPeers() {
    89          PEER=$1
    90          CH_NUM=$2
    91          setGlobals $PEER
    92  
    93          if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
    94  		peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME$CH_NUM -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors$CH_NUM.tx >>$LOG_FILE
    95  	else
    96  		peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME$CH_NUM -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors$CH_NUM.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >>$LOG_FILE
    97  	fi
    98  	res=$?
    99  	verifyResult $res "Anchor peer update failed"
   100  	echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== " >>$LOG_FILE
   101  	echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
   102  	echo
   103  }
   104  
   105  ## Sometimes Join takes time hence RETRY atleast for 5 times
   106  joinWithRetry () {
   107          for (( i=0; $i<$CHANNELS; i++))
   108            do
   109  		peer channel join -b $CHANNEL_NAME$i.block  >>$LOG_FILE
   110  		res=$?
   111  		if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
   112  			COUNTER=` expr $COUNTER + 1`
   113  			echo "PEER$1 failed to join the channel, Retry after 2 seconds" >> $LOG_FILE
   114  			sleep 2
   115  			joinWithRetry $1
   116  		else
   117  			COUNTER=1
   118  		fi
   119          	verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
   120                  echo "===================== PEER$1 joined on the channel \"$CHANNEL_NAME$i\" ===================== " >>$LOG_FILE
   121                  echo "===================== PEER$1 joined on the channel \"$CHANNEL_NAME$i\" successfully ===================== "
   122                  sleep 2
   123           done
   124  }
   125  
   126  joinChannel () {
   127          for (( peer=0; $peer<$ENDORSERS; peer++))
   128            do
   129  		setGlobals $peer
   130  		joinWithRetry $peer
   131  		sleep 2
   132  		echo >> $LOG_FILE
   133  	 done
   134  }
   135  
   136  validateArgs () {
   137          if [ -z "${fun}" ]; then
   138                  exit 1
   139          fi
   140          if [ "${fun}" == "create" -o "${fun}" == "join" ]; then
   141                  return
   142          else
   143                  echo "Invalid Argument from vaidateArgs" >>$LOG_FILE
   144                  exit 1
   145          fi
   146  }
   147  
   148  validateArgs
   149  if [ "${fun}" == "create" ]; then
   150          ## Create channel
   151          for (( ch=0; $ch<$CHANNELS; ch++))
   152          do
   153                  createChannel $ch
   154          done
   155  
   156  elif [ "${fun}" == "join" ]; then
   157          #Join all the peers to all the channels
   158          joinChannel
   159          ## Set the anchor peers for each org in the channel
   160          updateAnchorPeers 0 0
   161          updateAnchorPeers 2 0
   162          echo "===================== All GOOD, End-2-End for create_join channel successfully===================== " >>$LOG_FILE
   163          echo "===================== All GOOD, End-2-End for create_join channel successfully===================== "
   164          echo
   165          echo "Total execution time $(($(date +%s)-START_TIME)) secs" >>$LOG_FILE
   166          echo
   167          exit 0
   168  else
   169          printHelp
   170          exit 1
   171  fi