github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/examples/dchackfest/samples/e2e/network_setup.sh (about)

     1  #!/bin/bash
     2  
     3  UP_DOWN=$1
     4  CH_NAME=$2
     5  
     6  COMPOSE_FILE=docker-compose.yaml
     7  #COMPOSE_FILE=docker-compose-no-tls.yaml
     8  
     9  function printHelp () {
    10  	echo "Usage: ./network_setup <up|down> <channel-name>"
    11  }
    12  
    13  function validateArgs () {
    14  	if [ -z "${UP_DOWN}" ]; then
    15  		echo "Option up / down / restart not mentioned"
    16  		printHelp
    17  		exit 1
    18  	fi
    19  	if [ -z "${CH_NAME}" ]; then
    20  		echo "setting to default channel 'mychannel'"
    21  		CH_NAME=mychannel
    22  	fi
    23  }
    24  
    25  function clearContainers () {
    26          CONTAINER_IDS=$(docker ps -aq)
    27          if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; then
    28                  echo "---- No containers available for deletion ----"
    29          else
    30                  docker rm -f $CONTAINER_IDS
    31          fi
    32  }
    33  
    34  function removeUnwantedImages() {
    35          DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}')
    36          if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then
    37                  echo "---- No images available for deletion ----"
    38          else
    39                  docker rmi -f $DOCKER_IMAGE_IDS
    40          fi
    41  }
    42  
    43  function replacePrivateKey () {
    44  	ARCH=`uname -s | grep Darwin`
    45  	if [ "$ARCH" == "Darwin" ]
    46  		then
    47  				OPTS="-it"
    48  		else
    49  				OPTS="-i"
    50  	fi
    51  
    52  	cp docker-compose-template.yaml docker-compose.yaml
    53    PRIV_KEY=$(ls crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/keystore/)
    54    sed $OPTS  "s/ORDERER_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose.yaml
    55    PRIV_KEY=$(ls crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/keystore/)
    56    sed $OPTS "s/PEER0_ORG1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose.yaml
    57    PRIV_KEY=$(ls crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/keystore/)
    58    sed $OPTS "s/PEER1_ORG1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose.yaml
    59    PRIV_KEY=$(ls crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/keystore/)
    60    sed $OPTS "s/PEER0_ORG2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose.yaml
    61    PRIV_KEY=$(ls crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/keystore/)
    62    sed $OPTS "s/PEER1_ORG2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose.yaml
    63  }
    64  
    65  function generateArtifacts () {
    66          os_arch=$(echo "$(uname -s)-$(uname -m)" | awk '{print tolower($0)}')
    67          if [ "$(uname -m)" = "x86_64" ]; then
    68                  os_arch=$(echo "$(uname -s)-amd64" | awk '{print tolower($0)}')
    69          fi
    70  
    71          echo "OS_ARCH "$os_arch
    72  	echo
    73  	echo "##########################################################"
    74  	echo "############## Generate certificates #####################"
    75  	echo "##########################################################"
    76          ./../../$os_arch/bin/cryptogen generate --config=./crypto-config.yaml
    77  	echo
    78  	echo
    79  
    80          replacePrivateKey
    81  
    82  	echo "##########################################################"
    83  	echo "#########  Generating Orderer Genesis block ##############"
    84  	echo "##########################################################"
    85  	export ORDERER_CFG_PATH=$PWD
    86  	./../../$os_arch/bin/configtxgen -profile TwoOrgs -outputBlock orderer.block
    87  	echo
    88  	echo
    89  
    90  	echo "#################################################################"
    91  	echo "### Generating channel configuration transaction 'channel.tx' ###"
    92  	echo "#################################################################"
    93  	./../../$os_arch/bin/configtxgen -profile TwoOrgs -outputCreateChannelTx channel.tx -channelID $CH_NAME
    94  	echo
    95  	echo
    96  
    97  }
    98  
    99  function networkUp () {
   100  	#Lets generate all the artifacts which includes org certs, orderer.block,
   101          # channel configuration transaction and Also generate a docker-compose file
   102          generateArtifacts
   103          export ARCH_TAG=$(uname -m)
   104  	CHANNEL_NAME=$CH_NAME docker-compose -f $COMPOSE_FILE up -d 2>&1
   105  	if [ $? -ne 0 ]; then
   106  		echo "ERROR !!!! Unable to pull the images "
   107  		exit 1
   108  	fi
   109  	docker logs -f cli
   110  }
   111  
   112  function networkDown () {
   113          docker-compose -f $COMPOSE_FILE down
   114          #Cleanup the chaincode containers
   115  	clearContainers
   116  	#Cleanup images
   117  	removeUnwantedImages
   118          # remove orderer block and channel transaction
   119  	rm -rf orderer.block channel.tx crypto-config
   120  }
   121  
   122  validateArgs
   123  
   124  #Create the network using docker compose
   125  if [ "${UP_DOWN}" == "up" ]; then
   126  	networkUp
   127  elif [ "${UP_DOWN}" == "down" ]; then ## Clear the network
   128  	networkDown
   129  elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the network
   130  	networkDown
   131  	networkUp
   132  else
   133  	printHelp
   134  	exit 1
   135  fi