github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/examples/configtxupdate/common_scripts/common.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright IBM Corp. All Rights Reserved.
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  SDIR="$(dirname ${BASH_SOURCE[0]})"
     9  
    10  # Set default variables if they have not been set 
    11  : ${INTERACTIVE:=false}
    12  : ${CONFIGTXLATOR_URL:="http://127.0.0.1:7059"}
    13  : ${ORDERER_ADDRESS:=127.0.0.1:7050}
    14  
    15  bigMsg() {
    16  	echo
    17  	echo "#####################################################"
    18  	echo "$1"
    19  	echo "#####################################################"
    20  	echo
    21  }
    22  
    23  die() {
    24  	bigMsg "FAILURE: $1"
    25  	exit 1
    26  }
    27  
    28  pauseIfInteractive() {
    29  	if ${INTERACTIVE} ; then
    30  		echo -en "\nPress enter to continue:"
    31  		read
    32  		echo
    33  	fi
    34  }
    35  
    36  verifyEncodeDecodeArgs() {
    37  	if [ "$#" -ne 3 ] ; then
    38  		echo "Requires 3 arguments, got $#"
    39  		return 1
    40  	fi
    41  
    42  	if [ ! -e "$2" ] ; then
    43  		echo "Input file '$2' must exist"
    44  		return 1
    45  	fi
    46  
    47  	return 0
    48  }
    49  
    50  assertFileContainsProto() {
    51  	if [ ! -s "$1" ] ; then
    52  		echo "Unexpected empty file: '$1'"
    53  		return 1
    54  	fi
    55  
    56  	if ! (file -b --mime "$1" | grep 'charset=binary' &>/dev/null) ; then
    57  		echo "File '$1' does not appear to be protobuf"
    58  		return 1
    59  	fi
    60  
    61  	return 0
    62  }
    63  
    64  assertFileContainsJSON() {
    65  	if [ ! -s "$1" ] ; then
    66  		echo "Unexpected empty file: '$1'"
    67  		return 1
    68  	fi
    69  
    70  	if ! jq . "$1" &> /dev/null ; then
    71  		echo "File '$1' does not appear to be json"
    72  		return 1
    73  	fi
    74  
    75  	return 0
    76  }
    77  
    78  decode() {
    79  	verifyEncodeDecodeArgs $@ || die "bad invocation of decode"
    80  
    81  	MSG_TYPE=$1
    82  		INPUT_FILE=$2
    83  	OUTPUT_FILE=$3
    84  
    85  	echo Executing:
    86  	echo -e "\t" curl -X POST --data-binary @${INPUT_FILE} "$CONFIGTXLATOR_URL/protolator/decode/${MSG_TYPE} > ${OUTPUT_FILE}"
    87  
    88  	curl -s -X POST --data-binary @${INPUT_FILE} "$CONFIGTXLATOR_URL/protolator/decode/${MSG_TYPE}" | jq . >  "${OUTPUT_FILE}"
    89  	[ $? -eq 0 ] || die "unable to decode via REST"
    90  
    91  	assertFileContainsJSON "${OUTPUT_FILE}" || die "expected JSON output"
    92  
    93  	pauseIfInteractive
    94  }
    95  
    96  encode() {
    97  	verifyEncodeDecodeArgs $@ || die "with bad invocation of encode"
    98  
    99  	MSG_TYPE=$1
   100  		INPUT_FILE=$2
   101  	OUTPUT_FILE=$3
   102  
   103  	echo Executing:
   104  	echo -e "\t" curl -X POST --data-binary @${INPUT_FILE} "$CONFIGTXLATOR_URL/protolator/decode/${MSG_TYPE} > ${OUTPUT_FILE}"
   105  
   106  	curl -s -X POST --data-binary @${INPUT_FILE} "$CONFIGTXLATOR_URL/protolator/encode/${MSG_TYPE}" >  "${OUTPUT_FILE}"
   107  	[ $? -eq 0 ] || die "unable to encode via REST"
   108  
   109  	assertFileContainsProto "${OUTPUT_FILE}" || die "expected protobuf output"
   110  
   111  	pauseIfInteractive
   112  }
   113  
   114  computeUpdate() {
   115  	if [ "$#" -ne 4 ] ; then
   116  		echo "Requires 4 arguments, got $#"
   117  		exit 1
   118  	fi
   119  
   120  	CHANNEL_ID=$1
   121  		ORIGINAL_CONFIG=$2
   122  	UPDATED_CONFIG=$3
   123  	OUTPUT_FILE=$4
   124  
   125  	if [ ! -e "${ORIGINAL_CONFIG}" ] ; then
   126  		echo "Input file '${ORIGINAL_CONFIG}' must exist"
   127  		exit 1
   128  	fi
   129  
   130  	if [ ! -e "${UPDATED_CONFIG}" ] ; then
   131  		echo "Input file '${UPDATED_CONFIG}' must exist"
   132  		exit 1
   133  	fi
   134  
   135  	echo Executing:
   136  	echo -e "\tcurl -X POST -F channel=${CHANNEL_ID} -F original=@${ORIGINAL_CONFIG} -F updated=@${UPDATED_CONFIG} ${CONFIGTXLATOR_URL}/configtxlator/compute/update-from-configs > ${OUTPUT_FILE}"
   137  
   138  	curl -s -X POST -F channel="${CHANNEL_ID}" -F "original=@${ORIGINAL_CONFIG}" -F "updated=@${UPDATED_CONFIG}" "${CONFIGTXLATOR_URL}/configtxlator/compute/update-from-configs" > "${OUTPUT_FILE}"
   139  	[ $? -eq 0 ] || die "unable to compute config update via REST"
   140  
   141  	assertFileContainsProto "${OUTPUT_FILE}" || die "expected protobuf output"
   142  
   143  	pauseIfInteractive
   144  }
   145  
   146  fetchConfigBlock() {
   147  	if [ "$#" -ne 2 ] ; then
   148  		echo "Requires 2 arguments, got $#"
   149  		exit 1
   150  	fi
   151  
   152  	CHANNEL_ID="$1"
   153  	OUTPUT_FILE="$2"
   154  
   155  	echo Executing:
   156  	echo -e "\t$PEER channel fetch config '${OUTPUT_FILE}' -o '${ORDERER_ADDRESS}' -c '${CHANNEL_ID}'"
   157  	$PEER channel fetch config "${OUTPUT_FILE}" -o "${ORDERER_ADDRESS}" -c "${CHANNEL_ID}" &> /dev/null || die "Unable to fetch config block"
   158  
   159  	assertFileContainsProto "${OUTPUT_FILE}" || die "expected protobuf output for config block"
   160  
   161  	pauseIfInteractive
   162  }
   163  
   164  wrapConfigEnvelope() {
   165  	CONFIG_UPDATE_JSON="$1"
   166  	OUTPUT_FILE="$2"
   167  
   168  	echo "Wrapping config update in envelope message, like an SDK would."
   169  	echo '{"payload":{"header":{"channel_header":{"channel_id":"'$CHANNEL'", "type":2}},"data":{"config_update":'$(cat ${CONFIG_UPDATE_JSON})'}}}' | jq . > ${OUTPUT_FILE} || die "malformed json"
   170  }
   171  
   172  updateConfig() {
   173  	if [ "$#" -ne 2 ] ; then
   174  		echo "Requires 2 arguments, got $#"
   175  		exit 1
   176  	fi
   177  
   178  	CHANNEL_ID="$1"
   179  	CONFIGTX="$2"
   180  
   181  	echo Executing:
   182  	echo -e "\t$PEER channel update -f '${CONFIGTX}' -c '${CHANNEL_ID}' -o '${ORDERER_ADDRESS}'"
   183  	$PEER channel update -f "${CONFIGTX}" -c "${CHANNEL_ID}" -o "${ORDERER_ADDRESS}" &> /dev/null || die "Error updating channel"
   184  
   185  	pauseIfInteractive
   186  }
   187  
   188  createChannel() {
   189  	if [ "$#" -ne 2 ] ; then
   190  		echo "Requires 2 arguments, got $#"
   191  		exit 1
   192  	fi
   193  
   194  	CHANNEL_ID="$1"
   195  	CONFIGTX="$2"
   196  
   197  	echo Executing:
   198  	echo -e "\t$PEER channel create -f '${CONFIGTX}' -c '${CHANNEL_ID}' -o '${ORDERER_ADDRESS}'"
   199  	$PEER channel create -f "${CONFIGTX}" -c "${CHANNEL_ID}" -o "${ORDERER_ADDRESS}" &> /dev/null || die "Error creating channel"
   200  
   201  	pauseIfInteractive
   202  }
   203  
   204  
   205  findConfigtxgen() {
   206  	if hash configtxgen 2>/dev/null ; then
   207  		CONFIGTXGEN=configtxgen
   208  		return 0
   209  	fi
   210  
   211  	if [ -e "../../../build/bin/configtxgen" ] ; then
   212  		CONFIGTXGEN="../../../build/bin/configtxgen" 
   213  		return 0
   214  	fi
   215  
   216  	echo "No configtxgen found, try 'make configtxgen'"
   217  	return 1
   218  }
   219  
   220  findConfigtxlator() {
   221  	if hash configtxlator 2>/dev/null ; then
   222  		CONFIGTXGEN=configtxgen
   223  		return 0
   224  	fi
   225  
   226  	if [ -e "${SDIR}/../../../build/bin/configtxlator" ] ; then
   227  		CONFIGTXGEN="${SDIR}/../../../build/bin/configtxlator" 
   228  		return 0
   229  	fi
   230  
   231  	echo "No configtxlator found, try 'make configtxlator'"
   232  	return 1
   233  }
   234  
   235  findPeer() {
   236  	if hash peer 2>/dev/null ; then
   237  		PEER=peer
   238  		return 0
   239  	fi
   240  
   241  	if [ -e "${SDIR}/../../../build/bin/peer" ] ; then
   242  		PEER="${SDIR}/../../../build/bin/peer" 
   243  		return 0
   244  	fi
   245  
   246  	echo "No configtxlator found, try 'make configtxlator'"
   247  	return 1
   248  }