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