add utils.sh and colorize messages (#310)

* add utils.sh and colorize messages

Signed-off-by: Naser Mirzaei <nasermirzaei89@gmail.com>

* rename utils to scriptUtils

Signed-off-by: Naser Mirzaei <nasermirzaei89@gmail.com>
This commit is contained in:
Naser Mirzaei 2020-08-28 01:13:19 +04:30 committed by GitHub
parent c1424748b0
commit eb88315107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 451 additions and 536 deletions

View file

@ -16,55 +16,57 @@ export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=${PWD}/configtx export FABRIC_CFG_PATH=${PWD}/configtx
export VERBOSE=false export VERBOSE=false
source scriptUtils.sh
# Print the usage message # Print the usage message
function printHelp() { function printHelp() {
echo "Usage: " println "Usage: "
echo " network.sh <Mode> [Flags]" println " network.sh <Mode> [Flags]"
echo " Modes:" println " Modes:"
echo " "$'\e[0;32m'up$'\e[0m' - bring up fabric orderer and peer nodes. No channel is created println " "$'\e[0;32m'up$'\e[0m' - bring up fabric orderer and peer nodes. No channel is created
echo " "$'\e[0;32m'up createChannel$'\e[0m' - bring up fabric network with one channel println " "$'\e[0;32m'up createChannel$'\e[0m' - bring up fabric network with one channel
echo " "$'\e[0;32m'createChannel$'\e[0m' - create and join a channel after the network is created println " "$'\e[0;32m'createChannel$'\e[0m' - create and join a channel after the network is created
echo " "$'\e[0;32m'deployCC$'\e[0m' - deploy the asset transfer basic chaincode on the channel or specify println " "$'\e[0;32m'deployCC$'\e[0m' - deploy the asset transfer basic chaincode on the channel or specify
echo " "$'\e[0;32m'down$'\e[0m' - clear the network with docker-compose down println " "$'\e[0;32m'down$'\e[0m' - clear the network with docker-compose down
echo " "$'\e[0;32m'restart$'\e[0m' - restart the network println " "$'\e[0;32m'restart$'\e[0m' - restart the network
echo println
echo " Flags:" println " Flags:"
echo " Used with "$'\e[0;32m'network.sh up$'\e[0m', $'\e[0;32m'network.sh createChannel$'\e[0m': println " Used with "$'\e[0;32m'network.sh up$'\e[0m', $'\e[0;32m'network.sh createChannel$'\e[0m':
echo " -ca <use CAs> - create Certificate Authorities to generate the crypto material" println " -ca <use CAs> - create Certificate Authorities to generate the crypto material"
echo " -c <channel name> - channel name to use (defaults to \"mychannel\")" println " -c <channel name> - channel name to use (defaults to \"mychannel\")"
echo " -s <dbtype> - the database backend to use: goleveldb (default) or couchdb" println " -s <dbtype> - the database backend to use: goleveldb (default) or couchdb"
echo " -r <max retry> - CLI times out after certain number of attempts (defaults to 5)" println " -r <max retry> - CLI times out after certain number of attempts (defaults to 5)"
echo " -d <delay> - delay duration in seconds (defaults to 3)" println " -d <delay> - delay duration in seconds (defaults to 3)"
echo " -i <imagetag> - the tag to be used to launch the network (defaults to \"latest\")" println " -i <imagetag> - the tag to be used to launch the network (defaults to \"latest\")"
echo " -cai <ca_imagetag> - the image tag to be used for CA (defaults to \"${CA_IMAGETAG}\")" println " -cai <ca_imagetag> - the image tag to be used for CA (defaults to \"${CA_IMAGETAG}\")"
echo " -verbose - verbose mode" println " -verbose - verbose mode"
echo " Used with "$'\e[0;32m'network.sh deployCC$'\e[0m' println " Used with "$'\e[0;32m'network.sh deployCC$'\e[0m'
echo " -c <channel name> - deploy chaincode to channel" println " -c <channel name> - deploy chaincode to channel"
echo " -ccn <name> - the short name of the chaincode to deploy: basic (default),ledger, private, sbe, secured" println " -ccn <name> - the short name of the chaincode to deploy: basic (default),ledger, private, sbe, secured"
echo " -ccl <language> - the programming language of the chaincode to deploy: go (default), java, javascript, typescript" println " -ccl <language> - the programming language of the chaincode to deploy: go (default), java, javascript, typescript"
echo " -ccv <version> - chaincode version. 1.0 (default)" println " -ccv <version> - chaincode version. 1.0 (default)"
echo " -ccs <sequence> - chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc" println " -ccs <sequence> - chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc"
echo " -ccp <path> - Optional, path to the chaincode. When provided the -ccn will be used as the deployed name and not the short name of the known chaincodes." println " -ccp <path> - Optional, path to the chaincode. When provided the -ccn will be used as the deployed name and not the short name of the known chaincodes."
echo " -ccep <policy> - Optional, chaincode endorsement policy, using signature policy syntax. The default policy requires an endorsement from Org1 and Org2" println " -ccep <policy> - Optional, chaincode endorsement policy, using signature policy syntax. The default policy requires an endorsement from Org1 and Org2"
echo " -cccg <collection-config> - Optional, path to a private data collections configuration file" println " -cccg <collection-config> - Optional, path to a private data collections configuration file"
echo " -cci <fcn name> - Optional, chaincode init required function to invoke. When provided this function will be invoked after deployment of the chaincode and will define the chaincode as initialization required." println " -cci <fcn name> - Optional, chaincode init required function to invoke. When provided this function will be invoked after deployment of the chaincode and will define the chaincode as initialization required."
echo println
echo " -h - print this message" println " -h - print this message"
echo println
echo " Possible Mode and flag combinations" println " Possible Mode and flag combinations"
echo " "$'\e[0;32m'up$'\e[0m' -ca -c -r -d -s -i -verbose println " "$'\e[0;32m'up$'\e[0m' -ca -c -r -d -s -i -verbose
echo " "$'\e[0;32m'up createChannel$'\e[0m' -ca -c -r -d -s -i -verbose println " "$'\e[0;32m'up createChannel$'\e[0m' -ca -c -r -d -s -i -verbose
echo " "$'\e[0;32m'createChannel$'\e[0m' -c -r -d -verbose println " "$'\e[0;32m'createChannel$'\e[0m' -c -r -d -verbose
echo " "$'\e[0;32m'deployCC$'\e[0m' -ccn -ccl -ccv -ccs -ccp -cci -r -d -verbose println " "$'\e[0;32m'deployCC$'\e[0m' -ccn -ccl -ccv -ccs -ccp -cci -r -d -verbose
echo println
echo " Taking all defaults:" println " Taking all defaults:"
echo " network.sh up" println " network.sh up"
echo println
echo " Examples:" println " Examples:"
echo " network.sh up createChannel -ca -c mychannel -s couchdb -i 2.0.0" println " network.sh up createChannel -ca -c mychannel -s couchdb -i 2.0.0"
echo " network.sh createChannel -c channelName" println " network.sh createChannel -c channelName"
echo " network.sh deployCC -ccn basic -ccl javascript" println " network.sh deployCC -ccn basic -ccl javascript"
echo " network.sh deployCC -ccn mychaincode -ccp ./user/mychaincode -ccv 1 -ccl javascript" println " network.sh deployCC -ccn mychaincode -ccp ./user/mychaincode -ccv 1 -ccl javascript"
} }
# Obtain CONTAINER_IDS and remove them # Obtain CONTAINER_IDS and remove them
@ -73,7 +75,7 @@ function printHelp() {
function clearContainers() { function clearContainers() {
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /dev-peer.*/) {print $1}') CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /dev-peer.*/) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "---- No containers available for deletion ----" infoln "No containers available for deletion"
else else
docker rm -f $CONTAINER_IDS docker rm -f $CONTAINER_IDS
fi fi
@ -85,7 +87,7 @@ function clearContainers() {
function removeUnwantedImages() { function removeUnwantedImages() {
DOCKER_IMAGE_IDS=$(docker images | awk '($1 ~ /dev-peer.*/) {print $3}') DOCKER_IMAGE_IDS=$(docker images | awk '($1 ~ /dev-peer.*/) {print $3}')
if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" == " " ]; then if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" == " " ]; then
echo "---- No images available for deletion ----" infoln "No images available for deletion"
else else
docker rmi -f $DOCKER_IMAGE_IDS docker rmi -f $DOCKER_IMAGE_IDS
fi fi
@ -102,10 +104,10 @@ function checkPrereqs() {
peer version > /dev/null 2>&1 peer version > /dev/null 2>&1
if [[ $? -ne 0 || ! -d "../config" ]]; then if [[ $? -ne 0 || ! -d "../config" ]]; then
echo "ERROR! Peer binary and configuration files not found.." errorln "Peer binary and configuration files not found.."
echo errorln
echo "Follow the instructions in the Fabric docs to install the Fabric Binaries:" errorln "Follow the instructions in the Fabric docs to install the Fabric Binaries:"
echo "https://hyperledger-fabric.readthedocs.io/en/latest/install.html" errorln "https://hyperledger-fabric.readthedocs.io/en/latest/install.html"
exit 1 exit 1
fi fi
# use the fabric tools container to see if the samples and binaries match your # use the fabric tools container to see if the samples and binaries match your
@ -113,27 +115,22 @@ function checkPrereqs() {
LOCAL_VERSION=$(peer version | sed -ne 's/ Version: //p') LOCAL_VERSION=$(peer version | sed -ne 's/ Version: //p')
DOCKER_IMAGE_VERSION=$(docker run --rm hyperledger/fabric-tools:$IMAGETAG peer version | sed -ne 's/ Version: //p' | head -1) DOCKER_IMAGE_VERSION=$(docker run --rm hyperledger/fabric-tools:$IMAGETAG peer version | sed -ne 's/ Version: //p' | head -1)
echo "LOCAL_VERSION=$LOCAL_VERSION" infoln "LOCAL_VERSION=$LOCAL_VERSION"
echo "DOCKER_IMAGE_VERSION=$DOCKER_IMAGE_VERSION" infoln "DOCKER_IMAGE_VERSION=$DOCKER_IMAGE_VERSION"
if [ "$LOCAL_VERSION" != "$DOCKER_IMAGE_VERSION" ]; then if [ "$LOCAL_VERSION" != "$DOCKER_IMAGE_VERSION" ]; then
echo "=================== WARNING ===================" warnln "Local fabric binaries and docker images are out of sync. This may cause problems."
echo " Local fabric binaries and docker images are "
echo " out of sync. This may cause problems. "
echo "==============================================="
fi fi
for UNSUPPORTED_VERSION in $NONWORKING_VERSIONS; do for UNSUPPORTED_VERSION in $NONWORKING_VERSIONS; do
echo "$LOCAL_VERSION" | grep -q $UNSUPPORTED_VERSION infoln "$LOCAL_VERSION" | grep -q $UNSUPPORTED_VERSION
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "ERROR! Local Fabric binary version of $LOCAL_VERSION does not match the versions supported by the test network." fatalln "Local Fabric binary version of $LOCAL_VERSION does not match the versions supported by the test network."
exit 1
fi fi
echo "$DOCKER_IMAGE_VERSION" | grep -q $UNSUPPORTED_VERSION infoln "$DOCKER_IMAGE_VERSION" | grep -q $UNSUPPORTED_VERSION
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "ERROR! Fabric Docker image version of $DOCKER_IMAGE_VERSION does not match the versions supported by the test network." fatalln "Fabric Docker image version of $DOCKER_IMAGE_VERSION does not match the versions supported by the test network."
exit 1
fi fi
done done
@ -142,27 +139,23 @@ function checkPrereqs() {
fabric-ca-client version > /dev/null 2>&1 fabric-ca-client version > /dev/null 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
echo "ERROR! fabric-ca-client binary not found.." errorln "fabric-ca-client binary not found.."
echo errorln
echo "Follow the instructions in the Fabric docs to install the Fabric Binaries:" errorln "Follow the instructions in the Fabric docs to install the Fabric Binaries:"
echo "https://hyperledger-fabric.readthedocs.io/en/latest/install.html" errorln "https://hyperledger-fabric.readthedocs.io/en/latest/install.html"
exit 1 exit 1
fi fi
CA_LOCAL_VERSION=$(fabric-ca-client version | sed -ne 's/ Version: //p') CA_LOCAL_VERSION=$(fabric-ca-client version | sed -ne 's/ Version: //p')
CA_DOCKER_IMAGE_VERSION=$(docker run --rm hyperledger/fabric-ca:$CA_IMAGETAG fabric-ca-client version | sed -ne 's/ Version: //p' | head -1) CA_DOCKER_IMAGE_VERSION=$(docker run --rm hyperledger/fabric-ca:$CA_IMAGETAG fabric-ca-client version | sed -ne 's/ Version: //p' | head -1)
echo "CA_LOCAL_VERSION=$CA_LOCAL_VERSION" infoln "CA_LOCAL_VERSION=$CA_LOCAL_VERSION"
echo "CA_DOCKER_IMAGE_VERSION=$CA_DOCKER_IMAGE_VERSION" infoln "CA_DOCKER_IMAGE_VERSION=$CA_DOCKER_IMAGE_VERSION"
if [ "$CA_LOCAL_VERSION" != "$CA_DOCKER_IMAGE_VERSION" ]; then if [ "$CA_LOCAL_VERSION" != "$CA_DOCKER_IMAGE_VERSION" ]; then
echo "=================== WARNING ======================" warnln "Local fabric-ca binaries and docker images are out of sync. This may cause problems."
echo " Local fabric-ca binaries and docker images are "
echo " out of sync. This may cause problems. "
echo "=================================================="
fi fi
fi fi
} }
# Before you can bring up a network, each organization needs to generate the crypto # Before you can bring up a network, each organization needs to generate the crypto
# material that will define that organization on the network. Because Hyperledger # material that will define that organization on the network. Because Hyperledger
# Fabric is a permissioned blockchain, each node and user on the network needs to # Fabric is a permissioned blockchain, each node and user on the network needs to
@ -172,7 +165,7 @@ function checkPrereqs() {
# material. # material.
# By default, the sample network uses cryptogen. Cryptogen is a tool that is # By default, the sample network uses cryptogen. Cryptogen is a tool that is
# meant for development and testing that can quicky create the certificates and keys # meant for development and testing that can quickly create the certificates and keys
# that can be consumed by a Fabric network. The cryptogen tool consumes a series # that can be consumed by a Fabric network. The cryptogen tool consumes a series
# of configuration files for each organization in the "organizations/cryptogen" # of configuration files for each organization in the "organizations/cryptogen"
# directory. Cryptogen uses the files to generate the crypto material for each # directory. Cryptogen uses the files to generate the crypto material for each
@ -182,12 +175,12 @@ function checkPrereqs() {
# and keys that they generate to create a valid root of trust for each organization. # and keys that they generate to create a valid root of trust for each organization.
# The script uses Docker Compose to bring up three CAs, one for each peer organization # The script uses Docker Compose to bring up three CAs, one for each peer organization
# and the ordering organization. The configuration file for creating the Fabric CA # and the ordering organization. The configuration file for creating the Fabric CA
# servers are in the "organizations/fabric-ca" directory. Within the same diectory, # servers are in the "organizations/fabric-ca" directory. Within the same directory,
# the "registerEnroll.sh" script uses the Fabric CA client to create the identites, # the "registerEnroll.sh" script uses the Fabric CA client to create the identities,
# certificates, and MSP folders that are needed to create the test network in the # certificates, and MSP folders that are needed to create the test network in the
# "organizations/ordererOrganizations" directory. # "organizations/ordererOrganizations" directory.
# Create Organziation crypto material using cryptogen or CAs # Create Organization crypto material using cryptogen or CAs
function createOrgs() { function createOrgs() {
if [ -d "organizations/peerOrganizations" ]; then if [ -d "organizations/peerOrganizations" ]; then
@ -198,52 +191,38 @@ function createOrgs() {
if [ "$CRYPTO" == "cryptogen" ]; then if [ "$CRYPTO" == "cryptogen" ]; then
which cryptogen which cryptogen
if [ "$?" -ne 0 ]; then if [ "$?" -ne 0 ]; then
echo "cryptogen tool not found. exiting" fatalln "cryptogen tool not found. exiting"
exit 1
fi fi
echo infoln "Generate certificates using cryptogen tool"
echo "##########################################################"
echo "##### Generate certificates using cryptogen tool #########"
echo "##########################################################"
echo
echo "##########################################################" infoln "Create Org1 Identities"
echo "############ Create Org1 Identities ######################"
echo "##########################################################"
set -x set -x
cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output="organizations" cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output="organizations"
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
if [ $res -ne 0 ]; then if [ $res -ne 0 ]; then
echo $'\e[1;32m'"Failed to generate certificates..."$'\e[0m' fatalln "Failed to generate certificates..."
exit 1
fi fi
echo "##########################################################" infoln "Create Org2 Identities"
echo "############ Create Org2 Identities ######################"
echo "##########################################################"
set -x set -x
cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output="organizations" cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output="organizations"
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
if [ $res -ne 0 ]; then if [ $res -ne 0 ]; then
echo $'\e[1;32m'"Failed to generate certificates..."$'\e[0m' fatalln "Failed to generate certificates..."
exit 1
fi fi
echo "##########################################################" infoln "Create Orderer Org Identities"
echo "############ Create Orderer Org Identities ###############"
echo "##########################################################"
set -x set -x
cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output="organizations" cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output="organizations"
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
if [ $res -ne 0 ]; then if [ $res -ne 0 ]; then
echo $'\e[1;32m'"Failed to generate certificates..."$'\e[0m' fatalln "Failed to generate certificates..."
exit 1
fi fi
fi fi
@ -251,10 +230,7 @@ function createOrgs() {
# Create crypto material using Fabric CAs # Create crypto material using Fabric CAs
if [ "$CRYPTO" == "Certificate Authorities" ]; then if [ "$CRYPTO" == "Certificate Authorities" ]; then
echo infoln "Generate certificates using Fabric CA's"
echo "##########################################################"
echo "##### Generate certificates using Fabric CA's ############"
echo "##########################################################"
IMAGE_TAG=${CA_IMAGETAG} docker-compose -f $COMPOSE_FILE_CA up -d 2>&1 IMAGE_TAG=${CA_IMAGETAG} docker-compose -f $COMPOSE_FILE_CA up -d 2>&1
@ -262,28 +238,21 @@ function createOrgs() {
sleep 10 sleep 10
echo "##########################################################" infoln "Create Org1 Identities"
echo "############ Create Org1 Identities ######################"
echo "##########################################################"
createOrg1 createOrg1
echo "##########################################################" infoln "Create Org2 Identities"
echo "############ Create Org2 Identities ######################"
echo "##########################################################"
createOrg2 createOrg2
echo "##########################################################" infoln "Create Orderer Org Identities"
echo "############ Create Orderer Org Identities ###############"
echo "##########################################################"
createOrderer createOrderer
fi fi
echo infoln "Generate CCP files for Org1 and Org2"
echo "Generate CCP files for Org1 and Org2"
./organizations/ccp-generate.sh ./organizations/ccp-generate.sh
} }
@ -293,14 +262,14 @@ function createOrgs() {
# The configtxgen tool is used to create the genesis block. Configtxgen consumes a # The configtxgen tool is used to create the genesis block. Configtxgen consumes a
# "configtx.yaml" file that contains the definitions for the sample network. The # "configtx.yaml" file that contains the definitions for the sample network. The
# genesis block is defiend using the "TwoOrgsOrdererGenesis" profile at the bottom # genesis block is defined using the "TwoOrgsOrdererGenesis" profile at the bottom
# of the file. This profile defines a sample consortium, "SampleConsortium", # of the file. This profile defines a sample consortium, "SampleConsortium",
# consisting of our two Peer Orgs. This consortium defines which organizations are # consisting of our two Peer Orgs. This consortium defines which organizations are
# recognized as members of the network. The peer and ordering organizations are defined # recognized as members of the network. The peer and ordering organizations are defined
# in the "Profiles" section at the top of the file. As part of each organization # in the "Profiles" section at the top of the file. As part of each organization
# profile, the file points to a the location of the MSP directory for each member. # profile, the file points to a the location of the MSP directory for each member.
# This MSP is used to create the channel MSP that defines the root of trust for # This MSP is used to create the channel MSP that defines the root of trust for
# each organization. In essense, the channel MSP allows the nodes and users to be # each organization. In essence, the channel MSP allows the nodes and users to be
# recognized as network members. The file also specifies the anchor peers for each # recognized as network members. The file also specifies the anchor peers for each
# peer org. In future steps, this same file is used to create the channel creation # peer org. In future steps, this same file is used to create the channel creation
# transaction and the anchor peer updates. # transaction and the anchor peer updates.
@ -318,11 +287,10 @@ function createConsortium() {
which configtxgen which configtxgen
if [ "$?" -ne 0 ]; then if [ "$?" -ne 0 ]; then
echo "configtxgen tool not found. exiting" fatalln "configtxgen tool not found."
exit 1
fi fi
echo "######### Generating Orderer Genesis block ##############" infoln "Generating Orderer Genesis block"
# Note: For some unknown reason (at least for now) the block file can't be # Note: For some unknown reason (at least for now) the block file can't be
# named orderer.genesis.block or the orderer will fail to launch! # named orderer.genesis.block or the orderer will fail to launch!
@ -331,13 +299,12 @@ function createConsortium() {
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
if [ $res -ne 0 ]; then if [ $res -ne 0 ]; then
echo $'\e[1;32m'"Failed to generate orderer genesis block..."$'\e[0m' fatalln "Failed to generate orderer genesis block..."
exit 1
fi fi
} }
# After we create the org crypto material and the system channel genesis block, # After we create the org crypto material and the system channel genesis block,
# we can now bring up the peers and orderering service. By default, the base # we can now bring up the peers and ordering service. By default, the base
# file for creating the network is "docker-compose-test-net.yaml" in the ``docker`` # file for creating the network is "docker-compose-test-net.yaml" in the ``docker``
# folder. This file defines the environment variables and file mounts that # folder. This file defines the environment variables and file mounts that
# point the crypto material and genesis block that were created in earlier. # point the crypto material and genesis block that were created in earlier.
@ -362,18 +329,17 @@ function networkUp() {
docker ps -a docker ps -a
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "ERROR !!!! Unable to start network" fatalln "Unable to start network"
exit 1
fi fi
} }
## call the script to join create the channel and join the peers of org1 and org2 ## call the script to join create the channel and join the peers of org1 and org2
function createChannel() { function createChannel() {
## Bring up the network if it is not arleady up. ## Bring up the network if it is not already up.
if [ ! -d "organizations/peerOrganizations" ]; then if [ ! -d "organizations/peerOrganizations" ]; then
echo "Bringing up network" infoln "Bringing up network"
networkUp networkUp
fi fi
@ -383,21 +349,19 @@ function createChannel() {
# create the channel artifacts # create the channel artifacts
scripts/createChannel.sh $CHANNEL_NAME $CLI_DELAY $MAX_RETRY $VERBOSE scripts/createChannel.sh $CHANNEL_NAME $CLI_DELAY $MAX_RETRY $VERBOSE
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Error !!! Create channel failed" fatalln "Create channel failed"
exit 1
fi fi
} }
## Call the script to isntall and instantiate a chaincode on the channel ## Call the script to install and instantiate a chaincode on the channel
function deployCC() { function deployCC() {
scripts/deployCC.sh $CHANNEL_NAME $CC_NAME $CC_SRC_PATH $CC_SRC_LANGUAGE $CC_VERSION $CC_SEQUENCE $CC_INIT_FCN $CC_END_POLICY $CC_COLL_CONFIG $CLI_DELAY $MAX_RETRY $VERBOSE scripts/deployCC.sh $CHANNEL_NAME $CC_NAME $CC_SRC_PATH $CC_SRC_LANGUAGE $CC_VERSION $CC_SEQUENCE $CC_INIT_FCN $CC_END_POLICY $CC_COLL_CONFIG $CLI_DELAY $MAX_RETRY $VERBOSE
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "ERROR !!! Deploying chaincode failed" fatalln "Deploying chaincode failed"
exit 1
fi fi
exit 0 exit 0
@ -568,9 +532,7 @@ while [[ $# -ge 1 ]] ; do
shift shift
;; ;;
* ) * )
echo errorln "Unknown flag: $key"
echo "Unknown flag: $key"
echo
printHelp printHelp
exit 1 exit 1
;; ;;
@ -587,22 +549,16 @@ fi
# Determine mode of operation and printing out what we asked for # Determine mode of operation and printing out what we asked for
if [ "$MODE" == "up" ]; then if [ "$MODE" == "up" ]; then
echo "Starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE}' ${CRYPTO_MODE}" infoln "Starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE}' ${CRYPTO_MODE}"
echo
elif [ "$MODE" == "createChannel" ]; then elif [ "$MODE" == "createChannel" ]; then
echo "Creating channel '${CHANNEL_NAME}'." infoln "Creating channel '${CHANNEL_NAME}'."
echo infoln "If network is not up, starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE} ${CRYPTO_MODE}"
echo "If network is not up, starting nodes with CLI timeout of '${MAX_RETRY}' tries and CLI delay of '${CLI_DELAY}' seconds and using database '${DATABASE} ${CRYPTO_MODE}"
echo
elif [ "$MODE" == "down" ]; then elif [ "$MODE" == "down" ]; then
echo "Stopping network" infoln "Stopping network"
echo
elif [ "$MODE" == "restart" ]; then elif [ "$MODE" == "restart" ]; then
echo "Restarting network" infoln "Restarting network"
echo
elif [ "$MODE" == "deployCC" ]; then elif [ "$MODE" == "deployCC" ]; then
echo "deploying chaincode on channel '${CHANNEL_NAME}'" infoln "deploying chaincode on channel '${CHANNEL_NAME}'"
echo
else else
printHelp printHelp
exit 1 exit 1

View file

@ -1,15 +1,15 @@
#!/bin/bash
source scriptUtils.sh
function createOrg1 { function createOrg1() {
echo infoln "Enroll the CA admin"
echo "Enroll the CA admin" mkdir -p organizations/peerOrganizations/org1.example.com/
echo
mkdir -p organizations/peerOrganizations/org1.example.com/
export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org1.example.com/ export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org1.example.com/
# rm -rf $FABRIC_CA_CLIENT_HOME/fabric-ca-client-config.yaml # rm -rf $FABRIC_CA_CLIENT_HOME/fabric-ca-client-config.yaml
# rm -rf $FABRIC_CA_CLIENT_HOME/msp # rm -rf $FABRIC_CA_CLIENT_HOME/msp
set -x set -x
fabric-ca-client enroll -u https://admin:adminpw@localhost:7054 --caname ca-org1 --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client enroll -u https://admin:adminpw@localhost:7054 --caname ca-org1 --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
@ -28,49 +28,38 @@ function createOrg1 {
OrganizationalUnitIdentifier: admin OrganizationalUnitIdentifier: admin
OrdererOUIdentifier: OrdererOUIdentifier:
Certificate: cacerts/localhost-7054-ca-org1.pem Certificate: cacerts/localhost-7054-ca-org1.pem
OrganizationalUnitIdentifier: orderer' > ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml OrganizationalUnitIdentifier: orderer' >${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml
echo infoln "Register peer0"
echo "Register peer0"
echo
set -x set -x
fabric-ca-client register --caname ca-org1 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client register --caname ca-org1 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
echo infoln "Register user"
echo "Register user"
echo
set -x set -x
fabric-ca-client register --caname ca-org1 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client register --caname ca-org1 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
echo infoln "Register the org admin"
echo "Register the org admin"
echo
set -x set -x
fabric-ca-client register --caname ca-org1 --id.name org1admin --id.secret org1adminpw --id.type admin --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client register --caname ca-org1 --id.name org1admin --id.secret org1adminpw --id.type admin --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
mkdir -p organizations/peerOrganizations/org1.example.com/peers mkdir -p organizations/peerOrganizations/org1.example.com/peers
mkdir -p organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com mkdir -p organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com
echo infoln "Generate the peer0 msp"
echo "## Generate the peer0 msp"
echo
set -x set -x
fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp --csr.hosts peer0.org1.example.com --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp --csr.hosts peer0.org1.example.com --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/config.yaml cp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/config.yaml
echo infoln "Generate the peer0-tls certificates"
echo "## Generate the peer0-tls certificates"
echo
set -x set -x
fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls --enrollment.profile tls --csr.hosts peer0.org1.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client enroll -u https://peer0:peer0pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls --enrollment.profile tls --csr.hosts peer0.org1.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/tlscacerts/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt cp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/tlscacerts/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
cp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/signcerts/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt cp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/signcerts/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
cp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/keystore/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key cp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/keystore/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
@ -87,39 +76,32 @@ function createOrg1 {
mkdir -p organizations/peerOrganizations/org1.example.com/users mkdir -p organizations/peerOrganizations/org1.example.com/users
mkdir -p organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com mkdir -p organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com
echo infoln "Generate the user msp"
echo "## Generate the user msp"
echo
set -x set -x
fabric-ca-client enroll -u https://user1:user1pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client enroll -u https://user1:user1pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/config.yaml cp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/config.yaml
mkdir -p organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com mkdir -p organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com
echo infoln "Generate the org admin msp"
echo "## Generate the org admin msp"
echo
set -x set -x
fabric-ca-client enroll -u https://org1admin:org1adminpw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem fabric-ca-client enroll -u https://org1admin:org1adminpw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/config.yaml cp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/config.yaml
} }
function createOrg2() {
function createOrg2 { infoln "Enroll the CA admin"
mkdir -p organizations/peerOrganizations/org2.example.com/
echo export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org2.example.com/
echo "Enroll the CA admin" # rm -rf $FABRIC_CA_CLIENT_HOME/fabric-ca-client-config.yaml
echo # rm -rf $FABRIC_CA_CLIENT_HOME/msp
mkdir -p organizations/peerOrganizations/org2.example.com/
export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org2.example.com/
# rm -rf $FABRIC_CA_CLIENT_HOME/fabric-ca-client-config.yaml
# rm -rf $FABRIC_CA_CLIENT_HOME/msp
set -x set -x
fabric-ca-client enroll -u https://admin:adminpw@localhost:8054 --caname ca-org2 --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client enroll -u https://admin:adminpw@localhost:8054 --caname ca-org2 --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
@ -138,49 +120,38 @@ function createOrg2 {
OrganizationalUnitIdentifier: admin OrganizationalUnitIdentifier: admin
OrdererOUIdentifier: OrdererOUIdentifier:
Certificate: cacerts/localhost-8054-ca-org2.pem Certificate: cacerts/localhost-8054-ca-org2.pem
OrganizationalUnitIdentifier: orderer' > ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml OrganizationalUnitIdentifier: orderer' >${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml
echo infoln "Register peer0"
echo "Register peer0"
echo
set -x set -x
fabric-ca-client register --caname ca-org2 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client register --caname ca-org2 --id.name peer0 --id.secret peer0pw --id.type peer --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
echo infoln "Register user"
echo "Register user"
echo
set -x set -x
fabric-ca-client register --caname ca-org2 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client register --caname ca-org2 --id.name user1 --id.secret user1pw --id.type client --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
echo infoln "Register the org admin"
echo "Register the org admin"
echo
set -x set -x
fabric-ca-client register --caname ca-org2 --id.name org2admin --id.secret org2adminpw --id.type admin --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client register --caname ca-org2 --id.name org2admin --id.secret org2adminpw --id.type admin --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
mkdir -p organizations/peerOrganizations/org2.example.com/peers mkdir -p organizations/peerOrganizations/org2.example.com/peers
mkdir -p organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com mkdir -p organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com
echo infoln "Generate the peer0 msp"
echo "## Generate the peer0 msp"
echo
set -x set -x
fabric-ca-client enroll -u https://peer0:peer0pw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp --csr.hosts peer0.org2.example.com --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client enroll -u https://peer0:peer0pw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp --csr.hosts peer0.org2.example.com --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/config.yaml cp ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/config.yaml
echo infoln "Generate the peer0-tls certificates"
echo "## Generate the peer0-tls certificates"
echo
set -x set -x
fabric-ca-client enroll -u https://peer0:peer0pw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls --enrollment.profile tls --csr.hosts peer0.org2.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client enroll -u https://peer0:peer0pw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls --enrollment.profile tls --csr.hosts peer0.org2.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/tlscacerts/* ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt cp ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/tlscacerts/* ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
cp ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/signcerts/* ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt cp ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/signcerts/* ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
cp ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/keystore/* ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key cp ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/keystore/* ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
@ -197,38 +168,32 @@ function createOrg2 {
mkdir -p organizations/peerOrganizations/org2.example.com/users mkdir -p organizations/peerOrganizations/org2.example.com/users
mkdir -p organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com mkdir -p organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com
echo infoln "Generate the user msp"
echo "## Generate the user msp"
echo
set -x set -x
fabric-ca-client enroll -u https://user1:user1pw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client enroll -u https://user1:user1pw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/config.yaml cp ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/config.yaml
mkdir -p organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com mkdir -p organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com
echo infoln "Generate the org admin msp"
echo "## Generate the org admin msp"
echo
set -x set -x
fabric-ca-client enroll -u https://org2admin:org2adminpw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem fabric-ca-client enroll -u https://org2admin:org2adminpw@localhost:8054 --caname ca-org2 -M ${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/org2/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/config.yaml cp ${PWD}/organizations/peerOrganizations/org2.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/config.yaml
} }
function createOrderer { function createOrderer() {
echo infoln "Enroll the CA admin"
echo "Enroll the CA admin" mkdir -p organizations/ordererOrganizations/example.com
echo
mkdir -p organizations/ordererOrganizations/example.com
export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/ordererOrganizations/example.com export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/ordererOrganizations/example.com
# rm -rf $FABRIC_CA_CLIENT_HOME/fabric-ca-client-config.yaml # rm -rf $FABRIC_CA_CLIENT_HOME/fabric-ca-client-config.yaml
# rm -rf $FABRIC_CA_CLIENT_HOME/msp # rm -rf $FABRIC_CA_CLIENT_HOME/msp
set -x set -x
fabric-ca-client enroll -u https://admin:adminpw@localhost:9054 --caname ca-orderer --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem fabric-ca-client enroll -u https://admin:adminpw@localhost:9054 --caname ca-orderer --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem
@ -247,40 +212,31 @@ function createOrderer {
OrganizationalUnitIdentifier: admin OrganizationalUnitIdentifier: admin
OrdererOUIdentifier: OrdererOUIdentifier:
Certificate: cacerts/localhost-9054-ca-orderer.pem Certificate: cacerts/localhost-9054-ca-orderer.pem
OrganizationalUnitIdentifier: orderer' > ${PWD}/organizations/ordererOrganizations/example.com/msp/config.yaml OrganizationalUnitIdentifier: orderer' >${PWD}/organizations/ordererOrganizations/example.com/msp/config.yaml
infoln "Register orderer"
echo
echo "Register orderer"
echo
set -x set -x
fabric-ca-client register --caname ca-orderer --id.name orderer --id.secret ordererpw --id.type orderer --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem fabric-ca-client register --caname ca-orderer --id.name orderer --id.secret ordererpw --id.type orderer --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
echo infoln "Register the orderer admin"
echo "Register the orderer admin"
echo
set -x set -x
fabric-ca-client register --caname ca-orderer --id.name ordererAdmin --id.secret ordererAdminpw --id.type admin --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem fabric-ca-client register --caname ca-orderer --id.name ordererAdmin --id.secret ordererAdminpw --id.type admin --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
mkdir -p organizations/ordererOrganizations/example.com/orderers mkdir -p organizations/ordererOrganizations/example.com/orderers
mkdir -p organizations/ordererOrganizations/example.com/orderers/example.com mkdir -p organizations/ordererOrganizations/example.com/orderers/example.com
mkdir -p organizations/ordererOrganizations/example.com/orderers/orderer.example.com mkdir -p organizations/ordererOrganizations/example.com/orderers/orderer.example.com
echo infoln "Generate the orderer msp"
echo "## Generate the orderer msp"
echo
set -x set -x
fabric-ca-client enroll -u https://orderer:ordererpw@localhost:9054 --caname ca-orderer -M ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp --csr.hosts orderer.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem fabric-ca-client enroll -u https://orderer:ordererpw@localhost:9054 --caname ca-orderer -M ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp --csr.hosts orderer.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/ordererOrganizations/example.com/msp/config.yaml ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/config.yaml cp ${PWD}/organizations/ordererOrganizations/example.com/msp/config.yaml ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/config.yaml
echo infoln "Generate the orderer-tls certificates"
echo "## Generate the orderer-tls certificates"
echo
set -x set -x
fabric-ca-client enroll -u https://orderer:ordererpw@localhost:9054 --caname ca-orderer -M ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls --enrollment.profile tls --csr.hosts orderer.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem fabric-ca-client enroll -u https://orderer:ordererpw@localhost:9054 --caname ca-orderer -M ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls --enrollment.profile tls --csr.hosts orderer.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
@ -298,14 +254,11 @@ function createOrderer {
mkdir -p organizations/ordererOrganizations/example.com/users mkdir -p organizations/ordererOrganizations/example.com/users
mkdir -p organizations/ordererOrganizations/example.com/users/Admin@example.com mkdir -p organizations/ordererOrganizations/example.com/users/Admin@example.com
echo infoln "Generate the admin msp"
echo "## Generate the admin msp"
echo
set -x set -x
fabric-ca-client enroll -u https://ordererAdmin:ordererAdminpw@localhost:9054 --caname ca-orderer -M ${PWD}/organizations/ordererOrganizations/example.com/users/Admin@example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem fabric-ca-client enroll -u https://ordererAdmin:ordererAdminpw@localhost:9054 --caname ca-orderer -M ${PWD}/organizations/ordererOrganizations/example.com/users/Admin@example.com/msp --tls.certfiles ${PWD}/organizations/fabric-ca/ordererOrg/tls-cert.pem
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cp ${PWD}/organizations/ordererOrganizations/example.com/msp/config.yaml ${PWD}/organizations/ordererOrganizations/example.com/users/Admin@example.com/msp/config.yaml cp ${PWD}/organizations/ordererOrganizations/example.com/msp/config.yaml ${PWD}/organizations/ordererOrganizations/example.com/users/Admin@example.com/msp/config.yaml
} }

43
test-network/scriptUtils.sh Executable file
View file

@ -0,0 +1,43 @@
#!/bin/bash
C_RESET='\033[0m'
C_RED='\033[0;31m'
C_GREEN='\033[0;32m'
C_BLUE='\033[0;34m'
C_YELLOW='\033[1;33m'
# println echos string
function println() {
echo -e "$1"
}
# errorln echos i red color
function errorln() {
println "${C_RED}${1}${C_RESET}"
}
# successln echos in green color
function successln() {
println "${C_GREEN}${1}${C_RESET}"
}
# infoln echos in blue color
function infoln() {
println "${C_BLUE}${1}${C_RESET}"
}
# warnln echos in yellow color
function warnln() {
println "${C_YELLOW}${1}${C_RESET}"
}
# fatalln echos in red color and exits with fail status
function fatalln() {
errorln "$1"
exit 1
}
export -f errorln
export -f successln
export -f infoln
export -f warnln

View file

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
source scriptUtils.sh
CHANNEL_NAME="$1" CHANNEL_NAME="$1"
DELAY="$2" DELAY="$2"
@ -24,10 +25,8 @@ createChannelTx() {
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
if [ $res -ne 0 ]; then if [ $res -ne 0 ]; then
echo "Failed to generate channel configuration transaction..." fatalln "Failed to generate channel configuration transaction..."
exit 1
fi fi
echo
} }
@ -35,16 +34,14 @@ createAncorPeerTx() {
for orgmsp in Org1MSP Org2MSP; do for orgmsp in Org1MSP Org2MSP; do
echo "####### Generating anchor peer update transaction for ${orgmsp} ##########" infoln "Generating anchor peer update transaction for ${orgmsp}"
set -x set -x
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/${orgmsp}anchors.tx -channelID $CHANNEL_NAME -asOrg ${orgmsp} configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/${orgmsp}anchors.tx -channelID $CHANNEL_NAME -asOrg ${orgmsp}
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
if [ $res -ne 0 ]; then if [ $res -ne 0 ]; then
echo "Failed to generate anchor peer update transaction for ${orgmsp}..." fatalln "Failed to generate anchor peer update transaction for ${orgmsp}..."
exit 1
fi fi
echo
done done
} }
@ -64,9 +61,7 @@ createChannel() {
done done
cat log.txt cat log.txt
verifyResult $res "Channel creation failed" verifyResult $res "Channel creation failed"
echo successln "Channel '$CHANNEL_NAME' created"
echo "===================== Channel '$CHANNEL_NAME' created ===================== "
echo
} }
# queryCommitted ORG # queryCommitted ORG
@ -86,7 +81,6 @@ joinChannel() {
COUNTER=$(expr $COUNTER + 1) COUNTER=$(expr $COUNTER + 1)
done done
cat log.txt cat log.txt
echo
verifyResult $res "After $MAX_RETRY attempts, peer0.org${ORG} has failed to join channel '$CHANNEL_NAME' " verifyResult $res "After $MAX_RETRY attempts, peer0.org${ORG} has failed to join channel '$CHANNEL_NAME' "
} }
@ -107,49 +101,44 @@ updateAnchorPeers() {
done done
cat log.txt cat log.txt
verifyResult $res "Anchor peer update failed" verifyResult $res "Anchor peer update failed"
echo "===================== Anchor peers updated for org '$CORE_PEER_LOCALMSPID' on channel '$CHANNEL_NAME' ===================== " successln "Anchor peers updated for org '$CORE_PEER_LOCALMSPID' on channel '$CHANNEL_NAME'"
sleep $DELAY sleep $DELAY
echo
} }
verifyResult() { verifyResult() {
if [ $1 -ne 0 ]; then if [ $1 -ne 0 ]; then
echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!" fatalln "$2"
echo
exit 1
fi fi
} }
FABRIC_CFG_PATH=${PWD}/configtx FABRIC_CFG_PATH=${PWD}/configtx
## Create channeltx ## Create channeltx
echo "### Generating channel create transaction '${CHANNEL_NAME}.tx' ###" infoln "Generating channel create transaction '${CHANNEL_NAME}.tx'"
createChannelTx createChannelTx
## Create anchorpeertx ## Create anchorpeertx
echo "### Generating anchor peer update transactions ###" infoln "Generating anchor peer update transactions"
createAncorPeerTx createAncorPeerTx
FABRIC_CFG_PATH=$PWD/../config/ FABRIC_CFG_PATH=$PWD/../config/
## Create channel ## Create channel
echo "Creating channel "$CHANNEL_NAME infoln "Creating channel ${CHANNEL_NAME}"
createChannel createChannel
## Join all the peers to the channel ## Join all the peers to the channel
echo "Join Org1 peers to the channel..." infoln "Join Org1 peers to the channel..."
joinChannel 1 joinChannel 1
echo "Join Org2 peers to the channel..." infoln "Join Org2 peers to the channel..."
joinChannel 2 joinChannel 2
## Set the anchor peers for each org in the channel ## Set the anchor peers for each org in the channel
echo "Updating anchor peers for org1..." infoln "Updating anchor peers for org1..."
updateAnchorPeers 1 updateAnchorPeers 1
echo "Updating anchor peers for org2..." infoln "Updating anchor peers for org2..."
updateAnchorPeers 2 updateAnchorPeers 2
echo successln "Channel successfully joined"
echo "========= Channel successfully joined =========== "
echo
exit 0 exit 0

View file

@ -1,3 +1,7 @@
#!/bin/bash
source scriptUtils.sh
CHANNEL_NAME=${1:-"mychannel"} CHANNEL_NAME=${1:-"mychannel"}
CC_NAME=${2:-"basic"} CC_NAME=${2:-"basic"}
CC_SRC_PATH=${3:-"NA"} CC_SRC_PATH=${3:-"NA"}
@ -11,134 +15,125 @@ DELAY=${10:-"3"}
MAX_RETRY=${11:-"5"} MAX_RETRY=${11:-"5"}
VERBOSE=${12:-"false"} VERBOSE=${12:-"false"}
echo --- executing with the following println "executing with the following"
echo - CHANNEL_NAME:$'\e[0;32m'$CHANNEL_NAME$'\e[0m' println "- CHANNEL_NAME: ${C_GREEN}${CHANNEL_NAME}${C_RESET}"
echo - CC_NAME:$'\e[0;32m'$CC_NAME$'\e[0m' println "- CC_NAME: ${C_GREEN}${CC_NAME}${C_RESET}"
echo - CC_SRC_PATH:$'\e[0;32m'$CC_SRC_PATH$'\e[0m' println "- CC_SRC_PATH: ${C_GREEN}${CC_SRC_PATH}${C_RESET}"
echo - CC_SRC_LANGUAGE:$'\e[0;32m'$CC_SRC_LANGUAGE$'\e[0m' println "- CC_SRC_LANGUAGE: ${C_GREEN}${CC_SRC_LANGUAGE}${C_RESET}"
echo - CC_VERSION:$'\e[0;32m'$CC_VERSION$'\e[0m' println "- CC_VERSION: ${C_GREEN}${CC_VERSION}${C_RESET}"
echo - CC_SEQUENCE:$'\e[0;32m'$CC_SEQUENCE$'\e[0m' println "- CC_SEQUENCE: ${C_GREEN}${CC_SEQUENCE}${C_RESET}"
echo - CC_END_POLICY:$'\e[0;32m'$CC_END_POLICY$'\e[0m' println "- CC_END_POLICY: ${C_GREEN}${CC_END_POLICY}${C_RESET}"
echo - CC_COLL_CONFIG:$'\e[0;32m'$CC_COLL_CONFIG$'\e[0m' println "- CC_COLL_CONFIG: ${C_GREEN}${CC_COLL_CONFIG}${C_RESET}"
echo - CC_INIT_FCN:$'\e[0;32m'$CC_INIT_FCN$'\e[0m' println "- CC_INIT_FCN: ${C_GREEN}${CC_INIT_FCN}${C_RESET}"
echo - DELAY:$'\e[0;32m'$DELAY$'\e[0m' println "- DELAY: ${C_GREEN}${DELAY}${C_RESET}"
echo - MAX_RETRY:$'\e[0;32m'$MAX_RETRY$'\e[0m' println "- MAX_RETRY: ${C_GREEN}${MAX_RETRY}${C_RESET}"
echo - VERBOSE:$'\e[0;32m'$VERBOSE$'\e[0m' println "- VERBOSE: ${C_GREEN}${VERBOSE}${C_RESET}"
CC_SRC_LANGUAGE=`echo "$CC_SRC_LANGUAGE" | tr [:upper:] [:lower:]` CC_SRC_LANGUAGE=$(echo "$CC_SRC_LANGUAGE" | tr [:upper:] [:lower:])
FABRIC_CFG_PATH=$PWD/../config/ FABRIC_CFG_PATH=$PWD/../config/
# User has not provided a path, therefore the CC_NAME must # User has not provided a path, therefore the CC_NAME must
# be the short name of a known chaincode sample # be the short name of a known chaincode sample
if [ "$CC_SRC_PATH" = "NA" ]; then if [ "$CC_SRC_PATH" = "NA" ]; then
echo Determining the path to the chaincode infoln "Determining the path to the chaincode"
# first see which chaincode we have. This will be based on the # first see which chaincode we have. This will be based on the
# short name of the known chaincode sample # short name of the known chaincode sample
if [ "$CC_NAME" = "basic" ]; then if [ "$CC_NAME" = "basic" ]; then
echo $'\e[0;32m'asset-transfer-basic$'\e[0m' chaincode println $'\e[0;32m'asset-transfer-basic$'\e[0m' chaincode
CC_SRC_PATH="../asset-transfer-basic" CC_SRC_PATH="../asset-transfer-basic"
elif [ "$CC_NAME" = "secured" ]; then elif [ "$CC_NAME" = "secured" ]; then
echo $'\e[0;32m'asset-transfer-secured-agreeement$'\e[0m' chaincode println $'\e[0;32m'asset-transfer-secured-agreeement$'\e[0m' chaincode
CC_SRC_PATH="../asset-transfer-secured-agreement" CC_SRC_PATH="../asset-transfer-secured-agreement"
elif [ "$CC_NAME" = "ledger" ]; then elif [ "$CC_NAME" = "ledger" ]; then
echo $'\e[0;32m'asset-transfer-ledger-agreeement$'\e[0m' chaincode println $'\e[0;32m'asset-transfer-ledger-agreeement$'\e[0m' chaincode
CC_SRC_PATH="../asset-transfer-ledger-queries" CC_SRC_PATH="../asset-transfer-ledger-queries"
elif [ "$CC_NAME" = "private" ]; then elif [ "$CC_NAME" = "private" ]; then
echo $'\e[0;32m'asset-transfer-private-data$'\e[0m' chaincode println $'\e[0;32m'asset-transfer-private-data$'\e[0m' chaincode
CC_SRC_PATH="../asset-transfer-private-data" CC_SRC_PATH="../asset-transfer-private-data"
elif [ "$CC_NAME" = "sbe" ]; then elif [ "$CC_NAME" = "sbe" ]; then
echo $'\e[0;32m'asset-transfer-sbe$'\e[0m' chaincode println $'\e[0;32m'asset-transfer-sbe$'\e[0m' chaincode
CC_SRC_PATH="../asset-transfer-sbe" CC_SRC_PATH="../asset-transfer-sbe"
else else
echo The chaincode name ${CC_NAME} is not supported by this script fatalln "The chaincode name ${CC_NAME} is not supported by this script. Supported chaincode names are: basic, ledger, private, sbe, secured"
echo Supported chaincode names are: basic, ledger, private, sbe, secured fi
exit 1
fi
# now see what language it is written in # now see what language it is written in
if [ "$CC_SRC_LANGUAGE" = "go" ]; then if [ "$CC_SRC_LANGUAGE" = "go" ]; then
CC_SRC_PATH="$CC_SRC_PATH/chaincode-go/" CC_SRC_PATH="$CC_SRC_PATH/chaincode-go/"
elif [ "$CC_SRC_LANGUAGE" = "java" ]; then elif [ "$CC_SRC_LANGUAGE" = "java" ]; then
CC_SRC_PATH="$CC_SRC_PATH/chaincode-java/" CC_SRC_PATH="$CC_SRC_PATH/chaincode-java/"
elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then
CC_SRC_PATH="$CC_SRC_PATH/chaincode-javascript/" CC_SRC_PATH="$CC_SRC_PATH/chaincode-javascript/"
elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then
CC_SRC_PATH="$CC_SRC_PATH/chaincode-typescript/" CC_SRC_PATH="$CC_SRC_PATH/chaincode-typescript/"
fi fi
# check that the language is available for the sample chaincode # check that the language is available for the sample chaincode
if [ ! -d "$CC_SRC_PATH" ]; then if [ ! -d "$CC_SRC_PATH" ]; then
echo The smart contract language "$CC_SRC_LANGUAGE" is not yet available for fatalln "The smart contract language \"$CC_SRC_LANGUAGE\" is not yet available for the \"$CC_NAME\" sample smart contract"
echo the "$CC_NAME" sample smart contract fi
exit 1
fi
## Make sure that the path the chaincode exists if provided ## Make sure that the path the chaincode exists if provided
elif [ ! -d "$CC_SRC_PATH" ]; then elif [ ! -d "$CC_SRC_PATH" ]; then
echo Path to chaincode does not exist. Please provide different path fatalln "Path to chaincode does not exist. Please provide different path"
exit 1
fi fi
# do some language specific preparation to the chaincode before packaging # do some language specific preparation to the chaincode before packaging
if [ "$CC_SRC_LANGUAGE" = "go" ]; then if [ "$CC_SRC_LANGUAGE" = "go" ]; then
CC_RUNTIME_LANGUAGE=golang CC_RUNTIME_LANGUAGE=golang
echo Vendoring Go dependencies at $CC_SRC_PATH infoln "Vendoring Go dependencies at $CC_SRC_PATH"
pushd $CC_SRC_PATH pushd $CC_SRC_PATH
GO111MODULE=on go mod vendor GO111MODULE=on go mod vendor
popd popd
echo Finished vendoring Go dependencies successln "Finished vendoring Go dependencies"
elif [ "$CC_SRC_LANGUAGE" = "java" ]; then elif [ "$CC_SRC_LANGUAGE" = "java" ]; then
CC_RUNTIME_LANGUAGE=java CC_RUNTIME_LANGUAGE=java
echo Compiling Java code ... infoln "Compiling Java code..."
pushd $CC_SRC_PATH pushd $CC_SRC_PATH
./gradlew installDist ./gradlew installDist
popd popd
echo Finished compiling Java code successln "Finished compiling Java code"
CC_SRC_PATH=$CC_SRC_PATH/build/install/$CC_NAME CC_SRC_PATH=$CC_SRC_PATH/build/install/$CC_NAME
elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then
CC_RUNTIME_LANGUAGE=node CC_RUNTIME_LANGUAGE=node
elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then
CC_RUNTIME_LANGUAGE=node CC_RUNTIME_LANGUAGE=node
echo Compiling TypeScript code into JavaScript ... infoln "Compiling TypeScript code into JavaScript..."
pushd $CC_SRC_PATH pushd $CC_SRC_PATH
npm install npm install
npm run build npm run build
popd popd
echo Finished compiling TypeScript code into JavaScript successln "Finished compiling TypeScript code into JavaScript"
else else
echo The chaincode language ${CC_SRC_LANGUAGE} is not supported by this script fatalln "The chaincode language ${CC_SRC_LANGUAGE} is not supported by this script. Supported chaincode languages are: go, java, javascript, and typescript"
echo Supported chaincode languages are: go, java, javascript, and typescript exit 1
exit 1
fi fi
INIT_REQUIRED="--init-required" INIT_REQUIRED="--init-required"
# check if the init fcn should be called # check if the init fcn should be called
if [ "$CC_INIT_FCN" = "NA" ]; then if [ "$CC_INIT_FCN" = "NA" ]; then
INIT_REQUIRED="" INIT_REQUIRED=""
fi fi
if [ "$CC_END_POLICY" = "NA" ]; then if [ "$CC_END_POLICY" = "NA" ]; then
CC_END_POLICY="" CC_END_POLICY=""
else else
CC_END_POLICY="--signature-policy $CC_END_POLICY" CC_END_POLICY="--signature-policy $CC_END_POLICY"
fi fi
if [ "$CC_COLL_CONFIG" = "NA" ]; then if [ "$CC_COLL_CONFIG" = "NA" ]; then
CC_COLL_CONFIG="" CC_COLL_CONFIG=""
else else
CC_COLL_CONFIG="--collections-config $CC_COLL_CONFIG" CC_COLL_CONFIG="--collections-config $CC_COLL_CONFIG"
fi fi
#if [ "$CC_INIT_FCN" = "NA" ]; then #if [ "$CC_INIT_FCN" = "NA" ]; then
# INIT_REQUIRED="" # INIT_REQUIRED=""
#fi #fi
@ -146,208 +141,188 @@ fi
# import utils # import utils
. scripts/envVar.sh . scripts/envVar.sh
packageChaincode() { packageChaincode() {
ORG=$1 ORG=$1
setGlobals $ORG setGlobals $ORG
set -x 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 peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} >&log.txt
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cat log.txt cat log.txt
verifyResult $res "Chaincode packaging on peer0.org${ORG} has failed" verifyResult $res "Chaincode packaging on peer0.org${ORG} has failed"
echo "===================== Chaincode is packaged on peer0.org${ORG} ===================== " successln "Chaincode is packaged on peer0.org${ORG}"
echo
} }
# installChaincode PEER ORG # installChaincode PEER ORG
installChaincode() { installChaincode() {
ORG=$1 ORG=$1
setGlobals $ORG setGlobals $ORG
set -x set -x
peer lifecycle chaincode install ${CC_NAME}.tar.gz >&log.txt peer lifecycle chaincode install ${CC_NAME}.tar.gz >&log.txt
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cat log.txt cat log.txt
verifyResult $res "Chaincode installation on peer0.org${ORG} has failed" verifyResult $res "Chaincode installation on peer0.org${ORG} has failed"
echo "===================== Chaincode is installed on peer0.org${ORG} ===================== " successln "Chaincode is installed on peer0.org${ORG}"
echo
} }
# queryInstalled PEER ORG # queryInstalled PEER ORG
queryInstalled() { queryInstalled() {
ORG=$1 ORG=$1
setGlobals $ORG setGlobals $ORG
set -x set -x
peer lifecycle chaincode queryinstalled >&log.txt peer lifecycle chaincode queryinstalled >&log.txt
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cat log.txt cat log.txt
PACKAGE_ID=$(sed -n "/${CC_NAME}_${CC_VERSION}/{s/^Package ID: //; s/, Label:.*$//; p;}" 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" verifyResult $res "Query installed on peer0.org${ORG} has failed"
echo "===================== Query installed successful on peer0.org${ORG} on channel ===================== " successln "Query installed successful on peer0.org${ORG} on channel"
echo
} }
# approveForMyOrg VERSION PEER ORG # approveForMyOrg VERSION PEER ORG
approveForMyOrg() { approveForMyOrg() {
ORG=$1 ORG=$1
setGlobals $ORG setGlobals $ORG
set -x 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 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=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cat log.txt cat log.txt
verifyResult $res "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME' failed" verifyResult $res "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME' failed"
echo "===================== Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME' ===================== " successln "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME'"
echo
} }
# checkCommitReadiness VERSION PEER ORG # checkCommitReadiness VERSION PEER ORG
checkCommitReadiness() { checkCommitReadiness() {
ORG=$1 ORG=$1
shift 1 shift 1
setGlobals $ORG setGlobals $ORG
echo "===================== Checking the commit readiness of the chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'... ===================== " infoln "Checking the commit readiness of the chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'..."
local rc=1 local rc=1
local COUNTER=1 local COUNTER=1
# continue to poll # continue to poll
# we either get a successful response, or reach MAX RETRY # we either get a successful response, or reach MAX RETRY
while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do
sleep $DELAY sleep $DELAY
echo "Attempting to check the commit readiness of the chaincode definition on peer0.org${ORG}, Retry after $DELAY seconds." infoln "Attempting to check the commit readiness of the chaincode definition on peer0.org${ORG}, Retry after $DELAY seconds."
set -x 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 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=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
let rc=0 let rc=0
for var in "$@"; do for var in "$@"; do
grep "$var" log.txt &>/dev/null || let rc=1 grep "$var" log.txt &>/dev/null || let rc=1
done done
COUNTER=$(expr $COUNTER + 1) COUNTER=$(expr $COUNTER + 1)
done done
cat log.txt cat log.txt
if test $rc -eq 0; then if test $rc -eq 0; then
echo "===================== Checking the commit readiness of the chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME' ===================== " infoln "Checking the commit readiness of the chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME'"
else else
echo fatalln "After $MAX_RETRY attempts, Check commit readiness result on peer0.org${ORG} is INVALID!"
echo $'\e[1;31m'"!!!!!!!!!!!!!!! After $MAX_RETRY attempts, Check commit readiness result on peer0.org${ORG} is INVALID !!!!!!!!!!!!!!!!"$'\e[0m' fi
echo
exit 1
fi
} }
# commitChaincodeDefinition VERSION PEER ORG (PEER ORG)... # commitChaincodeDefinition VERSION PEER ORG (PEER ORG)...
commitChaincodeDefinition() { commitChaincodeDefinition() {
parsePeerConnectionParameters $@ parsePeerConnectionParameters $@
res=$? res=$?
verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters " verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters "
# while 'peer chaincode' command can get the orderer endpoint from the # while 'peer chaincode' command can get the orderer endpoint from the
# peer (if join was successful), let's supply it directly as we know # peer (if join was successful), let's supply it directly as we know
# it using the "-o" option # it using the "-o" option
set -x 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 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=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cat log.txt cat log.txt
verifyResult $res "Chaincode definition commit failed on peer0.org${ORG} on channel '$CHANNEL_NAME' failed" verifyResult $res "Chaincode definition commit failed on peer0.org${ORG} on channel '$CHANNEL_NAME' failed"
echo "===================== Chaincode definition committed on channel '$CHANNEL_NAME' ===================== " successln "Chaincode definition committed on channel '$CHANNEL_NAME'"
echo
} }
# queryCommitted ORG # queryCommitted ORG
queryCommitted() { queryCommitted() {
ORG=$1 ORG=$1
setGlobals $ORG setGlobals $ORG
EXPECTED_RESULT="Version: ${CC_VERSION}, Sequence: ${CC_SEQUENCE}, Endorsement Plugin: escc, Validation Plugin: vscc" EXPECTED_RESULT="Version: ${CC_VERSION}, Sequence: ${CC_SEQUENCE}, Endorsement Plugin: escc, Validation Plugin: vscc"
echo "===================== Querying chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'... ===================== " infoln "Querying chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'..."
local rc=1 local rc=1
local COUNTER=1 local COUNTER=1
# continue to poll # continue to poll
# we either get a successful response, or reach MAX RETRY # we either get a successful response, or reach MAX RETRY
while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do
sleep $DELAY sleep $DELAY
echo "Attempting to Query committed status on peer0.org${ORG}, Retry after $DELAY seconds." infoln "Attempting to Query committed status on peer0.org${ORG}, Retry after $DELAY seconds."
set -x set -x
peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME} >&log.txt peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME} >&log.txt
res=$? res=$?
{ set +x; } 2>/dev/null { 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 $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 test "$VALUE" = "$EXPECTED_RESULT" && let rc=0
COUNTER=$(expr $COUNTER + 1) COUNTER=$(expr $COUNTER + 1)
done done
echo cat log.txt
cat log.txt if test $rc -eq 0; then
if test $rc -eq 0; then successln "Query chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME'"
echo "===================== Query chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME' ===================== " else
echo fatalln "After $MAX_RETRY attempts, Query chaincode definition result on peer0.org${ORG} is INVALID!"
else fi
echo
echo $'\e[1;31m'"!!!!!!!!!!!!!!! After $MAX_RETRY attempts, Query chaincode definition result on peer0.org${ORG} is INVALID !!!!!!!!!!!!!!!!"$'\e[0m'
echo
exit 1
fi
} }
chaincodeInvokeInit() { chaincodeInvokeInit() {
parsePeerConnectionParameters $@ parsePeerConnectionParameters $@
res=$? res=$?
verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters " verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters "
# while 'peer chaincode' command can get the orderer endpoint from the # while 'peer chaincode' command can get the orderer endpoint from the
# peer (if join was successful), let's supply it directly as we know # peer (if join was successful), let's supply it directly as we know
# it using the "-o" option # it using the "-o" option
set -x set -x
fcn_call='{"function":"'${CC_INIT_FCN}'","Args":[]}' fcn_call='{"function":"'${CC_INIT_FCN}'","Args":[]}'
echo invoke fcn call:${fcn_call} 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 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=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
cat log.txt cat log.txt
verifyResult $res "Invoke execution on $PEERS failed " verifyResult $res "Invoke execution on $PEERS failed "
echo "===================== Invoke transaction successful on $PEERS on channel '$CHANNEL_NAME' ===================== " successln "Invoke transaction successful on $PEERS on channel '$CHANNEL_NAME'"
echo
} }
chaincodeQuery() { chaincodeQuery() {
ORG=$1 ORG=$1
setGlobals $ORG setGlobals $ORG
echo "===================== Querying on peer0.org${ORG} on channel '$CHANNEL_NAME'... ===================== " infoln "Querying on peer0.org${ORG} on channel '$CHANNEL_NAME'..."
local rc=1 local rc=1
local COUNTER=1 local COUNTER=1
# continue to poll # continue to poll
# we either get a successful response, or reach MAX RETRY # we either get a successful response, or reach MAX RETRY
while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do
sleep $DELAY sleep $DELAY
echo "Attempting to Query peer0.org${ORG}, Retry after $DELAY seconds." infoln "Attempting to Query peer0.org${ORG}, Retry after $DELAY seconds."
set -x set -x
peer chaincode query -C $CHANNEL_NAME -n ${CC_NAME} -c '{"Args":["queryAllCars"]}' >&log.txt peer chaincode query -C $CHANNEL_NAME -n ${CC_NAME} -c '{"Args":["queryAllCars"]}' >&log.txt
res=$? res=$?
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
let rc=$res let rc=$res
COUNTER=$(expr $COUNTER + 1) COUNTER=$(expr $COUNTER + 1)
done done
echo cat log.txt
cat log.txt if test $rc -eq 0; then
if test $rc -eq 0; then successln "Query successful on peer0.org${ORG} on channel '$CHANNEL_NAME'"
echo "===================== Query successful on peer0.org${ORG} on channel '$CHANNEL_NAME' ===================== " else
echo fatalln "After $MAX_RETRY attempts, Query result on peer0.org${ORG} is INVALID!"
else fi
echo
echo $'\e[1;31m'"!!!!!!!!!!!!!!! After $MAX_RETRY attempts, Query result on peer0.org${ORG} is INVALID !!!!!!!!!!!!!!!!"$'\e[0m'
echo
exit 1
fi
} }
## package the chaincode ## package the chaincode
packageChaincode 1 packageChaincode 1
## Install chaincode on peer0.org1 and peer0.org2 ## Install chaincode on peer0.org1 and peer0.org2
echo "Installing chaincode on peer0.org1..." infoln "Installing chaincode on peer0.org1..."
installChaincode 1 installChaincode 1
echo "Install chaincode on peer0.org2..." infoln "Install chaincode on peer0.org2..."
installChaincode 2 installChaincode 2
## query whether the chaincode is installed ## query whether the chaincode is installed
@ -379,10 +354,9 @@ queryCommitted 2
## Invoke the chaincode - this does require that the chaincode have the 'initLedger' ## Invoke the chaincode - this does require that the chaincode have the 'initLedger'
## method defined ## method defined
if [ "$CC_INIT_FCN" = "NA" ]; then if [ "$CC_INIT_FCN" = "NA" ]; then
echo "===================== Chaincode initialization is not required ===================== " infoln "Chaincode initialization is not required"
echo
else else
chaincodeInvokeInit 1 2 chaincodeInvokeInit 1 2
fi fi
exit 0 exit 0

View file

@ -6,6 +6,8 @@
# This is a collection of bash functions used by different scripts # This is a collection of bash functions used by different scripts
source scriptUtils.sh
export CORE_PEER_TLS_ENABLED=true export CORE_PEER_TLS_ENABLED=true
export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
@ -27,7 +29,7 @@ setGlobals() {
else else
USING_ORG="${OVERRIDE_ORG}" USING_ORG="${OVERRIDE_ORG}"
fi fi
echo "Using organization ${USING_ORG}" infoln "Using organization ${USING_ORG}"
if [ $USING_ORG -eq 1 ]; then if [ $USING_ORG -eq 1 ]; then
export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
@ -45,7 +47,7 @@ setGlobals() {
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp
export CORE_PEER_ADDRESS=localhost:11051 export CORE_PEER_ADDRESS=localhost:11051
else else
echo "================== ERROR !!! ORG Unknown ==================" errorln "ORG Unknown"
fi fi
if [ "$VERBOSE" == "true" ]; then if [ "$VERBOSE" == "true" ]; then
@ -63,7 +65,7 @@ parsePeerConnectionParameters() {
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
setGlobals $1 setGlobals $1
PEER="peer0.org$1" PEER="peer0.org$1"
## Set peer adresses ## Set peer addresses
PEERS="$PEERS $PEER" PEERS="$PEERS $PEER"
PEER_CONN_PARMS="$PEER_CONN_PARMS --peerAddresses $CORE_PEER_ADDRESS" PEER_CONN_PARMS="$PEER_CONN_PARMS --peerAddresses $CORE_PEER_ADDRESS"
## Set path to TLS certificate ## Set path to TLS certificate
@ -78,8 +80,6 @@ parsePeerConnectionParameters() {
verifyResult() { verifyResult() {
if [ $1 -ne 0 ]; then if [ $1 -ne 0 ]; then
echo $'\e[1;31m'!!!!!!!!!!!!!!! $2 !!!!!!!!!!!!!!!!$'\e[0m' fatalln "$2"
echo
exit 1
fi fi
} }