From 3da7498fb3e0a182be3e8f5850590aeff0a1661e Mon Sep 17 00:00:00 2001 From: sam-at-luther Date: Sat, 6 Jul 2024 20:25:56 -0700 Subject: [PATCH 1/3] Fix formatting --- template/scripts/luther_utils.sh | 879 +++++++++++++++---------------- 1 file changed, 437 insertions(+), 442 deletions(-) diff --git a/template/scripts/luther_utils.sh b/template/scripts/luther_utils.sh index cccef73..11fd5be 100755 --- a/template/scripts/luther_utils.sh +++ b/template/scripts/luther_utils.sh @@ -12,174 +12,172 @@ ORDERER_CA="/crypto-config/ordererOrganizations/${DOMAIN_NAME}/orderers/orderer0 # verify the result of the end-to-end test verifyResult() { - if [ $1 -ne 0 ] ; then - echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!" >&2 - echo "========= ERROR !!! FAILED to execute End-2-End Scenario ===========" >&2 - echo >&2 - exit 1 - fi + if [ $1 -ne 0 ]; then + echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!" >&2 + echo "========= ERROR !!! FAILED to execute End-2-End Scenario ===========" >&2 + echo >&2 + exit 1 + fi } firstPeer() { - echo "${PEER_INDICES[0]}" + echo "${PEER_INDICES[0]}" } firstOrg() { - echo "${ORG_INDICES[0]}" + echo "${ORG_INDICES[0]}" } peerAddress() { - PEER=$1 - ORG=$2 - echo "peer${PEER}.org${ORG}.${DOMAIN_NAME}:7051" + PEER=$1 + ORG=$2 + echo "peer${PEER}.org${ORG}.${DOMAIN_NAME}:7051" } peerRootCert() { - PEER=$1 - ORG=$2 - echo "/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/peers/peer${PEER}.org${ORG}.${DOMAIN_NAME}/tls/ca.crt" + PEER=$1 + ORG=$2 + echo "/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/peers/peer${PEER}.org${ORG}.${DOMAIN_NAME}/tls/ca.crt" } peerArgs() { - PEER=$1 - ORG=$2 - echo "--peerAddresses $(peerAddress "$PEER" "$ORG") --tlsRootCertFiles $(peerRootCert "$PEER" "$ORG")" + PEER=$1 + ORG=$2 + echo "--peerAddresses $(peerAddress "$PEER" "$ORG") --tlsRootCertFiles $(peerRootCert "$PEER" "$ORG")" } peerArgsEachOrg() { - { set +x; } 2>/dev/null - local peer_args="" - local first_peer="$(firstPeer)" - for orgIdx in "${ORG_INDICES[@]}"; do - peer_args="${peer_args} $(peerArgs "$first_peer" "$orgIdx")" - done - echo "$peer_args" + { set +x; } 2>/dev/null + local peer_args="" + local first_peer="$(firstPeer)" + for orgIdx in "${ORG_INDICES[@]}"; do + peer_args="${peer_args} $(peerArgs "$first_peer" "$orgIdx")" + done + echo "$peer_args" } setGlobals() { - PEER=$1 - ORG=$2 - - export CORE_PEER_LOCALMSPID="Org${ORG}MSP" - export CORE_PEER_MSPCONFIGPATH="/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/users/Admin@org${ORG}.${DOMAIN_NAME}/msp" - export CORE_PEER_TLS_CERT_FILE="/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/peers/peer${PEER}.org${ORG}.${DOMAIN_NAME}/tls/server.crt" - export CORE_PEER_TLS_KEY_FILE="/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/peers/peer${PEER}.org${ORG}.${DOMAIN_NAME}/tls/server.key" - export CORE_PEER_ADDRESS="$(peerAddress "$PEER" "$ORG")" - export CORE_PEER_TLS_ROOTCERT_FILE="$(peerRootCert "$PEER" "$ORG")" - export CORE_PEER_TLS_CLIENTCERT_FILE="${CORE_PEER_TLS_CERT_FILE}" - export CORE_PEER_TLS_CLIENTKEY_FILE="${CORE_PEER_TLS_KEY_FILE}" - export CORE_PEER_TLS_CLIENTAUTHREQUIRED=true - - env |grep CORE >&2 + PEER=$1 + ORG=$2 + + export CORE_PEER_LOCALMSPID="Org${ORG}MSP" + export CORE_PEER_MSPCONFIGPATH="/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/users/Admin@org${ORG}.${DOMAIN_NAME}/msp" + export CORE_PEER_TLS_CERT_FILE="/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/peers/peer${PEER}.org${ORG}.${DOMAIN_NAME}/tls/server.crt" + export CORE_PEER_TLS_KEY_FILE="/crypto-config/peerOrganizations/org${ORG}.${DOMAIN_NAME}/peers/peer${PEER}.org${ORG}.${DOMAIN_NAME}/tls/server.key" + export CORE_PEER_ADDRESS="$(peerAddress "$PEER" "$ORG")" + export CORE_PEER_TLS_ROOTCERT_FILE="$(peerRootCert "$PEER" "$ORG")" + export CORE_PEER_TLS_CLIENTCERT_FILE="${CORE_PEER_TLS_CERT_FILE}" + export CORE_PEER_TLS_CLIENTKEY_FILE="${CORE_PEER_TLS_KEY_FILE}" + export CORE_PEER_TLS_CLIENTAUTHREQUIRED=true + + env | grep CORE >&2 } updateAnchorPeers() { - PEER=$1 - ORG=$2 - setGlobals $PEER $ORG - - if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - set -x - peer channel update -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt - res=$? - set +x - else - set -x - peer channel update -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE >&log.txt - res=$? - set +x - fi - cat log.txt - verifyResult $res "Anchor peer update failed" - echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== " - sleep $DELAY - echo + PEER=$1 + ORG=$2 + setGlobals $PEER $ORG + + if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then + set -x + peer channel update -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt + res=$? + set +x + else + set -x + peer channel update -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE >&log.txt + res=$? + set +x + fi + cat log.txt + verifyResult $res "Anchor peer update failed" + echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== " + sleep $DELAY + echo } createChannel() { - CHANNEL_NAME=$1 - DELAY=3 - setGlobals 0 1 - - sleep $DELAY - if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then - set -x - peer channel create -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/channel.tx >&log.txt - res=$? - set +x - else - set -x - peer channel create -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE >&log.txt - res=$? - set +x - fi - cat log.txt - verifyResult $res "Channel creation failed" - echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== " - echo + CHANNEL_NAME=$1 + DELAY=3 + setGlobals 0 1 + + sleep $DELAY + if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then + set -x + peer channel create -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/channel.tx >&log.txt + res=$? + set +x + else + set -x + peer channel create -o orderer0."$DOMAIN_NAME":7050 -c $CHANNEL_NAME -f /channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE >&log.txt + res=$? + set +x + fi + cat log.txt + verifyResult $res "Channel creation failed" + echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== " + echo } joinChannel() { - CHANNEL_NAME=$1 - DELAY=3 - - for i in "${ORG_INDICES[@]}" - do - for j in "${PEER_INDICES[@]}" - do - joinChannelWithRetry "$j" "$i" $CHANNEL_NAME $DELAY 1 5 - echo "===================== peer${j}.org${i} joined on the channel \"$CHANNEL_NAME\" ===================== " - sleep $DELAY - echo - done - done + CHANNEL_NAME=$1 + DELAY=3 + + for i in "${ORG_INDICES[@]}"; do + for j in "${PEER_INDICES[@]}"; do + joinChannelWithRetry "$j" "$i" $CHANNEL_NAME $DELAY 1 5 + echo "===================== peer${j}.org${i} joined on the channel \"$CHANNEL_NAME\" ===================== " + sleep $DELAY + echo + done + done } ## Sometimes Join takes time hence RETRY so this recursive function takes an ## iteration counter and a maximum number of iterations as its 5th and 6th ## arguments. joinChannelWithRetry() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - DELAY=$4 - COUNTER=$5 - MAX_RETRY=$6 - setGlobals $PEER $ORG - - set -x - peer channel join -b $CHANNEL_NAME.block >&log.txt - res=$? - set +x - - cat log.txt - if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then - COUNTER=` expr $COUNTER + 1` - echo "peer${PEER}.org${ORG} failed to join the channel, Retry after $DELAY seconds" - sleep $DELAY - joinChannelWithRetry $PEER $ORG $CHANNEL_NAME $DELAY $COUNTER $MAX_RETRY - else - verifyResult $res "After $MAX_RETRY attempts, peer${PEER}.org${ORG} has failed to Join the Channel" - fi + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + DELAY=$4 + COUNTER=$5 + MAX_RETRY=$6 + setGlobals $PEER $ORG + + set -x + peer channel join -b $CHANNEL_NAME.block >&log.txt + res=$? + set +x + + cat log.txt + if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then + COUNTER=$(expr $COUNTER + 1) + echo "peer${PEER}.org${ORG} failed to join the channel, Retry after $DELAY seconds" + sleep $DELAY + joinChannelWithRetry $PEER $ORG $CHANNEL_NAME $DELAY $COUNTER $MAX_RETRY + else + verifyResult $res "After $MAX_RETRY attempts, peer${PEER}.org${ORG} has failed to Join the Channel" + fi } generateChaincode() { - echo "generateChaincode: ""$*" + echo "generateChaincode: ""$*" - CC_SRC_PATH=$1 - CC_NAME=$2 - CC_VERSION=$3 - IS_EXTERNAL=$4 + CC_SRC_PATH=$1 + CC_NAME=$2 + CC_VERSION=$3 + IS_EXTERNAL=$4 - TIMESTAMP="2016-01-01T00:00:00Z" - CC_LABEL=${CC_NAME}-${CC_VERSION} - CC_TYPE="golang" + TIMESTAMP="2016-01-01T00:00:00Z" + CC_LABEL=${CC_NAME}-${CC_VERSION} + CC_TYPE="golang" - CC_SRC_DIR=$(dirname ${CC_SRC_PATH}) - CC_PATH=${CC_SRC_DIR}/${CC_LABEL}.tar.gz + CC_SRC_DIR=$(dirname ${CC_SRC_PATH}) + CC_PATH=${CC_SRC_DIR}/${CC_LABEL}.tar.gz - if [ "$IS_EXTERNAL" == "True" ]; then - cat >/tmp/connection.json </tmp/connection.json </tmp/metadata.json + echo '{"path":"main","type":"'"${CC_TYPE}"'","label":"'"${CC_LABEL}"'"}' >/tmp/metadata.json - tar -zcf ${CC_PATH} -C /tmp --mtime=$TIMESTAMP metadata.json code.tar.gz - md5sum ${CC_PATH} - peer lifecycle chaincode calculatepackageid ${CC_PATH} | tee ${CC_SRC_DIR}/${CC_LABEL}.id + tar -zcf ${CC_PATH} -C /tmp --mtime=$TIMESTAMP metadata.json code.tar.gz + md5sum ${CC_PATH} + peer lifecycle chaincode calculatepackageid ${CC_PATH} | tee ${CC_SRC_DIR}/${CC_LABEL}.id } installChaincode() { - echo "installChaincode: ""$*" - - PEER=$1 - ORG=$2 - CC_SRC_PATH=$3 - CC_NAME=$4 - CC_VERSION=$5 - - queryChaincodePackage "$PEER" "$ORG" "$CC_NAME" "$CC_VERSION" - if [ $? -eq 0 ]; then - echo "===================== Chaincode ${CC_NAME}:${CC_VERSION} already installed on peer${PEER}.org${ORG} ===================== " - echo - return - fi - - - echo "Installing chaincode ${CC_NAME}:${CC_VERSION} on peer${PEER}.org${ORG}..." - echo - setGlobals $PEER $ORG - set -x - CC_SRC_DIR=$(dirname ${CC_SRC_PATH}) - CC_PATH=${CC_SRC_DIR}/${CC_NAME}-${CC_VERSION}.tar.gz - - peer lifecycle chaincode install ${CC_PATH} >&log.txt - res=$? - - set +x - cat log.txt - verifyResult $res "Chaincode installation on peer${PEER}.org${ORG} has Failed" - peer lifecycle chaincode queryinstalled -O json - echo "===================== Chaincode is installed on peer${PEER}.org${ORG} ===================== " - echo + echo "installChaincode: ""$*" + + PEER=$1 + ORG=$2 + CC_SRC_PATH=$3 + CC_NAME=$4 + CC_VERSION=$5 + + queryChaincodePackage "$PEER" "$ORG" "$CC_NAME" "$CC_VERSION" + if [ $? -eq 0 ]; then + echo "===================== Chaincode ${CC_NAME}:${CC_VERSION} already installed on peer${PEER}.org${ORG} ===================== " + echo + return + fi + + echo "Installing chaincode ${CC_NAME}:${CC_VERSION} on peer${PEER}.org${ORG}..." + echo + setGlobals $PEER $ORG + set -x + CC_SRC_DIR=$(dirname ${CC_SRC_PATH}) + CC_PATH=${CC_SRC_DIR}/${CC_NAME}-${CC_VERSION}.tar.gz + + peer lifecycle chaincode install ${CC_PATH} >&log.txt + res=$? + + set +x + cat log.txt + verifyResult $res "Chaincode installation on peer${PEER}.org${ORG} has Failed" + peer lifecycle chaincode queryinstalled -O json + echo "===================== Chaincode is installed on peer${PEER}.org${ORG} ===================== " + echo } queryChaincodePackage() { - PEER=$1 - ORG=$2 - CC_NAME=$3 - CC_VERSION=$4 - - CC_LABEL=${CC_NAME}-${CC_VERSION} - - echo "Querying installed chaincodes on peer${PEER}.org${ORG}..." >&2 - echo >&2 - setGlobals $PEER $ORG &>/dev/null - set -x - peer lifecycle chaincode queryinstalled -O json >chaincodes.json - res=$? - set +x - if [ $res -ne 0 ]; then - return 1 - fi - query=$(cat <&2 + echo >&2 + setGlobals $PEER $ORG &>/dev/null + set -x + peer lifecycle chaincode queryinstalled -O json >chaincodes.json + res=$? + set +x + if [ $res -ne 0 ]; then + return 1 + fi + query=$( + cat <&2 - res=$? - set +x - if [ $res -eq 0 ]; then - return 1 - fi - set -x - jq -er "$query" chaincodes.json - res=$? - set +x - return $res + ) + set -x + jq -er '. == {}' chaincodes.json >&2 + res=$? + set +x + if [ $res -eq 0 ]; then + return 1 + fi + set -x + jq -er "$query" chaincodes.json + res=$? + set +x + return $res } approveChaincode() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - CC_NAME=$4 - CC_VERSION=$5 - SEQ_NO=$6 - shift 6 - - package_id="$(queryChaincodePackage "$PEER" "$ORG" "$CC_NAME" "$CC_VERSION")" - verifyResult $? "queryChaincodePackage on peer${PEER}.org${ORG} has Failed" - echo package_id="$package_id" - echo - - if chaincodeApprovedForOrg \ - "$PEER" "$ORG" \ - "$CHANNEL_NAME" \ - "$CC_NAME" "$CC_VERSION" \ - "$SEQ_NO" "$@"; then - echo "===================== Chaincode already approved on peer${PEER}.org${ORG} ===================== " - echo - return - fi - - echo - echo "Approving chaincode on peer${PEER}.org${ORG}..." - echo - setGlobals $PEER $ORG - set -x - peer lifecycle chaincode approveformyorg \ - --channelID "$CHANNEL_NAME" --tls --cafile "$ORDERER_CA" \ - --orderer orderer0."$DOMAIN_NAME":7050 \ - --name "$CC_NAME" --version "$CC_VERSION" \ - --collections-config /collections.json \ - --signature-policy "$ENDORSEMENT_POLICY" \ - --sequence "$SEQ_NO" \ - --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE \ - --package-id "$package_id" "$@" >&log.txt - res=$? - set +x - cat log.txt - verifyResult $res "Approving chaincode on peer${PEER}.org${ORG} has Failed" - echo "===================== Chaincode is approved on peer${PEER}.org${ORG} ===================== " - echo + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + CC_NAME=$4 + CC_VERSION=$5 + SEQ_NO=$6 + shift 6 + + package_id="$(queryChaincodePackage "$PEER" "$ORG" "$CC_NAME" "$CC_VERSION")" + verifyResult $? "queryChaincodePackage on peer${PEER}.org${ORG} has Failed" + echo package_id="$package_id" + echo + + if chaincodeApprovedForOrg \ + "$PEER" "$ORG" \ + "$CHANNEL_NAME" \ + "$CC_NAME" "$CC_VERSION" \ + "$SEQ_NO" "$@"; then + echo "===================== Chaincode already approved on peer${PEER}.org${ORG} ===================== " + echo + return + fi + + echo + echo "Approving chaincode on peer${PEER}.org${ORG}..." + echo + setGlobals $PEER $ORG + set -x + peer lifecycle chaincode approveformyorg \ + --channelID "$CHANNEL_NAME" --tls --cafile "$ORDERER_CA" \ + --orderer orderer0."$DOMAIN_NAME":7050 \ + --name "$CC_NAME" --version "$CC_VERSION" \ + --collections-config /collections.json \ + --signature-policy "$ENDORSEMENT_POLICY" \ + --sequence "$SEQ_NO" \ + --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE \ + --package-id "$package_id" "$@" >&log.txt + res=$? + set +x + cat log.txt + verifyResult $res "Approving chaincode on peer${PEER}.org${ORG} has Failed" + echo "===================== Chaincode is approved on peer${PEER}.org${ORG} ===================== " + echo } chaincodeApprovedForOrg() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - CC_NAME=$4 - CC_VERSION=$5 - SEQ_NO=$6 - shift 6 - - setGlobals $PEER $ORG &>/dev/null - msp="$CORE_PEER_LOCALMSPID" - - echo "Checking chaincode approval for $MSP..." - echo - checkChaincodeCommitReadiness \ - "$PEER" "$ORG" \ - "$CHANNEL_NAME" \ - "$CC_NAME" "$CC_VERSION" \ - "$SEQ_NO" "$@" > status.json - res=$? - if [ $res -ne 0 ]; then - return 1 - fi - - set -x - jq -er ".approvals.${msp} == true" status.json - res=$? - set +x - return $res + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + CC_NAME=$4 + CC_VERSION=$5 + SEQ_NO=$6 + shift 6 + + setGlobals $PEER $ORG &>/dev/null + msp="$CORE_PEER_LOCALMSPID" + + echo "Checking chaincode approval for $MSP..." + echo + checkChaincodeCommitReadiness \ + "$PEER" "$ORG" \ + "$CHANNEL_NAME" \ + "$CC_NAME" "$CC_VERSION" \ + "$SEQ_NO" "$@" >status.json + res=$? + if [ $res -ne 0 ]; then + return 1 + fi + + set -x + jq -er ".approvals.${msp} == true" status.json + res=$? + set +x + return $res } waitForChaincodeCommitReadiness() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - CC_NAME=$4 - CC_VERSION=$5 - SEQ_NO=$6 - shift 6 - - DELAY=3 - MAX_RETRY=5 - - echo "Waiting for chaincode commit readiness on peer${PEER}.org${ORG}..." - echo - setGlobals $PEER $ORG &>/dev/null - counter=0 - while [ $counter -lt $MAX_RETRY ] ; do - counter=$(expr $counter + 1) - sleep $DELAY - set -x - checkChaincodeCommitReadiness \ - "$PEER" "$ORG" \ - "$CHANNEL_NAME" \ - "$CC_NAME" "$CC_VERSION" \ - "$SEQ_NO" "$@" > status.json - res=$? - set +x - if [ $res -ne 0 ]; then - continue - fi - set -x - cat status.json | jq - jq -e '.approvals | all' status.json - res=$? - set +x - if [ $res -eq 0 ]; then - echo "===================== Chaincode commit readiness confirmed on peer${PEER}.org${ORG} ===================== " - echo - return - fi - done - verifyResult 1 "Chaincode commit readiness confirmation on peer${PEER}.org${ORG} has Failed" + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + CC_NAME=$4 + CC_VERSION=$5 + SEQ_NO=$6 + shift 6 + + DELAY=3 + MAX_RETRY=5 + + echo "Waiting for chaincode commit readiness on peer${PEER}.org${ORG}..." + echo + setGlobals $PEER $ORG &>/dev/null + counter=0 + while [ $counter -lt $MAX_RETRY ]; do + counter=$(expr $counter + 1) + sleep $DELAY + set -x + checkChaincodeCommitReadiness \ + "$PEER" "$ORG" \ + "$CHANNEL_NAME" \ + "$CC_NAME" "$CC_VERSION" \ + "$SEQ_NO" "$@" >status.json + res=$? + set +x + if [ $res -ne 0 ]; then + continue + fi + set -x + cat status.json | jq + jq -e '.approvals | all' status.json + res=$? + set +x + if [ $res -eq 0 ]; then + echo "===================== Chaincode commit readiness confirmed on peer${PEER}.org${ORG} ===================== " + echo + return + fi + done + verifyResult 1 "Chaincode commit readiness confirmation on peer${PEER}.org${ORG} has Failed" } checkChaincodeCommitReadiness() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - CC_NAME=$4 - CC_VERSION=$5 - SEQ_NO=$6 - shift 6 - - setGlobals $PEER $ORG &>/dev/null - set -x - peer lifecycle chaincode checkcommitreadiness \ - --channelID "$CHANNEL_NAME" \ - --name "$CC_NAME" --version "$CC_VERSION" \ - --collections-config /collections.json \ - --signature-policy "$ENDORSEMENT_POLICY" \ - --sequence "$SEQ_NO" \ - --output json "$@" - res=$? - set +x - if [ $res -ne 0 ]; then - return 1 - fi + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + CC_NAME=$4 + CC_VERSION=$5 + SEQ_NO=$6 + shift 6 + + setGlobals $PEER $ORG &>/dev/null + set -x + peer lifecycle chaincode checkcommitreadiness \ + --channelID "$CHANNEL_NAME" \ + --name "$CC_NAME" --version "$CC_VERSION" \ + --collections-config /collections.json \ + --signature-policy "$ENDORSEMENT_POLICY" \ + --sequence "$SEQ_NO" \ + --output json "$@" + res=$? + set +x + if [ $res -ne 0 ]; then + return 1 + fi } commitChaincode() { - CHANNEL_NAME=$1 - CC_NAME=$2 - CC_VERSION=$3 - SEQ_NO=$4 - shift 4 - - echo "Committing chaincode definition..." - echo - set -x - peer lifecycle chaincode commit \ - $(peerArgsEachOrg) \ - --channelID "$CHANNEL_NAME" --tls --cafile "$ORDERER_CA" \ - --orderer orderer0."$DOMAIN_NAME":7050 \ - --collections-config /collections.json \ - --signature-policy "$ENDORSEMENT_POLICY" \ - --sequence "$SEQ_NO" \ - --name "$CC_NAME" --version "$CC_VERSION" \ - --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE \ - "$@" >&log.txt - res=$? - set +x - cat log.txt - verifyResult $res "Committing chaincode definition failed" - echo "===================== Chaincode definition committed ===================== " - echo + CHANNEL_NAME=$1 + CC_NAME=$2 + CC_VERSION=$3 + SEQ_NO=$4 + shift 4 + + echo "Committing chaincode definition..." + echo + set -x + peer lifecycle chaincode commit \ + $(peerArgsEachOrg) \ + --channelID "$CHANNEL_NAME" --tls --cafile "$ORDERER_CA" \ + --orderer orderer0."$DOMAIN_NAME":7050 \ + --collections-config /collections.json \ + --signature-policy "$ENDORSEMENT_POLICY" \ + --sequence "$SEQ_NO" \ + --name "$CC_NAME" --version "$CC_VERSION" \ + --cafile $ORDERER_CA --clientauth --certfile $CORE_PEER_TLS_CERT_FILE --keyfile $CORE_PEER_TLS_KEY_FILE \ + "$@" >&log.txt + res=$? + set +x + cat log.txt + verifyResult $res "Committing chaincode definition failed" + echo "===================== Chaincode definition committed ===================== " + echo } waitForCommittedVersion() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - CC_NAME=$4 - CC_VERSION=$5 - - DELAY=3 - MAX_RETRY=5 - - echo "Waiting for chaincode version $CC_VERSION (for $CC_NAME) to be committed on peer${PEER}.org${ORG}..." >&2 - echo >&2 - counter=0 - while [ $counter -lt $MAX_RETRY ] ; do - counter=$(expr $counter + 1) - sleep $DELAY - queryCommitted "$PEER" "$ORG" "$CHANNEL_NAME" "$CC_NAME" "$CC_VERSION" > committed.json - res=$? - if [ $res -ne 0 ]; then - continue - fi - set -x - installed_ver="$(jq -er '.version' committed.json)" - res=$? - set +x - if [ $res -ne 0 ]; then - continue - fi - if [ "$installed_ver" == "$CC_VERSION" ]; then - echo "===================== Query chaincode definition successful on peer${PEER}.org${ORG} ===================== " >&2 - echo >&2 - return - fi - done - verifyResult 1 "Query chaincode definition failed on peer${PEER}.org${ORG} has Failed" + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + CC_NAME=$4 + CC_VERSION=$5 + + DELAY=3 + MAX_RETRY=5 + + echo "Waiting for chaincode version $CC_VERSION (for $CC_NAME) to be committed on peer${PEER}.org${ORG}..." >&2 + echo >&2 + counter=0 + while [ $counter -lt $MAX_RETRY ]; do + counter=$(expr $counter + 1) + sleep $DELAY + queryCommitted "$PEER" "$ORG" "$CHANNEL_NAME" "$CC_NAME" "$CC_VERSION" >committed.json + res=$? + if [ $res -ne 0 ]; then + continue + fi + set -x + installed_ver="$(jq -er '.version' committed.json)" + res=$? + set +x + if [ $res -ne 0 ]; then + continue + fi + if [ "$installed_ver" == "$CC_VERSION" ]; then + echo "===================== Query chaincode definition successful on peer${PEER}.org${ORG} ===================== " >&2 + echo >&2 + return + fi + done + verifyResult 1 "Query chaincode definition failed on peer${PEER}.org${ORG} has Failed" } nextSequenceNumber() { - CHANNEL_NAME=$1 - CC_NAME=$2 - CC_VERSION=$3 - - PEER="$(firstPeer)" - ORG="$(firstOrg)" - - queryCommitted "$PEER" "$ORG" "$CHANNEL_NAME" "$CC_NAME" "$CC_VERSION" > committed.json - res=$? - if [ $res -ne 0 ]; then - echo 1 - return - fi - set -x - seq_num="$(jq -er 'select(.version = "$CC_VERSION") | .sequence' committed.json)" - res=$? - set +x - if [ $res -ne 0 ]; then - echo 1 - return - fi - - echo $(($seq_num + 1)) + CHANNEL_NAME=$1 + CC_NAME=$2 + CC_VERSION=$3 + + PEER="$(firstPeer)" + ORG="$(firstOrg)" + + queryCommitted "$PEER" "$ORG" "$CHANNEL_NAME" "$CC_NAME" "$CC_VERSION" >committed.json + res=$? + if [ $res -ne 0 ]; then + echo 1 + return + fi + set -x + seq_num="$(jq -er 'select(.version = "$CC_VERSION") | .sequence' committed.json)" + res=$? + set +x + if [ $res -ne 0 ]; then + echo 1 + return + fi + + echo $(($seq_num + 1)) } queryCommitted() { - PEER=$1 - ORG=$2 - CHANNEL_NAME=$3 - CC_NAME=$4 - CC_VERSION=$5 - - echo "Querying chaincode ${CC_NAME}:${CC_VERSION} committed status on peer${PEER}.org${ORG}..." >&2 - echo >&2 - setGlobals $PEER $ORG &>/dev/null - set -x - peer lifecycle chaincode querycommitted \ - --channelID "$CHANNEL_NAME" \ - --name "$CC_NAME" \ - --output json + PEER=$1 + ORG=$2 + CHANNEL_NAME=$3 + CC_NAME=$4 + CC_VERSION=$5 + + echo "Querying chaincode ${CC_NAME}:${CC_VERSION} committed status on peer${PEER}.org${ORG}..." >&2 + echo >&2 + setGlobals $PEER $ORG &>/dev/null + set -x + peer lifecycle chaincode querycommitted \ + --channelID "$CHANNEL_NAME" \ + --name "$CC_NAME" \ + --output json } forEachOrg() { - local command=$1 - shift - for orgIdx in "${ORG_INDICES[@]}" - do - "${command}" "$(firstPeer)" "${orgIdx}" "$@" - done + local command=$1 + shift + for orgIdx in "${ORG_INDICES[@]}"; do + "${command}" "$(firstPeer)" "${orgIdx}" "$@" + done } forEachPeer() { - local command=$1 - shift - for orgIdx in "${ORG_INDICES[@]}" - do - for peerIdx in "${PEER_INDICES[@]}" - do - "${command}" "${peerIdx}" "${orgIdx}" "$@" - done - done + local command=$1 + shift + for orgIdx in "${ORG_INDICES[@]}"; do + for peerIdx in "${PEER_INDICES[@]}"; do + "${command}" "${peerIdx}" "${orgIdx}" "$@" + done + done } From 452d39214006677639a19c355ed8dfc6ecaf7aa1 Mon Sep 17 00:00:00 2001 From: sam-at-luther Date: Sun, 7 Jul 2024 08:32:06 -0700 Subject: [PATCH 2/3] Generalize dockerfile arch --- .github/workflows/release.yml | 27 ++++++++++++++++++++++++--- Dockerfile | 30 +++++++++++++++++++----------- Makefile | 31 ++++++++++++++++++++++++------- common.mk | 2 ++ 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7729273..c795306 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,10 +4,31 @@ on: tags: - "*" jobs: - build-push: + build-push-docker: + name: ${{ matrix.image }} - ${{ matrix.arch }} docker build push + runs-on: ${{ fromJSON('{"arm64":"buildjet-2vcpu-ubuntu-2204-arm","amd64":"ubuntu-22.04"}')[matrix.arch] }} + strategy: + matrix: + arch: + - amd64 + - arm64 + steps: + - uses: actions/checkout@v4 + with: + lfs: true + - name: Configure DockerHub + uses: ./.github/actions/configure-dockerhub + env: + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + DOCKERHUB_USERNAME: ${{ vars.DOCKERHUB_USERNAME }} + - name: Publish + run: make docker-push VERSION=$GITHUB_REF_NAME TAG_SUFFIX=-${{ matrix.arch }} + push-docker-manifests: runs-on: ubuntu-22.04 + needs: + - build-push-docker steps: - - uses: actions/checkout@v4.1.5 + - uses: actions/checkout@v4 with: lfs: true - name: Configure DockerHub @@ -16,4 +37,4 @@ jobs: DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} DOCKERHUB_USERNAME: ${{ vars.DOCKERHUB_USERNAME }} - name: Publish - run: make docker-push VERSION=$GITHUB_REF_NAME + run: make push-manifests VERSION=$GITHUB_REF_NAME diff --git a/Dockerfile b/Dockerfile index 76723e1..478f16d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # # byfn-vars builder # -FROM debian:bullseye as byfn-vars-builder +FROM debian:bullseye AS byfn-vars-builder ARG BASEIMAGETAG=0.4.20 ARG IMAGETAG=2.5.4 @@ -22,29 +22,37 @@ RUN envsubst < /tmp/byfn-vars.sh.template > /tmp/byfn-vars.sh # # fabric artifacts builder # -FROM --platform=linux/$TARGETARCH debian:bullseye as fabric-artifacts-builder -ARG TARGETARCH +FROM debian:bullseye AS fabric-artifacts-builder ARG FABRIC_VERSION=2.5.4 ARG FABRIC_CRYPTOGEN_VERSION=${FABRIC_VERSION} -RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* \ +SHELL ["/bin/bash", "-o", "pipefail", "-c"] + +# Function to map uname -m to amd64 or arm64 +RUN ARCH=$(uname -m) && \ + case "$ARCH" in \ + x86_64) ARCH="amd64" ;; \ + aarch64) ARCH="arm64" ;; \ + arm64) ARCH="arm64" ;; \ + *) echo "Unsupported architecture: $ARCH" && exit 1 ;; \ + esac && \ + apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* \ && mkdir -p /tmp/hyperledger/fabric \ && cd /tmp/hyperledger/fabric \ - && curl -sSL https://github.com/hyperledger/fabric/releases/download/v${FABRIC_VERSION}/hyperledger-fabric-linux-${TARGETARCH}-${FABRIC_VERSION}.tar.gz | tar xz \ + && curl -sSL "https://github.com/hyperledger/fabric/releases/download/v${FABRIC_VERSION}/hyperledger-fabric-linux-${ARCH}-${FABRIC_VERSION}.tar.gz" | tar xz \ && if [ "${FABRIC_VERSION}" != "${FABRIC_CRYPTOGEN_VERSION}" ]; then \ - curl -sSL https://github.com/hyperledger/fabric/releases/download/v${FABRIC_CRYPTOGEN_VERSION}/hyperledger-fabric-linux-${TARGETARCH}-${FABRIC_CRYPTOGEN_VERSION}.tar.gz | tar xz cryptogen; \ + curl -sSL "https://github.com/hyperledger/fabric/releases/download/v${FABRIC_CRYPTOGEN_VERSION}/hyperledger-fabric-linux-${ARCH}-${FABRIC_CRYPTOGEN_VERSION}.tar.gz" | tar xz cryptogen; \ fi -# + # FNB image # -FROM --platform=linux/$TARGETARCH python:3.12-bullseye -ARG TARGETARCH +FROM python:3.12-bullseye RUN apt-get update && apt-get install --no-install-recommends -y zip rsync gettext-base docker.io && rm -rf /var/lib/apt/lists/* ARG COMPOSE_VER=2.20.0 -RUN curl -L "https://github.com/docker/compose/releases/download/v${COMPOSE_VER}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose +RUN curl -sSL "https://github.com/docker/compose/releases/download/v${COMPOSE_VER}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose RUN mkdir /network WORKDIR /network @@ -54,7 +62,7 @@ RUN ln -s /var/lib/fabric-network-builder/network.py /usr/bin/fabric-network-bui ENV PATH=$PATH:/var/lib/fabric-network-builder/release/linux/bin COPY requirements.txt /var/lib/fabric-network-builder/ -RUN pip install -r /var/lib/fabric-network-builder/requirements.txt +RUN pip install --no-cache-dir -r /var/lib/fabric-network-builder/requirements.txt COPY byfn.sh /var/lib/fabric-network-builder/ COPY template /var/lib/fabric-network-builder/template diff --git a/Makefile b/Makefile index 3e4c5ea..0ed9319 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,8 @@ FABRIC_BASE_IMAGE_TAG=$(FABRIC_BASE_VERSION) FABRIC_CA_IMAGE_TAG=$(FABRIC_CA_VERSION) FABRIC_IMAGE_NS=hyperledger -DOCKER_IMAGE_TARGET=build/images/${DOCKER_IMAGE}/${VERSION}/.dummy +DOCKER_IMAGE_TARGET=build/images/${DOCKER_IMAGE}/${BUILD_VERSION}${TAG_SUFFIX}/.dummy +DOCKER_MANIFEST_TARGET=build/manifest/${DOCKER_IMAGE}/${BUILD_VERSION}/.dummy .PHONY: default default: docker-build @@ -26,11 +27,11 @@ docker-build: ${DOCKER_IMAGE_TARGET} .PHONY: docker-push docker-push: docker-build - docker push ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION} - docker push ${DOCKER_IMAGE_PUBLIC_FQN}:latest + ${DOCKER} push ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION}${TAG_SUFFIX} + ${DOCKER} push ${DOCKER_IMAGE_PUBLIC_FQN}:latest ${DOCKER_IMAGE_TARGET}: Dockerfile Makefile - docker build \ + ${DOCKER} build \ --build-arg FABRIC_VERSION=${FABRIC_VERSION} \ --build-arg FABRIC_CRYPTOGEN_VERSION=${FABRIC_CRYPTOGEN_VERSION} \ --build-arg BASEIMAGETAG=${FABRIC_BASE_IMAGE_TAG} \ @@ -38,6 +39,22 @@ ${DOCKER_IMAGE_TARGET}: Dockerfile Makefile --build-arg CAIMAGETAG=${FABRIC_CA_IMAGE_TAG} \ --build-arg IMAGENS=${FABRIC_IMAGE_NS} \ -t ${DOCKER_IMAGE} . - docker tag ${DOCKER_IMAGE}:latest ${DOCKER_IMAGE}:${BUILD_VERSION} - mkdir -p $(dir $@) - touch $@ + ${DOCKER} tag ${DOCKER_IMAGE}:latest ${DOCKER_IMAGE}:${BUILD_VERSION}${TAG_SUFFIX} + ${MKDIR_P} $(dir $@) + ${TOUCH} $@ + +.PHONY: push-manifests +push-manifests: ${DOCKER_MANIFEST_TARGET} + @ + +${DOCKER_MANIFEST_TARGET}: + ${DOCKER} buildx imagetools create \ + --tag ${DOCKER_IMAGE_PUBLIC_FQN}:latest \ + ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION}-arm64 \ + ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION}-amd64 + ${DOCKER} buildx imagetools create \ + --tag ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION} \ + ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION}-arm64 \ + ${DOCKER_IMAGE_PUBLIC_FQN}:${BUILD_VERSION}-amd64 + ${MKDIR_P} $(dir $@) + ${TOUCH} $@ diff --git a/common.mk b/common.mk index 30a11c0..53c2980 100644 --- a/common.mk +++ b/common.mk @@ -14,6 +14,8 @@ PACKAGE=${PROJECT_PATH} FQ_DOCKER_IMAGE ?= luthersystems/$(2) FABRIC_IMAGE_TAG=${FABRIC_VERSION} +TAG_SUFFIX ?= -amd64 + DOCKER_IN_DOCKER_MOUNT?=-v /var/run/docker.sock:/var/run/docker.sock ifeq ($(OS),Windows_NT) From 80af3a57dd67cb89413fe84235ffe173c44af0fe Mon Sep 17 00:00:00 2001 From: sam-at-luther Date: Thu, 11 Jul 2024 12:26:27 -0700 Subject: [PATCH 3/3] Remove unused var from step name --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c795306..775dad5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,7 +5,7 @@ on: - "*" jobs: build-push-docker: - name: ${{ matrix.image }} - ${{ matrix.arch }} docker build push + name: ${{ matrix.arch }} - docker build push runs-on: ${{ fromJSON('{"arm64":"buildjet-2vcpu-ubuntu-2204-arm","amd64":"ubuntu-22.04"}')[matrix.arch] }} strategy: matrix: