network_setup.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. UP_DOWN=$1
  3. CH_NAME=$2
  4. COMPOSE_FILE=docker-compose-cli.yaml
  5. function printHelp () {
  6. echo "Usage: ./network_setup <up|down> <channel-name>"
  7. }
  8. function validateArgs () {
  9. if [ -z "${UP_DOWN}" ]; then
  10. echo "Option up / down / restart not mentioned"
  11. printHelp
  12. exit 1
  13. fi
  14. if [ -z "${CH_NAME}" ]; then
  15. echo "setting to default channel 'mychannel'"
  16. CH_NAME=mychannel
  17. fi
  18. }
  19. function clearContainers () {
  20. CONTAINER_IDS=$(docker ps -aq)
  21. if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; then
  22. echo "---- No containers available for deletion ----"
  23. else
  24. docker rm -f $CONTAINER_IDS
  25. fi
  26. }
  27. function removeUnwantedImages() {
  28. DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}')
  29. if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then
  30. echo "---- No images available for deletion ----"
  31. else
  32. docker rmi -f $DOCKER_IMAGE_IDS
  33. fi
  34. }
  35. function networkUp () {
  36. CURRENT_DIR=$PWD
  37. source generateCfgTrx.sh $CH_NAME
  38. cd $CURRENT_DIR
  39. CHANNEL_NAME=$CH_NAME docker-compose -f $COMPOSE_FILE up -d 2>&1
  40. if [ $? -ne 0 ]; then
  41. echo "ERROR !!!! Unable to pull the images "
  42. exit 1
  43. fi
  44. docker logs -f cli
  45. }
  46. function networkDown () {
  47. docker-compose -f $COMPOSE_FILE down
  48. #Cleanup the chaincode containers
  49. clearContainers
  50. #Cleanup images
  51. removeUnwantedImages
  52. }
  53. validateArgs
  54. #Create the network using docker compose
  55. if [ "${UP_DOWN}" == "up" ]; then
  56. networkUp
  57. elif [ "${UP_DOWN}" == "down" ]; then ## Clear the network
  58. networkDown
  59. elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the network
  60. networkDown
  61. networkUp
  62. else
  63. printHelp
  64. exit 1
  65. fi