function printHelp () { echo "Usage: ./network_setup <up|down> <\$channel-name> <\$cli_timeout> <couchdb>.\nThe arguments must be in order." }
function validateArgs () { if [ -z "${UP_DOWN}" ]; then echo "Option up / down / restart not mentioned" printHelp exit 1 fi if [ -z "${CH_NAME}" ]; then echo "setting to default channel 'mychannel'" CH_NAME=mychannel fi }
function clearContainers () { CONTAINER_IDS=$(docker ps -aq) if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; then echo "---- No containers available for deletion ----" else docker rm -f $CONTAINER_IDS fi }
function removeUnwantedImages() { DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}') if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then echo "---- No images available for deletion ----" else docker rmi -f $DOCKER_IMAGE_IDS fi }
function networkUp () { if [ -f "./crypto-config" ]; then echo "crypto-config directory already exists." else #Generate all the artifacts that includes org certs, orderer genesis block, # channel configuration transaction source generateArtifacts.sh $CH_NAME fi
if [ "${IF_COUCHDB}" == "couchdb" ]; then CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1 else CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE up -d 2>&1 fi if [ $? -ne 0 ]; then echo "ERROR !!!! Unable to pull the images " exit 1 fi docker logs -f cli }
function networkDown () { docker-compose -f $COMPOSE_FILE down
#Cleanup the chaincode containers clearContainers
#Cleanup images removeUnwantedImages
# remove orderer block and other channel configuration transactions and certs rm -rf channel-artifacts/*.block channel-artifacts/*.tx crypto-config }
validateArgs
#Create the network using docker compose if [ "${UP_DOWN}" == "up" ]; then networkUp elif [ "${UP_DOWN}" == "down" ]; then ## Clear the network networkDown elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the network networkDown networkUp else printHelp exit 1 fi
CURRENT_DIR=$PWD cd crypto-config/peerOrganizations/org1.example.com/ca/ PRIV_KEY=$(ls *_sk) cd $CURRENT_DIR sed $OPTS "s/CA1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml cd crypto-config/peerOrganizations/org2.example.com/ca/ PRIV_KEY=$(ls *_sk) cd $CURRENT_DIR sed $OPTS "s/CA2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml }
## Generates Org certs using cryptogen tool function generateCerts (){ CRYPTOGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/cryptogen
if [ -f "$CRYPTOGEN" ]; then echo "Using cryptogen -> $CRYPTOGEN" else echo "Building cryptogen" make -C $FABRIC_ROOT release fi
## Generate orderer genesis block , channel configuration transaction and anchor peer update transactions function generateChannelArtifacts() {
CONFIGTXGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/configtxgen if [ -f "$CONFIGTXGEN" ]; then echo "Using configtxgen -> $CONFIGTXGEN" else echo "Building configtxgen" make -C $FABRIC_ROOT release fi
echo "##########################################################" echo "######### Generating Orderer Genesis block ##############" echo "##########################################################" # 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! $CONFIGTXGEN -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
## Join all the peers to the channel echo "Having all peers join the channel..." joinChannel
## Set the anchor peers for each org in the channel echo "Updating anchor peers for org1..." updateAnchorPeers 0 echo "Updating anchor peers for org2..." updateAnchorPeers 2
## Install chaincode on Peer0/Org1 and Peer2/Org2 echo "Installing chaincode on org1/peer0..." installChaincode 0 echo "Install chaincode on org2/peer2..." installChaincode 2
#Instantiate chaincode on Peer2/Org2 echo "Instantiating chaincode on org2/peer2..." instantiateChaincode 2
#Query on chaincode on Peer0/Org1 echo "Querying chaincode on org1/peer0..." chaincodeQuery 0 100
#Invoke on chaincode on Peer0/Org1 echo "Sending invoke transaction on org1/peer0..." chaincodeInvoke 0
## Install chaincode on Peer2/Org2 echo "Installing chaincode on org2/peer3..." installChaincode 3
#Query on chaincode on Peer2/Org2, check if the result is 90 echo "Querying chaincode on org2/peer3..." chaincodeQuery 3 90
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE dev-peer1.org2.example.com-mycc-1.0 latest 8c03b3cb4f3f 36 hours ago 173MB dev-peer0.org1.example.com-mycc-1.0 latest 9aa4e69a149f 36 hours ago 173MB dev-peer0.org2.example.com-mycc-1.0 latest f3fef07f5dcc 36 hours ago 173MB
function clearContainers () { CONTAINER_IDS=$(docker ps -aq) if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; then echo "---- No containers available for deletion ----" else docker rm -f $CONTAINER_IDS fi }
function removeUnwantedImages() { DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}') if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then echo "---- No images available for deletion ----" else docker rmi -f $DOCKER_IMAGE_IDS fi }