deployCC.sh (9144B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #!/usr/bin/env bash source scripts/utils.sh CHANNEL_NAME=${1:-"mychannel"} CC_NAME=${2} CC_SRC_PATH=${3} CC_SRC_LANGUAGE=${4} CC_VERSION=${5:-"1.0"} CC_SEQUENCE=${6:-"1"} CC_INIT_FCN=${7:-"NA"} CC_END_POLICY=${8:-"NA"} CC_COLL_CONFIG=${9:-"NA"} DELAY=${10:-"3"} MAX_RETRY=${11:-"5"} VERBOSE=${12:-"false"} println "executing with the following" println "- CHANNEL_NAME: ${C_GREEN}${CHANNEL_NAME}${C_RESET}" println "- CC_NAME: ${C_GREEN}${CC_NAME}${C_RESET}" println "- CC_SRC_PATH: ${C_GREEN}${CC_SRC_PATH}${C_RESET}" println "- CC_SRC_LANGUAGE: ${C_GREEN}${CC_SRC_LANGUAGE}${C_RESET}" println "- CC_VERSION: ${C_GREEN}${CC_VERSION}${C_RESET}" println "- CC_SEQUENCE: ${C_GREEN}${CC_SEQUENCE}${C_RESET}" println "- CC_END_POLICY: ${C_GREEN}${CC_END_POLICY}${C_RESET}" println "- CC_COLL_CONFIG: ${C_GREEN}${CC_COLL_CONFIG}${C_RESET}" println "- CC_INIT_FCN: ${C_GREEN}${CC_INIT_FCN}${C_RESET}" println "- DELAY: ${C_GREEN}${DELAY}${C_RESET}" println "- MAX_RETRY: ${C_GREEN}${MAX_RETRY}${C_RESET}" println "- VERBOSE: ${C_GREEN}${VERBOSE}${C_RESET}" FABRIC_CFG_PATH=$PWD/../config/ if [ -z "$CC_NAME" ] || [ "$CC_NAME" = "NA" ]; then fatalln "No chaincode name was provided." elif [ -z "$CC_SRC_PATH" ] || [ "$CC_SRC_PATH" = "NA" ]; then fatalln "No chaincode path was provided." elif [ -z "$CC_SRC_LANGUAGE" ] || [ "$CC_SRC_LANGUAGE" = "NA" ]; then fatalln "No chaincode language was provided." elif [ ! -d "$CC_SRC_PATH" ]; then fatalln "Path to chaincode does not exist." fi CC_SRC_LANGUAGE=$(echo "$CC_SRC_LANGUAGE" | tr [:upper:] [:lower:]) if [ "$CC_SRC_LANGUAGE" = "go" ]; then CC_RUNTIME_LANGUAGE=golang infoln "Vendoring Go dependencies at $CC_SRC_PATH" pushd $CC_SRC_PATH GO111MODULE=on go mod vendor popd successln "Finished vendoring Go dependencies" elif [ "$CC_SRC_LANGUAGE" = "java" ]; then CC_RUNTIME_LANGUAGE=java infoln "Compiling Java code..." pushd $CC_SRC_PATH ./gradlew installDist popd successln "Finished compiling Java code" CC_SRC_PATH=$CC_SRC_PATH/build/install/$CC_NAME elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then CC_RUNTIME_LANGUAGE=node elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then CC_RUNTIME_LANGUAGE=node infoln "Compiling TypeScript code into JavaScript..." pushd $CC_SRC_PATH npm install npm run build popd successln "Finished compiling TypeScript code into JavaScript" else fatalln "The chaincode language ${CC_SRC_LANGUAGE} is not supported by this script." fi INIT_REQUIRED="--init-required" if [ "$CC_INIT_FCN" = "NA" ]; then INIT_REQUIRED="" fi if [ "$CC_END_POLICY" = "NA" ]; then CC_END_POLICY="" else CC_END_POLICY="--signature-policy $CC_END_POLICY" fi if [ "$CC_COLL_CONFIG" = "NA" ]; then CC_COLL_CONFIG="" else CC_COLL_CONFIG="--collections-config $CC_COLL_CONFIG" fi . scripts/envVar.sh packageChaincode() { set -x peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} >&log.txt res=$? { set +x; } 2>/dev/null cat log.txt verifyResult $res "Chaincode packaging has failed" successln "Chaincode is packaged" } installChaincode() { ORG=$1 setGlobals $ORG set -x peer lifecycle chaincode install ${CC_NAME}.tar.gz >&log.txt res=$? { set +x; } 2>/dev/null cat log.txt verifyResult $res "Chaincode installation on peer0.org${ORG} has failed" successln "Chaincode is installed on peer0.org${ORG}" } queryInstalled() { ORG=$1 setGlobals $ORG set -x peer lifecycle chaincode queryinstalled >&log.txt res=$? { set +x; } 2>/dev/null cat log.txt PACKAGE_ID=$(sed -n "/${CC_NAME}_${CC_VERSION}/{s/^Package ID: //; s/, Label:.*$//; p;}" log.txt) verifyResult $res "Query installed on peer0.org${ORG} has failed" successln "Query installed successful on peer0.org${ORG} on channel" } approveForMyOrg() { ORG=$1 setGlobals $ORG set -x peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} --version ${CC_VERSION} --package-id ${PACKAGE_ID} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} >&log.txt res=$? { set +x; } 2>/dev/null cat log.txt verifyResult $res "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME' failed" successln "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME'" } checkCommitReadiness() { ORG=$1 shift 1 setGlobals $ORG infoln "Checking the commit readiness of the chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'..." local rc=1 local COUNTER=1 while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do sleep $DELAY infoln "Attempting to check the commit readiness of the chaincode definition on peer0.org${ORG}, Retry after $DELAY seconds." set -x peer lifecycle chaincode checkcommitreadiness --channelID $CHANNEL_NAME --name ${CC_NAME} --version ${CC_VERSION} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} --output json >&log.txt res=$? { set +x; } 2>/dev/null let rc=0 for var in "$@"; do grep "$var" log.txt &>/dev/null || let rc=1 done COUNTER=$(expr $COUNTER + 1) done cat log.txt if test $rc -eq 0; then infoln "Checking the commit readiness of the chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME'" else fatalln "After $MAX_RETRY attempts, Check commit readiness result on peer0.org${ORG} is INVALID!" fi } commitChaincodeDefinition() { parsePeerConnectionParameters $@ res=$? verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters " set -x peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} "${PEER_CONN_PARMS[@]}" --version ${CC_VERSION} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} >&log.txt res=$? { set +x; } 2>/dev/null cat log.txt verifyResult $res "Chaincode definition commit failed on peer0.org${ORG} on channel '$CHANNEL_NAME' failed" successln "Chaincode definition committed on channel '$CHANNEL_NAME'" } queryCommitted() { ORG=$1 setGlobals $ORG EXPECTED_RESULT="Version: ${CC_VERSION}, Sequence: ${CC_SEQUENCE}, Endorsement Plugin: escc, Validation Plugin: vscc" infoln "Querying chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'..." local rc=1 local COUNTER=1 while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do sleep $DELAY infoln "Attempting to Query committed status on peer0.org${ORG}, Retry after $DELAY seconds." set -x peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME} >&log.txt res=$? { set +x; } 2>/dev/null test $res -eq 0 && VALUE=$(cat log.txt | grep -o '^Version: '$CC_VERSION', Sequence: [0-9]*, Endorsement Plugin: escc, Validation Plugin: vscc') test "$VALUE" = "$EXPECTED_RESULT" && let rc=0 COUNTER=$(expr $COUNTER + 1) done cat log.txt if test $rc -eq 0; then successln "Query chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME'" else fatalln "After $MAX_RETRY attempts, Query chaincode definition result on peer0.org${ORG} is INVALID!" fi } chaincodeInvokeInit() { parsePeerConnectionParameters $@ res=$? verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters " set -x fcn_call='{"function":"'${CC_INIT_FCN}'","Args":[]}' infoln "invoke fcn call:${fcn_call}" peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" -C $CHANNEL_NAME -n ${CC_NAME} "${PEER_CONN_PARMS[@]}" --isInit -c ${fcn_call} >&log.txt res=$? { set +x; } 2>/dev/null cat log.txt verifyResult $res "Invoke execution on $PEERS failed " successln "Invoke transaction successful on $PEERS on channel '$CHANNEL_NAME'" } packageChaincode infoln "Installing chaincode on peer0.org1..." installChaincode 1 infoln "Installing chaincode on peer0.org2..." installChaincode 2 infoln "Installing chaincode on peer0.org3..." installChaincode 3 infoln "Installing chaincode on peer0.org4..." installChaincode 4 queryInstalled 1 approveForMyOrg 1 checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": false" "\"Org3MSP\": false" "\"Org4MSP\": false" approveForMyOrg 2 checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": true" "\"Org3MSP\": false" "\"Org4MSP\": false" approveForMyOrg 3 checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": true" "\"Org3MSP\": true" "\"Org4MSP\": false" approveForMyOrg 4 checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": true" "\"Org3MSP\": true" "\"Org4MSP\": true" commitChaincodeDefinition 1 2 3 4 queryCommitted 1 queryCommitted 2 queryCommitted 3 queryCommitted 4 if [ "$CC_INIT_FCN" = "NA" ]; then infoln "Chaincode initialization is not required" else chaincodeInvokeInit 1 2 3 4 fi exit 0 |